Skip to content

Commit 98d5e16

Browse files
committed
docs: add doc for client-side handleFetch
1 parent bd66c78 commit 98d5e16

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,14 @@ 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

113+
#### on the server
114+
109115
This function allows you to modify (or replace) the result of an [`event.fetch`](load#Making-fetch-requests) call that runs on the server (or during prerendering) inside an endpoint, `load`, `action`, `handle`, `handleError` or `reroute`.
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).
@@ -143,9 +149,33 @@ export async function handleFetch({ event, request, fetch }) {
143149
}
144150
```
145151

146-
## Shared hooks
152+
#### on the client
153+
154+
This function allows you to modify (or replace) `fetch` requests that happens on the client.
155+
156+
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.
157+
158+
*Note: on the client, the `event` argument is not passed to the hook.*
159+
160+
161+
```js
162+
/// file: src/hooks.client.js
163+
/** @type {import('@sveltejs/kit').HandleClientFetch} */
164+
export async function handleFetch({ request, fetch }) {
165+
if (request.url.startsWith(location.origin)) {
166+
request.headers.set('X-Auth-Token', 'my-custom-auth-token');
167+
} else if (request.url.startsWith('https://api.my-domain.com/')) {
168+
request.headers.set('Authorization', 'Bearer my-api-token');
169+
}
170+
171+
console.time(`request: ${request.url}`);
172+
173+
return fetch(request).finally(() => {
174+
console.timeEnd(`request: ${request.url}`);
175+
});
176+
}
177+
```
147178

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

150180
### handleError
151181

0 commit comments

Comments
 (0)