Repository navigation
Replies: 1 comment
|
This is expected behaviour, not a bug: Why it happens: when you prerender, SvelteKit's crawler treats every page as its own isolated request. For each route it discovers, it builds a This is consistent with how prerendering is documented to work: each prerendered route is generated by exercising your app as if it were a normal request (see the The fix: cache the CMS call yourself at module scope in // src/routes/+layout.server.ts
let cache: Promise<LayoutData> | undefined;
export const load: LayoutServerLoad = () => {
cache ??= fetchFromCms();
return cache;
};
async function fetchFromCms() {
// your existing CMS call
}Caching the promise (not just the resolved value) matters if you ever raise One caveat: only do this for data that's the same for everyone (which CMS content usually is). Don't use module-level caching like this for anything user-specific in a normal (non-prerendered) SSR route, since the module stays alive across real requests from different users on a live server and you'd leak data between them. |
Uh oh!
There was an error while loading. Please reload this page.
I have layout.server.ts set with
prerender = true. It is collecting data from the headless CMS.As I have components using this data, in layout.svelte I have
setContext('layoutData', data)and in the componentsgetContext('layoutData').This is resulting in layout.server.ts collecting data from the CMS multiple times during build. How do I stop this?
All reactions