Skip to content

Commit ed22b63

Browse files
committed
migrate server rendering reference pages
1 parent e3b30c6 commit ed22b63

1 file changed

Lines changed: 24 additions & 50 deletions

File tree

Lines changed: 24 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,56 @@
11
---
22
title: getRequestEvent
33
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
65
tags:
76
- server
87
- ssr
98
- request
10-
- headers
11-
- authentication
129
- context
1310
version: "1.0"
1411
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`.
1713
---
1814

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.
2216

23-
```js
24-
import { getRequestEvent } from "solid-js/web"
25-
import type { RequestEvent } from "solid-js/web"
17+
## Import
2618

27-
function getRequestEvent(): RequestEvent | undefined
19+
```ts
20+
import { getRequestEvent } from "solid-js/web";
2821
```
2922

30-
You can retrieve the request event by calling `getRequestEvent`:
23+
## Type
3124

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+
}
3430

35-
const event = getRequestEvent();
31+
function getRequestEvent(): RequestEvent | undefined;
3632
```
3733

38-
## Request
34+
## Return value
3935

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`
4437

45-
```js
46-
import { getRequestEvent } from "solid-js/web";
38+
## Behavior
4739

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`.
5343

54-
## Response
44+
## Examples
5545

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
5847

59-
```js
48+
```ts
6049
import { getRequestEvent } from "solid-js/web";
6150

6251
const event = getRequestEvent();
52+
6353
if (event) {
64-
event.response.headers.append("Set-Cookie", "foo=hello");
65-
event.response.status = 201;
54+
const auth = event.request.headers.get("Authorization");
6655
}
6756
```
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

Comments
 (0)