@rsc-kit/mcp 0.40.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.
Files changed (60) hide show
  1. package/dist/recipes.js +37 -20
  2. package/dist/recipes.js.map +1 -1
  3. package/guides/admin-plugins.md +206 -6
  4. package/guides/api-routes.md +159 -215
  5. package/guides/authorization.md +103 -114
  6. package/guides/backend-answered-pages.md +301 -250
  7. package/guides/bap-vs-inertia.md +67 -25
  8. package/guides/blueprints.md +307 -174
  9. package/guides/bun.md +42 -25
  10. package/guides/caching.md +46 -49
  11. package/guides/coming-from-inertia.md +5 -5
  12. package/guides/commands.md +251 -200
  13. package/guides/connection.md +50 -54
  14. package/guides/content-security-policy.md +133 -84
  15. package/guides/domains.md +190 -149
  16. package/guides/edge-caching.md +242 -268
  17. package/guides/emails.md +96 -45
  18. package/guides/errors.md +116 -137
  19. package/guides/feature-flags.md +100 -30
  20. package/guides/file-uploads.md +101 -26
  21. package/guides/fonts.md +139 -110
  22. package/guides/forms.md +520 -538
  23. package/guides/images.md +87 -28
  24. package/guides/index.json +13 -33
  25. package/guides/instrumentation.md +134 -95
  26. package/guides/isr.md +3 -3
  27. package/guides/live-data-in-production.md +695 -0
  28. package/guides/live-data.md +170 -839
  29. package/guides/mcp.md +83 -59
  30. package/guides/metadata.md +124 -179
  31. package/guides/navigation.md +118 -147
  32. package/guides/no-javascript.md +49 -57
  33. package/guides/offline.md +118 -153
  34. package/guides/openapi.md +100 -35
  35. package/guides/pages.md +160 -50
  36. package/guides/ppr.md +178 -256
  37. package/guides/pwa.md +251 -218
  38. package/guides/queries.md +171 -300
  39. package/guides/react-compiler.md +90 -50
  40. package/guides/redirects.md +141 -177
  41. package/guides/response-headers.md +76 -74
  42. package/guides/route-interception.md +112 -111
  43. package/guides/routing.md +216 -322
  44. package/guides/sections.md +63 -66
  45. package/guides/seo-files.md +127 -65
  46. package/guides/server-actions.md +285 -357
  47. package/guides/static-generation.md +131 -199
  48. package/guides/testing.md +272 -150
  49. package/guides/third-party-scripts.md +94 -47
  50. package/guides/typed-routes.md +115 -113
  51. package/guides/url-validation.md +113 -61
  52. package/guides/validation.md +53 -62
  53. package/guides/view-transitions.md +159 -132
  54. package/guides/where-it-runs.md +226 -162
  55. package/package.json +2 -2
  56. package/guides/admin.md +0 -520
  57. package/guides/deployment.md +0 -598
  58. package/guides/go.md +0 -538
  59. package/guides/laravel.md +0 -786
  60. package/guides/your-own-backend.md +0 -587
@@ -2,11 +2,9 @@
2
2
 
3
3
  > Web-standard endpoints, colocated with your pages, with no configuration.
4
4
 
5
- Server actions and [queries](/guides/queries/) cover your own client. An API
6
- route is for everyone else: a webhook, an OAuth callback, a health check, a
7
- mobile app, a third party.
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. The argument is an ordinary `Request` and a
18
- `Response` goes back — no event object, no adapter, no handler wrapper.
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
- Nothing to configure. `route.ts` lives in the same `src/app` tree as your
21
- pages, and the url comes from the directory the same way a page's does.
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
- It also runs everywhere the pages do. A route is answered by the same handler
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
- ## One export per method
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
- `GET`, `POST`, `PUT`, `PATCH`, `DELETE`, `HEAD` and `OPTIONS`. Two you do not
52
- have to write:
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
- - **The 405.** A method you did not export is refused, with an `Allow` header
55
- naming the ones you did — without which a client cannot tell what would have
56
- worked.
57
- - **`HEAD`.** Answered by your `GET`, with the body stripped, which is what the
58
- spec says `HEAD` is. Refusing it breaks link checkers and anything that probes
59
- before it fetches.
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
- Reading a body is the web api: `request.json()`, `.formData()`, `.text()`.
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 route that exports no method at all fails the build rather than becoming a url
64
- that answers 405 to everything.
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. Without that, any page on the web
70
- could post a form to your route with your visitor's cookies, and a handler
71
- reading `cookies()` would act as them. No token is involved: the browser's
72
- `Sec-Fetch-Site` header says where the request came from, and an older browser's
73
- `Origin` is compared with your host. It is the rule Go's
74
- `http.CrossOriginProtection` and Laravel 13 use, and actions and forms are held
75
- to it too.
76
-
77
- A caller that is not a browser - a webhook, a cron job, another server - sends
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` change nothing and are answered from anywhere.
117
+ `GET`, `HEAD` and `OPTIONS` are answered from anywhere.
86
118
 
87
119
  ### An api other sites call
88
120
 
89
- A route other sites post to from the browser - a chat widget embedded on your
90
- customers' pages, whose origins live in your database rather than in a config
91
- file - says so itself:
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. A forged post needs those
105
- cookies, so taking them away is what makes the route safe to open; your own
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
- `[name]` and `[...rest]` work as they do for pages, and bind into the second
114
- argument — as a **promise**, the way a page's `params` are. Type the context
115
- from the route it answers and the names come from the segments; the pattern
116
- is checked against the routes the build found, so a typo fails `tsc`:
117
-
118
- ```ts title="src/app/api/greet/[name]/route.ts"
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
- A route is frozen by default, exactly the way a page is, and for the same
167
- reason — so there is one model to learn rather than two.
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
- Nothing to configure and nothing to opt into. Read the request and the route
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, and
192
- `connection()` is how you say it deliberately when nothing else in the route
193
- happens to give it away.
194
-
195
- A route is left to run per request when it:
196
-
197
- - **reads the request** — headers, the body, the signal, and `request.url`.
198
- The last is the one a Next port trips over: `new URL(request.url).searchParams`
199
- reads the query where the build cannot see it, and a webhook's verification
200
- handshake stored as a 403 would answer every real one. Read the query from
201
- the awaited `searchParams` and the bare url stays storable.
202
- - **takes a parameter**, since the build does not know which values exist
203
- - **sits under a `middleware.ts`**, because a guarded route answers differently
204
- per caller and one stored answer served to everyone is how a guard disappears
205
- - **sets a cookie**, which is an answer for one visitor whatever the route
206
- read to decide on it — stored, the build's cookie would be handed to everyone
207
- - **answers 4xx or 5xx to the build**, which sent no request to refuse — a
208
- route that says `403` with nothing read is checking something the build
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`. A `POST`, `PUT`, `PATCH` or `DELETE` is something meant to happen,
223
- and an answer kept on disk is an answer to something that already did.
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 carrying a **query string** runs the route only when the route reads
226
- one. `searchParams` is awaited rather than handed over resolved, so a route that
227
- never reaches for it provably does not vary by the query — and its stored answer
228
- is served for `?utm_source=anything`, which is most of the links people actually
229
- follow. Await it, or export a `searchParams` schema, and the stored answer is
230
- kept for the bare url alone. Read it off `request.url` instead and the route
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
- A route may answer for as long as the tab is open: server-sent events, from
237
- an async generator. Bytes go out only when something changed and arrive at
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
- An ordinary `route.ts`, so everything above applies: it lives beside its
262
- pages, runs the `middleware.ts` above it, takes the same params. `events()`
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
- ```tsx
272
- import { useEvents } from '@rsc-kit/core/useEvents'
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
- const { latest, status } = useEvents(`/api/orders/${id}/events`)
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
- And when the value already lives somewhere — a `useState`, a reducer, a
319
- cache library — `onMessage` hands every message to it, so that stays the
320
- source of truth and the stream nudges it:
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
- A dropped connection reaches you two ways as well: `error` is the state, and
337
- `onError` fires when it happens. The hook reconnects on its own, so this is
338
- for a notice rather than a retry.
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
- A stream is never stored, whatever it reads: it is an answer that keeps going.
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
- Reach for an API route when the caller cannot import your functions. If it can,
358
- the typed call is better than a url both sides have to agree on.
303
+ ### Streaming or polling
359
304
 
360
- :::note[Stored, or not]
361
- A route that reads nothing per request is answered once at build and served
362
- from the file, like a page — see [the build answers what it
363
- can](#the-build-answers-what-it-can). One that reads the request, or streams,
364
- runs per request. For data that never changes and is read by outsiders, a
365
- [query](/guides/queries/) with `cache: "public"` gets you a cacheable answer
366
- without giving up the type.
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.