After a Rails 8.1 deploy changes CSS or JavaScript, how does data-turbo-track="reload" make tabs already open under Turbo Drive load the new assets?
answer
- the head is merged, not reloaded
- tracked outerHTML signature compared
- digested URLs change the markup
- importmap script is tracked too
- turbo:reload, tracked_element_mismatch
basics
~20 sTurbo Drive compares the head elements marked data-turbo-track="reload" on the current and the new page. If their markup differs, as digested asset URLs do after a deploy, Turbo abandons the swap and does a full page load.
solid answer
~40 sTurbo Drive merges `<head>` instead of reloading it, so after a deploy an open tab would keep its old JavaScript and pile the new stylesheet on top of the old one. Elements marked `data-turbo-track="reload"` prevent that: on every visit Turbo concatenates their outer HTML on both pages and compares. A mismatch makes it fire `turbo:reload` with the reason `tracked_element_mismatch` and set `window.location`, so the browser loads the new page and assets from scratch. It only works because asset URLs carry a content digest. A new Rails 8.1 layout tracks the stylesheet with `stylesheet_link_tag :app, "data-turbo-track": "reload"`, and `javascript_importmap_tags` tracks the inline import map, whose JSON lists every pin's digested URL. `data-turbo-track="dynamic"` removes page-specific styles instead, and `turbo_page_requires_reload` forces a full load for one page.
code
erb · 8 lines<%# app/views/layouts/application.html.erb %>
<head>
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= yield :head %>
<%= stylesheet_link_tag :app, "data-turbo-track": "reload" %>
<%= javascript_importmap_tags %>
</head>go deeper
Know that the generated layout marks the stylesheet and import map with data-turbo-track="reload" so a deploy triggers a full page load on the next visit.
Explain the head merge, the outer-HTML signature comparison and why digested asset URLs are what make a change visible to Turbo.
Audit layouts for tracked tags with per-request values and untracked hand-written assets, and use turbo:reload to confirm deploy reloads in production.
Treat tracked-asset reloads as one layer of release safety: they refresh a tab only on its next visit, so compatibility between releases still has to be designed.
## The problem a persistent document creates With Turbo Drive the browser loads the document once and swaps `<body>` on every visit. The `<head>` is **merged**: a `<title>` or `<meta>` that changed is updated, a stylesheet or script tag that is identical on both pages is left alone, and new ones are appended. After a deploy, an open tab therefore keeps running the JavaScript it loaded at the start, and a renamed stylesheet would be added next to the old one rather than replacing it. The server renders HTML that expects the new assets; the tab still has the old ones. ## How data-turbo-track="reload" works On each Drive render Turbo builds a **signature** of the current and the new head: the outer HTML of every element whose `data-turbo-track` is `reload`, joined together. Then: 1. If the two signatures match, the visit renders normally. 2. If they differ, Turbo does not render the response. It dispatches `turbo:reload` with `detail.reason` set to `tracked_element_mismatch`. 3. It sets `window.location` to the visit's URL, and the browser performs a full load that fetches the new assets. The comparison is textual. It catches a deploy only if the tracked tags' markup changes, which is why asset URLs need a **content digest**. Propshaft, the Rails 8 asset pipeline, serves every asset under a digested path, so a changed file yields a new `href` or `src` and a new signature. ## What a new Rails 8.1 app tracks | Tag in the generated layout | Tracked? | Why it changes on deploy | |---|---|---| | `stylesheet_link_tag :app, "data-turbo-track": "reload"` | yes | each stylesheet's digested URL changes with its content | | the inline `<script type="importmap">` from `javascript_importmap_tags` | yes, importmap-rails adds the attribute | the JSON maps every pin to its digested URL, so any JavaScript change alters it | | `csrf_meta_tags`, `csp_meta_tag` | no | their values can differ between responses (the CSRF token is masked afresh each time), so tracking them would reload on every visit | The last row is the key rule: **track only what changes per deploy, never what changes per request**. A tracked tag with a per-request value (a nonce, a token, a timestamp) turns every visit into a full reload. ## Related controls - **`data-turbo-track="dynamic"`** on a `<link>` or `<style>` tells Turbo to remove it when the next page's head does not contain it, which keeps page-specific styles from piling up without forcing a reload. Turbo's handbook warns against using it on scripts, since removing a script element does not unload code it already ran. - **`<meta name="turbo-visit-control" content="reload">`**, written by turbo-rails' `turbo_page_requires_reload`, forces a full load whenever a visit reaches that page; it is meant for pages whose third-party JavaScript cannot survive a body swap. - A `turbo:reload` listener can log the reason, which helps confirm that deploys trigger reloads. ## Diagnosing unexpected full reloads If Turbo seems to be "off" because every click does a full load, the tracked signature is usually unstable: 1. Log `event.detail.reason` in a `turbo:reload` listener; `tracked_element_mismatch` confirms the cause. 2. Fetch the same page twice and compare every tag that carries `data-turbo-track="reload"`. 3. Remove the attribute from any tag whose value differs between responses for reasons other than a deploy. During a rolling deploy, old and new servers answer side by side, so consecutive visits can see different signatures and reload more than once; this settles once the rollout finishes. ## Limits worth stating - The check runs on the **next** Drive render. A tab left open keeps its old code until the user navigates, and requests that code sends in the meantime still meet the new server. - Hand-written `<script>` or `<link>` tags without digests, or without the attribute, are never detected. - Only the current tab's next page is affected; nothing pushes the reload to idle tabs. For a team the practical checklist is short: keep the generated attributes, add `"data-turbo-track": "reload"` to any extra asset tag you add to the layout, and make sure every such tag carries a digest.
- Why would adding data-turbo-track="reload" to the CSRF meta tag be a mistake?The comparison is on outer HTML, and the CSRF token in that tag can differ between responses. Every visit would see a mismatch and fall back to a full page load, silently turning Turbo Drive off. Track only tags that change per deploy.
- Which JavaScript change makes a Rails 8.1 import-map app reload under Turbo?Any change to a pinned file. `javascript_importmap_tags` renders the import map inline with `data-turbo-track="reload"`, and its JSON maps each pin to a digested URL. A changed file gets a new digest, the import map's text changes and the next visit triggers a full load.
- Turbo Drive reloads the whole head on every visit, so new assets load automatically.
- data-turbo-track="reload" polls the server and reloads idle tabs as soon as a deploy lands.
- Tracking works without digests because Turbo compares the asset files' contents.
- Putting data-turbo-track="reload" on csrf_meta_tags is a safe extra precaution.
- data-turbo-track="dynamic" forces a full page reload when the asset changes.
- data-turbo-permanent on the stylesheet link is what makes Turbo pick up a new stylesheet.
Assumes Turbo 8 with a Rails 8.1 layout using Propshaft and importmap-rails 2.2, whose javascript_importmap_tags marks the import map data-turbo-track="reload".