Content-Length: 250716 | pFad | https://github.com/sveltejs/kit/issues/13780

4A Redirect from +layout.server.ts not running redirected pages onMount · Issue #13780 · sveltejs/kit · GitHub
Skip to content

Redirect from +layout.server.ts not running redirected pages onMount #13780

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

Open
gregg-cbs opened this issue May 9, 2025 · 3 comments
Open

Comments

@gregg-cbs
Copy link

gregg-cbs commented May 9, 2025

Describe the bug

Short; When redirecting from a page to another page and then back quickly it seems onMount does not run again.

If i have page1 and in does a goto("/page2"), and page2 has does a redirect back to page1 like this redirect(302, "/page1") then the onMount of page1 does not run again.

Im guessing the redirect is so quick that the unmount probably does not fully happen and so a mount does not happen. Im not sure. But it is causing issues for me as explained below and shown in the stackblitz reproduction.


Instructions

Login page does "goto("/dashboard")"

onMount(() => {
  if (somecondition) {
   goto("/dashboard")
  }
})

Dashboard does redirect back to the login: /login?redirect="blabla"

export async function load({ parent, url, untrack }) {
  throw redirect(302, `/login?redirect=blabla`)
}

Login page onMount does not run

  onMount(() => {
    console.log("MOUNTING")
    console.log(window.location.href) 
  })
}

Reproduction

This behaviour can be seen in this reproduction which has clear instructions and UI for ease:
https://stackblitz.com/edit/sveltejs-kit-template-default-guhascda?file=src%2Froutes%2Flogin%2F%2Bpage.svelte

System Info

System:
    OS: Windows 10 10.0.19045
    CPU: (16) x64 AMD Ryzen 7 3700X 8-Core Processor
    Memory: 15.49 GB / 31.92 GB
  Binaries:
    Node: 22.8.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.21 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 10.9.2 - D:\Devbox\playground\utickets-monorepo\node_modules\.bin\npm.CMD
    pnpm: 8.6.3 - ~\AppData\Local\pnpm\pnpm.EXE
    bun: 1.2.2 - C:\Program Files\nodejs\bun.CMD
  Browsers:
    Edge: Chromium (135.0.3179.98)

Severity

serious, but I can work around it

@gregg-cbs gregg-cbs changed the title Redirect from +page.layout.ts not showing new url in onMount Redirect from +page.layout.ts not running redirected pages onMount May 9, 2025
@gregg-cbs gregg-cbs changed the title Redirect from +page.layout.ts not running redirected pages onMount Redirect from +layout.server.ts not running redirected pages onMount May 9, 2025
@hyunbinseo
Copy link
Contributor

Isn't this expected? The /login/+page.svelte has never been unmounted.

I guess you can achieve the expected with $effect and page.url — on re-redirect to self, change the URL search query.

@gregg-cbs
Copy link
Author

gregg-cbs commented May 12, 2025

Yeah i get technically it hasnt unmounted because of SSR smartness but not because of expected behaviour.

If I hit the route in the dashboard: /dashboard
From there i redirect back to login: /login
^ That is a route change and I would expect the components to behave as such.

What you are saying is that any page that gets redirected back to itself from another page will not behave as other redirects do? Which means if I have a case where my dashboard returns the user back to the page they came from, which could be any page in the app... then every page needs $effect just to account for this specific redirect behavior... because redirect has split personalities.

This is a problem, a small one and an unlikely one but one that could still cause bugs in apps. In some cases this might actually benefit developers but in my case it is unexpected and the $effect is not an ideal solution, its a patch.

I guess that is the tradeoffs of doing the SSR ahead of time and there is not much i can do about this? I dont want to use $effect

@adamjkb
Copy link
Contributor

adamjkb commented Jun 3, 2025

on/after/beforeNavigate is still available as a hook so that could also be used to get the correct URLs. Because of client side navigation it'll be always difficult to rely on onMount to happen at the "expected" time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://github.com/sveltejs/kit/issues/13780

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy