With Turbo Drive in Rails 8.1, what happens when a user clicks an ordinary same-origin link, and how do you opt it out?
answer
- fetch, then swap the body
- head merged, not replaced
- advance pushes a history entry
- data-turbo="false" on element or ancestor
- Turbo.session.drive = false flips to opt-in
basics
~20 sTurbo Drive cancels the browser navigation, fetches the page, replaces the <body>, merges the <head> and pushes a history entry, so loaded scripts and styles survive. data-turbo="false" on the link or an ancestor hands the click back to the browser.
solid answer
~40 sTurbo Drive listens for clicks on same-origin links. It cancels the native navigation, fetches the URL, swaps the current `<body>` for the response's `<body>`, merges the `<head>` (new `<title>`, meta and assets are added, unchanged ones are left alone) and pushes a history entry, because the default visit action is `advance`. The `window` and the JavaScript already loaded stay alive, so code hooks `turbo:load` rather than `DOMContentLoaded`. To opt one link or form out, put `data-turbo="false"` on it or on any ancestor; a nearer `data-turbo="true"` opts back in. `Turbo.session.drive = false` (a setter over `Turbo.config.drive.enabled` in Turbo 8) inverts the default, so only elements inside `data-turbo="true"` are driven. Cross-origin links, links outside `turbo-root`, file extensions such as `.pdf`, `target="_blank"`, `download` and modifier-key clicks are left to the browser anyway.
code
html · 10 lines<nav>
<a href="/articles">Articles</a>
<a href="/reports/annual.pdf">Annual report</a>
<a href="/legacy/search" data-turbo="false">Old search</a>
</nav>
<div data-turbo="false">
<a href="/widgets">Native load</a>
<a href="/widgets/new" data-turbo="true">Driven again</a>
</div>go deeper
Recall the swap: fetch, replace the body, merge the head, push history. Know that data-turbo="false" on the link or any ancestor gives the click back to the browser.
Explain why turbo:load replaces DOMContentLoaded, list the clicks Turbo never drives (cross-origin, target, download, .pdf, modifier keys) and show how Turbo.session.drive = false plus data-turbo="true" makes Drive opt-in.
Decide where opting out is the right call (third-party widgets, file downloads, legacy sections) and how the persistent document changes the way JavaScript must be written to avoid leaking listeners.
Frame Drive as a progressive enhancement over server-rendered pages: the server contract stays plain HTML, so the cost is discipline in client code rather than a second rendering stack.
## What a Turbo Drive visit is **Turbo Drive** is the part of Turbo that takes over page-level navigation. A new Rails 8.1 app gets it through the `turbo-rails` gem: `import "@hotwired/turbo-rails"` in `app/javascript/application.js` starts a Turbo session on import, and from then on every eligible link click and form submission becomes a **visit**. A visit to a location with the default action `advance` does this: 1. Turbo cancels the click's default navigation and fires `turbo:before-visit` (cancelable) and `turbo:visit`. 2. If a cached snapshot of the target exists, it can show it as a preview while the request runs. 3. It fetches the URL with `fetch`, sending the session cookie like any same-origin request. 4. It **replaces the `<body>`** with the response's `<body>`, **merges the `<head>`** (a changed `<title>` or meta tag is updated, identical stylesheet and script tags are left untouched) and updates the `lang` attribute on `<html>`. 5. It pushes a history entry with `history.pushState`, then fires `turbo:load`. Because the document never unloads, the JavaScript runtime and anything left in memory persist across pages. That is why code that sets up per-page behaviour listens for `turbo:load` (or lives in a Stimulus controller) instead of `DOMContentLoaded`, which fires only once. ## Clicks Drive leaves to the browser Turbo only drives a click it considers navigable. It does nothing, and the browser performs a normal navigation, when: - the click uses a modifier key (Ctrl, Cmd, Shift, Alt) or a non-primary button, or a handler already called `preventDefault()`; - the link has a `target` other than `_self`, or a `download` attribute; - the URL is cross-origin or does not start with the path in `<meta name="turbo-root">`; - the path ends in an extension Turbo treats as unvisitable, such as `.pdf`, `.zip`, `.csv` or `.png` (the list is `Turbo.config.drive.unvisitableExtensions`); - the element, or an ancestor, carries `data-turbo="false"`. ## Opting out, and opting back in `data-turbo="false"` is read from the **nearest** element that has a `data-turbo` attribute, so it works on the link itself or on any wrapper: | Markup | Result with Drive on | |---|---| | `<a href="/" data-turbo="false">` | full browser navigation | | `<div data-turbo="false">` around links and forms | every link and form inside navigates natively | | `<a data-turbo="true">` inside that div | driven again, because the nearer attribute wins | | `<form data-turbo="false">` | the browser submits and renders the response itself | The usual reasons to opt out are a page that loads a third-party widget that cannot survive a body swap, a link to a route Rails serves as a file download, or a legacy section still built around full page loads. ## Making Drive opt-in Setting `Turbo.session.drive = false` at boot inverts the rule: an element is navigable only when its nearest `data-turbo` ancestor says `"true"`. In Turbo 8 that property is a setter over `Turbo.config.drive.enabled`, and form handling has its own switch, `Turbo.config.forms.mode` (`"on"`, `"off"` or `"optin"`). Two details trip people up: - Links inside a `<turbo-frame>` still navigate their frame when Drive is off; the switch governs full-page visits. - Hover prefetching uses the same navigability check, so a link that Drive would not handle is not prefetched either. ## Visiting from code `Turbo.visit(location)` starts the same application visit from JavaScript, for instance after a Stimulus controller finishes some work. `Turbo.visit("/articles", { action: "replace" })` swaps the page in place of the current history entry instead of pushing a new one; `restore` is reserved for Back and Forward. If the location is cross-origin or outside `turbo-root`, Turbo simply sets `window.location` and the browser does a full load. ## Lifecycle events worth knowing Turbo dispatches its navigation events on `document`, and most client code hooks one of these: - `turbo:click` when a driven link is clicked; - `turbo:before-visit`, cancelable, to stop an application visit (restoration visits skip it); - `turbo:visit` when the visit starts; - `turbo:before-render`, which can pause rendering or swap in a custom render function; - `turbo:render` after each render, including the render of a cached preview; - `turbo:load` once per visit, after the page has rendered, and once on the first full page load. Picking `turbo:load` over `turbo:render` avoids running set-up code twice when a preview is followed by the fresh page. ## What this means in an interview The model to convey is **HTML over the wire with a persistent document**: the server renders whole pages exactly as without Turbo, and Turbo only changes how the browser swaps them in. Nothing about the controller changes for a GET page. The costs are the ones a persistent document brings: global listeners that pile up, scripts in the `<body>` that re-run on every render, and stale state unless code hooks the right Turbo events.
- How do you navigate from JavaScript so the user still gets a Turbo visit, and when does it fall back to a full load?Call `Turbo.visit(url)`, optionally with `{ action: "replace" }` to replace the current history entry instead of pushing one. It fires `turbo:before-visit`, so a listener can cancel it. If the URL is cross-origin or outside `turbo-root`, Turbo sets `window.location` and the browser does an ordinary page load.
- Why does a jQuery plugin initialised in a DOMContentLoaded handler stop working after the first Turbo navigation?`DOMContentLoaded` fires once, on the first real page load. Later Turbo visits swap the `<body>` without a new document, so the handler never runs for the new elements. Initialise in a `turbo:load` listener, or better in a Stimulus controller whose `connect()` runs whenever its element enters the page.
- What does Turbo.session.drive = false not switch off?It only makes full-page Drive visits opt-in. Links and forms inside a `<turbo-frame>` still navigate that frame, `<turbo-stream>` elements still render, and anything under a `data-turbo="true"` ancestor is still driven.
Turbo Drive is a stagehand who swaps the scenery between acts while the audience stays seated: the theatre, the lights and the orchestra (head assets and JavaScript) stay put, and only the set on stage (the body) changes.
- Turbo Drive replaces the whole document and re-runs every head script on each click.
- data-turbo-prefetch="false" on a link turns Turbo Drive off for that link.
- data-turbo="false" works only on the anchor itself, not on a wrapping element.
- Turbo.session.drive = false also stops Turbo Frames and Turbo Streams from working.
- Turbo Drive needs a JSON endpoint for each page, the way an SPA router does.
- data-turbo-action="replace" makes a link fall back to a full browser reload.
Assumes Turbo 8 (source 8.0.23) via turbo-rails 2.0.x, where Turbo.session.drive is a setter over Turbo.config.drive.enabled.