+page.server.js and writable stores, load always re-run #6529
Unanswered
fabienc974
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi all,
I'm doing my app from scratch with latest sveltekit.
I have an issue with the load function in my +page.server.js which always re-run when I visit my +page.svelte
I've read the doc and it says that the load function won't re-run except in specific case : https://kit.svelte.dev/docs/load#invalidation
I'm think I'm not in any of these cases.
What I do is :
``
// imports
import { read } from '$lib/graphql/organisation-gql';
import { error } from '@sveltejs/kit';
// variables
export const load = async (
/** @type {{ locals: { jwt: any; id: any; username: any; }; }} */ event
) => {
// récupération des données de sessions
const { jwt, id, username } = event.locals;
};
``
`<script>
// imports
import Slotter from '$lib/ui/back/Slotter.svelte';
import { organisationStore } from '$lib/store/organisation-store';
But, when I navigate my site, if I navigate to my organisation page, I'm not getting the datas from the store, but from the load function which re-runs again;
To be sure my store was populated with datas, I call it in integration page, and the datas loaded from the store as expected, without new query. (To make this example work, I first need to visit my organisation page, otherwise the store will never be instantiated with initial values, and my integration page will throw an error.)
////// tested another way
I've tested another approach, without +page.server.js on other pages of my project
Problem is, when the page load/shows, I should await the datas to be fetched before rendering it for the first time.
When I navigate again, datas are gathered from the store.
(In the following page, the store has already been called in my +layout.svelte)
`
<script> // imports import { integrationStore } from '$lib/store/integration-store'; import IntegrationTable from '$lib/ui/back/account/IntegrationTable.svelte'; import Slotter from '$lib/ui/back/Slotter.svelte'; // variables // functions //reactives $: elem = $integrationStore; </script> {#if elem.length !== 0} {:else} Loading... {/if} `I don't know which approach is better.
I'll prefer the first approach with the load function, but I don't know how to stop it to re-runs.
If you have a better idea !
Thanks for your help
Beta Was this translation helpful? Give feedback.
All reactions