Layout load functions help you get data before showing a page layout. This makes sure your page has what it needs right away.
Layout load functions in Svelte
export async function load({ fetch, params, url }) { // fetch or prepare data here return { key: value }; }
The load function runs before the layout renders.
It returns an object whose properties become available in the layout component.
export async function load() { return { title: 'Welcome to My Site' }; }
export async function load({ fetch }) { const res = await fetch('/api/user'); const user = await res.json(); return { user }; }
export async function load({ url }) { const lang = url.searchParams.get('lang') || 'en'; return { lang }; }
This example shows a load function in +layout.js providing a greeting message to +layout.svelte before rendering. The greeting is shown in the header.
<!-- +layout.js --> export async function load() { return { greeting: 'Hello from layout load!' }; } <!-- +layout.svelte --> <script> export let data; </script> <header> <h1>{data.greeting}</h1> </header> <slot />
Layout load functions run on both server and client during navigation.
Returned properties are reactive and update the layout when changed.
Use fetch inside load to get data safely with SvelteKit's routing.
Layout load functions fetch or prepare data before the layout shows.
They return an object whose properties the layout component can use.
This helps share data across pages and improve user experience.
Practice
load function in a Svelte layout?Solution
Step 1: Understand the role of load functions in Svelte layouts
Load functions run before the layout renders to get data ready.Step 2: Identify what load functions do
They fetch or prepare data so the layout can use it immediately.Final Answer:
To fetch or prepare data before the layout renders -> Option BQuick Check:
Layout load = fetch data before render [OK]
- Confusing load with styling or event handling
- Thinking load defines routes
- Assuming load runs after layout renders
load function in SvelteKit?Solution
Step 1: Recall the modern SvelteKit load function syntax
Load functions are async and return data directly, not inside a props object.Step 2: Check each option's return value
export async function load() { return { user: 'Alice' }; } returns { user: 'Alice' } directly, which is correct for layout load.Final Answer:
export async function load() { return { user: 'Alice' }; } -> Option AQuick Check:
Load returns data directly, no props wrapper [OK]
- Returning data inside a props object (old pattern)
- Missing async keyword
- Not exporting the load function
export async function load() {
return { user: 'Bob', theme: 'dark' };
}What data will be available in the layout component's
data prop?Solution
Step 1: Understand what the load function returns
The load function returns an object with keys user and theme.Step 2: Know how layout receives load data
The layout component receives the returned object as its data prop directly.Final Answer:
{ user: 'Bob', theme: 'dark' } -> Option AQuick Check:
Load return = layout data prop [OK]
- Expecting data inside a props object
- Assuming data is undefined without explicit props
- Only partial data returned
export function load() {
const user = fetch('/api/user');
return { user };
}Solution
Step 1: Check if fetch is awaited
fetch returns a promise, so it must be awaited inside an async function.Step 2: Verify load function declaration
load must be async to use await; here it is missing async.Final Answer:
Missing async keyword and await for fetch -> Option DQuick Check:
Async + await needed for fetch in load [OK]
- Forgetting async keyword
- Not awaiting fetch promise
- Returning unresolved promise instead of data
Solution
Step 1: Understand data flow in SvelteKit layouts
Layout load functions run once and share data with nested pages via the data prop.Step 2: Identify best practice for shared data
Fetching user data in layout load and returning it allows nested pages to access it automatically.Final Answer:
Use a layout load function that fetches user data and returns it; nested pages access via data -> Option CQuick Check:
Layout load shares data with nested pages [OK]
- Fetching data repeatedly in each page
- Using global variables causing stale data
- Manually passing props instead of using data prop
Ready to prove it?
Pass the test and this pattern moves unknown
