@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
|
@@ -1,602 +0,0 @@
|
|
|
1
|
-
# Your own backend
|
|
2
|
-
|
|
3
|
-
> The one endpoint a backend in any language answers.
|
|
4
|
-
|
|
5
|
-
The model is a [BAP — Backend-Answered Pages](/hosts/backend-answered-pages);
|
|
6
|
-
this page is the wire.
|
|
7
|
-
|
|
8
|
-
There is no adapter to write on the JavaScript side. The renderer is what
|
|
9
|
-
Nitro builds, on every host, and it already knows how to ask a backend for
|
|
10
|
-
things: a server component calls `rpc()`, and the call leaves the process as
|
|
11
|
-
an ordinary POST. What a backend implements is that one endpoint. Laravel's is
|
|
12
|
-
[about 400 lines of PHP](/hosts/laravel); [Go's](/hosts/go) is about the same.
|
|
13
|
-
|
|
14
|
-
This page is the contract, so a Rails, Django, .NET or Elixir application can
|
|
15
|
-
answer it the same way.
|
|
16
|
-
|
|
17
|
-
## Telling the renderer where you are
|
|
18
|
-
|
|
19
|
-
Two variables, and the renderer wires itself. Both in development — `vite`
|
|
20
|
-
reads the project's `.env` — and in production, where the built server reads
|
|
21
|
-
its process environment:
|
|
22
|
-
|
|
23
|
-
```ini title=".env"
|
|
24
|
-
RSC_BACKEND=http://127.0.0.1:8080
|
|
25
|
-
RSC_HOST_CALL_SECRET=… # openssl rand -base64 32 - see Secrets under Deployment
|
|
26
|
-
```
|
|
27
|
-
|
|
28
|
-
`APP_URL` is read where `RSC_BACKEND` is absent, which is what makes a Laravel
|
|
29
|
-
app need nothing extra. Optional beside them: `RSC_HOST_CALL_PATH` (default
|
|
30
|
-
`/__rsc/host-call`) and `RSC_HOST_GLOBAL` (default `rpc`, the name server
|
|
31
|
-
components call). Both, or neither: a secret without a backend has nowhere to
|
|
32
|
-
go, and a backend without a secret is refused at the door — the renderer does
|
|
33
|
-
not gate on one of the pair alone.
|
|
34
|
-
|
|
35
|
-
Both are read when the first request arrives, not when the module loads, so
|
|
36
|
-
they work wherever the built server runs: a process with an environment, or a
|
|
37
|
-
Worker, where Nitro maps the bindings in `wrangler.json` — a `var` for the
|
|
38
|
-
address, a `secret` for the secret — onto `process.env` per request. On a
|
|
39
|
-
Worker the backend has to be reachable from Cloudflare's network, so it is an
|
|
40
|
-
`https` origin behind the secret rather than a loopback address.
|
|
41
|
-
|
|
42
|
-
Then the plugin's `hostCall` option overrides any of it for a setup that
|
|
43
|
-
would rather not use the environment:
|
|
44
|
-
|
|
45
|
-
```ts
|
|
46
|
-
rscKit({ hostCall: { endpoint: 'http://127.0.0.1:8080', secret, path: '/__rsc/host-call' } })
|
|
47
|
-
```
|
|
48
|
-
|
|
49
|
-
## The request
|
|
50
|
-
|
|
51
|
-
```http
|
|
52
|
-
POST /__rsc/host-call
|
|
53
|
-
Content-Type: application/json
|
|
54
|
-
X-Rsc-Host-Secret: a-long-random-string
|
|
55
|
-
Cookie: <the visitor's, forwarded unchanged>
|
|
56
|
-
Authorization: <likewise, if the page request had one>
|
|
57
|
-
X-Rsc-Client-Ip: <the visitor's address, as the renderer's server saw it>
|
|
58
|
-
X-Rsc-Client-User-Agent: <the visitor's User-Agent>
|
|
59
|
-
|
|
60
|
-
{ "function": "Orders.recent", "args": [5] }
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
`function` is whatever the component passed to `rpc()`; the naming scheme is
|
|
64
|
-
yours. `args` is positional, exactly as passed. `Cookie` and `Authorization`
|
|
65
|
-
are the only headers copied from the page request — everything else either
|
|
66
|
-
describes this POST or is meaningless to you — and they are what let the call
|
|
67
|
-
run *as the visitor*: your session middleware reads the cookie and finds the
|
|
68
|
-
same person the page is being rendered for. During a build there is no
|
|
69
|
-
visitor and the headers are absent.
|
|
70
|
-
|
|
71
|
-
**`X-Rsc-Client-Ip` is the visitor's address**, not the renderer's: the peer
|
|
72
|
-
the renderer's server accepted the page request from, or the first hop of
|
|
73
|
-
`X-Forwarded-For` when the renderer runs with `RSC_TRUST_FORWARDED=1` behind a
|
|
74
|
-
proxy of its own. Believe it only once the secret matches - before that,
|
|
75
|
-
anybody can send it - and then in place of the connection's address, which is
|
|
76
|
-
the renderer's and the same for every visitor. A throttle keyed on that puts
|
|
77
|
-
everyone in one bucket. Absent when there is no visitor: a build, or a page
|
|
78
|
-
stored for everyone.
|
|
79
|
-
|
|
80
|
-
**`X-Rsc-Client-User-Agent` is the visitor's browser**, for whatever you keep
|
|
81
|
-
about a request: a log, a list of signed-in devices, an audit entry. The
|
|
82
|
-
call's own `User-Agent` is the renderer's runtime, the same for everyone.
|
|
83
|
-
Believe it on the same terms as the address, and expect it absent on the same
|
|
84
|
-
occasions. Laravel puts it where `$request->userAgent()` reads; Go's is
|
|
85
|
-
`rsckit.ClientUserAgent(ctx)`.
|
|
86
|
-
|
|
87
|
-
**Arguments that do not fit the function are a failure, not a refusal.**
|
|
88
|
-
Too few, or a value your language cannot decode into the parameter's type:
|
|
89
|
-
answer 500 with `error`, as Go and Laravel do. The renderer's calls are typed
|
|
90
|
-
from your own manifest, so a mismatch is a bug in a caller - most often a
|
|
91
|
-
manifest older than the code - and a 500 is what makes it show up in your
|
|
92
|
-
logs. Extra arguments may be ignored. Values that decode but break your
|
|
93
|
-
rules - an email that is not one - are the input being invalid: a 422 with
|
|
94
|
-
`validationErrors`, below.
|
|
95
|
-
|
|
96
|
-
**Check the secret first, in constant time, and refuse before dispatch.** This
|
|
97
|
-
endpoint runs functions by name with none of your routing in front of it;
|
|
98
|
-
`hash_equals('', '')` is true in PHP and its equivalents elsewhere, so an
|
|
99
|
-
unconfigured secret has to be rejected before the comparison rather than
|
|
100
|
-
trusted to it. Better still, do not register the endpoint at all when no
|
|
101
|
-
secret is configured — absent, not open.
|
|
102
|
-
|
|
103
|
-
**Answer the previous secret too, while `RSC_HOST_CALL_SECRET_PREVIOUS` is
|
|
104
|
-
set.** That is how the secret rotates without a refused call: the backend takes
|
|
105
|
-
the new one and the old, the renderers move to the new one, then the old is
|
|
106
|
-
removed ([rotating one](/hosts/deployment/)). Compare it the same way, unset
|
|
107
|
-
matching nothing, and never send it.
|
|
108
|
-
|
|
109
|
-
## The reply
|
|
110
|
-
|
|
111
|
-
JSON, and the fields keep the outcomes apart so the renderer never reads a
|
|
112
|
-
message to tell an invalid form from a broken server:
|
|
113
|
-
|
|
114
|
-
```json
|
|
115
|
-
{ "result": [ … ], "revalidate": ["orders"] }
|
|
116
|
-
```
|
|
117
|
-
|
|
118
|
-
| you want to say | status | fields |
|
|
119
|
-
| --- | --- | --- |
|
|
120
|
-
| here is the answer | 200 | `result` |
|
|
121
|
-
| …and the action made these regions stale | 200 | `result`, `revalidate: ["orders", "page"]` |
|
|
122
|
-
| the input is invalid | 422 | `validationErrors: { "email": ["…"], "address.city": ["…"] }`, `error` |
|
|
123
|
-
| there is no session | 401 | `unauthenticated: true`, `error` |
|
|
124
|
-
| there is one, and still no | 403 | `unauthorized: true`, `error` |
|
|
125
|
-
| go somewhere else | **200** | `redirect: "/login"`, and `redirectStatus` when you mean something other than 307 |
|
|
126
|
-
| refused, with its own status | that status | `error` (shown to the visitor), `refusalStatus: 429` (any 4xx or 5xx: a 503 while a dependency is down) |
|
|
127
|
-
| refused, with something to act on | that status, 409 by default | `error` (shown to the visitor), `refusalStatus`, `refusalData: { "blockers": [ … ] }` |
|
|
128
|
-
| the function failed | 500 | `error`, and in development `debug` |
|
|
129
|
-
|
|
130
|
-
What each becomes on the other side: a `redirect` resolves the browser's call
|
|
131
|
-
with `{ redirected }` after the navigation starts, which is why every generated
|
|
132
|
-
stub is typed `Promise<T | Redirected>`. `validationErrors` reaches the form
|
|
133
|
-
that submitted, each message under its input, dot-joined for a nested field
|
|
134
|
-
and under `""` for a message about the form itself. `unauthenticated` and
|
|
135
|
-
`unauthorized` become the engine's own `ServerAuthenticationError` and
|
|
136
|
-
`ServerAuthorizationError`, so a page answers 401 or 403 the way it would
|
|
137
|
-
have if a JavaScript guard had thrown them; in an action, each is the
|
|
138
|
-
result's `serverError` with your `error` as written. A `refusalStatus` makes
|
|
139
|
-
`error` a message for the visitor, with data or without: the page answers that
|
|
140
|
-
status, an action shows it, and a 404 is the not-found page on a page and the
|
|
141
|
-
message in an action, a generated stub's included. Leave it out of
|
|
142
|
-
a failure, whose message stays in your logs. `redirect` travels the path every
|
|
143
|
-
other redirect travels — a real 3xx above a Suspense boundary, a digest below
|
|
144
|
-
one. `revalidate` names [sections](/guides/live-data) or `page`, and the answer
|
|
145
|
-
to the action carries the re-rendered region with it rather than the browser
|
|
146
|
-
being told to ask again.
|
|
147
|
-
|
|
148
|
-
Two things that are easy to get wrong:
|
|
149
|
-
|
|
150
|
-
**A redirect is a 200.** An HTTP client follows a 3xx transparently, so a real
|
|
151
|
-
one here would send the host call itself to the destination and hand whatever
|
|
152
|
-
it found back to the render as the function's result. The redirect's own
|
|
153
|
-
status goes in `redirectStatus`; without it the renderer uses 307, which keeps
|
|
154
|
-
the method. Send it whenever your framework says something else - Laravel's
|
|
155
|
-
redirects are 302, so the Laravel package always sends it.
|
|
156
|
-
|
|
157
|
-
**Refusing is not failing.** A form filled in wrongly is the ordinary case, and
|
|
158
|
-
`validationErrors` is checked before `error` — a reply carrying both is read as
|
|
159
|
-
a refusal with fields, not a failure with none. Reserve `error` alone, with a
|
|
160
|
-
500, for the thing the visitor did not cause.
|
|
161
|
-
|
|
162
|
-
**Say where it failed, in development only.** A 500 may carry `debug`: the
|
|
163
|
-
thrown type, its message, and frames newest first.
|
|
164
|
-
|
|
165
|
-
```json
|
|
166
|
-
{ "error": "boom", "debug": { "type": "RuntimeException", "message": "boom",
|
|
167
|
-
"trace": ["app/Rsc/Orders.php:42 App\\Rsc\\Orders->recent()", "…"] } }
|
|
168
|
-
```
|
|
169
|
-
|
|
170
|
-
The renderer puts it on the error the render sees, as its `cause` and under
|
|
171
|
-
its stack, so a failed `rpc()` points at the line of your backend that failed,
|
|
172
|
-
not only at the JavaScript that called it. Send it only when the app is in
|
|
173
|
-
development - Laravel's `app.debug`, Go's `CallbackHandler.Debug` - never in
|
|
174
|
-
production, where it would name your files to whoever caused the error, and
|
|
175
|
-
never with a refusal, which is an answer and not a bug.
|
|
176
|
-
|
|
177
|
-
In development, a server action that fails this way opens Vite's error overlay
|
|
178
|
-
in the browser: the `type` and `message` as its heading, the first frame not under
|
|
179
|
-
`vendor/`, `node_modules/` or `pkg/mod/` as its file, and the rest beneath. A
|
|
180
|
-
frame is easiest to read as `path:line` followed by what was running there.
|
|
181
|
-
|
|
182
|
-
## Batches
|
|
183
|
-
|
|
184
|
-
Calls issued in the same tick of a render — sibling components each awaiting
|
|
185
|
-
`rpc()` — arrive as one POST, so a page's parallel reads cost you one request
|
|
186
|
-
rather than one each:
|
|
187
|
-
|
|
188
|
-
```json
|
|
189
|
-
{ "calls": [ { "function": "Orders.recent", "args": [5] }, { "function": "Me.profile", "args": [] } ] }
|
|
190
|
-
```
|
|
191
|
-
|
|
192
|
-
Answer **as each call finishes**: `Content-Type: application/x-ndjson`, one
|
|
193
|
-
JSON line per call, carrying its `index` in the batch, the `status` it would
|
|
194
|
-
have had alone, and the reply — in whatever order the calls complete, flushed
|
|
195
|
-
as they do:
|
|
196
|
-
|
|
197
|
-
```text
|
|
198
|
-
{ "index": 1, "status": 401, "unauthenticated": true }
|
|
199
|
-
{ "index": 0, "status": 200, "result": [ … ] }
|
|
200
|
-
```
|
|
201
|
-
|
|
202
|
-
That is what keeps a page's boundaries streaming independently after their
|
|
203
|
-
reads travelled together: the renderer resolves each call the moment its
|
|
204
|
-
line lands, so a component waiting on a fast read paints while a slow
|
|
205
|
-
sibling's is still running. The Go module runs the calls concurrently and
|
|
206
|
-
writes each as it returns; Laravel runs them in order and flushes after each.
|
|
207
|
-
Set `X-Accel-Buffering: no` so a proxy in front does not hold the lines back.
|
|
208
|
-
|
|
209
|
-
A backend that would rather answer the whole batch at once may: one JSON
|
|
210
|
-
object of `replies`, one per call in order, each with its `status`:
|
|
211
|
-
|
|
212
|
-
```json
|
|
213
|
-
{ "replies": [ { "status": 200, "result": [ … ] }, { "status": 401, "unauthenticated": true } ] }
|
|
214
|
-
```
|
|
215
|
-
|
|
216
|
-
The renderer reads either. The saving of the batch is the same; with the
|
|
217
|
-
whole-batch form every call in it waits for the slowest.
|
|
218
|
-
|
|
219
|
-
Either way, run every call and answer every one — a refusal in the second is
|
|
220
|
-
that call's answer, not a reason to leave the third out. Each call's
|
|
221
|
-
`revalidate` stays with that call. Batches never mix visitors; every call in
|
|
222
|
-
one carried the same forwarded headers.
|
|
223
|
-
|
|
224
|
-
**At most 50 calls.** The renderer never sends more in one batch; a page with
|
|
225
|
-
more is split. Refusing a larger one - 413, before running anything - is a
|
|
226
|
-
safe limit, and Laravel's.
|
|
227
|
-
|
|
228
|
-
**Batches are not optional.** The renderer always sends calls from the same
|
|
229
|
-
tick together, and an answer that is neither form - whatever its status -
|
|
230
|
-
fails every call in the batch with what your backend said. None is sent
|
|
231
|
-
again one by one: they may have run, and an action run twice is worse than
|
|
232
|
-
one reported as failed. The conformance suite checks batches, so a backend
|
|
233
|
-
that passes it takes them.
|
|
234
|
-
|
|
235
|
-
## Route middleware
|
|
236
|
-
|
|
237
|
-
A `middleware.ts` beside or above a page may name guards in your vocabulary:
|
|
238
|
-
|
|
239
|
-
```ts title="app/admin/middleware.ts"
|
|
240
|
-
export const middleware = ['auth', 'can:manage-users']
|
|
241
|
-
```
|
|
242
|
-
|
|
243
|
-
Only the names, no default export: the engine's own guards are a
|
|
244
|
-
`middleware.ts` *default export*, a function it runs itself, and a file may
|
|
245
|
-
carry either or both. (`route.ts` may carry the names too, from before
|
|
246
|
-
`middleware.ts` could.)
|
|
247
|
-
|
|
248
|
-
The renderer does not know what those mean. Before anything at or below that
|
|
249
|
-
directory renders — including a page frozen at build time, before the file is
|
|
250
|
-
served — it calls the reserved function with the list:
|
|
251
|
-
|
|
252
|
-
```json
|
|
253
|
-
{
|
|
254
|
-
"function": "__rsc.middleware",
|
|
255
|
-
"args": [
|
|
256
|
-
["auth", "can:manage-users"],
|
|
257
|
-
{ "url": "https://app.example.com/users/7/edit?tab=roles", "params": { "user": "7" } }
|
|
258
|
-
]
|
|
259
|
-
}
|
|
260
|
-
```
|
|
261
|
-
|
|
262
|
-
The second argument is **the page being asked about**, because the call is
|
|
263
|
-
not it: a guard run against the POST to `/__rsc/host-call` checks a signature
|
|
264
|
-
on the endpoint's url, and finds no `{user}` to authorize against. `url` is
|
|
265
|
-
what the visitor asked for, under the public host and scheme when a proxy
|
|
266
|
-
forwarded them, or `null` during a build. `params` holds only the values the
|
|
267
|
-
route's own segments bound - never the query, so `?user=8` cannot stand in for
|
|
268
|
-
`{user}` - with a catch-all's parts joined by `/`. Run the guards as if that
|
|
269
|
-
were the request. A backend that ignores the argument still works for guards
|
|
270
|
-
that read only the session.
|
|
271
|
-
|
|
272
|
-
One request asks once. A page the renderer looks for in its store and then
|
|
273
|
-
renders is still one visit to the guards, so a throttle counts it once.
|
|
274
|
-
|
|
275
|
-
Answer `{ "result": true }` to let the render go ahead. **Anything else is a
|
|
276
|
-
refusal**: `false`, `null`, a string, an object, a 4xx, a connection error.
|
|
277
|
-
The engine reads the literal `true` and nothing else, so a guard that aborts,
|
|
278
|
-
redirects or simply throws keeps the page from rendering rather than being
|
|
279
|
-
read as silence. Refuse with the fields above — `unauthenticated` when there
|
|
280
|
-
is no session, `redirect` to send them to sign in, `refusalStatus` for a
|
|
281
|
-
throttle's 429 or a 503 during maintenance — and the page answers accordingly,
|
|
282
|
-
with the status and the `error` message. Only a deliberate refusal carries a
|
|
283
|
-
`refusalStatus`: a crash leaves it out and is answered 500.
|
|
284
|
-
|
|
285
|
-
Run them in order, outermost first, and stop at the first refusal: an outer
|
|
286
|
-
guard saying no means the inner one should never have been asked. An empty
|
|
287
|
-
list passes - `{ "result": true }`: it is what the renderer's health check
|
|
288
|
-
asks, to prove the backend answers without running anything.
|
|
289
|
-
|
|
290
|
-
## Saying data changed
|
|
291
|
-
|
|
292
|
-
A section names the data it shows:
|
|
293
|
-
|
|
294
|
-
```tsx
|
|
295
|
-
export default section('repos', Repos, { refreshOn: ({ params }) => [`team:${params.team}:repos`] })
|
|
296
|
-
```
|
|
297
|
-
|
|
298
|
-
and your backend says when that changed, from a webhook or a job as much as
|
|
299
|
-
an action. Every open tab showing it refreshes, with nothing polling. Two
|
|
300
|
-
things to provide:
|
|
301
|
-
|
|
302
|
-
**A way to say a name changed**, callable from anywhere in your backend. It
|
|
303
|
-
moves the name's version to the larger of one past where it was and the
|
|
304
|
-
current time in milliseconds - so a version never comes round again, and an
|
|
305
|
-
old name can be deleted at any time - kept somewhere every instance of your
|
|
306
|
-
backend shares - a cache, a table - since a webhook lands on whichever
|
|
307
|
-
instance the balancer picked. A name never changed is at 0.
|
|
308
|
-
|
|
309
|
-
**The reserved function `__rsc.changed`**, asked with what a watcher holds and
|
|
310
|
-
how long it would wait:
|
|
311
|
-
|
|
312
|
-
```json
|
|
313
|
-
{ "function": "__rsc.changed", "args": [{ "since": { "team:1:repos": 3, "team:1:members": 0 }, "wait": 5000 }] }
|
|
314
|
-
```
|
|
315
|
-
|
|
316
|
-
Answer `{ "result": { "versions": { ... } } }` with every name in `since`
|
|
317
|
-
whose version differs now, and nothing else - an empty object when none
|
|
318
|
-
does. If you can, hold the call up to `wait` milliseconds for one to move
|
|
319
|
-
and answer the moment it does: that is what makes the refresh immediate.
|
|
320
|
-
The renderer asks with 5 seconds; hold no longer than `wait`, and never past
|
|
321
|
-
30 seconds, where the renderer gives up on any call. A name you have never
|
|
322
|
-
seen is at 0, compared like any other: held while the asker also holds 0,
|
|
323
|
-
answered the moment it is changed. If
|
|
324
|
-
you cannot - PHP-FPM, a worker that must answer and go - answer at once; the
|
|
325
|
-
renderer asks again on its interval, a couple of seconds apart. Both are
|
|
326
|
-
conformant, and the page is the same.
|
|
327
|
-
|
|
328
|
-
The renderer asks once per process for every open tab it holds, never once
|
|
329
|
-
per tab, so the cost to you is one small call per interval per renderer. A
|
|
330
|
-
tab may only watch names its page was rendered with: the renderer signs them,
|
|
331
|
-
so no authorization is needed here beyond the secret.
|
|
332
|
-
|
|
333
|
-
**A purge, when a CDN keeps pages.** A `revalidate` page goes out with the
|
|
334
|
-
`Cache-Tag` of every `cache()` answer it used, and the names you change are
|
|
335
|
-
those tags. A change you make never passes through the renderer, so when
|
|
336
|
-
`CDN_PURGE_URL` is set, your backend tells the CDN itself: after the
|
|
337
|
-
version has moved - after the transaction commits, and after the call has
|
|
338
|
-
answered if you can - post
|
|
339
|
-
|
|
340
|
-
```json
|
|
341
|
-
{ "tags": ["team:1:repos"] }
|
|
342
|
-
```
|
|
343
|
-
|
|
344
|
-
to that url, `Content-Type: application/json`, at most 100 tags a call, each
|
|
345
|
-
at most 1024 bytes. It needs no token. Only a `202` means it was accepted. A
|
|
346
|
-
429 may be tried once more after its `Retry-After`; anything else is logged
|
|
347
|
-
and never fails the change, since the CDN drops its copy at its `max-age` in `CDN-Cache-Control` anyway.
|
|
348
|
-
Without the variable, do nothing.
|
|
349
|
-
|
|
350
|
-
## Server actions
|
|
351
|
-
|
|
352
|
-
A `"use server"` function the browser can call is a name the renderer forwards
|
|
353
|
-
to you. Write `rsc-host.json` at the project root, beside `vite.config.ts`:
|
|
354
|
-
`actions` maps a module - one per class, its path under `server-actions/` -
|
|
355
|
-
to the names it exports, each to whatever your side dispatches on, and
|
|
356
|
-
`functions` lists every name `rpc()` may call. A class's single action, an
|
|
357
|
-
`__invoke`, is the module's `default`.
|
|
358
|
-
|
|
359
|
-
```json title="rsc-host.json"
|
|
360
|
-
{
|
|
361
|
-
"actions": { "orders": { "cancel": "Orders.cancel" }, "profile": { "update": "Profile.update" } },
|
|
362
|
-
"functions": ["Orders.cancel", "Orders.recent", "Profile.update"]
|
|
363
|
-
}
|
|
364
|
-
```
|
|
365
|
-
|
|
366
|
-
A function can also carry its types, so the app's `rpc()` and the stubs are
|
|
367
|
-
typed: `types` maps a name to its positional `params`, how many trailing ones
|
|
368
|
-
are `optional`, a variadic `rest`, and its `result`, each a JSON Schema;
|
|
369
|
-
`defs` holds the named object types they refer to by `"$ref": "#/defs/Name"`.
|
|
370
|
-
|
|
371
|
-
```json
|
|
372
|
-
"types": { "Orders.recent": { "params": [{ "type": "integer" }], "result": { "type": "array", "items": { "$ref": "#/defs/Order" } } } },
|
|
373
|
-
"defs": { "Order": { "type": "object", "properties": { "id": { "type": "integer" } }, "required": ["id"] } }
|
|
374
|
-
```
|
|
375
|
-
|
|
376
|
-
A function taking one object with `properties` can be [a form with no
|
|
377
|
-
schema](/guides/blueprints#a-laravel-or-go-backend), read from these same
|
|
378
|
-
types: `required`, `format` (`email`, `date`), `minLength`, `maxLength`,
|
|
379
|
-
`minimum`, `maximum` and `enum` become its fields and browser checks. The
|
|
380
|
-
properties are drawn in the order written. If your language's JSON sorts a map,
|
|
381
|
-
say the order with `"x-order": ["name", "email"]` beside them.
|
|
382
|
-
|
|
383
|
-
The build writes a module per class under `server-actions/` in the source
|
|
384
|
-
directory, `server-actions/orders.ts` exporting `cancel`, so a client
|
|
385
|
-
component imports `* as orders` from it and calls `orders.cancel`, and types `rpc()`'s first argument with the functions. Have Vite
|
|
386
|
-
run the command that writes it, `rscKit({ hostManifest: { command } })`, so
|
|
387
|
-
it cannot go stale: a stale one names a method since renamed, and nothing
|
|
388
|
-
fails until the browser calls it. Laravel's `rsc:host-manifest` is this
|
|
389
|
-
step; a Go registry's `WriteManifest` writes the same file.
|
|
390
|
-
|
|
391
|
-
### Checking a form before it is submitted
|
|
392
|
-
|
|
393
|
-
`<Form>` asks whether a field would pass before the form is sent, and an
|
|
394
|
-
action whose validation runs before its body can answer without running.
|
|
395
|
-
List those functions in `rsc-host.json` - a function not listed is never sent
|
|
396
|
-
a check, because a backend that has never heard of one would run the call:
|
|
397
|
-
|
|
398
|
-
```json
|
|
399
|
-
"validates": ["Profile.update"]
|
|
400
|
-
```
|
|
401
|
-
|
|
402
|
-
The check is an ordinary call with `validate` beside its `args`, never in a
|
|
403
|
-
batch: the fields to check, `*` matching one segment of a dotted name, and an
|
|
404
|
-
empty list meaning every field.
|
|
405
|
-
|
|
406
|
-
```json
|
|
407
|
-
{ "function": "Profile.update", "args": [{ "email": "taken@example.com" }], "validate": ["email"] }
|
|
408
|
-
```
|
|
409
|
-
|
|
410
|
-
Run what would refuse the call - authentication, authorization, validation,
|
|
411
|
-
limited to those fields - and stop there. Fields that pass answer
|
|
412
|
-
`{ "result": null }`; fields that do not answer the [422 with
|
|
413
|
-
`validationErrors`](#the-reply) a submit would. Nothing may be written. Any
|
|
414
|
-
other refusal is no verdict on the fields, and the form shows nothing for it.
|
|
415
|
-
|
|
416
|
-
Laravel does this for a function whose first parameter is a form request,
|
|
417
|
-
with Precognition's own rule filtering.
|
|
418
|
-
|
|
419
|
-
### A message for the next page
|
|
420
|
-
|
|
421
|
-
`useFlash()` on the page shows what an action leaves for the next one. Your
|
|
422
|
-
side leaves one by setting a cookie on the reply, the way a login does: named
|
|
423
|
-
`rsc-flash`, its value a URI-encoded JSON list of `{ "message", "kind" }` -
|
|
424
|
-
`kind` one of `success`, `error`, `info`, `warning` - with `Path=/`, a short
|
|
425
|
-
`Max-Age` and `SameSite=Lax`.
|
|
426
|
-
|
|
427
|
-
```
|
|
428
|
-
Set-Cookie: rsc-flash=%5B%7B%22message%22%3A%22Saved.%22%2C%22kind%22%3A%22success%22%7D%5D; Path=/; Max-Age=60; SameSite=Lax
|
|
429
|
-
```
|
|
430
|
-
|
|
431
|
-
The browser reads it, so it is never `HttpOnly` and never encrypted - Laravel's
|
|
432
|
-
`Rsc::flash()` exempts it from `EncryptCookies`. A message the visitor's cookie
|
|
433
|
-
still carries goes first: append to the list, do not replace it.
|
|
434
|
-
|
|
435
|
-
## Urls you own
|
|
436
|
-
|
|
437
|
-
The renderer forwards any url the route tree does not own — `/login`, a
|
|
438
|
-
webhook, an uploaded file — to `RSC_BACKEND`, with `X-Forwarded-Host` and
|
|
439
|
-
`X-Forwarded-Proto` set and the header `x-rsc-renderer-fallback: 1`. Trust
|
|
440
|
-
the renderer as a proxy so your absolute urls come out against the public
|
|
441
|
-
origin.
|
|
442
|
-
|
|
443
|
-
If your application also proxies to the renderer — sitting in front of it,
|
|
444
|
-
the way Laravel does with `RSC_RENDERER_URL` — two things keep a url neither
|
|
445
|
-
side owns from bouncing between you forever:
|
|
446
|
-
|
|
447
|
-
- Set `x-rsc-proxied-by-backend: 1` on what you forward. The renderer answers
|
|
448
|
-
404 itself instead of handing it back.
|
|
449
|
-
- When a request arrives carrying `x-rsc-renderer-fallback`, answer 404 for
|
|
450
|
-
anything you do not route. It has already been through the renderer's table.
|
|
451
|
-
|
|
452
|
-
Whichever process faces the internet, the host-call endpoint must not:
|
|
453
|
-
restrict it at the web server, bind the listener to loopback, or serve it on
|
|
454
|
-
a unix socket. The secret is the layer the protocol guarantees; the network
|
|
455
|
-
is the one it cannot.
|
|
456
|
-
|
|
457
|
-
## What the renderer expects of you
|
|
458
|
-
|
|
459
|
-
- **Answer within 30 seconds.** A render blocked on a host that never answers
|
|
460
|
-
is a hung request, and the renderer gives up at 30s (`timeoutMs` on
|
|
461
|
-
`httpHostCalls`, for a host that embeds the engine itself).
|
|
462
|
-
- **Starting after the renderer is fine.** A refused connection is tried again
|
|
463
|
-
for up to 2 seconds, because a refused connection means the call never
|
|
464
|
-
reached you, so nothing runs twice. This covers a backend started beside
|
|
465
|
-
the renderer in one container, still coming up when the first page renders.
|
|
466
|
-
Nothing else is retried: a timeout or a reset may have run the call
|
|
467
|
-
(`refusedRetryMs` on `httpHostCalls`; 0 turns it off).
|
|
468
|
-
- **One process is not enough if you proxy.** A server that proxies a page
|
|
469
|
-
to the renderer holds a worker for the whole render, and the render calls
|
|
470
|
-
back to that same server for its data. With one worker, nobody is left to
|
|
471
|
-
answer. Laravel refuses `php artisan serve` for exactly this.
|
|
472
|
-
- **A panic is one failed call.** Recover it into a 500 with `error`; the
|
|
473
|
-
other renders in flight should survive it.
|
|
474
|
-
- **Serialise what you return as JSON.** Where your manifest gives a
|
|
475
|
-
function a type, what you send must fit it: an empty list is `[]`, never
|
|
476
|
-
`null`; a time is an ISO 8601 string.
|
|
477
|
-
|
|
478
|
-
## Before you ship: the conformance suite
|
|
479
|
-
|
|
480
|
-
An adapter is ready when it passes rsc-kit's conformance suite - all of it.
|
|
481
|
-
Every bug found at this boundary so far was a difference between how a
|
|
482
|
-
backend and the engine read the wire - a nil list sent as `null` where the
|
|
483
|
-
type said a list, a timestamp typed `unknown`, a refusal that did not arrive
|
|
484
|
-
as the error a page checks for - and each is a case in the suite. The Laravel
|
|
485
|
-
and Go adapters run it in CI on every change, against rsc-kit's main.
|
|
486
|
-
|
|
487
|
-
Register these functions in your backend, written with your adapter's
|
|
488
|
-
ordinary API, the way an app would write them:
|
|
489
|
-
|
|
490
|
-
| Function | Must |
|
|
491
|
-
| --- | --- |
|
|
492
|
-
| `Conformance.echo` | return its one argument unchanged |
|
|
493
|
-
| `Conformance.emptyList` | return an empty list, the way your language says "no rows" |
|
|
494
|
-
| `Conformance.time` | return 2026-01-02T03:04:05Z as your language's time value |
|
|
495
|
-
| `Conformance.noTime` | return an absent time |
|
|
496
|
-
| `Conformance.unauthenticated` | refuse as not signed in |
|
|
497
|
-
| `Conformance.unauthorized` | refuse as not allowed |
|
|
498
|
-
| `Conformance.notFound` | refuse as not found (404) |
|
|
499
|
-
| `Conformance.refuse` | refuse with 429 and "Slow down." |
|
|
500
|
-
| `Conformance.invalid` | refuse the input, on the field `name` |
|
|
501
|
-
| `Conformance.invalidNested` | refuse the input, on `address.city` and on the form itself, under `""` |
|
|
502
|
-
| `Conformance.redirect` | send the visitor to `/login` |
|
|
503
|
-
| `Conformance.revalidate` | mark `orders` stale and return `"ok"` |
|
|
504
|
-
| `Conformance.fail` | fail with an ordinary error |
|
|
505
|
-
| `Conformance.double` | take one integer and return it doubled; anything else fails it |
|
|
506
|
-
| `Conformance.authorization` | return the forwarded `Authorization` header |
|
|
507
|
-
| `Conformance.cookie` | return the forwarded `Cookie` header |
|
|
508
|
-
| `Conformance.login` | set a cookie named `conformance_login` on the response, and return `"ok"` |
|
|
509
|
-
| `Conformance.flash` | leave the one-time message `"Saved."` (`"success"`) for the next page, after any the visitor's `rsc-flash` cookie still carries, and return `"ok"` |
|
|
510
|
-
| `Conformance.change` | say `conformance:changed` changed, as your webhook would, and return `"ok"` |
|
|
511
|
-
|
|
512
|
-
Then `Conformance.ip`, returning the visitor's address as your request
|
|
513
|
-
reports it. And four guards: `conformance-allow`, which passes,
|
|
514
|
-
`conformance-deny`, which refuses, `conformance-redirect`, which sends the
|
|
515
|
-
visitor to `/conformance-login`, and `conformance-page`, which passes only when
|
|
516
|
-
the page it is asked about is `/conformance/orders/7` with `{order}` bound to
|
|
517
|
-
`"7"`. Then serve them and run:
|
|
518
|
-
|
|
519
|
-
Start it with `CDN_PURGE_URL=http://127.0.0.1:8790/`, where the suite
|
|
520
|
-
listens for its purges, and `RSC_HOST_CALL_SECRET_PREVIOUS=old`, then:
|
|
521
|
-
|
|
522
|
-
```sh
|
|
523
|
-
npx -y -p @rsc-kit/core rsc-kit-conformance \
|
|
524
|
-
--endpoint http://127.0.0.1:8123/__rsc/host-call --secret test \
|
|
525
|
-
--previous-secret old --manifest rsc-host.json --purge-port 8790
|
|
526
|
-
```
|
|
527
|
-
|
|
528
|
-
It checks the whole contract on this page:
|
|
529
|
-
|
|
530
|
-
- **values** - every JSON shape unchanged, `[]` for no rows, ISO 8601 times,
|
|
531
|
-
arguments decoded to their types, and every value against the type your
|
|
532
|
-
manifest declares;
|
|
533
|
-
- **outcomes** - each refusal as the engine's own error, field errors with
|
|
534
|
-
dotted paths and the form's under `""`, redirects, revalidation staying
|
|
535
|
-
with its call, a crash staying a failure, and arguments that do not fit
|
|
536
|
-
failing rather than refusing;
|
|
537
|
-
- **forms** - a function taking one object, its fields in order with the hints
|
|
538
|
-
a browser can check, and a refusal landing on its field;
|
|
539
|
-
- **the request** - `Cookie` and `Authorization` forwarded, the visitor's
|
|
540
|
-
address in place of the renderer's, a cookie set by a
|
|
541
|
-
call reaching the page, no secret or the wrong one refused with 403, the
|
|
542
|
-
previous one answered while `RSC_HOST_CALL_SECRET_PREVIOUS` names it (so a
|
|
543
|
-
rotation fails no call; without `--previous-secret` this fails), an
|
|
544
|
-
unknown function 404, a malformed body 400;
|
|
545
|
-
- **batches** - every call answered, a refusal only its own, and more than 50
|
|
546
|
-
refused with 413 before any runs;
|
|
547
|
-
- **guards** - in the order named, stopping at the first that does not pass,
|
|
548
|
-
a redirect from one, a name nobody registered refusing, and a guard reading
|
|
549
|
-
the page it is asked about rather than the call;
|
|
550
|
-
- **live data** - a name never changed at 0, a change moving it to a time in
|
|
551
|
-
milliseconds that never comes round again, only the names that differ
|
|
552
|
-
answered, and a held call bounded by `wait`;
|
|
553
|
-
- **purges** - a change posted to `CDN_PURGE_URL` as its tag, as JSON,
|
|
554
|
-
only once the version has moved. Without `--purge-port` these fail: every
|
|
555
|
-
adapter purges.
|
|
556
|
-
- **debug** - with `--debug`, for an adapter started with its debug on, a
|
|
557
|
-
failure carrying `debug` with a type, a message and at least one frame.
|
|
558
|
-
Without the flag it is not checked: a backend sends a trace only in
|
|
559
|
-
development.
|
|
560
|
-
|
|
561
|
-
It exits non-zero on any failure, so it can gate your CI. The Go adapter's
|
|
562
|
-
`cmd/conformance` and Laravel's `tests/Conformance` are working fixtures to
|
|
563
|
-
copy.
|
|
564
|
-
|
|
565
|
-
## The imports for adapter authors
|
|
566
|
-
|
|
567
|
-
An app never imports these. An adapter written in JavaScript, or a host that
|
|
568
|
-
embeds the engine, does:
|
|
569
|
-
|
|
570
|
-
- `@rsc-kit/core/adapter/host-calls`: `httpHostCalls`, the renderer's side
|
|
571
|
-
of this protocol, and the `HostCallReply` and batch types it reads.
|
|
572
|
-
- `@rsc-kit/core/adapter/conformance`: `runConformance` and `report`, the
|
|
573
|
-
suite above as a function, for a test runner that drives it itself.
|
|
574
|
-
- `@rsc-kit/core/adapter/manifest`: `RouteManifest`, the type of the
|
|
575
|
-
`routes.json` the build writes, for a backend that registers the app's
|
|
576
|
-
urls as routes of its own.
|
|
577
|
-
|
|
578
|
-
## Proposing an adapter
|
|
579
|
-
|
|
580
|
-
rsc-kit standardises on adapters: a backend in a language rsc-kit has none
|
|
581
|
-
for gets one, rather than a second way to reach it. To propose one, open a
|
|
582
|
-
discussion or a pull request on [rsc-kit/rsc-kit](https://github.com/rsc-kit/rsc-kit).
|
|
583
|
-
To be adopted, an adapter:
|
|
584
|
-
|
|
585
|
-
- passes the whole conformance suite, in its own CI, against rsc-kit's main
|
|
586
|
-
on every change - not a subset, and not with cases skipped;
|
|
587
|
-
- registers the fixtures with its ordinary API, the way an app would;
|
|
588
|
-
- covers everything on this page, live data included - `__rsc.changed`, a
|
|
589
|
-
way to say a name changed, a store every instance shares, and the purge.
|
|
590
|
-
|
|
591
|
-
## Testing it without the renderer
|
|
592
|
-
|
|
593
|
-
The contract is plain HTTP, so a backend's own test suite can cover it
|
|
594
|
-
without a JavaScript process: POST the request shape, assert the reply
|
|
595
|
-
shape. Laravel's Pest suite does this; the Go adapter's `go test` does the
|
|
596
|
-
same. The end-to-end proof — a real page rendered with data from your
|
|
597
|
-
process — lives with the engine, which is where a rendering regression can be
|
|
598
|
-
caught.
|
|
599
|
-
|
|
600
|
-
---
|
|
601
|
-
|
|
602
|
-
Reference implementations: [Laravel](/hosts/laravel) and [Go](/hosts/go).
|