With Turbo Drive's page cache, why does a Rails page flash stale content on navigation, and how do turbo-cache-control and data-turbo-temporary help?
answer
- previews on application visits
- Back restores from cache, no request
- no-preview vs no-cache
- turbo_exempts_page_from_cache needs yield :head
- data-turbo-temporary strips flashes
basics
~20 sTurbo Drive caches the last pages visited and shows a cached copy as a preview while it fetches a fresh one, and restores from cache on Back. turbo-cache-control no-preview or no-cache opts a page out; data-turbo-temporary removes flashes first.
solid answer
~50 sBefore rendering a new page, Turbo Drive fires `turbo:before-cache` and stores a clone of the current page in a cache of recent pages (ten in Turbo 8). On an application visit to a cached URL it renders that copy at once as a preview, marking `<html>` with `data-turbo-preview`, then replaces it with the network response. On Back or Forward it restores the copy without a request when it can. Old numbers or a stale flash on screen come from that copy. `<meta name="turbo-cache-control" content="no-preview">` keeps the page out of previews but still uses it for Back; `content="no-cache"` never caches it, so even Back goes to the network. In Rails, `turbo_exempts_page_from_preview` and `turbo_exempts_page_from_cache` write those tags through `provide :head`, which needs `yield :head` in the layout. `data-turbo-temporary` removes flash messages before caching, and `Turbo.cache.clear()` empties the cache.
code
erb · 13 lines<%# app/views/dashboards/show.html.erb %>
<% turbo_exempts_page_from_preview %>
<%# app/views/layouts/application.html.erb %>
<head>
<%= yield :head %>
</head>
<body>
<% if notice %>
<p class="flash" data-turbo-temporary><%= notice %></p>
<% end %>
<%= yield %>
</body>go deeper
Know that Turbo shows a cached copy of a page first, and that a flash message marked data-turbo-temporary will not come back from the cache.
Explain previews versus restoration visits, the two turbo-cache-control directives, and why the Rails helpers depend on yield :head.
Pick a cache policy per page from how wrong stale data would be, and clean snapshots with turbo:before-cache instead of disabling the cache app-wide.
Weigh perceived speed against correctness: decide which screens may ever show old data and make that rule visible in the layout and page helpers.
## What Turbo Drive caches Turbo Drive keeps a **snapshot cache** of recently visited pages, ten entries in Turbo 8. Just before it renders a new page, it fires `turbo:before-cache` on the document and stores a deep clone of the current page (`cloneNode(true)`, so event listeners are not copied). The cache serves two purposes: - **Previews during application visits.** When a link or `Turbo.visit` targets a URL that is in the cache, Turbo renders the cached copy immediately, adds `data-turbo-preview` to `<html>`, and swaps in the network response when it arrives. - **Restoration visits.** On Back or Forward, Turbo renders the cached copy without a network request when it can, and restores the saved scroll position. Both make navigation feel instant. Both also mean the user can see a page **as it was** when they left it. ## Symptoms of stale snapshots 1. A dashboard's counters show yesterday's numbers for a moment before the real ones arrive (the preview). 2. After Back, a list shows a record that another tab deleted (the restoration). 3. A flash message such as "Article published" reappears when the user returns to a page. 4. A dropdown or modal that was open when the user left is open again in the snapshot. Turbo already clears the cache after a successful non-GET form submission, so the user's own writes do not leave stale pages behind. Stale content comes from changes made elsewhere, or from page state captured at the moment of caching. ## How a preview turns into the real page 1. A visit starts and Turbo looks up the URL in the snapshot cache. 2. If a snapshot exists and its page allows previews (or the visit is a restoration), Turbo renders it and marks `<html>` with `data-turbo-preview`. 3. For an application visit, the network request runs at the same time. 4. When the response arrives, Turbo renders it over the preview and removes `data-turbo-preview`. The flash of old numbers is the interval between steps 2 and 4. On a fast network it is barely visible; on a slow one, users read and act on stale figures. ## Opting a page out The directive lives in a meta tag in the page's `<head>`: | Directive | Previews on application visits | Back / Forward | |---|---|---| | none | shown from cache | restored from cache | | `<meta name="turbo-cache-control" content="no-preview">` | not shown | restored from cache | | `<meta name="turbo-cache-control" content="no-cache">` | not shown | always fetched from the network | turbo-rails wraps both: `turbo_exempts_page_from_preview` and `turbo_exempts_page_from_cache` call `provide :head` with the matching tag, and the `_tag` variants return the bare `<meta>`. The helpers do nothing unless the layout renders `yield :head`; the Rails 8.1 generated layout does. The two cannot be combined, since there is one directive per page. From JavaScript, `Turbo.cache.exemptPageFromCache()`, `Turbo.cache.exemptPageFromPreview()` and `Turbo.cache.resetCacheControl()` write the same meta tag, and `Turbo.cache.clear()` empties the whole cache (it replaced `Turbo.clearCache()`, now deprecated). ## Cleaning a page before it is cached Some elements should never be stored: - **`data-turbo-temporary`** marks an element that Turbo removes from the page just before caching. Flash messages and toasts are the classic case. - A **`turbo:before-cache`** listener can reset forms, close menus and modals, or tear down a third-party widget so the snapshot is clean. ## Choosing a policy - Use `no-preview` for pages whose top half changes often (dashboards, inboxes): the preview would mislead, but Back should stay instant. - Use `no-cache` for pages where showing old data is wrong, such as a balance or a page that depends on the session. - Use `data-turbo-temporary` on flashes in the layout once, rather than per page. - Call `Turbo.cache.clear()` after client-side events that invalidate many pages, such as a live update telling the user their permissions changed. The cache is per tab and per page load: it is in memory and is lost on a full reload. It is separate from the browser's own back/forward cache and from HTTP caching, which is why HTTP headers such as `Cache-Control: no-store` on the response do not change what Turbo shows from its snapshot cache; the meta tag does.
- You added turbo_exempts_page_from_cache to a view and the page is still cached. What is the likely cause?The helper calls `provide :head`, so it only takes effect if the layout renders `<%= yield :head %>` inside `<head>`. A custom layout without it silently drops the meta tag; check the rendered `<head>` for `turbo-cache-control`.
- How can JavaScript tell whether it is running against a preview rather than the real page?Turbo adds `data-turbo-preview` to the `<html>` element while a cached preview is on screen and removes it when the fresh response renders. Code that should not act on stale content, such as an analytics call, can check for that attribute.
- no-preview stops Turbo from using the cached page on Back as well.
- Sending Cache-Control: no-store on the response stops Turbo's snapshot previews.
- turbo_exempts_page_from_cache works in any layout, with or without yield :head.
- Turbo.clearCache() is the current way to empty the cache in Turbo 8.
- Turbo caches pages in localStorage, so stale snapshots survive a full reload.
- data-turbo-permanent is how you keep a flash message out of the cached snapshot.
Assumes Turbo 8 (8.0.23 source): ten cached snapshots, and Turbo.cache.clear() replacing the deprecated Turbo.clearCache().