Server load functions let you get data on the server before showing a page. This helps keep your app fast and secure.
Server load functions (+page.server.js) in Svelte
export async function load(event) { // get data here return { props: { key: value } }; }
The load function runs only on the server in +page.server.js.
You return an object with props that become available to your page.
export async function load() { return { props: { message: 'Hello from server' } }; }
export async function load({ params }) { const id = params.id; const data = await fetchDataById(id); return { props: { data } }; }
This example shows a server load function that gets the current time on the server. The time is sent as a prop to the page and displayed.
// +page.server.js export async function load() { const time = new Date().toLocaleTimeString(); return { props: { serverTime: time } }; } // +page.svelte <script> export let serverTime; </script> <h1>Current Server Time</h1> <p>The time on the server is: {serverTime}</p>
Server load functions run only on the server, so you can safely use secrets or server-only APIs.
Returned props are merged into the page component's props automatically.
If you want to load data on the client too, use +page.js load functions instead.
Server load functions run on the server before the page loads.
They help fetch data safely and prepare it for the page.
Use +page.server.js to keep code server-only.
Practice
+page.server.js file in SvelteKit?Solution
Step 1: Understand server load function role
Server load functions run on the server to get data before the page renders.Step 2: Compare options to this role
Only To fetch or prepare data on the server before the page loads describes fetching or preparing data on the server side before page load.Final Answer:
To fetch or prepare data on the server before the page loads -> Option AQuick Check:
Server load function = fetch data before page load [OK]
- Confusing server load with client-side event handlers
- Thinking server load manages styles or cookies
- Assuming server load runs in the browser
+page.server.js?Solution
Step 1: Recall SvelteKit server load export syntax
Server load functions are exported as named exports, usually as a const arrow function.Step 2: Check each option
export const load = () => { return { data: 'hello' }; } usesexport const load = () => { ... }, which is correct. export function load() { return { data: 'hello' }; } exports a function declaration but not as const, which is allowed but less common. export default function load() { return { data: 'hello' }; } uses default export which is incorrect. function load() { return { data: 'hello' }; } does not export at all.Final Answer:
export const load = () => { return { data: 'hello' }; } -> Option BQuick Check:
Named export const load = arrow function [OK]
- Using default export instead of named export
- Not exporting the load function at all
- Using function declaration without export
+page.server.js code, what will be the value of data.message in the page?export const load = () => {
return { message: 'Hello from server' };
};Solution
Step 1: Understand the load function return
The load function returns an object with a propertymessageset to 'Hello from server'.Step 2: Recognize data passed to page
The returned object properties become available asdatain the page, sodata.messageequals 'Hello from server'.Final Answer:
'Hello from server' -> Option DQuick Check:
Returned object property = data property [OK]
- Expecting undefined if not wrapped in another object
- Confusing server message with client message
- Thinking load function causes error without async
+page.server.js code?export const load = async () => {
const res = await fetch('/api/data');
const data = await res.json();
return data;
};Solution
Step 1: Analyze fetch call in server load
The load function fetches from '/api/data', which is likely a route handled by the same app.Step 2: Understand server fetch behavior
Fetching from an internal API route in server load can cause the server to call itself repeatedly, causing an infinite loop.Final Answer:
Fetching from '/api/data' on server causes an infinite loop -> Option CQuick Check:
Internal fetch in server load risks infinite loop [OK]
- Thinking async is not allowed in load
- Returning data directly is valid
- Forgetting to export load function
+page.server.js to fetch user info from a database and expose only the username?Solution
Step 1: Identify secure data fetching on server
export const load = async () => { const user = await db.getUser(); return { username: user.username }; }; fetches user data directly from the database on the server, which is secure.Step 2: Check data exposure
export const load = async () => { const user = await db.getUser(); return { username: user.username }; }; returns only the username property, exposing minimal info to the page.Step 3: Evaluate other options
export const load = async () => { const user = await db.getUser(); return user; }; returns full user object, exposing sensitive data. export const load = () => { fetch('/api/user').then(res => res.json()); return {}; }; uses client fetch incorrectly in server load. export const load = async () => { const user = await fetch('/api/user'); return { user }; }; fetches from API route which may cause loops or expose more data.Final Answer:
export const load = async () => { const user = await db.getUser(); return { username: user.username }; }; -> Option AQuick Check:
Server fetch minimal data, return only needed props [OK]
- Returning full user object exposing sensitive info
- Fetching from API route inside server load
- Using client fetch inside server load
Ready to prove it?
Pass the test and this pattern moves unknown
