Closed Fruup closed 1 week ago
me too.
Does anyone know why this happens? My $effect runs twice also when console.log() an update on my state.
I'm running into this as well. I want to register a page view with my analytics library, so I have somethign like this:
onMount(() => {
if (!dev) {
analytics.init(env.PUBLIC_PROJECT_ID, { apiURL: env.PUBLIC_API_HOST })
}
})
$effect(() => {
const url = $page.url.pathname
if ($page.url.searchParams.toString() !== "") {
url += `?${$page.url.searchParams.toString()}`
}
if (!dev) {
analytics.trackPageview(url)
}
})
However, I end up getting 2x page views for each navigation. Looks like that trackpageView
call in the $effect
(which depends on $page
) is called twice :thinking:
Using the following versions:
"@sveltejs/kit": "2.5.18",
"svelte": "5.0.0-next.175",
I'm not able to reproduce this. Does this still occur in the latest version? If so, please provide a reproduction link (Stackblitz or github repo)
@dummdidumm Seems to be fixed. Can't reproduce either (https://stackblitz.com/edit/sveltejs-kit-template-default-yd5fgs?file=src%2Froutes%2F%2Blayout.svelte).
Closing as fixed then 👍
Describe the bug
This concerns the early access version of Svelte 5.
I have a page with a dynamic route parameter (
/docs/[...path]/+page.svelte
) that listens for the parameter (in$page.params.path
) to change with an$effect
. When initially loading the page, the effect is run twice instead of once.This issue is not limited to dynamic route parameters.
Reproduction
Put this in a
+page.svelte
file somewhere:Logs
System Info
Severity
annoyance
Additional Information
Weirdly, inserting another
$effect
depending on$page
solves this issue:Now the effect only runs once.