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
I'm using SvelteKit and I want to save some information on the index route that is going to be useful in the load function of another route, and I wanted to save that but when I tried to save it from onDestroy, that onDestroy it turns out runs after the load function which doesn't make much sense.
Reproduction
Will be added soon if needed, but this is sum up:
src/routes/index.svelte:
<scriptcontext="module">
// irrelevant load function
</script><script>import{onDestroy}from'svelte';importmyStorefrom'$lib/mystore';exportletposts;onDestroy(()=>{console.log('before navigation');myStore=[...posts];});</script>
src/routes/[slug].svelte:
<scriptcontext="module">
import myStore from '$lib/mystore';
import {browser} from '$app/env';
export async function load({page,fetch}){if(browser){console.log('after navigation');return{props:{posts: [...myStore]}};}}</script><script>// some irrelevant code</script>
Logs
This code logs:
after navigation
before navigation
expected:
before navigation
after navigation
It makes sense because the new page isn't created in place of the current one until it has loaded data. For that reason, the current page isn't destroyed until after load finishes running.
You probably want an onNavigate lifecycle function, which there are other issues for.
As @babichjacob mentioned, we need to run load first because, until it runs, we don't know what pages/components we're going to be rendering, and we don't want to have an interval where there's nothing on the screen.
Describe the bug
I'm using SvelteKit and I want to save some information on the index route that is going to be useful in the
load
function of another route, and I wanted to save that but when I tried to save it fromonDestroy
, thatonDestroy
it turns out runs after theload
function which doesn't make much sense.Reproduction
Will be added soon if needed, but this is sum up:
src/routes/index.svelte
:src/routes/[slug].svelte
:Logs
System Info
Severity
blocking an upgrade
Additional Information
No response
The text was updated successfully, but these errors were encountered: