In Rails fragment caching, how does the template digest expire fragments when views change, and when must you add a Template Dependency comment?
answer
- digest of the whole template tree
- render calls become dependencies
- helpers hide render calls
- # Template Dependency: path or dir/*
- render_tracker :ruby in 8.1 defaults
basics
~20 sThe fragment key embeds a digest of the template and every partial its render calls name, so editing any of them yields a new key. Rails cannot see partials rendered from helpers, so those need a Template Dependency comment.
solid answer
~40 s`ActionView::Digestor` hashes the template's source together with the digests of its dependencies, found by scanning its `render` calls (`render "reviews/review"`, `render @product`, `render partial:, collection:`) and recursing. The digest goes into the key after the template path, so any edit anywhere in the tree, even outside the cached block, gives new keys. Calls it cannot resolve, such as a partial rendered inside a helper or `render` on a variable whose class it cannot infer, are invisible, and edits there leave stale fragments. Declare them with `<%# Template Dependency: reviews/review %>`, or `gear/*` for a whole directory. Helper code itself is never digested, so bump a comment or add a `view_cache_dependency` in the controller when a helper's output changes. Rails 8.1's defaults switch `config.action_view.render_tracker` to `:ruby`, which parses the compiled template instead of regex-scanning ERB.
code
erb · 10 lines<%# app/views/products/show.html.erb %>
<% cache product do %>
<%# Template Dependency: reviews/stars %>
<%= render_review_stars(product) %>
<%# Template Dependency: gear/badges/* %>
<%= render_gear_badges(product) %>
<%= render product.reviews %>
<% end %>go deeper
Recall that editing a view gives its fragments new keys through the template digest, so deploys do not need a cache clear.
Explain how render calls become dependencies, which calls the digestor cannot resolve, and how the Template Dependency comment and wildcard declare them.
Show you know what is never digested, helpers, presenters, locale strings, and how you would rotate keys for them; mention the 8.1 Ruby tracker and miss bursts after deploys.
Decide team conventions that keep digests trustworthy, such as rendering partials from templates rather than helpers and owning key-rotation for non-template inputs.
## What the digest is for A fragment key written by the `cache` helper looks like `views/products/show:4f1c.../products/42`. The part after the colon is the **template digest**. Its job is to make a deploy that changes a view produce **new keys**, so nobody has to clear the cache by hand when markup changes. Without it, a fragment rendered by last week's template would keep being served after the template was edited, because the product it is keyed on has not changed. ## How `ActionView::Digestor` builds it 1. Start from the template that contains the `cache` call, for example `products/show.html.erb`. 2. Find its **dependencies**: the partials its `render` calls name. 3. Recurse into each dependency and find its dependencies, building a tree; cycles are cut. 4. Hash each node's **entire source** together with its children's digests, using `ActiveSupport::Digest`. Since the 7.0 defaults that is SHA256 truncated to 32 hex characters; the helper's own documentation still says MD5, which was the earlier default. 5. Memoize the result per template and format; in production that memo lasts for the life of the process. Two consequences follow. Editing **anything** in the file, even markup outside the `cache` block, changes the digest. And editing a **partial** changes the digest of every template that renders it, which is why Russian-doll fragments pick up a changed `_review.html.erb` without any touch. ## What it can see and what it cannot | `render` call | Dependency found? | |---|---| | `render "reviews/review"` | yes, `reviews/_review` | | `render "header"` | yes, resolved in the current template's directory | | `render @product` or `render product.reviews` | yes, inferred from the name (`products/product`, `reviews/review`) | | `render partial: "reviews/review", collection: reviews` | yes | | `render "gear/specs/#{product.kind}"` | every partial in `gear/specs` | | `render featured_items` (a local holding products) | no: guessed as `featured_items/_featured_item` | | `render @product.reviews.where(verified: true)` | no: guessed from the last call, `where` | | `render_review_stars(product)`, a helper that renders a partial | no | For the unresolvable cases, either rewrite the call in the explicit form (`render partial: "reviews/review", collection: ...`) or declare the dependency in a comment the digestor matches with `/# Template Dependency: (\S+)/`: ```erb <%# Template Dependency: reviews/stars %> <%= render_review_stars(product) %> <%# Template Dependency: gear/badges/* %> <%= render_gear_badges(product) %> ``` - One dependency per line. - The wildcard form takes a path relative to `app/views` (or another view path) and marks every template in that directory. - A dependency the digestor cannot find is logged as "Couldn't find template for digesting" and contributes an empty digest, so a typo fails quietly. ## What is never digested The digestor reads **templates only**. These changes leave every key the same: - a helper method's Ruby code, such as a new format in `gear_weight(product)`; - a decorator, presenter or model method called in the block; - a locale string looked up with `t(...)`. The fixes, in rising order of reach: 1. Change the template so its digest moves, for instance an `<%# Helper Dependency Updated: 2026-10-06 %>` comment beside the helper call. 2. Put the changing input in the **key**, for example `cache [product, I18n.locale]`. 3. Declare a controller-level `view_cache_dependency { "gear-helpers-v2" }`; its values are fed into every digest that controller's views compute, so bumping the string rotates them all. `cache product, skip_digest: true` drops the digest entirely. It is only reasonable when you expire the key yourself, because template edits will then never rotate it. ## The Rails 8.1 change `config.action_view.render_tracker` chooses how dependencies are found. The original value, `:regex`, scans the ERB source with regular expressions. `load_defaults 8.1` switches it to `:ruby`, which compiles the template to Ruby and parses its `render` calls with Prism (falling back to Ripper when Prism is not available), so render calls written in less common shapes are found more reliably. Explicit `Template Dependency` comments work the same under both trackers. Rails 8.1.4 fixed a bug in which the `:ruby` tracker was not registered for ERB when `eager_load` was on, which is the production setting, so apps on earlier 8.1 patches may have been digesting with the regex tracker there. ## Operational notes - In production, digests are computed once per process and memoized, so a deploy that restarts the processes is what rotates them. - A deploy that edits a widely shared partial rotates the keys of every template above it, so expect a burst of misses on busy pages right after release.
- Why can a fragment change after you edited markup outside its cache block?The digest hashes the whole template file plus its dependency tree, not just the block. Any edit in the file, inside or outside the block, changes the digest and therefore every fragment key computed from that template.
- What does skip_digest: true trade away?`cache product, skip_digest: true` keeps the key free of the template digest, so the key is predictable and can be expired by hand. In return, template edits no longer rotate it; you must expire or version such fragments yourself on every markup change.
- The digest covers only the markup inside the cache block.
- Rails digests helper methods called inside a cached block.
- A partial rendered from a helper is tracked like any render call.
- Template Dependency comments stop working under the Ruby render tracker.
- You must clear the cache store on every deploy that changes views.
Assumes Rails 8.1.4 with load_defaults 8.1, under which config.action_view.render_tracker is :ruby (it was :regex before 8.1); 8.1.4 fixed the :ruby tracker not registering for ERB when eager_load is on.