New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SPA Mode: Redirect from +layout.ts doesn't work on 404 #11099
Comments
The issue stems from not handling errors thrown while rendering the root error page. If an error occurs during hydration, such as a 404 missing route, we load the root error page. kit/packages/kit/src/runtime/client/client.js Lines 1829 to 1834 in 07c76b1
The root error page includes the root layout, so run those layout files at kit/packages/kit/src/runtime/client/client.js Lines 864 to 871 in 07c76b1
We run the load function, which throws an error (in our case a redirect), and that error isn't caught anywhere. kit/packages/kit/src/runtime/client/client.js Line 542 in 07c76b1
Ideal solutionWe need to catch and handle any redirects or errors thrown from the error helper. For all other errors, we need to display the static fallback error page (according to https://kit.svelte.dev/docs/errors#responses).
We should handle the error thrown by the kit/packages/kit/src/runtime/client/client.js Lines 1821 to 1835 in 07c76b1
|
Seems to be related to #11494 |
I just opened the PR for fallback to default error page when navigating |
Describe the bug
With the following conditions:
Expected Behavior: It either redirects or the error page shows up.
Actual Behavior: Neither the error page nor the redirect works. Instead nothing loads at all.
Notes
In the actual case where I encountered this, the redirect was to a login page when we detect that the user is not logged in.
This behavior happens either on initial load or from a client-side navigation, but only when SSR is disabled.
The redirect shows up in the console as an uncaught rejection, which hints at it maybe starting the layout load function but not actually awaiting it once it can't find a page? I haven't looked into it more yet though.
Workaround
Avoid throwing the redirect if
route.id
is empty. Might not work for unexpected errors though?Reproduction
https://github.com/dimfeld/sveltekit-redirect-on-404
Load the app at
http://localhost:5173/bad_route
or click the bad route link from the login page, and see that nothing shows up at all.Logs
No response
System Info
Severity
serious, but I can work around it
Additional Information
No response
The text was updated successfully, but these errors were encountered: