-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
slots are being called twice from __layout.svelte when dealing with $page store and the logic {#if block #2527
Comments
Hi. I think this is a timing issue between component lifecycle and store lifecycle. Component lifecycle and store lifecycle don't have a relationship regarding timing. By the way, can you apply this change as a workaround? {#if path.endsWith('MyComp')}
{log('about to display MyComp from __layout.svelte')}
<p>Comp1 from __layout.svelte</p>
{:else}
{log('about to display index.svelte from __layout.svelte')}
<p>index.svelte from __layout.svelte</p>
{/if}
<!-- MOVE SLOT HERE -->
<slot /> I would be happy if this information is helpful for you. |
Oh, I simplified the case for the example, I need to add a container, something like
thats why I can't move the slot any other idea? |
IMO at least for now:
<script lang="ts">
import { page } from '$app/stores';
$: className = $page.path.endsWith('MyComp') ? 'container' : '';
</script>
<div class={className}>
<slot />
</div>
<a href="/">root</a>
<a href="/MyComp">MyComp</a> But anyway, So I personally prefer option 1,2 or some related way. I don't know these instant ideas can be applied to your real problem. |
Gah, this is tricky. We basically can't trigger page store subscribers until navigation has occurred, because otherwise they will fire inside components that are being unmounted as a result of the navigation. Which means that navigation happens, the If there's a solution to this, it's not obvious to me. The best workaround is probably to use named layouts. |
Since this is a SvelteKit internal issue, a pragmatic solution is to expose the current url(a piece of) value from your +layout.server.ts and use that as the key (your path) instead of relying on the $page store. |
Please fix this Mr Rich Harris, this is "small" but annoying for sveltekit user like me, thankyou mr rich harris
|
I am having the same issue with svelte@4.2.0 (not sveltekit) |
Describe the bug
I have a _layout which should get the
<slot />
content inside a container depending on the path.So I use $page.path to detect when there's a change in the path, and an
{#if path === 'xxx'}
block to handle each case.something like this:
At start everything works fine, layout is initialized one and the component is initialized once too
From there on, every time I navigate to another page the component is initialized twice
Reproduction
Repo at gihub: https://github.com/opensas/sveltekit-slots-called-twice-error
Open the repo in gitpod: https://gitpod.io#github.com/opensas/sveltekit-slots-called-twice-error
in terminal
click open browser (allow popups)
Logs
No response
System Info
Severity
serious, but I can work around it
Additional Information
No response
The text was updated successfully, but these errors were encountered: