What are SvelteKit hooks?
Answer
SvelteKit hooks are functions defined in src/hooks.server.js (or hooks.client.js) that intercept requests and responses. handle: runs on every request. export async function handle({ event, resolve }) { event.locals.user = await getUser(event.cookies); return resolve(event); }. Use it for authentication, logging, A/B testing, and adding data to event.locals (passed to load functions and actions). handleFetch: intercepts fetch calls in load functions — modify the request, add auth headers, or redirect to different endpoints. handleError: called when an unexpected error occurs — log to your error tracking service, return a safe error message. hooks.client.js: handleError for client-side errors. Hooks run before routing, making them ideal for request-level cross-cutting concerns like authentication and request logging.
Previous
What is the difference between +page.js and +page.server.js in SvelteKit?
Next
What are SvelteKit form actions?