Performance: Server load functions (+page.server.js)
This affects the server response time and initial page load speed by controlling data fetching before the page renders.
export async function load() { const [res1, res2] = await Promise.all([ fetch('https://api.example.com/data1'), fetch('https://api.example.com/data2') ]); const data1 = await res1.json(); const data2 = await res2.json(); return { data1, data2 }; }
export async function load() { const res1 = await fetch('https://api.example.com/data1'); const data1 = await res1.json(); const res2 = await fetch('https://api.example.com/data2'); const data2 = await res2.json(); return { data1, data2 }; }
| Pattern | Server Fetches | Parallelism | Server Response Time | Verdict |
|---|---|---|---|---|
| Sequential fetches in load function | Multiple sequential fetch calls | No | High - waits for each fetch | [X] Bad |
| Parallel fetches with Promise.all | Multiple fetch calls in parallel | Yes | Lower - waits for all fetches together | [OK] Good |
+page.server.js file in SvelteKit?+page.server.js?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.+page.server.js code, what will be the value of data.message in the page?export const load = () => {
return { message: 'Hello from server' };
};message set to 'Hello from server'.data in the page, so data.message equals 'Hello from server'.+page.server.js code?export const load = async () => {
const res = await fetch('/api/data');
const data = await res.json();
return data;
};+page.server.js to fetch user info from a database and expose only the username?Pass the test and this pattern moves unknown