What is the purpose of the handle hook in SvelteKit?
The handle hook lets you run code for every request before it reaches your routes. You can modify the request or response, add headers, or run authentication checks.
Click to reveal answer
beginner
How does the handleError hook help in SvelteKit applications?
The handleError hook catches errors that happen during requests. It lets you log errors or show custom error messages to users in a friendly way.
Click to reveal answer
intermediate
What is the role of the handleFetch hook in SvelteKit?
The handleFetch hook lets you intercept and modify fetch requests made during server-side rendering. You can add headers, change URLs, or mock responses.
Click to reveal answer
beginner
Where do you define the handle, handleError, and handleFetch hooks in a SvelteKit project?
You define these hooks inside the src/hooks.server.js or src/hooks.server.ts file. This file runs on the server for all requests.
Click to reveal answer
intermediate
Can the handleFetch hook modify fetch requests made from the client side in SvelteKit?
No, handleFetch only affects fetch requests made during server-side rendering or load functions on the server. Client-side fetches are not intercepted by this hook.
Click to reveal answer
Which hook in SvelteKit runs before every request to modify the request or response?
Ahandle
BhandleError
ChandleFetch
DonMount
✗ Incorrect
The handle hook runs before every request and can modify the request or response.
What is the main use of the handleError hook?
ATo catch and handle errors during requests
BTo run code after the response is sent
CTo intercept fetch requests
DTo modify client-side events
✗ Incorrect
handleError is used to catch and handle errors during requests.
Where do you place the hooks like handle and handleFetch in a SvelteKit project?
Asrc/routes/hooks.js
Bsrc/hooks.server.js
Csrc/lib/hooks.js
Dstatic/hooks.js
✗ Incorrect
Hooks are defined in src/hooks.server.js or src/hooks.server.ts.
Which hook allows you to modify fetch requests during server-side rendering?
AhandleError
Bhandle
ConDestroy
DhandleFetch
✗ Incorrect
handleFetch lets you modify fetch requests during server-side rendering.
Can handleFetch modify fetch requests made from the browser after the page loads?
AYes, always
BOnly if enabled in config
CNo, only server-side fetches
DOnly for POST requests
✗ Incorrect
handleFetch only affects fetches made on the server, not client-side fetches.
Explain how the handle hook works in SvelteKit and give an example of when you might use it.
Think about what you want to do before your app handles a user request.
You got /3 concepts.
Describe the difference between handleError and handleFetch hooks in SvelteKit.
One deals with problems, the other with data fetching.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of the handle hook in SvelteKit?
easy
A. To catch and handle errors during server-side rendering
B. To define client-side event handlers for UI elements
C. To intercept and modify fetch requests made by the app
D. To manage and modify incoming requests and outgoing responses
Solution
Step 1: Understand the role of handle hook
The handle hook runs on every request and can modify the request or response.
Step 2: Compare with other hooks
handleError is for errors, handleFetch is for fetch interception, so they don't manage requests/responses directly.
Final Answer:
To manage and modify incoming requests and outgoing responses -> Option D
A. Cannot modify headers on the response object directly; it is immutable
B. Missing await before resolve(event)
C. Incorrect parameter names; should be { request, resolve }
D. handle hook must be synchronous, not async
Solution
Step 1: Check response header modification
The response object is immutable; headers cannot be set directly on it.
Step 2: Confirm other parts are correct
Await is used correctly, parameter names are valid, and handle can be async.
Final Answer:
Cannot modify headers on the response object directly; it is immutable -> Option A
Quick Check:
Response headers are immutable [OK]
Hint: Response headers are immutable; clone before modifying [OK]
Common Mistakes:
Trying to set headers directly on response
Confusing parameter names
Thinking handle must be synchronous
5. You want to add a custom header X-User with the username from event.locals.user to every fetch request made by your app. Which handleFetch hook implementation correctly achieves this?
hard
A. export async function handleFetch({ request, fetch }) {
request.headers.set('X-User', event.locals.user);
return fetch(request);
}
B. export async function handleFetch({ request, fetch, event }) {
const modifiedRequest = new Request(request, {
headers: { ...Object.fromEntries(request.headers), 'X-User': event.locals.user }
});
return fetch(modifiedRequest);
}
C. export async function handleFetch({ request, fetch, event }) {
request.headers.append('X-User', event.locals.user);
return fetch(request);
}
D. export async function handleFetch({ request, fetch, event }) {
const headers = new Headers(request.headers);
headers.set('X-User', event.locals.user);
const modifiedRequest = new Request(request.url, { method: request.method, headers });
return fetch(modifiedRequest);
}
Solution
Step 1: Understand immutability of request headers
Request headers are immutable; to modify them, create a new Request with new headers while preserving other properties like body.
Step 2: Analyze the correct implementation
The code that uses new Request(request, { headers: { ...Object.fromEntries(request.headers), 'X-User': event.locals.user } }) clones the original request fully (including body, method, etc.) and overrides the headers correctly.
Step 3: Identify errors in other options
Directly modifying request.headers with set or append fails because headers are immutable. Creating a new Request with only url, method, and headers omits the body and other request properties.
Final Answer:
export async function handleFetch({ request, fetch, event }) {
const modifiedRequest = new Request(request, {
headers: { ...Object.fromEntries(request.headers), 'X-User': event.locals.user }
});
return fetch(modifiedRequest);
} -> Option B
Quick Check:
new Request(request, { headers: ... }) clones fully [OK]
Hint: Clone request with new Request(request, { headers: updated }) [OK]
Common Mistakes:
Modifying headers directly on original request
Not cloning request before changing headers
Omitting request body when recreating Request
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.