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

Most upvoted comments

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

User attempts to access application, an unknown route is hit __layouat.svelte is then processed, and determines in this case it’s best to redirect to /login

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

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)

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 /admin/users.svelte /admin/news.svelte /admin/groups.svelte and so on…

In that case we can use __layout.svelte or __layout.reset.svelte. And load 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?