|
1 | 1 | --- |
2 | 2 | title: getRequestEvent |
3 | 3 | use_cases: >- |
4 | | - server-side rendering, authentication, headers manipulation, cookies, server |
5 | | - functions, request context access |
| 4 | + reading the current request object, accessing request-scoped server context |
6 | 5 | tags: |
7 | 6 | - server |
8 | 7 | - ssr |
9 | 8 | - request |
10 | | - - headers |
11 | | - - authentication |
12 | 9 | - context |
13 | 10 | version: "1.0" |
14 | 11 | description: >- |
15 | | - Access server request context with getRequestEvent. Read headers, set cookies, |
16 | | - and manage response status in SSR and server functions. |
| 12 | + Read the current request event from `solid-js/web`. |
17 | 13 | --- |
18 | 14 |
|
19 | | -Solid uses Async Local Storage as a way of injecting the request context anywhere on the server. |
20 | | -The server provides a utility function to access this context |
21 | | -(called a `RequestEvent`). |
| 15 | +`getRequestEvent` returns the current request event when one is available. |
22 | 16 |
|
23 | | -```js |
24 | | -import { getRequestEvent } from "solid-js/web" |
25 | | -import type { RequestEvent } from "solid-js/web" |
| 17 | +## Import |
26 | 18 |
|
27 | | -function getRequestEvent(): RequestEvent | undefined |
| 19 | +```ts |
| 20 | +import { getRequestEvent } from "solid-js/web"; |
28 | 21 | ``` |
29 | 22 |
|
30 | | -You can retrieve the request event by calling `getRequestEvent`: |
| 23 | +## Type |
31 | 24 |
|
32 | | -```js |
33 | | -import { getRequestEvent } from "solid-js/web"; |
| 25 | +```ts |
| 26 | +interface RequestEvent { |
| 27 | + request: Request; |
| 28 | + locals?: Record<string | number | symbol, any>; |
| 29 | +} |
34 | 30 |
|
35 | | -const event = getRequestEvent(); |
| 31 | +function getRequestEvent(): RequestEvent | undefined; |
36 | 32 | ``` |
37 | 33 |
|
38 | | -## Request |
| 34 | +## Return value |
39 | 35 |
|
40 | | -`.request` is the most important property of the `RequestEvent`. |
41 | | -This is a Web [Request object](https://developer.mozilla.org/en-US/docs/Web/API/Request) that represents the current request to the server. |
42 | | -You can access properties off of it such as `url` and `headers`. |
43 | | -`body`, however, does not typically need to be handled directly for things such as server functions or rendering, which already handle mapping. |
| 36 | +- **Type:** `RequestEvent | undefined` |
44 | 37 |
|
45 | | -```js |
46 | | -import { getRequestEvent } from "solid-js/web"; |
| 38 | +## Behavior |
47 | 39 |
|
48 | | -const event = getRequestEvent(); |
49 | | -if (event) { |
50 | | - const auth = event.request.headers.get("Authorization"); |
51 | | -} |
52 | | -``` |
| 40 | +- On the server, the returned event includes the current `Request`. |
| 41 | +- In the server declarations here, `RequestEvent` also includes `locals`. |
| 42 | +- If no request event is available, `getRequestEvent` returns `undefined`. |
53 | 43 |
|
54 | | -## Response |
| 44 | +## Examples |
55 | 45 |
|
56 | | -The `getRequestEvent` can also be used to stub out the Response - this extends the [options that can be passed to the `Response constructor`](https://developer.mozilla.org/en-US/docs/Web/API/Response/Response#options). |
57 | | -This is kept up to date so it can be used to read and write headers and status for the current response. |
| 46 | +### Basic usage |
58 | 47 |
|
59 | | -```js |
| 48 | +```ts |
60 | 49 | import { getRequestEvent } from "solid-js/web"; |
61 | 50 |
|
62 | 51 | const event = getRequestEvent(); |
| 52 | + |
63 | 53 | if (event) { |
64 | | - event.response.headers.append("Set-Cookie", "foo=hello"); |
65 | | - event.response.status = 201; |
| 54 | + const auth = event.request.headers.get("Authorization"); |
66 | 55 | } |
67 | 56 | ``` |
68 | | - |
69 | | -### Change event.response or create a new Response |
70 | | - |
71 | | -The `getRequestEvent` event is considered global and lasts the life of the request. |
72 | | -Therefore, if you are calling a server function on the server during SSR or an RPC call, setting values on `event.response` will reflect on that request. |
73 | | - |
74 | | -The returned response will only impact the response when it is an RPC call. |
75 | | -This is important because some headers previously set may not be needed to be set for the whole page, but only for a specific request. |
76 | | - |
77 | | -**Note:** This is important to keep in mind when choosing where to set headers and responses. |
78 | | - |
79 | | -:::note[Usage with SolidStart] |
80 | | -See this guide on [Request |
81 | | -Events](/solid-start/advanced/request-events). |
82 | | -::: |
0 commit comments