Open dimfeld opened 11 months ago
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. https://github.com/sveltejs/kit/blob/07c76b1c2a5c5fe79214b585f1cebc1fba4bdc94/packages/kit/src/runtime/client/client.js#L1829-L1834
The root error page includes the root layout, so run those layout files at src/routes/+layout...
https://github.com/sveltejs/kit/blob/07c76b1c2a5c5fe79214b585f1cebc1fba4bdc94/packages/kit/src/runtime/client/client.js#L864-L871
We run the load function, which throws an error (in our case a redirect), and that error isn't caught anywhere. https://github.com/sveltejs/kit/blob/07c76b1c2a5c5fe79214b585f1cebc1fba4bdc94/packages/kit/src/runtime/client/client.js#L542
We 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).
when the error occurs inside the root +layout.js or +layout.server.js, since the root layout would ordinarily contain the +error.svelte component. In this case, SvelteKit uses the fallback error page.
We should handle the error thrown by the load_root_error_page
call
I'm getting this error too. In my case, my app is an SPA, and I've implemented a check in load()
which calls error(404)
whenever a certain value is not retrieved. The page just shows a white screen and in the console:
After doing a step through in Dev Tools, I can confirm that the issue plays out just as eltigerchino narrated.
My workaround is to make an error route where I This won't work, it just threw the page into an infinite loop. What I did was nest my whole routes directory one level down via goto()
every time I have an error./(first-level)/
, and place an +error.svelte
file at the root.
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