Skip to main content
Hooks are app-wide functions you declare that SvelteKit calls in response to specific events, giving you fine-grained control over the framework’s behavior.

Overview

There are three hooks files, all optional:

src/hooks.server.js

Server-only hooks

src/hooks.client.js

Client-only hooks

src/hooks.js

Universal hooks (both)
Code in these modules runs when the application starts up, making them useful for initializing database clients and other setup tasks.

Server hooks

The following hooks can be added to src/hooks.server.js:

handle

This function runs every time the SvelteKit server receives a request and determines the response. It receives an event object and a resolve function that renders the route.
Requests for static assets and prerendered pages are not handled by SvelteKit.

Advanced resolve options

The resolve function accepts a second parameter with the following options:
function
Applies custom transforms to HTML chunks. Useful for search-and-replace operations.
function
Determines which headers are included in serialized responses when a load function uses fetch.
function
Determines which files are added to the <head> tag for preloading.

handleFetch

Modify or replace fetch requests that run on the server during SSR or prerendering.
Use handleFetch to bypass proxies and load balancers when making internal API requests during SSR.

handleValidationError

Called when a remote function receives an argument that doesn’t match its schema.
Be thoughtful about what information you expose here, as validation failures often indicate malicious requests.

Shared hooks

These hooks work in both src/hooks.server.js and src/hooks.client.js:

handleError

Called when an unexpected error is thrown during loading or rendering.
1

Log the error

Send errors to a reporting service like Sentry
2

Generate safe representation

Return a sanitized error object that’s safe to show users
3

Add custom properties

Include tracking IDs or other custom fields
Make sure that handleError never throws an error itself.

init

Runs once when the server starts or the app initializes in the browser.
In the browser, asynchronous work in init will delay hydration. Be mindful of what you put here.

Universal hooks

These hooks run on both server and client (in src/hooks.js):

reroute

Runs before handle and changes how URLs are translated into routes.
Using reroute will not change the browser’s address bar or the value of event.url.

transport

Allows custom types to be serialized across the server/client boundary.

Sequencing multiple hooks

You can use the sequence helper to run multiple handle functions:

Learn more

Follow the interactive tutorial on hooks