You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you have a [slug].svelte route page that has a link to itself, when you click on the link, the page <script> is not executed as expected. This causes all the page variables not to be refreshed and the page renders the previous values instead of the new ones.
Reproduction
To reproduce the issue:
Create a new sveltekit project
Create /routes/test/[slug].svelte file with this code:
This is expected — components aren't destroyed and recreated when you navigate, partly because it's inefficient but mostly because it prevents you from e.g. making smooth page transitions (by tweening between values and so on). Effectively this is what's happening:
<YourPagedata={{ slug }}/>
As slug changes, the data prop updates, but let slugWas = data.slug is not re-evaluated. You have a couple of options, depending on your exact use case:
$: slugWas=data.slug;
afterNavigate(()=>{slugWas=data.slug;});
There is a bug shown above — clicking slug2 yields slug2/slug2 when it should yield slug1/slug2 — but that's because of an unrelated HMR issue that will be fixed by #4891.
@Rich-Harris thanks for the explanation.
This is somehow surprising as it seems that it can easily lead to unexpected errors. Anyway, to avoid this problem, are you suggesting that I should create EVERY variable using the reactive syntax $: variable = ...?
Describe the bug
If you have a [slug].svelte route page that has a link to itself, when you click on the link, the page <script> is not executed as expected. This causes all the page variables not to be refreshed and the page renders the previous values instead of the new ones.
Reproduction
To reproduce the issue:
npm run dev
slug2
link. Now the page renders (correct):slug1
link. Now the page renders (WRONG, theslugWas
variable is not updated):Logs
No response
System Info
Severity
blocking all usage of SvelteKit
Additional Information
No response
The text was updated successfully, but these errors were encountered: