Rails 7 adds #caching? and #uncacheable!

Posted . Visible to the public.

Rails' fragment caching caches subtrees of an HTML document tree. While constructing that tree though, it can be really hard to keep track of whether some code is run in a caching context. Fortunately, Rails 7 brings two helpers that simplify this.

Note that these helpers are all about Rails' fragment caching and not about downstream caching (i.e. Cache-Control).

uncacheable!

Invoke this helper in a partial or another helper that should never be cached. Used outside of fragment caches, the helper does just nothing. But should it ever, by complex partial or helper chains, be invoked in a fragment caching context, it will raise an error.

caching?

This underlying helper ist more of a plumbing tool. You can use it to add more control around what to do in a caching context and what to do outside of it.

Dominik Schöler
Last edit
Dominik Schöler
License
Source code in this card is licensed under the MIT License.
Posted by Dominik Schöler to makandra dev (2024-11-26 07:40)