0
0
Svelteframework~20 mins

Why load functions fetch data server-side in Svelte - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
SvelteKit Load Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why does the load function run on the server in SvelteKit?

In SvelteKit, the load function can run on the server before the page renders. Why is this behavior important?

AIt forces the page to reload every time the user clicks a link.
BIt allows fetching data securely without exposing secrets to the browser.
CIt prevents any data from being fetched, making the page static.
DIt runs only after the page is fully loaded in the browser.
Attempts:
2 left
💡 Hint

Think about where sensitive data like API keys should be kept.

component_behavior
intermediate
2:00remaining
What happens if you fetch data client-side instead of in load()?

Consider a SvelteKit page that fetches data inside the load function versus fetching data inside onMount in the component. What is a key difference in user experience?

AFetching in <code>load</code> only works on mobile devices, <code>onMount</code> works on desktop.
BFetching in <code>load</code> causes the page to flicker, while <code>onMount</code> is smooth.
CFetching in <code>load</code> shows data immediately on page load, while <code>onMount</code> fetch delays data display.
DFetching in <code>load</code> disables navigation, <code>onMount</code> enables it.
Attempts:
2 left
💡 Hint

Think about when the data is ready to show to the user.

📝 Syntax
advanced
2:00remaining
Identify the correct load function fetching data server-side

Which of the following load functions correctly fetches JSON data from an API server-side in SvelteKit?

Svelte
export async function load() {
  // fetch data here
}
A
export function load() {
  fetch('/api/data').then(res =&gt; res.json()).then(data =&gt; ({ data }));
}
B
export async function load() {
  const res = await fetch('/api/data');
  const data = await res.json();
  return { data };
}
C
export async function load() {
  const data = fetch('/api/data').json();
  return { data };
}
D
export async function load() {
  const res = await fetch('https://api.example.com/data');
  const data = await res.json();
  return { data };
}
Attempts:
2 left
💡 Hint

Remember that server-side fetch needs full URL or relative to server, and load must be async to await fetch.

🔧 Debug
advanced
2:00remaining
Why does this load function cause a runtime error?

Given this load function, why does it cause a runtime error?

Svelte
export async function load() {
  const response = await fetch('/api/data');
  const data = await response.json();
  return data;
}
ABecause load must return an object, not raw data, so returning data directly causes an error.
BBecause fetch cannot be used inside load functions in SvelteKit.
CBecause the function is missing the <code>async</code> keyword.
DBecause <code>response.json()</code> is not awaited.
Attempts:
2 left
💡 Hint

Check what the load function expects to return.

state_output
expert
2:00remaining
What is the rendered output when load fetches data server-side?

Consider this SvelteKit page code:

+page.svelte
<script>
  export let data;
</script>

<h1>User: {data.user.name}</h1>

+page.server.js
export async function load() {
  return { user: { name: 'Alice' } };
}

What will the page display when loaded?

A<h1>User: Alice</h1>
B<h1>User: {data.user.name}</h1>
CAn error because data is undefined
D<h1>User: </h1>
Attempts:
2 left
💡 Hint

Remember how data returned from load is passed as props.