Page load functions help you get data or do setup work before a page shows. This makes sure the page has what it needs right away.
Page load functions (+page.js) in Svelte
export async function load({ params, fetch, url, session }) { // your code here return { /* data for the page's `data` prop */ }; }
The load function runs before the page shows.
You return an object whose properties are available on the page's data prop.
export async function load() { return { message: 'Hello!' }; }
export async function load({ params }) { const id = params.id; return { id }; }
export async function load({ fetch }) { const res = await fetch('/api/data'); const data = await res.json(); return { data }; }
This example fetches a todo item before the page loads. The data is passed via the data prop and shown on the page.
/* +page.js */ export async function load({ fetch }) { const response = await fetch('https://jsonplaceholder.typicode.com/todos/1'); const todo = await response.json(); return { todo }; } /* +page.svelte */ <script> export let data; </script> <h1>Todo Item</h1> <p><strong>ID:</strong> {data.todo.id}</p> <p><strong>Title:</strong> {data.todo.title}</p> <p><strong>Completed:</strong> {data.todo.completed ? 'Yes' : 'No'}</p>
The load function runs on the server during the first page load and on the client for navigation.
Always return an object whose properties will be available on the data prop to pass data to your page component.
Use fetch inside load to get data safely with the same origin.
Page load functions run before the page appears to get data or do setup.
They return a data object whose properties are available on the page component's data prop.
Use them to fetch data, check user state, or redirect before showing the page.
Practice
load function in a +page.js file in SvelteKit?Solution
Step 1: Understand the role of
Theloadin SvelteKitloadfunction runs before the page renders to get data or do setup.Step 2: Match the purpose with options
Only To fetch data or prepare information before the page is shown describes fetching or preparing data before showing the page.Final Answer:
To fetch data or prepare information before the page is shown -> Option AQuick Check:
Page load functions fetch data before rendering [OK]
- Confusing load with styling or event handling
- Thinking load defines routes
- Assuming load runs after page renders
load function in +page.js?Solution
Step 1: Recall correct export syntax for load
In SvelteKit,loadis exported as a named export, usually a const arrow function returning data without wrapping inprops.Step 2: Check each option
export const load = () => { return { message: 'Hi' }; }; exportsloadas a named const returning an object with data directly, which is correct. export function load() { return { props: { message: 'Hi' } }; } wrongly usespropskey, function load() { return { message: 'Hi' }; } lacks export, export default function load() { return { props: { message: 'Hi' } }; } uses default export which is incorrect.Final Answer:
export const load = () => { return { message: 'Hi' }; }; -> Option DQuick Check:
Named export const load returns data object [OK]
- Using default export instead of named export
- Returning data inside a props object
- Not exporting the load function
+page.js code, what will be the value of data.greeting inside the page component?export const load = () => {
return { greeting: 'Hello, Svelte!' };
};Solution
Step 1: Understand what load returns
The load function returns an object with a keygreetingand value 'Hello, Svelte!'.Step 2: How data is passed to the page
The returned object keys become properties on thedataobject in the page component. Sodata.greetingequals 'Hello, Svelte!'.Final Answer:
'Hello, Svelte!' -> Option BQuick Check:
Returned keys become data props [OK]
- Expecting the whole object as data.greeting
- Thinking load returns props key
- Assuming undefined if not wrapped in props
+page.js load function:export const load = async () => {
const res = await fetch('/api/data');
const data = await res.json();
return data;
};Solution
Step 1: Check the return value of load
The load function must return an object whose keys become props for the page. Returning raw data (like an array or primitive) is invalid.Step 2: Identify the fix
Wrap the fetched data inside an object, e.g.,return { data }, so the page receives it properly.Final Answer:
The returned data must be wrapped in an object -> Option AQuick Check:
Load returns object with keys for props [OK]
- Returning raw data instead of object
- Forgetting to export load
- Thinking fetch is disallowed in load
+page.js load function. Which code snippet correctly does this?Solution
Step 1: Understand how to redirect in load
SvelteKit uses theredirectfunction from '@sveltejs/kit' which throws an exception to redirect the user.Step 2: Check each option
import { redirect } from '@sveltejs/kit'; export const load = () => { const user = null; if (!user) throw redirect(302, '/login'); return { user }; }; correctly importsredirectand throws it when user is not authenticated. export const load = () => { const user = null; if (!user) return '/login'; return { user }; }; returns a string which is invalid. export const load = () => { const user = null; if (!user) window.location.href = '/login'; return { user }; }; uses browser redirect which won't work server-side. import { redirect } from '@sveltejs/kit'; export const load = () => { const user = null; if (!user) return redirect(302, '/login'); return { user }; }; returns redirect instead of throwing it, which is incorrect.Final Answer:
import { redirect } from '@sveltejs/kit'; export const load = () => { const user = null; if (!user) throw redirect(302, '/login'); return { user }; }; -> Option CQuick Check:
Throw redirect to redirect in load [OK]
- Returning redirect instead of throwing it
- Using window.location in load (server-side)
- Returning a string URL to redirect
Ready to prove it?
Pass the test and this pattern moves unknown
