-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Why is redirect from __layout.svelte
ignored, when an unkown route is hit?
#1575
Comments
Can you explain your use case and why you'd want to redirect in the layout even if an error occurs? |
I expect the following:
BUT the above is not what happens, despite the layout's load function actually being called(!) |
Right. But what's your use case? I.e. what application are you building, what is the user doing in your application, what are you trying to accomplish? |
My usecase here is to redirect user to login, when trying to access a route that does not exists. Combined with layout resets, that would be a powerfull way to show some users 404 pages, and redirect others to login (depending on what layout is used). |
If you have a redirect on the error page as requested in #1574 why do you need one on the layout to do that? |
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 😅 |
It's not ignored in the general case. But if you hit an error then it might not have a chance to execute. That actually seems like the more intuitive behavior in my mind. |
But the __error.svelte is using the __layout.svelte, isnt it? But to sump up, the bug is here. __error.svelte ignores the redirect of the __layout.load What happens if multiple redirect statements are up here, like nested layouts or one in layout and one in the route itsself? |
Wouldn't you expect to put the redirect in |
the issue goes for __error.svelte as well, it does not respect the redirect return - you linked it already But in my case, I worked in folders and nested layouts and with reset, So i knew, if the basic layout loaded, its either 404 or not logged in - special case. but still we have to point out
|
My use case is the following: when a page not found, instead of a dedicated error page I would like to be able to display a notice on the page to which I redirected to. Something like this: I can imagine two solutions for this:
The latter might be useful in case of non-existing details page automatically redirect to the list page: |
I'm still not understanding how the layout is involved in that case |
Think about a /admin/ route... Put there a layout file, and do the isAdmin check here in the load function of the layout file, and redirect if not logged in to a login page. Doing this is useful, because you dont have to do it in every route of the /admin/ folder and so on.... But well we could just export a variable in load function and check it afterwards and redirect with goto |
@Kapsonfire-DE if you're hitting those routes and they exist then it's not a 404. The question would be what should happen when you hit |
in that case yes - but I have another usecase in my mobile game, where i want to reset to a valid state and in that case im gonna redirect |
I think I might have figured out a likely culprit to missing redirects (be it in The redirect returned by your page's So after this function call of |
Now I'm working on #2424. So I read all comments but still, I'm confusing what is the issue. I summarized this issue. (CLOSED) Issue1 : Redirect if request page is 404
Already maintainers talked about it at maintainer's meeting. Some comments in the issue mentioned regarding 404 case, (CLOSED maybe) Issue2: Redirect if some condition is true (e.g. Login Status)
In that case we can use So I think this is also not issue. (Is this correct understanding??) (???) Issue3: DocumentationAccording Issue1, https://kit.svelte.dev/docs#layouts-error-pages |
The semantics get awfully fuzzy if you try to redirect while rendering an error page. What if an error happens while rendering the page you were redirected to — do we start again, re-executing the root In fact I would say that in general, interfering with errors is dangerous — if something goes wrong, it's usually better to just communicate it than to try and course-correct, since the number of ways in which things can fail usually exceeds our ability to plan for them. Luckily though, there is a very easy way to get the behaviour you want in the 404 case — just add a catch-all rest route (and I would typically do the redirecting there rather than in the We probably do need better error messages here — if a |
…ng error pages (#4945) * document that status, error, redirect, cache are ignored when rendering error pages (#1575) * move note * move it again * Update documentation/docs/04-loading.md Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>
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 |
I have a small reproducible example here.
In the above repo, there is
__layout.svelte
file, that will redirect like this: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?
The text was updated successfully, but these errors were encountered: