@rsc-kit/mcp 0.41.0 → 0.42.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/recipes.js +12 -2
- package/dist/recipes.js.map +1 -1
- package/guides/admin-plugins.md +206 -6
- package/guides/api-routes.md +159 -215
- package/guides/authorization.md +103 -114
- package/guides/backend-answered-pages.md +301 -252
- package/guides/bap-vs-inertia.md +67 -25
- package/guides/blueprints.md +300 -169
- package/guides/bun.md +42 -25
- package/guides/caching.md +46 -49
- package/guides/coming-from-inertia.md +2 -2
- package/guides/commands.md +251 -200
- package/guides/connection.md +50 -54
- package/guides/content-security-policy.md +133 -84
- package/guides/domains.md +190 -149
- package/guides/edge-caching.md +242 -268
- package/guides/emails.md +96 -45
- package/guides/errors.md +116 -137
- package/guides/feature-flags.md +100 -30
- package/guides/file-uploads.md +101 -26
- package/guides/fonts.md +139 -110
- package/guides/forms.md +520 -542
- package/guides/images.md +87 -28
- package/guides/index.json +13 -33
- package/guides/instrumentation.md +134 -95
- package/guides/isr.md +3 -3
- package/guides/live-data-in-production.md +695 -0
- package/guides/live-data.md +170 -839
- package/guides/mcp.md +83 -59
- package/guides/metadata.md +124 -179
- package/guides/navigation.md +118 -147
- package/guides/no-javascript.md +49 -57
- package/guides/offline.md +118 -153
- package/guides/openapi.md +100 -35
- package/guides/pages.md +160 -50
- package/guides/ppr.md +178 -256
- package/guides/pwa.md +251 -218
- package/guides/queries.md +171 -300
- package/guides/react-compiler.md +90 -50
- package/guides/redirects.md +141 -177
- package/guides/response-headers.md +76 -74
- package/guides/route-interception.md +112 -111
- package/guides/routing.md +216 -322
- package/guides/sections.md +63 -66
- package/guides/seo-files.md +127 -65
- package/guides/server-actions.md +285 -357
- package/guides/static-generation.md +131 -199
- package/guides/testing.md +269 -147
- package/guides/third-party-scripts.md +94 -47
- package/guides/typed-routes.md +115 -113
- package/guides/url-validation.md +113 -61
- package/guides/validation.md +53 -62
- package/guides/view-transitions.md +159 -132
- package/guides/where-it-runs.md +226 -162
- package/package.json +2 -2
- package/guides/admin.md +0 -520
- package/guides/deployment.md +0 -598
- package/guides/go.md +0 -539
- package/guides/laravel.md +0 -787
- package/guides/your-own-backend.md +0 -602
package/guides/api-routes.md
CHANGED
|
@@ -2,11 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
> Web-standard endpoints, colocated with your pages, with no configuration.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
`src/app/api/health/route.ts` answers `/api/health`:
|
|
5
|
+
An API route answers a caller that isn't your own pages: a webhook, an OAuth
|
|
6
|
+
callback, a health check, a mobile app. A `route.ts` sits in the same
|
|
7
|
+
`src/app` tree as your pages, and its URL comes from its folder:
|
|
10
8
|
|
|
11
9
|
```ts title="src/app/api/health/route.ts"
|
|
12
10
|
export function GET(request: Request): Response {
|
|
@@ -14,18 +12,19 @@ export function GET(request: Request): Response {
|
|
|
14
12
|
}
|
|
15
13
|
```
|
|
16
14
|
|
|
17
|
-
The export name is the method
|
|
18
|
-
`
|
|
15
|
+
`GET /api/health` now answers. The export name is the method, the argument is
|
|
16
|
+
an ordinary `Request`, and you return a `Response`. It runs the same way on Bun,
|
|
17
|
+
Node and Cloudflare Workers.
|
|
19
18
|
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
For your own client, a [query](/guides/queries/) reads data and a
|
|
20
|
+
[server action](/guides/server-actions/) writes it, both typed with no
|
|
21
|
+
endpoint to write. Use an API route when the caller can't import your
|
|
22
|
+
functions.
|
|
22
23
|
|
|
23
|
-
|
|
24
|
-
that answers a page, not by a separate server, so a Worker, a Node process and a
|
|
25
|
-
Bun binary all get the identical behaviour from the identical code — verified on
|
|
26
|
-
Cloudflare Workers, which is the one with no Node apis to fall back on.
|
|
24
|
+
## Handling methods
|
|
27
25
|
|
|
28
|
-
|
|
26
|
+
Export one function per method: `GET`, `POST`, `PUT`, `PATCH`, `DELETE`, `HEAD`
|
|
27
|
+
or `OPTIONS`.
|
|
29
28
|
|
|
30
29
|
```ts title="src/app/api/echo/route.ts"
|
|
31
30
|
export async function POST(request: Request): Promise<Response> {
|
|
@@ -48,47 +47,80 @@ GET /api/echo → 200 {"usage":"POST { \"name\": \"…\" }"}
|
|
|
48
47
|
PUT /api/echo → 405 Allow: GET, POST, HEAD
|
|
49
48
|
```
|
|
50
49
|
|
|
51
|
-
|
|
52
|
-
|
|
50
|
+
- A method you didn't export is answered **405**, with an `Allow` header naming
|
|
51
|
+
the ones you did.
|
|
52
|
+
- `HEAD` is answered by your `GET` with the body stripped.
|
|
53
|
+
- A `route.ts` that exports no method fails the build.
|
|
54
|
+
|
|
55
|
+
Read the body with the web API: `request.json()`, `.formData()`, `.text()`.
|
|
56
|
+
|
|
57
|
+
## Dynamic segments
|
|
58
|
+
|
|
59
|
+
`[name]` and `[...rest]` work as they do for [pages](/guides/routing#dynamic-segments).
|
|
60
|
+
The values arrive in the second argument as a promise. Type it with
|
|
61
|
+
`RouteContext` and the route's pattern:
|
|
62
|
+
|
|
63
|
+
```ts title="src/app/api/greet/[name]/route.ts"
|
|
64
|
+
import type { RouteContext } from '@rsc-kit/core/route-schema'
|
|
65
|
+
|
|
66
|
+
export async function GET(request: Request, { params }: RouteContext<'/api/greet/[name]'>) {
|
|
67
|
+
const { name } = await params
|
|
68
|
+
|
|
69
|
+
return Response.json({ greeting: `Hello, ${name}` })
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
```
|
|
74
|
+
GET /api/greet/ada → {"greeting":"Hello, ada"}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
The pattern is checked against your routes, so a typo fails the typecheck, and
|
|
78
|
+
so does reading `params.name` without the `await`.
|
|
53
79
|
|
|
54
|
-
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
80
|
+
- `RouteHandler<'/api/greet/[name]'>` types the whole function instead:
|
|
81
|
+
`export const GET: RouteHandler<…> = async (request, { params }) => …`.
|
|
82
|
+
- With a [`params` schema](/guides/url-validation), `RouteContext<typeof params>`
|
|
83
|
+
types them from its output.
|
|
84
|
+
- A static segment beats a dynamic one: `/api/user/me` is answered by
|
|
85
|
+
`me/route.ts`, not `[id]/route.ts`.
|
|
60
86
|
|
|
61
|
-
|
|
87
|
+
## Guarding a route
|
|
88
|
+
|
|
89
|
+
A route runs the `middleware.ts` files above it, as a page in that folder
|
|
90
|
+
would, so an endpoint under a guarded path needs no second check.
|
|
91
|
+
See [Authorization](/guides/authorization/#protect-an-api-route).
|
|
62
92
|
|
|
63
|
-
A
|
|
64
|
-
|
|
93
|
+
A refusal answers code that fetched the route with a `401` or `403` and the
|
|
94
|
+
destination in `X-RSC-Redirect`, rather than a redirect `fetch` would follow
|
|
95
|
+
to a login page. A browser that navigated to the route gets a real `Location`.
|
|
96
|
+
|
|
97
|
+
A `redirect()` thrown from the handler is the route's own answer, a real `3xx`
|
|
98
|
+
for whoever asked. `notFound()` answers `404`.
|
|
65
99
|
|
|
66
100
|
## Posts from another site
|
|
67
101
|
|
|
68
102
|
A `POST`, `PUT`, `PATCH` or `DELETE` that a browser sends from another site is
|
|
69
|
-
answered **403** before your handler runs.
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
`
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
neither header and is never refused. A browser app on another origin that should
|
|
79
|
-
be able to post is named:
|
|
103
|
+
answered **403** before your handler runs. Otherwise any page on the web could
|
|
104
|
+
post a form to your route with your visitor's cookies. The browser's
|
|
105
|
+
`Sec-Fetch-Site` header says where the request came from, and an older
|
|
106
|
+
browser's `Origin` is compared with your host. Actions and forms follow the
|
|
107
|
+
same rule.
|
|
108
|
+
|
|
109
|
+
A caller that isn't a browser, such as a webhook, a cron job or another server,
|
|
110
|
+
sends neither header and is never refused. To let a browser app on another
|
|
111
|
+
origin post, name it:
|
|
80
112
|
|
|
81
113
|
```ts title="vite.config.ts"
|
|
82
114
|
rscKit({ trustedOrigins: ['https://admin.example.com'] })
|
|
83
115
|
```
|
|
84
116
|
|
|
85
|
-
`GET`, `HEAD` and `OPTIONS`
|
|
117
|
+
`GET`, `HEAD` and `OPTIONS` are answered from anywhere.
|
|
86
118
|
|
|
87
119
|
### An api other sites call
|
|
88
120
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
121
|
+
When other sites post to a route from the browser, and their origins live in
|
|
122
|
+
your database rather than in config, such as a chat widget embedded on your
|
|
123
|
+
customers' pages, the route says so itself:
|
|
92
124
|
|
|
93
125
|
```ts title="src/app/api/v1/chat/route.ts"
|
|
94
126
|
export const crossOrigin = true
|
|
@@ -101,74 +133,21 @@ export async function POST(request: Request) {
|
|
|
101
133
|
|
|
102
134
|
Another site's request then reaches the handler **without the visitor's
|
|
103
135
|
cookies**: `cookies()`, `headers()`, `request` and any backend call see none,
|
|
104
|
-
and a guard that reads the session finds nobody.
|
|
105
|
-
|
|
106
|
-
site's requests keep them. Authenticate with a key the caller sends, and check
|
|
107
|
-
the `Origin` yourself if each key belongs to particular sites. Answering the
|
|
108
|
-
browser's CORS preflight - an `OPTIONS` export - is still the route's job.
|
|
109
|
-
`true` or `false` only, written as a literal: the build reads it from the source.
|
|
110
|
-
|
|
111
|
-
## Dynamic segments
|
|
136
|
+
and a guard that reads the session finds nobody. Your own site's requests keep
|
|
137
|
+
them.
|
|
112
138
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
import type { RouteContext } from '@rsc-kit/core/route-schema'
|
|
120
|
-
|
|
121
|
-
export async function GET(request: Request, { params }: RouteContext<'/api/greet/[name]'>) {
|
|
122
|
-
const { name } = await params
|
|
123
|
-
|
|
124
|
-
return Response.json({ greeting: `Hello, ${name}` })
|
|
125
|
-
}
|
|
126
|
-
```
|
|
127
|
-
|
|
128
|
-
```
|
|
129
|
-
GET /api/greet/ada → {"greeting":"Hello, ada"}
|
|
130
|
-
```
|
|
131
|
-
|
|
132
|
-
A promise for the same reason a page's is: a route that never awaits its
|
|
133
|
-
query string provably does not vary by it, so the build can store one answer.
|
|
134
|
-
It also means `params.name` without the `await` is a compile error rather
|
|
135
|
-
than a route that quietly answers 404 to everything — which is what a
|
|
136
|
-
hand-written `{ params: { name: string } }` used to let through.
|
|
137
|
-
`RouteHandler<'/api/greet/[name]'>` types the whole function when you would
|
|
138
|
-
rather write `export const GET: RouteHandler<…> = async (request, { params }) => …`.
|
|
139
|
-
With a [`params` schema](/guides/url-validation), `RouteContext<typeof params>`
|
|
140
|
-
types them from its output instead.
|
|
141
|
-
|
|
142
|
-
It is the same router pages use, so a static segment beats a dynamic one —
|
|
143
|
-
`/api/user/me` is answered by `me/route.ts` rather than `[id]/route.ts`,
|
|
144
|
-
whichever was declared first.
|
|
145
|
-
|
|
146
|
-
## Guarding one
|
|
147
|
-
|
|
148
|
-
A route runs whatever `middleware.ts` files sit above it, exactly as a page in
|
|
149
|
-
that directory would — so an endpoint under a guarded path is guarded without
|
|
150
|
-
writing the check twice. To code that fetched the route, a refusal is a
|
|
151
|
-
`401` or `403` with the destination in `X-RSC-Redirect` rather than a
|
|
152
|
-
redirect, because `fetch` would follow one and hand back a login page as
|
|
153
|
-
though it were your data. To a browser that navigated to the route — followed
|
|
154
|
-
a link to it, typed the url — it is a real `Location`, because a browser
|
|
155
|
-
should be sent on, not shown "Unauthorized" over nothing.
|
|
156
|
-
|
|
157
|
-
A `redirect()` thrown from the handler itself is different: it is the
|
|
158
|
-
route's answer, not a refusal — the export lives on a signed url now, the old
|
|
159
|
-
endpoint moved — and it is a real `3xx` with a `Location` for whoever asked.
|
|
160
|
-
`notFound()` is its `404`.
|
|
161
|
-
|
|
162
|
-
See [Authorization](/guides/authorization/#protect-an-api-route).
|
|
139
|
+
- Authenticate with a key the caller sends, and check the `Origin` yourself if
|
|
140
|
+
each key belongs to particular sites.
|
|
141
|
+
- Answering the browser's CORS preflight, with an `OPTIONS` export, is still
|
|
142
|
+
the route's job.
|
|
143
|
+
- `crossOrigin` must be the literal `true` or `false`: the build reads it from
|
|
144
|
+
the source.
|
|
163
145
|
|
|
164
146
|
## The build answers what it can
|
|
165
147
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
At build time every `GET` is called once with no request behind it. A route
|
|
170
|
-
that answers without reading anything is stored, headers and status and all,
|
|
171
|
-
and served from disk afterwards:
|
|
148
|
+
The build calls every `GET` once. A route that answers without reading the
|
|
149
|
+
request is stored, status and headers included, and served from disk, like a
|
|
150
|
+
page:
|
|
172
151
|
|
|
173
152
|
```text
|
|
174
153
|
○ /api/health
|
|
@@ -179,8 +158,7 @@ and served from disk afterwards:
|
|
|
179
158
|
one url per param value, and none are listed
|
|
180
159
|
```
|
|
181
160
|
|
|
182
|
-
|
|
183
|
-
opts itself out, because with no request to read the accessors never resolve:
|
|
161
|
+
Reading the request makes a route run per request:
|
|
184
162
|
|
|
185
163
|
```ts
|
|
186
164
|
export async function GET(request: Request) {
|
|
@@ -188,61 +166,41 @@ export async function GET(request: Request) {
|
|
|
188
166
|
}
|
|
189
167
|
```
|
|
190
168
|
|
|
191
|
-
`cookies()`, `headers()` and `await connection()` do the same
|
|
192
|
-
`connection()`
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
- **
|
|
203
|
-
- **
|
|
204
|
-
|
|
205
|
-
- **
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
cannot see, and its refusal is not an answer to keep
|
|
210
|
-
- **answers with bytes rather than text**, which is a file and wants serving as
|
|
211
|
-
one
|
|
212
|
-
- **does not finish**, which with no request behind it usually means it was
|
|
213
|
-
waiting on one
|
|
214
|
-
|
|
215
|
-
And one thing it stores with a warning, the same one a page gets: a route
|
|
216
|
-
that froze `Date.now()` or `Math.random()` keeps whatever that returned at
|
|
217
|
-
build time, and a json body has no browser to move the value to. Read the
|
|
218
|
-
request — `await connection()` is enough — and it runs on demand.
|
|
169
|
+
`cookies()`, `headers()` and `await connection()` do the same. Use
|
|
170
|
+
`connection()` to say it outright when nothing else in the route does.
|
|
171
|
+
|
|
172
|
+
A route runs per request when it:
|
|
173
|
+
|
|
174
|
+
- **reads the request:** headers, the body, the signal, or `request.url`. To
|
|
175
|
+
read the query and stay storable, await `searchParams` instead of
|
|
176
|
+
`new URL(request.url).searchParams`.
|
|
177
|
+
- **takes a parameter**, since the build doesn't know which values exist
|
|
178
|
+
- **sits under a `middleware.ts`**, since a guarded route answers differently
|
|
179
|
+
per caller
|
|
180
|
+
- **sets a cookie**, which is an answer for one visitor
|
|
181
|
+
- **answers 4xx or 5xx to the build**, which sent no request to refuse
|
|
182
|
+
- **answers with bytes rather than text**, which is a file
|
|
183
|
+
- **does not finish**, usually because it was waiting on a request
|
|
184
|
+
|
|
185
|
+
A route that used `Date.now()` or `Math.random()` is stored with a warning, the
|
|
186
|
+
same one a page gets. `await connection()` makes it run per request.
|
|
219
187
|
|
|
220
188
|
### What is never stored
|
|
221
189
|
|
|
222
|
-
Only `GET
|
|
223
|
-
|
|
190
|
+
Only a `GET` is stored. A `POST`, `PUT`, `PATCH` or `DELETE` is something meant
|
|
191
|
+
to happen each time.
|
|
224
192
|
|
|
225
|
-
A request
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
is
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
is not stored at all — the table says `reads the request — url` and what to
|
|
232
|
-
await.
|
|
193
|
+
A request with a **query string** runs the route only if the route reads the
|
|
194
|
+
query. A stored route that never awaits `searchParams` serves its stored answer
|
|
195
|
+
for `?utm_source=anything`. Await it, or export a `searchParams` schema, and the
|
|
196
|
+
stored answer is kept for the bare URL alone. A route that reads the query off
|
|
197
|
+
`request.url` is not stored at all; the build output says
|
|
198
|
+
`reads the request — url` and what to await.
|
|
233
199
|
|
|
234
200
|
## A route that streams
|
|
235
201
|
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
once, which is better per update than [polling a query](/guides/queries/#live-data)
|
|
239
|
-
by a wide margin. The costs are a connection held open per tab for as long as
|
|
240
|
-
it is open — fine on Bun or Node, a limit on a Worker or a small container
|
|
241
|
-
behind a balancer — no caching, and a source of change to yield from: a
|
|
242
|
-
database listener, a queue, a timer. A generator that polls the database
|
|
243
|
-
itself has only moved the polling and kept a connection for the privilege.
|
|
244
|
-
Start with polling when you have no change feed yet or your host holds few
|
|
245
|
-
connections; move to events when you do and it does.
|
|
202
|
+
`events()` turns an async generator into server-sent events. Each `yield` is a
|
|
203
|
+
message, sent the moment it happens:
|
|
246
204
|
|
|
247
205
|
```ts title="src/app/api/orders/[id]/events/route.ts"
|
|
248
206
|
import { events, named } from '@rsc-kit/core/events'
|
|
@@ -258,44 +216,20 @@ export const GET = events(async function* ({ params, signal }) {
|
|
|
258
216
|
})
|
|
259
217
|
```
|
|
260
218
|
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
frames each yield as JSON, sends a keepalive every eight seconds so nothing in
|
|
264
|
-
front drops an idle stream ([`RSC_STREAM_KEEPALIVE_MS`](/hosts/deployment#streams-and-idle-timeouts)
|
|
265
|
-
to change it), sets `text/event-stream` and `no-store, no-transform`, is never
|
|
266
|
-
compressed, and ends the generator when
|
|
267
|
-
the browser goes away — that is what `signal` is for. `named(event, data, {
|
|
268
|
-
id })` names a message or gives it an id, for a client that listens by name or
|
|
269
|
-
resumes after a reconnect.
|
|
219
|
+
It is an ordinary `route.ts`: it runs the `middleware.ts` above it and takes the
|
|
220
|
+
same params.
|
|
270
221
|
|
|
271
|
-
|
|
272
|
-
|
|
222
|
+
- Each yield is sent as JSON.
|
|
223
|
+
- A keepalive goes out every eight seconds so a proxy doesn't drop an idle
|
|
224
|
+
stream. [`RSC_STREAM_KEEPALIVE_MS`](/hosts/deployment#streams-and-idle-timeouts)
|
|
225
|
+
changes it.
|
|
226
|
+
- The response is `text/event-stream`, `no-store, no-transform`, and never
|
|
227
|
+
compressed. A stream is never stored, whatever it reads.
|
|
228
|
+
- The generator ends when the browser goes away; that is what `signal` is for.
|
|
229
|
+
- `named(event, data, { id })` names a message or gives it an id, for a client
|
|
230
|
+
that listens by name or resumes after a reconnect.
|
|
273
231
|
|
|
274
|
-
|
|
275
|
-
// latest: { status: Status } | null - what the route yields
|
|
276
|
-
|
|
277
|
-
const closed = useEvents(`/api/orders/${id}/events`, { event: 'closed' })
|
|
278
|
-
// closed.latest: { id: string } | null - the messages named 'closed'
|
|
279
|
-
```
|
|
280
|
-
|
|
281
|
-
A message is typed from the route. The build records what each `events()`
|
|
282
|
-
route yields, as it records every url, so `latest` is the generator's type
|
|
283
|
-
and a route that changes what it sends fails the typecheck where the hook
|
|
284
|
-
reads it. A url no route answers does not compile. A stream with nothing to
|
|
285
|
-
read a type from — another site's, or a `route.ts` that builds its own
|
|
286
|
-
response — is `unknown` until you name it: `useEvents<Message>(url)`.
|
|
287
|
-
|
|
288
|
-
`useEvents` subscribes in an effect, so it is safe in a component that also
|
|
289
|
-
renders on the server. The browser's `EventSource` reconnects on its own after
|
|
290
|
-
a dropped connection and resumes with `Last-Event-ID` when you yielded ids.
|
|
291
|
-
What it does not survive is an error answer — a 502 while a deploy restarts, a
|
|
292
|
-
proxy's timeout page — after which the browser gives up for good; the hook
|
|
293
|
-
opens a new connection, waiting a second, then two, then four, up to thirty,
|
|
294
|
-
and at once when the browser comes back online. `status` says where it is, and
|
|
295
|
-
`close()` stops it.
|
|
296
|
-
|
|
297
|
-
**No library needed.** `latest` is state, and `all` is the messages so far.
|
|
298
|
-
A component that only wants to show the value reads it and is done:
|
|
232
|
+
### Reading a stream in a component
|
|
299
233
|
|
|
300
234
|
```tsx
|
|
301
235
|
'use client'
|
|
@@ -315,9 +249,21 @@ export function OrderStatus({ id, initial }: { id: string; initial: { status: St
|
|
|
315
249
|
}
|
|
316
250
|
```
|
|
317
251
|
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
252
|
+
`latest` is the last message and `all` is every message so far. Pass `event` to
|
|
253
|
+
read only the messages with that name:
|
|
254
|
+
|
|
255
|
+
```tsx
|
|
256
|
+
const closed = useEvents(`/api/orders/${id}/events`, { event: 'closed' })
|
|
257
|
+
// closed.latest: { id: string } | null
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
Messages are typed from the route: `latest` is what the generator yields, and
|
|
261
|
+
a route that changes what it sends fails the typecheck where the hook reads it.
|
|
262
|
+
A URL no route answers does not compile. For a stream with no type to read,
|
|
263
|
+
such as another site's, name it: `useEvents<Message>(url)`.
|
|
264
|
+
|
|
265
|
+
To keep the value in your own state, a reducer or a cache library, pass
|
|
266
|
+
`onMessage`:
|
|
321
267
|
|
|
322
268
|
```tsx
|
|
323
269
|
const [order, setOrder] = useState(initial)
|
|
@@ -333,9 +279,16 @@ useEvents(`/api/orders/${id}/events`, {
|
|
|
333
279
|
})
|
|
334
280
|
```
|
|
335
281
|
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
282
|
+
### Reconnecting
|
|
283
|
+
|
|
284
|
+
The hook reconnects on its own. The browser's `EventSource` reconnects after a
|
|
285
|
+
dropped connection, resuming with `Last-Event-ID` when you yielded ids. After an
|
|
286
|
+
error answer, such as a 502 during a deploy, the browser gives up, so the hook
|
|
287
|
+
opens a new connection, waiting a second, then two, then four, up to thirty, and
|
|
288
|
+
at once when the browser comes back online.
|
|
289
|
+
|
|
290
|
+
`status` says where it is, `error` holds the last error, and `close()` stops
|
|
291
|
+
it. `onError` fires when it happens, for a notice rather than a retry:
|
|
339
292
|
|
|
340
293
|
```tsx
|
|
341
294
|
useEvents(`/api/orders/${id}/events`, {
|
|
@@ -344,24 +297,15 @@ useEvents(`/api/orders/${id}/events`, {
|
|
|
344
297
|
})
|
|
345
298
|
```
|
|
346
299
|
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
## What to use when
|
|
350
|
-
|
|
351
|
-
| | |
|
|
352
|
-
| --- | --- |
|
|
353
|
-
| your client reads data | a [query](/guides/queries/) — typed, no endpoint to write |
|
|
354
|
-
| your client writes data | a [server action](/guides/server-actions/) |
|
|
355
|
-
| something that is not your client | an API route |
|
|
300
|
+
`useEvents` subscribes in an effect, so it is safe in a component that also
|
|
301
|
+
renders on the server.
|
|
356
302
|
|
|
357
|
-
|
|
358
|
-
the typed call is better than a url both sides have to agree on.
|
|
303
|
+
### Streaming or polling
|
|
359
304
|
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
:::
|
|
305
|
+
A stream sends bytes only when something changed, which is far cheaper per
|
|
306
|
+
update than [polling a query](/guides/queries/#live-data). It costs an open
|
|
307
|
+
connection per tab: fine on Bun or Node, a limit on a Worker or a small
|
|
308
|
+
container behind a load balancer. It needs a source of changes to yield from,
|
|
309
|
+
such as a database listener, a queue or a timer; a generator that polls the
|
|
310
|
+
database itself has only moved the polling. Start with polling when you have no
|
|
311
|
+
change feed or your host holds few connections.
|