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:
- Turbo FramePOST, Turbo-Frame: project-edit
- Controller302, Location: /projects/1
- TurboFrameListener204, Turbo-Location: /projects/1
- 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.
When a redirect is converted
Section titled “When a redirect is converted”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:
- the response is a redirection (
3xx) with aLocationheader: a304 Not Modified(a frame revalidated with anETag) is left alone, - and the request has no
Turbo-Frame-Follow-Redirectheader (the frame keeps its redirects), - and one of:
- the request has a
Turbo-Frameheader, - the request is a
DELETEsent by Turbo (anX-Turbo-Request-Idheader on Turbo 8, anAcceptheader withtext/vnd.turbo-stream.htmlon Turbo 7), andfollow_delete_redirectsis on.
- the request has a
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.
303 See Other for the other redirects
Section titled “303 See Other for the other redirects”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:
- the response is a
301or a302with aLocationheader, not converted to aTurbo-Locationvisit, - and the request method is not safe (anything but
GET,HEAD,OPTIONSandTRACE), - and Turbo sent the request: an
X-Turbo-Request-Idheader (Turbo 8), or anAcceptheader withtext/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.
DELETE redirects
Section titled “DELETE redirects”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:
silarhi_turbo: follow_delete_redirects: falseKeeping a redirect in its frame
Section titled “Keeping a redirect in its frame”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.
Without @silarhi/turbo
Section titled “Without @silarhi/turbo”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.
