Skip to content

Commit 7235e21

Browse files
author
Rich Harris
committed
add event exercise
1 parent e806416 commit 7235e21

File tree

14 files changed

+79
-1
lines changed

14 files changed

+79
-1
lines changed

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

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

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

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).
7+
The most elementary hook is `handle`, which lives in `src/hooks.server.js`. It receives an `event` object along with a `resolve` function, and returns a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response).
88

99
`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.
1010

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
title: The RequestEvent object
3+
---
4+
5+
The `event` object passed into `handle` is the same object — an instance of a [`RequestEvent`](https://kit.svelte.dev/docs/types#public-types-requestevent) — that is passed into [API routes](get-handlers) in `+server.js` files, [form actions](the-form-element) in `+page.server.js` files, and `load` functions in `+page.server.js` and `+layout.server.js`.
6+
7+
It contains a number of useful properties and methods, some of which we've already encountered:
8+
9+
* `cookies` — the [cookies API](cookies)
10+
* `fetch` — the standard [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API), with additional powers
11+
* `getClientAddress()` — a function to get the client's IP address
12+
* `isDataRequest``true` if the browser is requesting data for a page during client-side navigation, `false` if a page/route is being requested directly
13+
* `locals` — a place to put arbitrary data
14+
* `params` — the route parameters
15+
* `request` — the [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) object
16+
* `route` — an object with an `id` property representing the route that was matched
17+
* `setHeaders(...)` — a function for [setting HTTP headers](headers) on the response
18+
* `url` — a [URL](https://developer.mozilla.org/en-US/docs/Web/API/URL) object representing the current request
19+
20+
A useful pattern is to add some data to `event.locals` in `handle` so that it can be read in subsequent `load` functions:
21+
22+
```js
23+
/// file: src/hooks.server.js
24+
export async function handle({ event, resolve }) {
25+
+++event.locals.answer = 42;+++
26+
return await resolve(event);
27+
}
28+
```
29+
30+
```js
31+
/// file: src/routes/+page.server.js
32+
export function load(+++event+++) {
33+
return {
34+
message: `the answer is ${+++event.locals.answer+++}`
35+
};
36+
}
37+
```
38+
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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export function load() {
2+
return {
3+
message: `the answer is ???`
4+
};
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
export let data;
3+
</script>
4+
5+
<h1>{data.message}</h1>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export async function handle({ event, resolve }) {
2+
event.locals.answer = 42;
3+
return await resolve(event);
4+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export function load(event) {
2+
return {
3+
message: `the answer is ${event.locals.answer}`
4+
};
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export async function load({ fetch }) {
2+
const response = await fetch('/a');
3+
4+
return {
5+
message: await response.text()
6+
};
7+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
export let data;
3+
</script>
4+
5+
<h1>{data.message}</h1>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function GET() {
2+
return new Response('hello from a');
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function GET() {
2+
return new Response('hello from b');
3+
}

0 commit comments

Comments
 (0)