Laravel ↔ Vue/React: Inertia.js Redirect and Status‑Code Interoperability
0 reputation · 11 Jan 2020, 18:51 UTC
0 reputation · 11 Jan 2020, 18:51 UTC
Determine how Inertia.js should behave when a server framework (e.g., Laravel) returns a 302 status code together with a valid Inertia JSON payload that includes a redirect key, and how the various client adapters (Vue, React, Svelte) interpret that response.
redirect key is not formally part of the public contract, and the default shouldRedirect callback implementation is undocumented.Specific questions:
redirect key?redirect key guaranteed to remain stable across future Inertia releases?35275 reputation · 12 Jan 2020, 05:00 UTC
Inertia.js is designed around a specific contract: a successful page visit requires an HTTP 200 status code and the X-Inertia header. A 302 response containing an Inertia JSON payload is non-standard behavior and should not be used as a reliable mechanism for client-side navigation.
redirect KeyTo answer your specific questions regarding 302 responses and the redirect key:
redirect key is not part of the public Inertia.js contract. Relying on it for application logic is risky, as its presence and interpretation are implementation details that are not guaranteed to remain stable across future releases.The recommended pattern for error states is to return the appropriate HTTP status code (e.g., 403, 404, 500) along with a valid Inertia page object that renders a dedicated error component. Inertia adapters are designed to catch these statuses and render the returned error page rather than treating them as successful page transitions.
To ensure consistent behavior across all client adapters, avoid mixing 3xx status codes with JSON bodies. Use these established patterns instead:
| Goal | Laravel Implementation | Client Result |
|---|---|---|
| Navigate to new page | return redirect()->route('name'); |
Client performs a new Inertia visit to the target URL. |
| Render target content | return Inertia::render('Component'); |
Client renders the component with HTTP 200. |
| Show Error Page | abort(404); (configured to return Inertia) |
Client renders the 404 component with HTTP 404. |
To verify your current environment's behavior, you can use the following scoped checks:
node_modules/@inertiajs/core source code to see if your specific version contains logic for a redirect key; if it is not explicitly handled in the core visitor logic, it is not a supported feature.Missing Diagnostic: To provide a more specific recommendation, please specify the version of @inertiajs/core you are using, as response handling has evolved between major versions.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.