Skip to content

Commit 58b7526

Browse files
committed
docs: add doc for client-side handleFetch
1 parent bb9cb1b commit 58b7526

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

documentation/docs/30-advanced/20-hooks.md

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,15 @@ export async function handle({ event, resolve }) {
104104

105105
Note that `resolve(...)` will never throw an error, it will always return a `Promise<Response>` with the appropriate status code. If an error is thrown elsewhere during `handle`, it is treated as fatal, and SvelteKit will respond with a JSON representation of the error or a fallback error page — which can be customised via `src/error.html` — depending on the `Accept` header. You can read more about error handling [here](errors).
106106

107+
## Shared hooks
108+
109+
The following can be added to `src/hooks.server.js` _and_ `src/hooks.client.js`:
110+
107111
### handleFetch
108112

109-
This function allows you to modify (or replace) a `fetch` request that happens inside a `load`, `action` or `handle` function that runs on the server (or during prerendering).
113+
#### on the server
114+
115+
This function allows you to modify (or replace) a `fetch` request that happens inside a `load`, `action` or `handle` function that runs on the server (or during pre-rendering).
110116

111117
For example, your `load` function might make a request to a public URL like `https://api.yourapp.com` when the user performs a client-side navigation to the respective page, but during SSR it might make sense to hit the API directly (bypassing whatever proxies and load balancers sit between it and the public internet).
112118

@@ -147,9 +153,33 @@ export async function handleFetch({ event, request, fetch }) {
147153
}
148154
```
149155

150-
## Shared hooks
156+
#### on the client
157+
158+
This function allows you to modify (or replace) `fetch` requests that happens on the client.
159+
160+
This allows, for example, to pass custom headers to server when running `load` or `action` function on the server *(inside a `+page.server.ts` or `+layout.server.ts`)*, to automatically includes credentials to requests to your API or to collect logs or metrics.
161+
162+
*Note: on the client, the `event` argument is not passed to the hook.*
163+
164+
165+
```js
166+
/// file: src/hooks.client.js
167+
/** @type {import('@sveltejs/kit').HandleClientFetch} */
168+
export async function handleFetch({ request, fetch }) {
169+
if (request.url.startsWith(location.origin)) {
170+
request.headers.set('X-Auth-Token', 'my-custom-auth-token');
171+
} else if (request.url.startsWith('https://api.my-domain.com/')) {
172+
request.headers.set('Authorization', 'Bearer my-api-token');
173+
}
174+
175+
console.time(`request: ${request.url}`);
176+
177+
return fetch(request).finally(() => {
178+
console.timeEnd(`request: ${request.url}`);
179+
});
180+
}
181+
```
151182

152-
The following can be added to `src/hooks.server.js` _and_ `src/hooks.client.js`:
153183

154184
### handleError
155185

0 commit comments

Comments
 (0)