Livewire continues processing redirected HTML response after handling fetch redirect
What happened?
When a Livewire update request receives an HTTP redirect (for example because an auth/session middleware redirects to /login), the browser fetch() follows the redirect and Livewire receives the final HTML response.
Livewire detects response.redirected === true and invokes the redirect handler, which eventually calls window.location.href = response.url. However, the request flow continues processing the already-read response body after that point. Because the body is usually an HTML login page rather than a Livewire JSON payload, Livewire can still try to parse/process the redirected HTML before the browser completes the top-level navigation.
In practice this can cause a brief flash of redirected, unstyled HTML or an error fallback/modal before the navigation to the redirected URL completes.
Reproduction
- Have a Livewire component with any action that sends a normal Livewire update request.
- Make the server respond to that update request with a normal HTTP redirect, such as
302 Location: /loginfrom an expired session/auth middleware. - Trigger the Livewire action after the session expires.
- The update request is redirected to
/login, so the final fetch response body is the login page HTML. - Livewire calls the redirect handler, but then continues to parse/process that HTML response as if it were the Livewire payload.
Expected behavior
After Livewire handles a non-prevented response.redirected redirect, it should stop processing the response body for that request.
If a userland redirect hook calls preventDefault(), then continuing the current flow may still make sense. But when the default redirect behavior actually runs, the request should return early instead of attempting to parse the redirected HTML as JSON.
Possible fix direction
Have the redirect handler report whether it actually performed the redirect, then return early in the request flow only for non-prevented redirects. Roughly:
if (response.redirected) {
let handled = handlers.redirect(response.url)
if (handled) {
handlers.finish()
return
}
}
and make the redirect handler return false when preventDefault() was called, otherwise call window.location.href = url and return true.
Why this matters
Applications can work around this by returning JSON/401 for Livewire requests instead of 302, but a regular Laravel auth/session redirect is a common failure mode. Once Livewire has already decided to perform a top-level redirect, continuing to process the redirected HTML body is surprising and can briefly leak an unstyled intermediate page to users.
@jancinert thanks for reporting! I've submitted a PR #10369 with a fix.