0
0
Svelteframework~20 mins

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

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

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?

Svelte
export function load() {
  return { message: 'Hello from load!' };
}
AUndefined, because load must be async
BA string 'Hello from load!' directly
CAn object with a property 'message' containing the string 'Hello from load!'
DAn error because load must return a Promise
Attempts:
2 left
💡 Hint

The load function can return an object synchronously or a Promise resolving to an object.

📝 Syntax
intermediate
2:00remaining
Which +page.js load function syntax is correct for async data fetching?

Choose the correct syntax for an async load function that fetches JSON data from an API.

A
export function load() {
  const res = fetch('/api/data');
  const json = res.json();
  return { data: json };
}
B
export async function load() {
  const res = await fetch('/api/data');
  const json = await res.json();
  return { data: json };
}
C
export async function load() {
  const res = fetch('/api/data');
  const json = res.json();
  return { data: json };
}
D
export function load() {
  const res = await fetch('/api/data');
  const json = await res.json();
  return { data: json };
}
Attempts:
2 left
💡 Hint

Remember that await can only be used inside async functions.

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

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?

Svelte
export function load() {
  return fetch('/api/data')
    .then(res => res.json())
    .then(json => ({ data: json }));
}
AThe fetch URL is relative and causes a network error on server-side load
BThe load function returns a Promise but SvelteKit expects a plain object synchronously
CThe load function must be async and use await, promises are not allowed
DNothing wrong; this code works fine and returns data correctly
Attempts:
2 left
💡 Hint

Consider where the load function runs and how fetch URLs behave there.

state_output
advanced
2:00remaining
What is the value of data.count after this load function runs?

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?

Svelte
export async function load({ url }) {
  const count = Number(url.searchParams.get('count')) || 0;
  return { count: count + 1 };
}
A4
BNaN
C0
D5
Attempts:
2 left
💡 Hint

Check how url.searchParams.get and Number work.

🧠 Conceptual
expert
2:00remaining
Which statement about +page.js load function behavior is true?

Choose the correct statement about how +page.js load functions behave in SvelteKit.

AThe load function runs on both server and client during navigation and must be serializable
BThe load function runs only on the client after the page is fully loaded
CThe load function can directly modify component state variables
DThe load function can only return primitive values, not objects
Attempts:
2 left
💡 Hint

Think about when and where data loading happens in SvelteKit.