0
0
Svelteframework~20 mins

Hooks (handle, handleError, handleFetch) in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
SvelteKit Hooks Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1:30remaining
What does the handle hook do in SvelteKit?

Consider the handle hook in SvelteKit. What is its main purpose?

AIt intercepts every request and can modify the request or response before reaching the route handler.
BIt only handles errors thrown during server-side rendering.
CIt fetches data from external APIs automatically for all routes.
DIt manages client-side navigation events and updates the URL.
Attempts:
2 left
💡 Hint

Think about a hook that runs for every request and can change how the server responds.

🔧 Debug
intermediate
1:30remaining
What error does this handleError hook cause?

Given this handleError hook in SvelteKit:

export function handleError({ error, event }) {
  return { message: error.message };
}

What problem will this cause?

AIt will cause the server to crash because <code>handleError</code> cannot access <code>event</code>.
BThe error response will be missing the <code>stack</code> property, causing incomplete error info.
CIt will cause a syntax error due to missing parentheses around the return object.
DIt will cause a runtime error because <code>handleError</code> must return a string, not an object.
Attempts:
2 left
💡 Hint

Check what handleError expects to return for proper error display.

state_output
advanced
2:00remaining
What is the effect of this handleFetch hook on fetch requests?

Analyze this handleFetch hook:

export async function handleFetch({ request, fetch }) {
  if (request.url.includes('/api')) {
    const modifiedRequest = new Request(request, { headers: { 'X-Custom': 'true' } });
    return fetch(modifiedRequest);
  }
  return fetch(request);
}

What happens when the app fetches /api/data?

AThe fetch request to <code>/api/data</code> includes the custom header <code>X-Custom: true</code>.
BThe fetch request to <code>/api/data</code> is blocked and returns an error.
CThe fetch request to <code>/api/data</code> is unchanged and has no extra headers.
DThe fetch request to <code>/api/data</code> is redirected to another URL.
Attempts:
2 left
💡 Hint

Look at how the hook modifies requests with URLs containing /api.

📝 Syntax
advanced
1:30remaining
Which handle hook code snippet is syntactically correct?

Choose the valid handle hook implementation in SvelteKit:

A
export function handle({ event, resolve }) {
  const response = resolve(event);
  return response
}
B
export async function handle(event, resolve) {
  const response = await resolve(event);
  return response;
}
C
export async function handle({ event, resolve }) {
  const response = resolve(event);
  return response;
}
D
export async function handle({ event, resolve }) {
  const response = await resolve(event);
  return response;
}
Attempts:
2 left
💡 Hint

Check the function signature and usage of await.

🧠 Conceptual
expert
2:30remaining
How do handle, handleError, and handleFetch hooks differ in SvelteKit?

Match each hook to its main responsibility in SvelteKit:

  • handle
  • handleError
  • handleFetch
A<code>handle</code>: manages client-side state; <code>handleError</code>: retries failed fetches; <code>handleFetch</code>: modifies server responses.
B<code>handle</code>: only runs on client navigation; <code>handleError</code>: logs errors to console; <code>handleFetch</code>: caches fetch results.
C<code>handle</code>: modifies requests/responses globally; <code>handleError</code>: customizes error responses; <code>handleFetch</code>: intercepts and modifies fetch calls.
D<code>handle</code>: fetches data for routes; <code>handleError</code>: handles form submissions; <code>handleFetch</code>: manages cookies.
Attempts:
2 left
💡 Hint

Think about what each hook controls: requests, errors, or fetch calls.