# Turbo 8 Prefetch Breaks turbo_frame_request? Layouts

> Turbo 8 link prefetch can hand a frame the response meant for the full page. Why turbo_frame_request? branching fails, and how to fix it.

- Author: Curtis Miller
- Published: 2026-11-27
- Categories: rails, gotchas, javascript
- Canonical URL: https://millarian.com/posts/turbo-8-prefetch-turbo-frame-request-wrong-layout/

I had a Rails 8 modal that sometimes showed the wrong markup. The modal loads a `<turbo-frame>` and my view branches on `turbo_frame_request?`, but the frame was rendering the full page layout instead of the compact one. My system tests never caught it. The cause turned out to be Turbo 8's link prefetching.

## The setup

A pricing modal contains an empty frame. A Stimulus controller intercepts clicks on any link to `/plans`, opens a `<dialog>`, and points the frame at the link:

```js
// plans_modal_controller.js
open(event) {
  event.preventDefault()
  this.dialogTarget.showModal()
  this.frameTarget.src = event.currentTarget.href
}
```

The `/plans` view renders a compact list inside the frame and a full card layout on the standalone page. Turbo sends a `Turbo-Frame` request header on frame loads, so the view asks Rails:

```erb
<turbo-frame id="plans">
  <% if turbo_frame_request? %>
    <%= render "plans/compact", plans: @plans %>
  <% else %>
    <%= render "plans/cards", plans: @plans %>
  <% end %>
</turbo-frame>
```

Both branches wrap their output in the same frame id, so Turbo can pull the matching frame out of a response for the full page too.

## Why turbo\_frame\_request? returns false on a frame load

Turbo 8 prefetches a link's page when you hover it. That prefetch is a normal page visit, so it has no `Turbo-Frame` header, and Rails renders the full card layout. Then you click. The frame sets its `src` to the same URL, and Turbo's `turbo:before-fetch-request` handler looks for a prefetched response before hitting the network.

That check only asks whether the request is a GET and not a form, so frame loads qualify. The frame gets the cached response for the full page, which was rendered without the frame header, extracts the `<turbo-frame id="plans">` from it, and shows the card layout inside my modal.

My tests never saw this because Capybara clicks without hovering first, so nothing was ever prefetched. Adding a hover and a short wait before the click reproduced it:

```ruby
find_link("See plans").hover
sleep 0.3 # give the prefetch time to start
click_link "See plans"
```

## The fix: give the frame its own URL

Prefetches are keyed by URL, so a different URL can't hit the cache. Add a param in the Stimulus controller, and branch on `params[:modal]` in the view instead of the header:

```js
const url = new URL(event.currentTarget.href)
url.searchParams.set("modal", "1")
this.frameTarget.src = url.toString()
```

If the frame and the page can share the same markup, drop the `turbo_frame_request?` branch entirely and the problem goes away.
