What is the purpose of a +page.server.js file in SvelteKit?
It contains server-side load functions that run only on the server to fetch or prepare data before the page renders. This keeps sensitive logic and data secure.
Click to reveal answer
beginner
How does a server load function differ from a client load function in SvelteKit?
Server load functions run only on the server and can access secrets or databases safely. Client load functions run in the browser and cannot access server-only resources.
Click to reveal answer
beginner
What is the typical export name for a server load function inside +page.server.js?
The function is exported as load. This is the standard name SvelteKit looks for to run server-side data loading.
Click to reveal answer
intermediate
Can you access URL parameters inside a server load function in +page.server.js? How?
Yes, the server load function receives an argument with a params object containing URL parameters. You can use params.id or similar to get values.
Click to reveal answer
beginner
Why should you avoid putting sensitive API keys or secrets in client-side code and instead use +page.server.js?
Because client-side code is visible to users and can be inspected, exposing secrets. Server load functions run only on the server, keeping secrets hidden and secure.
Click to reveal answer
Where does the load function inside +page.server.js run?
AOnly on the server
BOnly in the browser
CBoth server and browser
DNowhere, it is ignored
✗ Incorrect
The load function in +page.server.js runs only on the server to keep data fetching secure.
How do you export a server load function in +page.server.js?
Aexport async function clientLoad() {}
Bexport default function() {}
Cexport const serverLoad = () => {}
Dexport function load() {}
✗ Incorrect
The server load function must be exported as load to be recognized by SvelteKit.
Which of these can you safely do inside a server load function?
AUse browser APIs like localStorage
BAccess environment secrets
CManipulate DOM elements
DRun client-side animations
✗ Incorrect
Server load functions run on the server and can access environment secrets safely.
How do you access URL parameters inside a server load function?
AUsing the <code>params</code> object passed as an argument
BUsing <code>window.location</code>
CUsing <code>document.querySelector</code>
DUsing <code>fetch</code> API
✗ Incorrect
The params object is provided to the server load function to access URL parameters.
What happens if you put a secret API key in client-side code instead of +page.server.js?
AIt stays hidden automatically
BIt runs faster
CIt becomes visible to anyone using the app
DIt encrypts itself
✗ Incorrect
Client-side code is visible to users, so secrets placed there can be exposed.
Explain what a server load function in +page.server.js does and why it is important.
Think about where the code runs and what it can access.
You got /4 concepts.
Describe how you would access URL parameters inside a server load function and why this is useful.
Consider how URLs can carry information for the page.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a server load function in a +page.server.js file in SvelteKit?
easy
A. To fetch or prepare data on the server before the page loads
B. To handle client-side user interactions
C. To style the page components
D. To manage browser cookies on the client
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 A
Quick Check:
Server load function = fetch data before page load [OK]
Hint: Server load functions run on server, not client [OK]
Common Mistakes:
Confusing server load with client-side event handlers
Thinking server load manages styles or cookies
Assuming server load runs in the browser
2. Which of the following is the correct way to export a server load function in +page.server.js?
easy
A. export function load() { return { data: 'hello' }; }
C. export default function load() { return { data: 'hello' }; }
D. function load() { return { data: 'hello' }; }
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' }; } uses export 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.
B. Returning data directly instead of an object causes error
C. Fetching from '/api/data' on server causes an infinite loop
D. Missing export keyword
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 C
Quick Check:
Internal fetch in server load risks infinite loop [OK]
Hint: Avoid fetching internal routes directly in server load [OK]
Common Mistakes:
Thinking async is not allowed in load
Returning data directly is valid
Forgetting to export load function
5. You want to load user data securely on the server and pass it to the page. Which approach correctly uses +page.server.js to fetch user info from a database and expose only the username?
hard
A. export const load = async () => {
const user = await db.getUser();
return { username: user.username };
};
B. export const load = async () => {
const user = await db.getUser();
return user;
};
D. export const load = async () => {
const user = await fetch('/api/user');
return { user };
};
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.