0
0
Svelteframework~20 mins

Server load functions (+page.server.js) in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Server Load Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What data does the page receive from this +page.server.js load function?

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?

Svelte
export async function load() {
  return { user: { name: 'Alice', age: 30 } };
}
A{ user: { name: 'Alice', age: 30 } }
B{ data: { user: { name: 'Alice', age: 30 } } }
C{ props: { user: { name: 'Alice', age: 30 } } }
D{ user: 'Alice', age: 30 }
Attempts:
2 left
💡 Hint

Remember that the object returned from load is passed as data to the page.

📝 Syntax
intermediate
2:00remaining
Which option correctly exports a server-only load function in +page.server.js?

In SvelteKit, which of the following is the correct syntax to export a server-only load function in +page.server.js?

Aexport function load() { return { message: 'Hello' }; }
Bexport const load = async () => { return { message: 'Hello' }; }
Cexport async function load() { return { message: 'Hello' }; }
Dexport async load() { return { message: 'Hello' }; }
Attempts:
2 left
💡 Hint

Check the correct way to declare an async function export in JavaScript.

state_output
advanced
2:00remaining
What is the output rendered by the page with this server load function and page code?

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?

Svelte
export async function load() {
  return { count: 5 };
}
ACount is undefined
BCount is 5
CCount is 0
DError: data is not defined
Attempts:
2 left
💡 Hint

The data prop contains the object returned by the load function.

🔧 Debug
advanced
2:00remaining
Why does this +page.server.js load function cause a runtime error?

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?

Svelte
export async function load() {
  const response = await fetch('https://api.example.com/data');
  const data = await response.json();
  return data;
}
ABecause the returned object must be wrapped inside another object
BBecause the load function must not be async
CBecause response.json() returns a promise that is not awaited
DBecause fetch is not available in server load functions by default
Attempts:
2 left
💡 Hint

Check what the load function must return for the page to receive data correctly.

🧠 Conceptual
expert
2:00remaining
What is the main difference between +page.server.js load functions and +page.js load functions in SvelteKit?

Choose the best explanation of the key difference between +page.server.js load functions and +page.js load functions in SvelteKit.

A+page.server.js load functions cannot return data; +page.js load functions can return data.
B+page.server.js load functions run in the browser; +page.js load functions run only on the server.
C+page.server.js load functions are synchronous; +page.js load functions are asynchronous.
D+page.server.js load functions run only on the server and can access server-only APIs; +page.js load functions run in the browser and cannot access server-only APIs.
Attempts:
2 left
💡 Hint

Think about where each load function runs and what APIs they can access.