A link-heavy news site moves to Turbo 8 and readers get signed out just by hovering over its 'Log out' link. Why, and how do you fix it?
answer
- InstantClick on by default
- mouseenter, then 100 ms
- X-Sec-Purpose: prefetch header
- a GET must be safe
- data-turbo-prefetch="false" is a band-aid
basics
~20 sTurbo 8 prefetches same-origin GET links on hover, so a GET /logout link runs the sign-out before any click. Make sign-out a non-GET request (button_to with method: :delete), which Turbo never prefetches; data-turbo-prefetch="false" only masks it.
solid answer
~50 sTurbo 8 turns on hover prefetching by default. About 100 ms after the pointer enters a navigable same-origin link, Turbo sends a real GET with `X-Sec-Purpose: prefetch` and keeps the response for about 10 seconds so a click can reuse it. The request runs the controller with the reader's cookies, so a GET route to `sessions#destroy` signs them out on hover, and the prefetch response's cookie changes apply. The real fix is to stop changing state on GET: route sign-out as DELETE and render it with `button_to "Log out", session_path, method: :delete`, redirecting with 303. Turbo never prefetches non-GET method links, links with `data-turbo-confirm` or `data-turbo-stream`, or leftover UJS links. `data-turbo-prefetch="false"` on the link, an ancestor, or `<meta name="turbo-prefetch" content="false">` stops the hover request but leaves an unsafe GET for any other prefetcher or crawler.
code
ruby · 5 lines# config/routes.rb
Rails.application.routes.draw do
# before: get "logout", to: "sessions#destroy" (prefetched on hover)
resource :session, only: [:new, :create, :destroy]
endgo deeper
Know that Turbo 8 fetches links on hover before the click, so a link must never change data when it is fetched.
Explain the 100 ms delay, the X-Sec-Purpose header, the 10-second reuse window and the attributes that opt a link out.
Diagnose side-effecting GETs exposed by prefetch, fix them at the route with DELETE and button_to, and weigh the extra server renders on link-dense pages.
Use the incident to enforce safe GETs across the app, and decide prefetch policy for expensive pages from measured cost rather than by switching it off globally.
## What changed with Turbo 8 Turbo 8 ships **hover prefetching** (the InstantClick technique), and the Turbo handbook states it is enabled by default since Turbo v8. The aim is to hide latency: by the time the user clicks, the page is often already downloaded. The mechanism in Turbo's link-prefetch observer: 1. A `mouseenter` listener on the document notices the pointer over an `<a href>` with no `target` and no `download` attribute. 2. If the link is prefetchable, Turbo waits **100 ms**, so links the pointer merely crosses are skipped, and leaving the link cancels the pending request. 3. It sends a GET at fetch priority `low`, with the header `X-Sec-Purpose: prefetch`. 4. The response is kept for **10 seconds** by default (`<meta name="turbo-prefetch-cache-time">` changes it); a click on the same URL within that window reuses it. ## Why the reader is signed out The prefetch is not a dry run. It is an ordinary same-origin GET carrying the session cookie, so Rails routes it, runs the action and returns its `Set-Cookie` headers, which the browser applies. A news site that routes `get "logout", to: "sessions#destroy"` and renders a plain `<a href="/logout">` therefore destroys the session the moment a reader's pointer rests on the link on its way across the header. The same failure hits other GET links with side effects: - "mark all as read" or "unsubscribe" links in a newsletter footer; - article view counters that increment in the `show` action; - "export" links that start an expensive job. ## The fix, in order of preference | Approach | Effect | Verdict | |---|---|---| | Route sign-out as DELETE and render `button_to "Log out", session_path, method: :delete` | A form, never prefetched; also correct HTTP | the real fix | | `link_to` with `data: { turbo_method: :delete }` | Turbo skips links whose method is not GET | works, less accessible | | `data-turbo-prefetch="false"` on the link or a wrapper | Turbo stops hovering it | hides the bug; any crawler or browser prefetcher still triggers it | | `<meta name="turbo-prefetch" content="false">` | prefetch off for the page | throws away the speed-up everywhere | The Rails 8.1 authentication generator already models the first row: its `SessionsController#destroy` ends with `redirect_to new_session_path, status: :see_other`, which is the redirect Turbo wants after a DELETE. ## What Turbo never prefetches Knowing the exclusions lets a reviewer see at a glance which links are exposed: - cross-origin links, links with a `target` or `download`, and links to the page already open; - links under `data-turbo-prefetch="false"` (a nearer `data-turbo-prefetch="true"` re-enables), or with `data-turbo="false"` on the link; - links Drive would not handle at all, for example with `Turbo.session.drive = false`; - links with a non-GET `data-turbo-method`, with `data-turbo-confirm`, or with `data-turbo-stream`; - links still carrying rails-ujs attributes: `data-remote`, `data-method`, `data-confirm` or `data-behavior`; - any link whose `turbo:before-prefetch` event a listener cancels with `preventDefault()`. ## Beyond the bug Prefetch also has a cost on the server. Every hover that lingers past 100 ms is a full page render, so a link-dense front page can multiply traffic to article pages. Reasonable responses: - opt expensive pages out with `data-turbo-prefetch="false"` on their links; - cancel `turbo:before-prefetch` when the browser reports a data saver or a slow connection; - keep analytics such as view counts out of GET handlers, or ignore requests that carry `X-Sec-Purpose: prefetch`, remembering a header is a hint, not a security boundary. ## Reviewing a codebase for exposure A quick audit after an upgrade to Turbo 8: 1. List GET routes in `config/routes.rb` (or `bin/rails routes`) that point at actions which write: sign-out, unsubscribe, confirm, toggle. 2. Check the layout and shared partials for plain links to those routes; headers and footers are hovered constantly. 3. Move each one to a non-GET route rendered with `button_to`, or at least mark the link with `data-turbo-method`. 4. Watch server logs for requests carrying `X-Sec-Purpose: prefetch` to see which pages hover traffic actually reaches. The lasting lesson is the old HTTP one, made urgent: **GET must be safe**. Turbo 8 simply made an existing unsafe GET fire far more often.
- Is data-turbo-prefetch="false" on the log-out link an acceptable fix?Only as a stop-gap. It stops Turbo's hover request, but the GET still changes state, so a browser's own speculative loading, a link-checking crawler or a mail scanner can still sign users out. Moving sign-out to DELETE fixes the cause.
- How would you stop hover prefetch for readers on slow or metered connections?Listen for `turbo:before-prefetch` and call `event.preventDefault()` when `navigator.connection` reports `saveData` or a 2g effective type. Turbo dispatches that event on the link before every prefetch, so one listener covers the whole site.
- Can the server tell a prefetch from a real click?Turbo marks prefetches with the request header `X-Sec-Purpose: prefetch`, so a controller can skip side effects such as view counters for them. Treat it as a hint for analytics only; it is not a reason to keep state changes on GET.
- Turbo only prefetches links that are marked with data-turbo-preload.
- A prefetch request is a dry run, so the controller action does not execute.
- data-turbo-prefetch="false" on the log-out link fully fixes the problem.
- Turbo prefetches a link the moment the pointer touches it, with no delay.
- Links with data-turbo-method="delete" are prefetched just like ordinary GET links.
- Turning off Turbo Drive's snapshot cache with turbo-cache-control stops hover prefetch.
Assumes Turbo 8 (8.0.23 source), where hover prefetch is on by default with a 100 ms delay and a 10-second cache.