Consider this SvelteKit handle hook that modifies the response headers. What will the browser receive as the Content-Type header?
export async function handle({ event, resolve }) { const response = await resolve(event); response.headers.set('Content-Type', 'application/json'); return response; }
Think about whether the response.headers object is mutable and if setting headers after resolve works.
The response object returned by resolve has mutable headers, so setting Content-Type to 'application/json' works and changes the header sent to the browser.
Given these hooks in hooks.server.js, what is the correct order they execute when a request comes in?
export async function handle({ event, resolve }) { console.log('handle start'); const response = await resolve(event); console.log('handle end'); return response; } export async function handleFetch({ request, fetch }) { console.log('handleFetch'); return fetch(request); } export async function getSession(event) { console.log('getSession'); return { user: null }; }
Remember that handle wraps the whole request, handleFetch runs during fetch calls inside handle, and getSession runs after handle.
The handle hook runs first and wraps the request. Inside handle, any fetch calls trigger handleFetch. The getSession hook runs after handle resolves.
Examine this handle hook code. Why does it throw an error when a request is made?
export async function handle({ event, resolve }) { const response = resolve(event); response.headers.set('x-custom', 'value'); return response; }
Check if resolve returns a Promise and if you need to wait for it.
The resolve function returns a Promise. Without await, response is a Promise object, which does not have a headers property, causing a runtime error.
handle hook that adds a cookie?Choose the code that correctly adds a cookie named session with value abc123 to the response.
Check the correct method to set headers on the response object.
The headers object has a set method to set or overwrite headers. append adds another header of the same name, but cookies should be set with set to avoid duplicates. headers is not a plain object, so bracket notation or add method do not work.
event.locals.user after this middleware chain?Given these two handle hooks in hooks.server.js, what will event.locals.user be when the request reaches the page?
export async function handle({ event, resolve }) { event.locals.user = 'guest'; const response = await resolve(event); return response; } export async function handle({ event, resolve }) { if (event.request.headers.get('authorization') === 'Bearer token123') { event.locals.user = 'admin'; } return resolve(event); }
Consider how SvelteKit handles multiple handle exports in the same file.
SvelteKit expects only one handle export per hooks.server.js file. Exporting two handle functions causes a syntax or runtime error because of duplicate exports.