Consider this +page.js file in a SvelteKit app:
export function load() {
return { message: 'Hello from load!' };
}What will the page component receive as data?
export function load() { return { message: 'Hello from load!' }; }
The load function can return an object synchronously or a Promise resolving to an object.
The load function returns an object that becomes the data prop in the page component. Returning { message: 'Hello from load!' } means data.message will be that string.
Choose the correct syntax for an async load function that fetches JSON data from an API.
Remember that await can only be used inside async functions.
Option B correctly marks load as async and uses await to wait for the fetch and JSON parsing. Options A and D misuse await outside async or miss awaiting promises.
Look at this +page.js code:
export function load() {
return fetch('/api/data')
.then(res => res.json())
.then(json => ({ data: json }));
}What is the cause of the runtime error when the page loads?
export function load() { return fetch('/api/data') .then(res => res.json()) .then(json => ({ data: json })); }
Consider where the load function runs and how fetch URLs behave there.
The load function runs on the server during SSR. Using a relative URL like '/api/data' in fetch causes a network error because the server does not know the base URL. Absolute URLs or special handling is needed.
Given this +page.js load function:
export async function load({ url }) {
const count = Number(url.searchParams.get('count')) || 0;
return { count: count + 1 };
}If the page URL is https://example.com/page?count=4, what is data.count in the page component?
export async function load({ url }) { const count = Number(url.searchParams.get('count')) || 0; return { count: count + 1 }; }
Check how url.searchParams.get and Number work.
The query parameter 'count' is '4'. Converting to Number gives 4. Adding 1 results in 5, so data.count is 5.
Choose the correct statement about how +page.js load functions behave in SvelteKit.
Think about when and where data loading happens in SvelteKit.
The load function runs on the server during initial page load and on the client during navigation. It must return data that can be serialized and passed to the page component.