Skip to content

Render the template for the current format when a lookup context changes formats - #2747

Merged
joelhawksley merged 2 commits into
ViewComponent:mainfrom
schilbach:fix-requested-details-formats
Oct 26, 2026
Merged

joelhawksley merged 2 commits into
ViewComponent:mainfrom
schilbach:fix-requested-details-formats

Conversation

@schilbach

Copy link
Copy Markdown
Contributor

What are you trying to accomplish?

Updating viewcomponent broke CI for us. The reason is that the cache for vc_requested_details did not work as expected for ActionMailer multipart mails.

#2710 memoizes the Requested details in @__vc_requested_details_cache on the LookupContext and never resets it. Action Mailer's collector sets formats on the mailer's lookup context for each part and renders on that same context, so the first part's formats stick. The implicit-template path (mail without a block) passes formats: to render, which builds a new lookup context, so it isn't affected.

What approach did you choose and why?

vc_requested_details now returns details_key for empty details. With template caching on, details_key is the Requested for the current details, memoized by ActionView::LookupContext and reset by _set_detail whenever a detail changes. It's the same object the previous code read from detail_args_for as from_cache, and returning it directly skips the tuple detail_args_for allocates. With caching off, details_key is nil and the method builds a Requested from the current details.

Anything you want to highlight for special attention from reviewers?

details_key is :nodoc:, so it's undocumented Action View API. RequestDetails already relies on the private detail_args_for, and details_key and _set_detail and this should exist since rails 7.1

I didn't run the tests on the other matrix targets, only on Rails 8.1.4 and Ruby 4.0.6

…ges formats

4.16.0 memoized the Requested details on the LookupContext and never reset
the memo. Action Mailer sets `formats` on one lookup context for each part
of a multipart email, so components rendered the text template into the
HTML part when `format.text` came first. Reuse Action View's `details_key`,
which it resets whenever a detail changes.
@joelhawksley
joelhawksley merged commit 9176179 into ViewComponent:main Oct 26, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants