Consider this +page.server.js load function in SvelteKit:
export async function load() {
return { user: { name: 'Alice', age: 30 } };
}What will the page component receive as data?
export async function load() { return { user: { name: 'Alice', age: 30 } }; }
Remember that the object returned from load is passed as data to the page.
The load function returns an object whose properties become available as data in the page. So the page receives { user: { name: 'Alice', age: 30 } }.
In SvelteKit, which of the following is the correct syntax to export a server-only load function in +page.server.js?
Check the correct way to declare an async function export in JavaScript.
The correct syntax is export async function load() { ... }. Option C is valid JavaScript but SvelteKit expects a named function export for load. Option C is missing async keyword. Option C is invalid syntax.
Given this +page.server.js:
export async function load() {
return { count: 5 };
}And this Svelte page component:
<script>
export let data;
</script>
<p>Count is {data.count}</p>What will the page display?
export async function load() { return { count: 5 }; }
The data prop contains the object returned by the load function.
The load function returns { count: 5 }, so data.count is 5. The page renders "Count is 5".
Examine this +page.server.js code:
export async function load() {
const response = await fetch('https://api.example.com/data');
const data = await response.json();
return data;
}Why might this cause a runtime error in SvelteKit?
export async function load() { const response = await fetch('https://api.example.com/data'); const data = await response.json(); return data; }
Check what the load function must return for the page to receive data correctly.
The load function must return an object whose properties become available as data. Returning data directly (which might be an array or other type) causes errors. Wrapping it inside an object fixes this.
Choose the best explanation of the key difference between +page.server.js load functions and +page.js load functions in SvelteKit.
Think about where each load function runs and what APIs they can access.
+page.server.js load functions run only on the server, so they can use server-only APIs like database access or secrets. +page.js load functions run in the browser and cannot access server-only resources.