Performance: Hooks (handle, handleError, handleFetch)
These hooks affect server-side request handling and error processing, impacting server response time and client load speed.
export const handle = async ({ event, resolve }) => { // minimal processing before resolve return await resolve(event); };
export const handle = async ({ event, resolve }) => { // heavy synchronous processing for (let i = 0; i < 100000000; i++) {} return await resolve(event); };
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Heavy synchronous code in handle hook | 0 | 0 | Blocks server response delaying paint | [X] Bad |
| Minimal async processing in handle hook | 0 | 0 | Fast server response enables quicker paint | [OK] Good |
| Unconditional header modification in handleFetch | 0 | 0 | Adds latency to fetch requests | [X] Bad |
| Conditional header modification in handleFetch | 0 | 0 | Reduces unnecessary fetch overhead | [OK] Good |
| Synchronous logging in handleError | 0 | 0 | Blocks error response delaying feedback | [X] Bad |
| Asynchronous logging in handleError | 0 | 0 | Non-blocking error handling improves responsiveness | [OK] Good |
handle hook in SvelteKit?handle hookhandle hook runs on every request and can modify the request or response.handleError is for errors, handleFetch is for fetch interception, so they don't manage requests/responses directly.handle = manage requests/responses [OK]handleError hook in hooks.server.js?handleError parametershandleError hook receives an object with error and event.{ error, event } and returns an object with a message.handleFetch hook, what will be logged when the app fetches /api/data?
export async function handleFetch({ request, fetch }) {
console.log('Fetching:', request.url);
const response = await fetch(request);
return response;
}handleFetch parametersrequest object has a full URL in its url property.request.url, which includes the origin like 'http://localhost/api/data'.handle hook code snippet:
export async function handle({ event, resolve }) {
const response = await resolve(event);
response.headers.set('X-Custom', 'value');
return response;
}X-User with the username from event.locals.user to every fetch request made by your app. Which handleFetch hook implementation correctly achieves this?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.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.Pass the test and this pattern moves unknown