0
0
Svelteframework~5 mins

Server load functions (+page.server.js) in Svelte - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
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
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() {}
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
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
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
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.