Skip to content

Frame-redirect following

When a request made by a Turbo Frame gets a redirect, Turbo follows it and loads the target inside the frame. After a form submit, you usually want the whole page to go there instead. TurboFrameListener makes that the default, without any change to your controllers:

  1. Turbo FramePOST, Turbo-Frame: project-edit
  2. Controller302, Location: /projects/1
  3. TurboFrameListener204, Turbo-Location: /projects/1
  4. Browserfull Drive visit of /projects/1

The browser side is the turbo:before-fetch-response listener of TurboHandler: it reads Turbo-Location, cancels the frame render and visits the URL.

The listener subscribes to kernel.response. It replaces the response with an empty 204 No Content carrying a Turbo-Location header set to the original Location when:

  1. the response is a redirection (3xx) with a Location header: a 304 Not Modified (a frame revalidated with an ETag) is left alone,
  2. and the request has no Turbo-Frame-Follow-Redirect header (the frame keeps its redirects),
  3. and one of:
    • the request has a Turbo-Frame header,
    • the request is a DELETE sent by Turbo (an X-Turbo-Request-Id header on Turbo 8, an Accept header with text/vnd.turbo-stream.html on Turbo 7), and follow_delete_redirects is on.

The 204 keeps the headers of the redirect response, cookies and Cache-Control included: only the status, the Location header (moved to Turbo-Location) and the body headers (Content-Type, Content-Length) change. Flash messages live in the session, whose cookie Symfony adds later in kernel.response.

A 301 / 302 that is not converted gets a 303 See Other when it answers a non-GET Turbo request. Any other response passes through unchanged.

redirectToRoute() answers a 302 Found. The browser follows a 301 or a 302 answered to a PATCH, PUT or DELETE fetch with the same method, on the redirect target: Turbo sends these methods for a data-turbo-method="patch" link, or a <form method="patch">. The target then gets a PATCH, and usually answers 405 Method Not Allowed. With a 303 See Other, the browser follows with a GET.

So TurboFrameListener turns a redirect into a 303, keeping its Location and other headers, when:

  1. the response is a 301 or a 302 with a Location header, not converted to a Turbo-Location visit,
  2. and the request method is not safe (anything but GET, HEAD, OPTIONS and TRACE),
  3. and Turbo sent the request: an X-Turbo-Request-Id header (Turbo 8), or an Accept header with text/vnd.turbo-stream.html (the form submissions of Turbo 7 and 8).

This covers the redirects that frames with data-turbo-follow-redirect keep: the frame loads the target with a GET. A POST redirect is followed with a GET either way, and gets the 303 too. A 307 or a 308 is left alone: these statuses ask the browser to keep the method. So is the redirect of a request Turbo did not send, such as an API client’s.

A 302 answered to a DELETE fetch is followed by the browser with the same DELETE method, on the redirect target. Converting the redirect into a Turbo-Location visit avoids that, for frame and non-frame Turbo requests alike, except for frames that keep their redirects, whose redirect gets a 303 instead.

A DELETE outside a frame is recognized as a Turbo request by its X-Turbo-Request-Id header, which Turbo sends since 8.0, or by the text/vnd.turbo-stream.html that Turbo 7 and 8 accept on their form submissions (a data-turbo-method="delete" link included).

Turn it off with the follow_delete_redirects option, or the followDeleteRedirects constructor argument without the framework. The redirect then gets a 303, which Turbo follows with a GET, outside a frame as inside:

config/packages/silarhi_turbo.yaml
silarhi_turbo:
follow_delete_redirects: false

Add data-turbo-follow-redirect to a <turbo-frame> to opt it out of the escalation: the handler sends a Turbo-Frame-Follow-Redirect header with that frame’s requests, and TurboFrameListener leaves all their redirects untouched, DELETE ones included. See Keeping a redirect in its frame.

Something in the browser must follow the Turbo-Location header: on its own, Turbo receives an empty 204 and the frame does not change. If you don’t use TurboHandler, add the equivalent listener:

import * as Turbo from '@hotwired/turbo'
document.addEventListener('turbo:before-fetch-response', (event) => {
const location = event.detail.fetchResponse.response.headers.get('Turbo-Location')
if (location) {
event.preventDefault()
Turbo.visit(location)
}
})

The HTTP headers reference lists the whole contract between the two halves.