Skip to content

Commit e806416

Browse files
author
Rich Harris
committed
handle docs
1 parent f2f07d6 commit e806416

File tree

5 files changed

+61
-3
lines changed

5 files changed

+61
-3
lines changed

content/tutorial/04-advanced-sveltekit/01-hooks/01-handle/README.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,47 @@ title: handle
44

55
SvelteKit provides several _hooks_ — ways to intercept and override the framework's default behaviour.
66

7-
> Coming soon
7+
The most elementary hook is `handle`, which lives in `src/hooks.server.js`. It receives an `event` — an instance of [`RequestEvent`](https://kit.svelte.dev/docs/types#public-types-requestevent) — along with a `resolve` function, and returns a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response).
8+
9+
`resolve` is where the magic happens: SvelteKit matches the incoming request URL to a route of your app, imports the relevant code (`+page.server.js` and `+page.svelte` files and so on), loads the data needed by the route, and generates the response.
10+
11+
The default `handle` hook looks like this:
12+
13+
```js
14+
/// file: src/hooks.server.js
15+
export async function handle({ event, resolve }) {
16+
return await resolve(event);
17+
}
18+
```
19+
20+
For pages (as opposed to [API routes](get-handlers)), you can modify the generated HTML with `transformPageChunk`:
21+
22+
```js
23+
/// file: src/hooks.server.js
24+
export async function handle({ event, resolve }) {
25+
return await resolve(event, {
26+
+++ transformPageChunk: ({ html }) => html.replace(
27+
'<body',
28+
'<body style="color: purple"'
29+
)+++
30+
});
31+
}
32+
```
33+
34+
You can also create entirely new routes:
35+
36+
```js
37+
/// file: src/hooks.server.js
38+
export async function handle({ event, resolve }) {
39+
+++ if (event.url.pathname === '/ping') {
40+
return new Response('pong');
41+
}+++
42+
43+
return await resolve(event, {
44+
transformPageChunk: ({ html }) => html.replace(
45+
'<body',
46+
'<body style="color: purple"'
47+
)
48+
});
49+
}
50+
```
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export async function handle({ event, resolve }) {
2+
return await resolve(event);
3+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<h1>hello world</h1>
2+
<a href="/ping">ping</a>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export async function handle({ event, resolve }) {
2+
if (event.url.pathname === '/ping') {
3+
return new Response('pong');
4+
}
5+
6+
return await resolve(event, {
7+
transformPageChunk: ({ html }) => html.replace(
8+
'<body',
9+
'<body style="color: hotpink"'
10+
)
11+
});
12+
}

content/tutorial/04-advanced-sveltekit/01-hooks/02-handlefetch/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,4 @@
22
title: handleFetch
33
---
44

5-
SvelteKit provides several _hooks_ — ways to intercept and override the framework's default behaviour.
6-
75
> Coming soon

0 commit comments

Comments
 (0)