navigationsveltesveltekit

How to trigger LayoutServerLoad on every navigation in SvelteKit?


I want to add a JWT verification/refresh in +layout.server.ts that is called on every navigation action.

I've got following code and noticed that the console.log() statement is only ran, when I reload the page with let's say F5 and not when navigating via anchor tags for example.

import type { LayoutServerLoad } from './$types';

export const load: LayoutServerLoad = async ({ cookies }) => {
    console.log("running layout on server");
};

How can I make it that the load function is called every time when navigating?


Solution

  • As far as I know, load functions only re-run if the parameters they depend on change (or if invalidateAll is invoked).

    This logic is probably better suited to the handle hook.