kit: Why is redirect from `__layout.svelte` ignored, when an unkown route is hit?
I have a small reproducible example here.
In the above repo, there is __layout.svelte
file, that will redirect like this:
return {
status: 302,
redirect: '/login'
}
Works great when a route is matched, but if there is an error the redirect is not respected. The load function does actually run, but the browser is not redirected to login.
Is this a bug, not allowed - or do I misunderstand something here?
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 1
- Comments: 19 (18 by maintainers)
Commits related to this issue
- document that status, error, redirect, cache are ignored when rendering error pages (#1575) — committed to sveltejs/kit by Rich-Harris 2 years ago
- allow disableScrollHandling to be called in afterNavigate - fixes #1575 — committed to sveltejs/kit by Rich-Harris 2 years ago
- document that status, error, redirect, cache are ignored when rendering error pages (#4945) * document that status, error, redirect, cache are ignored when rendering error pages (#1575) * move not... — committed to sveltejs/kit by Rich-Harris 2 years ago
Ok so It’s a bit hard to explain without a conversation for me. But let’s be concrete. Why is a redirect from __layout.svelte ignored? The load function runs, and returns a redirect. The redirect is then, silenty, ignored. That’s is the purpose of this issue to put clarification to. As a developer, I expect a redirect to happen when I return a redirect 😅
Going to close this as it’s been obsoleted by the changes in #5748 — it’s no longer possible for an error page to have a
load
function, meaning this situation can never arise (and errors need to be dealt with more explicitly)Now I’m working on https://github.com/sveltejs/kit/issues/2424. And inside this issue, there is a comment that https://github.com/sveltejs/kit/issues/2424 is maybe related to this.
So I read all comments but still, I’m confusing what is the issue.
I summarized this issue. So Could you please tell me if wrong or lacking point is there. And let me know about the Issue3 about your opinion.
(CLOSED) Issue1 : Redirect if request page is 404
Already maintainers talked about it at maintainer’s meeting. And their suggestion is below way (Please see the link). https://github.com/sveltejs/kit/issues/1574#issuecomment-919336290
Some comments in the issue mentioned regarding 404 case, but this is already closed and we don’t need to discuss it.
(CLOSED maybe) Issue2: Redirect if some condition is true (e.g. Login Status)
In that case we can use
__layout.svelte
or__layout.reset.svelte
. Andload
function is working properly at least in my environment. (I can redirect wherever I want.)So I think this is also not issue. (Is this correct understanding??)
(???) Issue3: Documentation
According Issue1,
__error.svelte
can not use redirect. But actually, the document doesn’t mention that. So maybe more people will have the same confusion in the future. We should update the document about it IMO but what do you think?https://kit.svelte.dev/docs#layouts-error-pages https://kit.svelte.dev/docs#loading-output-redirect
But the __error.svelte is using the __layout.svelte, isnt it? so the redirect should execute. this is not the case - I had this issue before. Why I needed it: I didnt want to give the user a 404 Error page for my mobile game - Instead I wanted to recover to a proper state (home route). I ended up, to make a redirect on the __error.svelte itsself with goto function - this worked.
But to sump up, the bug is here. __error.svelte ignores the redirect of the __layout.load But there are circumstances which should be pointed out…
What happens if multiple redirect statements are up here, like nested layouts or one in layout and one in the route itsself?