@rsc-kit/mcp 0.30.46 → 0.30.48
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 +18 -5
- package/dist/recipes.js.map +1 -1
- package/guides/caching.md +2 -1
- package/guides/coming-from-next.md +4 -3
- package/guides/deployment.md +3 -3
- package/guides/edge-caching.md +51 -6
- package/guides/go.md +16 -0
- package/guides/index.json +8 -3
- package/guides/installation.md +0 -1
- package/guides/isr.md +174 -0
- package/guides/laravel.md +16 -0
- package/guides/public-forms.md +167 -5
- package/guides/your-own-backend.md +10 -1
- package/package.json +2 -2
package/guides/caching.md
CHANGED
|
@@ -86,7 +86,8 @@ export async function savePage(values: PageValues) {
|
|
|
86
86
|
```
|
|
87
87
|
|
|
88
88
|
`changed()` is the same call that refreshes open tabs for
|
|
89
|
-
[live data](/guides/live-data)
|
|
89
|
+
[live data](/guides/live-data), and a page kept with [ISR](/guides/isr) that read
|
|
90
|
+
this answer renders again too. Nothing is purged. Each kept answer's key
|
|
90
91
|
includes its tags' versions, so a change makes a new key, and the old entry is
|
|
91
92
|
never read again. `tags` can also be a function of the arguments, for a tag per
|
|
92
93
|
record:
|
|
@@ -90,9 +90,10 @@ which, and why:
|
|
|
90
90
|
```
|
|
91
91
|
|
|
92
92
|
`await connection()` is the one explicit mark, for a page that must render per
|
|
93
|
-
visitor and does not happen to read anything.
|
|
94
|
-
|
|
95
|
-
|
|
93
|
+
visitor and does not happen to read anything. A frozen page changes when you
|
|
94
|
+
build, and a dynamic one on every request. [ISR](/guides/isr) is the middle, and
|
|
95
|
+
it works as you expect: `export const revalidate = 60`, with `changed(tag)` in
|
|
96
|
+
the place of `revalidateTag`.
|
|
96
97
|
|
|
97
98
|
### Middleware is per directory, not one file at the edge
|
|
98
99
|
|
package/guides/deployment.md
CHANGED
|
@@ -303,8 +303,8 @@ rsc-kit implements the [edge contract](/reference/edge-contract): one marker
|
|
|
303
303
|
and two endpoints that an edge uses the same way for every framework that
|
|
304
304
|
implements it. next-bun-compile implements it too. The edge serves a static
|
|
305
305
|
page whole and a PPR page's shell straight from its cache, and has the origin
|
|
306
|
-
render only a shell's holes, onto the same response. [
|
|
307
|
-
|
|
306
|
+
render only a shell's holes, onto the same response. [Edge
|
|
307
|
+
caching](/guides/edge-caching) covers what that buys. What rsc-kit puts in it:
|
|
308
308
|
|
|
309
309
|
- **`.output/edge.json`** is written by every server build, with empty lists
|
|
310
310
|
when there is nothing to list. An export writes none.
|
|
@@ -437,7 +437,7 @@ A route that declares middleware is never cached publicly: it is sent as
|
|
|
437
437
|
`private, no-store`, because middleware runs per visitor. If something in front
|
|
438
438
|
of your app also owns the response — an auth proxy re-issuing a session on
|
|
439
439
|
pass-through — give the paths it covers a `middleware.ts` so this host knows
|
|
440
|
-
they are covered. See [
|
|
440
|
+
they are covered. See [edge caching](/guides/edge-caching).
|
|
441
441
|
|
|
442
442
|
## Rebuild on deploy
|
|
443
443
|
|
package/guides/edge-caching.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Edge caching
|
|
2
2
|
|
|
3
|
-
>
|
|
3
|
+
> Serving prerendered pages, PPR shells and kept pages from a CDN, the headers they go out with, and purging by tag.
|
|
4
4
|
|
|
5
5
|
A prerendered page is a file. So is a PPR shell. Both can sit on a CDN and be
|
|
6
6
|
served without touching your origin.
|
|
@@ -61,7 +61,7 @@ new deploy does not leave old shells being served against a new payload.
|
|
|
61
61
|
|
|
62
62
|
A page whose data changes after the build is rendered for each request, and
|
|
63
63
|
sent `private`. When the same answer may go to everyone for a while, say how
|
|
64
|
-
long:
|
|
64
|
+
long. [ISR](/guides/isr) is the how-to; this is what goes over the wire:
|
|
65
65
|
|
|
66
66
|
```tsx
|
|
67
67
|
// app/posts/[slug]/page.tsx
|
|
@@ -91,6 +91,51 @@ pages out. A layout can export it too: the smallest value on the way down to
|
|
|
91
91
|
the page wins, and `0` anywhere means the page is rendered per request as
|
|
92
92
|
before. It must be a number literal the build can read.
|
|
93
93
|
|
|
94
|
+
To see whether the CDN answered, read `Cache-Status` (RFC 9211). A CDN that
|
|
95
|
+
sends it adds an entry under its own name, with `hit` when its copy answered.
|
|
96
|
+
`X-RSC-Kit` and `ETag` can't tell you: they were stored with the copy, so a hit
|
|
97
|
+
repeats them.
|
|
98
|
+
|
|
99
|
+
Homeport's edge adds this entry from its next engine release, so it isn't there
|
|
100
|
+
yet. See [what it will say](#cache-status-on-homeport).
|
|
101
|
+
|
|
102
|
+
### `Cache-Status` on Homeport
|
|
103
|
+
|
|
104
|
+
This arrives with Homeport's next engine release. Until then a response has no
|
|
105
|
+
`homeport` entry.
|
|
106
|
+
|
|
107
|
+
The entry is named `homeport` and comes last, after any `Cache-Status` the app
|
|
108
|
+
sends. It is a separate header line, so `curl -I` can show two `Cache-Status`
|
|
109
|
+
lines, while `headers.get()` in `fetch` joins them with a comma.
|
|
110
|
+
|
|
111
|
+
| Answer | Value |
|
|
112
|
+
| --- | --- |
|
|
113
|
+
| Fresh copy | `homeport; hit; ttl=42` |
|
|
114
|
+
| Stale copy, refreshing in the background | `homeport; hit; ttl=-5; detail=stale` |
|
|
115
|
+
| Stale copy, app unreachable | `homeport; hit; detail=stale-if-error` |
|
|
116
|
+
| Stale copy, app answered 5xx | `homeport; fwd=stale; fwd-status=503; detail=stale-if-error` |
|
|
117
|
+
| From the app, being kept | `homeport; fwd=uri-miss; stored` |
|
|
118
|
+
| From the app, not kept | `homeport; fwd=uri-miss; detail=no-cdn-cache-control` |
|
|
119
|
+
| Cache switched off | `homeport; fwd=bypass; detail=off` |
|
|
120
|
+
| Preview | `homeport; fwd=bypass; detail=preview` |
|
|
121
|
+
| Not a GET or HEAD | `homeport; fwd=method; detail=request` |
|
|
122
|
+
| `Authorization`, `Range` or a request body | `homeport; fwd=bypass; detail=request` |
|
|
123
|
+
|
|
124
|
+
On the two "from the app" rows, `fwd` can also be `stale` or `vary-miss`:
|
|
125
|
+
`uri-miss` is only the plain case. A page that was not kept can also give
|
|
126
|
+
`no-store`, `private`, `no-cache`, `set-cookie`, `status` or `too-large` as its
|
|
127
|
+
`detail`.
|
|
128
|
+
|
|
129
|
+
- **Look for `hit` in the `homeport` entry**, not anywhere in the header: the
|
|
130
|
+
app's own entry can say `hit` too. The stale copy after a 5xx came from the
|
|
131
|
+
edge and does not say `hit`.
|
|
132
|
+
- **`stored` is not proof of a kept copy.** It is sent before the body, so a
|
|
133
|
+
body that fails or is too large can say `stored` and keep nothing. The next
|
|
134
|
+
request's `hit` is the proof.
|
|
135
|
+
- **A static page, a shell or an asset has no entry.** Those hits say
|
|
136
|
+
`X-Homeport-Edge: page`, `shell` or `asset`. Only `X-Homeport-Edge: cached`
|
|
137
|
+
is gone.
|
|
138
|
+
|
|
94
139
|
### Kept by the engine, too
|
|
95
140
|
|
|
96
141
|
The host keeps a public `revalidate` page itself, so it is served without a
|
|
@@ -162,13 +207,13 @@ installPurge(async ({ tags }) => {
|
|
|
162
207
|
```
|
|
163
208
|
|
|
164
209
|
Every CDN's purge API is its own, so this is one hook rather than a list of
|
|
165
|
-
vendors. A purge that fails is logged
|
|
166
|
-
|
|
210
|
+
vendors. A purge that fails is logged and never fails the change: the CDN
|
|
211
|
+
still drops its copy at its `max-age` in `CDN-Cache-Control`. `purge({ tags, paths, all })` sends
|
|
167
212
|
one yourself, from a deploy step or a webhook.
|
|
168
213
|
|
|
169
214
|
On Homeport there is nothing to install. With `HOMEPORT_PURGE_URL` set, the
|
|
170
215
|
hook is `homeportPurge()` unless you installed another. With a Laravel or Go
|
|
171
|
-
backend, the change is made there - `Rsc::changed()`, `
|
|
216
|
+
backend, the change is made there - `Rsc::changed()`, `reg.Changed()` - and
|
|
172
217
|
never passes through the engine, so the backend posts the purge itself; both
|
|
173
218
|
do when `HOMEPORT_PURGE_URL` is set. A tag with a comma or a space cannot go in the header, and
|
|
174
219
|
is left out with a warning.
|
package/guides/go.md
CHANGED
|
@@ -389,6 +389,22 @@ it signs the names a page hands the tab, and is not the host-call secret.
|
|
|
389
389
|
Generating and rotating both: [Secrets](/hosts/deployment#secrets). [Sections](/guides/live-data)
|
|
390
390
|
has the whole picture.
|
|
391
391
|
|
|
392
|
+
## Pages kept between requests
|
|
393
|
+
|
|
394
|
+
`export const revalidate = 60` keeps a page and refreshes it behind the visitor,
|
|
395
|
+
and `reg.Changed(ctx, "posts")` renders it again at once on every instance, when
|
|
396
|
+
it read that tag through `cache()`. A kept page calls Go as nobody: no `Cookie`,
|
|
397
|
+
no `Authorization`. With `HOMEPORT_PURGE_URL` set, `Changed` purges the CDN as
|
|
398
|
+
well; `reg.SetPurger` replaces it. Call it after your transaction commits.
|
|
399
|
+
[ISR](/guides/isr) has the whole of it.
|
|
400
|
+
|
|
401
|
+
## Public forms
|
|
402
|
+
|
|
403
|
+
A contact form or a sign-up is `defineForm({ action: 'Contact.send' })` in
|
|
404
|
+
TypeScript: the fields come from the struct the function takes, with no schema
|
|
405
|
+
to write. `rsckit.InvalidField` shows under that field.
|
|
406
|
+
[Public forms](/guides/public-forms#go) has the code.
|
|
407
|
+
|
|
392
408
|
## Production
|
|
393
409
|
|
|
394
410
|
`bun run build` and `bun run start` with the same two variables in the
|
package/guides/index.json
CHANGED
|
@@ -71,8 +71,8 @@
|
|
|
71
71
|
},
|
|
72
72
|
{
|
|
73
73
|
"slug": "edge-caching",
|
|
74
|
-
"title": "
|
|
75
|
-
"description": "
|
|
74
|
+
"title": "Edge caching",
|
|
75
|
+
"description": "Serving prerendered pages, PPR shells and kept pages from a CDN, the headers they go out with, and purging by tag."
|
|
76
76
|
},
|
|
77
77
|
{
|
|
78
78
|
"slug": "emails",
|
|
@@ -134,6 +134,11 @@
|
|
|
134
134
|
"title": "Introduction",
|
|
135
135
|
"description": "React Server Components as a Vite plugin, deployed wherever you like."
|
|
136
136
|
},
|
|
137
|
+
{
|
|
138
|
+
"slug": "isr",
|
|
139
|
+
"title": "ISR",
|
|
140
|
+
"description": "A page rendered once and served to everyone, refreshed on a timer or the moment its data changes. On one server, several pods or a Worker, with Laravel, Go or no backend."
|
|
141
|
+
},
|
|
137
142
|
{
|
|
138
143
|
"slug": "laravel",
|
|
139
144
|
"title": "Laravel",
|
|
@@ -187,7 +192,7 @@
|
|
|
187
192
|
{
|
|
188
193
|
"slug": "public-forms",
|
|
189
194
|
"title": "Public forms",
|
|
190
|
-
"description": "A contact form, a sign-up, a survey - drawn from a Standard Schema, checked in the browser and on the server, sent to any backend."
|
|
195
|
+
"description": "A contact form, a sign-up, a survey - drawn from a Standard Schema or a backend function, checked in the browser and on the server, sent to any backend."
|
|
191
196
|
},
|
|
192
197
|
{
|
|
193
198
|
"slug": "pwa",
|
package/guides/installation.md
CHANGED
|
@@ -94,7 +94,6 @@ export default defineConfig({
|
|
|
94
94
|
nitro({ preset: "bun", serveStatic: "inline" }),
|
|
95
95
|
rscKit({
|
|
96
96
|
sourceDir: "src",
|
|
97
|
-
outDir: "build",
|
|
98
97
|
offline: true,
|
|
99
98
|
openapi: { info: { title: 'Example API', version: '1.0.0' } },
|
|
100
99
|
// On every page, and still served from an edge: see the edge contract.
|
package/guides/isr.md
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
# ISR
|
|
2
|
+
|
|
3
|
+
> A page rendered once and served to everyone, refreshed on a timer or the moment its data changes. On one server, several pods or a Worker, with Laravel, Go or no backend.
|
|
4
|
+
|
|
5
|
+
ISR is one line. A page that exports `revalidate` is rendered once and served
|
|
6
|
+
to everyone from a kept copy, then replaced in the background. It needs no CDN:
|
|
7
|
+
the host keeps the copy itself, on every platform, and with a Laravel or Go
|
|
8
|
+
backend the page is the same.
|
|
9
|
+
|
|
10
|
+
```tsx title="src/app/posts/[slug]/page.tsx"
|
|
11
|
+
export const revalidate = 60
|
|
12
|
+
|
|
13
|
+
export default async function Post({ params }: { params: Promise<{ slug: string }> }) {
|
|
14
|
+
const { slug } = await params
|
|
15
|
+
const post = await findPost(slug)
|
|
16
|
+
|
|
17
|
+
return <article>{post.title}</article>
|
|
18
|
+
}
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
The value is a number literal in seconds, since the build reads it. A layout can
|
|
22
|
+
export it too: the smallest on the way down to the page wins, and `0` anywhere
|
|
23
|
+
turns it off. With Laravel the page lives under `resources/js/app`.
|
|
24
|
+
|
|
25
|
+
| Request | What happens |
|
|
26
|
+
| --- | --- |
|
|
27
|
+
| The first | The page renders, and the copy is kept. A burst of visitors renders it once. |
|
|
28
|
+
| Within `revalidate` | The kept copy is served, with no render. |
|
|
29
|
+
| Past `revalidate` | The kept copy is still served at once, and one render replaces it behind the visitor. This lasts up to a day; after that, the page renders while the visitor waits. |
|
|
30
|
+
| After `changed(tag)` | The page renders, on every instance, and is never served stale. |
|
|
31
|
+
|
|
32
|
+
A 404 is kept for a minute at most, so a scanner walking random urls cannot
|
|
33
|
+
hold your pages out.
|
|
34
|
+
|
|
35
|
+
## See it working
|
|
36
|
+
|
|
37
|
+
Pages are never kept in development, so build and start the app first. Ask for
|
|
38
|
+
a page twice:
|
|
39
|
+
|
|
40
|
+
```sh
|
|
41
|
+
curl -sI http://localhost:3000/posts/hello | grep -iE 'x-rsc-kit|^age|cache-status'
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
The second answer has `X-RSC-Kit: cached` and an `Age`. On Homeport the edge
|
|
45
|
+
keeps the page instead. From its next engine release, its entry in
|
|
46
|
+
`Cache-Status` [says whether it answered](/guides/edge-caching#cache-status-on-homeport).
|
|
47
|
+
|
|
48
|
+
If it has `Cache-Status: rsc-kit; fwd=bypass; detail=cookies` instead, the page
|
|
49
|
+
was sent private, and `detail` says why. The server log says it once per page,
|
|
50
|
+
naming what read the visitor:
|
|
51
|
+
|
|
52
|
+
```
|
|
53
|
+
[rsc-kit] /posts/[slug] exports revalidate, and was sent private: cookies() in Header
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Refresh it the moment the data changes
|
|
57
|
+
|
|
58
|
+
A timer alone means a change can take `revalidate` seconds to show. To show it
|
|
59
|
+
at once, read the data through [`cache()`](/guides/caching#across-requests) with
|
|
60
|
+
tags, and say when those tags change. A page remembers the tags it read.
|
|
61
|
+
|
|
62
|
+
```ts title="src/data/posts.ts"
|
|
63
|
+
import { cache } from '@rsc-kit/core/cache'
|
|
64
|
+
|
|
65
|
+
export const findPost = cache(
|
|
66
|
+
async (slug: string) => db.posts.findBySlug(slug),
|
|
67
|
+
{ key: 'posts.find', revalidate: 3600, tags: (slug) => ['posts', `post:${slug}`] },
|
|
68
|
+
)
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
```ts title="src/app/posts/actions.ts"
|
|
72
|
+
'use server'
|
|
73
|
+
import { changed } from '@rsc-kit/core/changed'
|
|
74
|
+
|
|
75
|
+
export async function savePost(slug: string, values: PostValues) {
|
|
76
|
+
await db.posts.save(slug, values)
|
|
77
|
+
await changed(`post:${slug}`)
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### With Laravel
|
|
82
|
+
|
|
83
|
+
The read is an `rpc()` inside `cache()`, and PHP says what changed, from
|
|
84
|
+
anywhere: an action, a job, a webhook or an observer.
|
|
85
|
+
|
|
86
|
+
```ts title="resources/js/data/posts.ts"
|
|
87
|
+
import { cache } from '@rsc-kit/core/cache'
|
|
88
|
+
|
|
89
|
+
export const findPost = cache(
|
|
90
|
+
(slug: string) => rpc('Posts.find', slug),
|
|
91
|
+
{ key: 'posts.find', revalidate: 3600, tags: (slug) => ['posts', `post:${slug}`] },
|
|
92
|
+
)
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
```php title="app/Rsc/Posts.php"
|
|
96
|
+
namespace App\Rsc;
|
|
97
|
+
|
|
98
|
+
use App\Models\Post;
|
|
99
|
+
use RscKit\Rsc;
|
|
100
|
+
|
|
101
|
+
class Posts
|
|
102
|
+
{
|
|
103
|
+
public function find(string $slug): Post
|
|
104
|
+
{
|
|
105
|
+
return Post::where('slug', $slug)->firstOrFail();
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
```php
|
|
111
|
+
// after the write
|
|
112
|
+
$post->save();
|
|
113
|
+
|
|
114
|
+
Rsc::changed("post:{$post->slug}");
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### With Go
|
|
118
|
+
|
|
119
|
+
The same read, and `Changed` on the registry:
|
|
120
|
+
|
|
121
|
+
```go
|
|
122
|
+
reg.Handle("Posts.find", func(ctx context.Context, slug string) (Post, error) {
|
|
123
|
+
return posts.Find(ctx, slug)
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
// after the write, from a handler, a job or a webhook
|
|
127
|
+
reg.Changed(ctx, "post:"+slug)
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
Call it after your transaction commits, so nobody renders the old data under
|
|
131
|
+
the new version.
|
|
132
|
+
|
|
133
|
+
## What keeps a page from being kept
|
|
134
|
+
|
|
135
|
+
The page is judged after it renders, and sent private when:
|
|
136
|
+
|
|
137
|
+
- It read the visitor: `cookies()`, `headers()`, `request()` or `connection()`,
|
|
138
|
+
anywhere in the render, a Suspense'd child included.
|
|
139
|
+
- Its answer set a cookie, or carries a CSP nonce.
|
|
140
|
+
- Its `middleware.ts` declares Laravel or Go route middleware, which runs
|
|
141
|
+
against the visitor's session.
|
|
142
|
+
- It answered anything but a 200 or a 404, or its render failed.
|
|
143
|
+
|
|
144
|
+
**A kept page calls its backend as nobody.** Its `rpc()` calls carry no cookie
|
|
145
|
+
and no `Authorization`, and a cookie the backend sets is dropped. In Laravel,
|
|
146
|
+
`auth()->user()` is null there. That is the rule rather than a limit: the copy
|
|
147
|
+
goes to every visitor. A page that shows who is signed in is not one to keep.
|
|
148
|
+
|
|
149
|
+
A form in a kept page still works: a same-origin post needs no token. See
|
|
150
|
+
[public forms](/guides/public-forms).
|
|
151
|
+
|
|
152
|
+
## On several instances
|
|
153
|
+
|
|
154
|
+
Nothing changes in the page. Each instance keeps its own copy in memory, 64 MB
|
|
155
|
+
at most, and `changed()` reaches them all, because a kept page checks its tags'
|
|
156
|
+
versions on each lookup.
|
|
157
|
+
|
|
158
|
+
- **With Laravel or Go,** the backend holds the versions, so there is nothing to
|
|
159
|
+
set up beyond [a shared store for Go](/hosts/go#saying-data-changed).
|
|
160
|
+
- **With no backend,** install a shared
|
|
161
|
+
[version source](/guides/live-data), as `refreshOn` already needs.
|
|
162
|
+
- **On a Worker or Vercel,** memory does not last, so give the page cache a
|
|
163
|
+
store with `installCacheStore()`. The host warns without one.
|
|
164
|
+
|
|
165
|
+
[Where a kept page lives](/guides/edge-caching#kept-by-the-engine-too) has the
|
|
166
|
+
table for each platform. Turn the page cache off with `pageCache: false` in the
|
|
167
|
+
host's options.
|
|
168
|
+
|
|
169
|
+
## With a CDN in front
|
|
170
|
+
|
|
171
|
+
The same page is ready for one. It goes out with `Cache-Control`,
|
|
172
|
+
`CDN-Cache-Control` and the `Cache-Tag` of every `cache()` answer it used, and
|
|
173
|
+
`changed()` can purge the CDN too. [Edge caching](/guides/edge-caching) covers
|
|
174
|
+
the headers and the purge hook.
|
package/guides/laravel.md
CHANGED
|
@@ -418,6 +418,22 @@ the channel is public, and names may be one visitor's - so Laravel still
|
|
|
418
418
|
answers which names moved, and the renderer never touches the database. It is
|
|
419
419
|
sent after the transaction commits.
|
|
420
420
|
|
|
421
|
+
## Pages kept between requests
|
|
422
|
+
|
|
423
|
+
`export const revalidate = 60` keeps a page and refreshes it behind the visitor,
|
|
424
|
+
and `Rsc::changed('posts')` renders it again at once on every instance, when it
|
|
425
|
+
read that tag through `cache()`. A kept page calls PHP as nobody, so
|
|
426
|
+
`auth()->user()` is null there. With `HOMEPORT_PURGE_URL` set, `Rsc::changed()`
|
|
427
|
+
purges the CDN as well, after the transaction commits and the response has gone.
|
|
428
|
+
[ISR](/guides/isr) has the whole of it.
|
|
429
|
+
|
|
430
|
+
## Public forms
|
|
431
|
+
|
|
432
|
+
A contact form or a sign-up is `defineForm({ action: 'Contact.send' })` in
|
|
433
|
+
TypeScript: the fields come from the `FormRequest` on that method, with no schema
|
|
434
|
+
to write. What the rules refuse shows under each field. [Public forms](/guides/public-forms#laravel) has the
|
|
435
|
+
code.
|
|
436
|
+
|
|
421
437
|
## Route middleware
|
|
422
438
|
|
|
423
439
|
The renderer owns the route table, but Laravel still decides whether a route
|
package/guides/public-forms.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Public forms
|
|
2
2
|
|
|
3
|
-
> A contact form, a sign-up, a survey - drawn from a Standard Schema, checked in the browser and on the server, sent to any backend.
|
|
3
|
+
> A contact form, a sign-up, a survey - drawn from a Standard Schema or a backend function, checked in the browser and on the server, sent to any backend.
|
|
4
4
|
|
|
5
5
|
`@rsc-kit/press/forms` turns a Standard Schema into a form anyone may fill in.
|
|
6
6
|
It is the panel's form without the panel: the same fields read from the same
|
|
@@ -74,16 +74,172 @@ handle: async ({ email }) => {
|
|
|
74
74
|
|
|
75
75
|
## A Laravel or Go backend
|
|
76
76
|
|
|
77
|
-
|
|
77
|
+
Name the function, and the form needs no schema of its own. The backend already
|
|
78
|
+
says what the function takes, so the fields and the browser's checks come from
|
|
79
|
+
there, and every check that counts is the backend's:
|
|
80
|
+
|
|
81
|
+
```ts title="resources/js/forms/contact.ts"
|
|
82
|
+
import { defineForm } from '@rsc-kit/press/forms'
|
|
83
|
+
import { shadcn } from '@rsc-kit/press/shadcn'
|
|
84
|
+
|
|
85
|
+
export const contact = defineForm({
|
|
86
|
+
name: 'contact',
|
|
87
|
+
action: 'Contact.send',
|
|
88
|
+
fields: { message: { type: 'textarea' } },
|
|
89
|
+
theme: shadcn,
|
|
90
|
+
submit: 'Send message',
|
|
91
|
+
done: 'Thanks - we will be in touch.',
|
|
92
|
+
})
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
`action` is typed from the backend's functions, so a misspelt name fails the
|
|
96
|
+
typecheck. `fields` still sets a label or a control, and the bot trap, `done`
|
|
97
|
+
and `redirect` work as on any form. There is no `handle`: the values go to the
|
|
98
|
+
function. Nothing reaches the browser but the form, and no validation library
|
|
99
|
+
is added to the server.
|
|
100
|
+
|
|
101
|
+
The function must take one object with named fields: a `FormRequest` in Laravel,
|
|
102
|
+
a struct in Go. A form calling anything else says so when it first renders. So
|
|
103
|
+
does a field it cannot draw, such as a file: an action's values travel as JSON,
|
|
104
|
+
so an upload needs `form` and `handle`. Give an open-typed field a control in
|
|
105
|
+
`fields`.
|
|
106
|
+
|
|
107
|
+
To check something in TypeScript first, or to shape the values on the way, keep
|
|
108
|
+
a schema and call the backend yourself:
|
|
78
109
|
|
|
79
110
|
```ts
|
|
111
|
+
form: z.object({ email: z.email() }),
|
|
80
112
|
handle: (values) => rpc('Contact.send', values),
|
|
81
113
|
```
|
|
82
114
|
|
|
83
|
-
The
|
|
84
|
-
|
|
115
|
+
The schema's checks then run first, so a submission it refuses never reaches the
|
|
116
|
+
backend.
|
|
117
|
+
|
|
118
|
+
The values arrive as one object, keyed by field. What the backend refuses comes
|
|
119
|
+
back under the field of the same name, with what was typed kept, and there is
|
|
85
120
|
nothing to catch.
|
|
86
121
|
|
|
122
|
+
### Laravel
|
|
123
|
+
|
|
124
|
+
A `FormRequest` type-hinted on the method is built from the values and validated
|
|
125
|
+
before the method runs, as on a route. Its failure is the 422 the form reads.
|
|
126
|
+
|
|
127
|
+
```php title="app/Http/Requests/SendContact.php"
|
|
128
|
+
namespace App\Http\Requests;
|
|
129
|
+
|
|
130
|
+
use Illuminate\Foundation\Http\FormRequest;
|
|
131
|
+
|
|
132
|
+
class SendContact extends FormRequest
|
|
133
|
+
{
|
|
134
|
+
public function authorize(): bool
|
|
135
|
+
{
|
|
136
|
+
return true; // anyone may send it; make:request writes false
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
public function rules(): array
|
|
140
|
+
{
|
|
141
|
+
return [
|
|
142
|
+
'name' => ['required', 'string', 'min:2'],
|
|
143
|
+
'email' => ['required', 'email', 'unique:subscribers,email'],
|
|
144
|
+
'message' => ['required', 'string', 'min:10', 'max:2000'],
|
|
145
|
+
];
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
```php title="app/Rsc/Contact.php"
|
|
151
|
+
namespace App\Rsc;
|
|
152
|
+
|
|
153
|
+
use App\Http\Requests\SendContact;
|
|
154
|
+
use App\Models\Message;
|
|
155
|
+
|
|
156
|
+
class Contact
|
|
157
|
+
{
|
|
158
|
+
public function send(SendContact $request): void
|
|
159
|
+
{
|
|
160
|
+
Message::create($request->validated());
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
The method takes the request alone. Without a `FormRequest`, take `array $values`
|
|
166
|
+
and call `Validator::make($values, [...])->validate()`, or throw
|
|
167
|
+
`ValidationException::withMessages(['email' => 'That address is blocked.'])`.
|
|
168
|
+
Either lands the same way.
|
|
169
|
+
|
|
170
|
+
Keep the class under `app/Rsc`, and `action` and `rpc('Contact.send', ...)` are
|
|
171
|
+
typed from it at the next build.
|
|
172
|
+
|
|
173
|
+
The form's fields are read from `rules()`, in order, at build time: `required`,
|
|
174
|
+
the type rules, `email`, `url`, `date`, `min`, `max`, `size`, `between`, and
|
|
175
|
+
`in` or `Rule::in()`, which draws a select. Anything else stays the server's
|
|
176
|
+
check alone, and its field a plain input: `regex` (PCRE is not the browser's
|
|
177
|
+
pattern), `Rule::enum()`, a closure. `rules()` runs with no request behind it,
|
|
178
|
+
so one that reads the request cannot be described: list the fields in `fields`.
|
|
179
|
+
|
|
180
|
+
`rsc:install` does not set up Tailwind or shadcn, so the `shadcn` theme comes out
|
|
181
|
+
unstyled until the app has both.
|
|
182
|
+
|
|
183
|
+
### Go
|
|
184
|
+
|
|
185
|
+
Register a function that takes the values as a struct. Its `json` tags are the
|
|
186
|
+
form's field names, and `rsckit.InvalidField` or `rsckit.Invalid` is the 422.
|
|
187
|
+
|
|
188
|
+
```go
|
|
189
|
+
type ContactInput struct {
|
|
190
|
+
Name string `json:"name"`
|
|
191
|
+
Email string `json:"email"`
|
|
192
|
+
Message string `json:"message"`
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
reg.Handle("Contact.send", func(ctx context.Context, in ContactInput) error {
|
|
196
|
+
if blocked(in.Email) {
|
|
197
|
+
return rsckit.InvalidField("email", "That address is blocked.")
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
return messages.Insert(ctx, in)
|
|
201
|
+
})
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
For several fields at once:
|
|
205
|
+
|
|
206
|
+
```go
|
|
207
|
+
return rsckit.Invalid(map[string][]string{
|
|
208
|
+
"email": {"That address is blocked."},
|
|
209
|
+
"message": {"That looks like spam."},
|
|
210
|
+
})
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
`reg.WriteManifest("rsc-host.json")` types `action` and `rpc('Contact.send', ...)`
|
|
214
|
+
for the app, as with any function.
|
|
215
|
+
|
|
216
|
+
The form's fields are the struct's, in its order, and a field is required unless
|
|
217
|
+
its `json` tag says `omitempty`. A `validate` tag, in go-playground/validator's
|
|
218
|
+
spelling, adds what the browser can check: `required`, `omitempty`, `email`,
|
|
219
|
+
`url`, `min`, `max`, `len`, `gte`, `lte`, `oneof` (a select) and
|
|
220
|
+
`datetime=2006-01-02`.
|
|
221
|
+
|
|
222
|
+
```go
|
|
223
|
+
type ContactInput struct {
|
|
224
|
+
Name string `json:"name" validate:"required,min=2"`
|
|
225
|
+
Email string `json:"email" validate:"required,email"`
|
|
226
|
+
Topic string `json:"topic" validate:"required,oneof=sales support"`
|
|
227
|
+
Message string `json:"message" validate:"required,min=10,max=2000"`
|
|
228
|
+
}
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
**The tag is only read, never run.** `rsc-kit/go` depends on no validator, so
|
|
232
|
+
the function still checks what it was sent, with validator or by hand: a post
|
|
233
|
+
need not come from a browser.
|
|
234
|
+
|
|
235
|
+
### On a kept page
|
|
236
|
+
|
|
237
|
+
A public form can sit in an [ISR](/guides/isr) page. The page is kept for
|
|
238
|
+
everyone, and the post is the visitor's own: it needs no token, because the
|
|
239
|
+
engine and Laravel 13 accept a same-origin post by its `Sec-Fetch-Site` header.
|
|
240
|
+
The call to the backend runs in that post, so it carries the visitor's cookie,
|
|
241
|
+
unlike the calls the kept page made while rendering.
|
|
242
|
+
|
|
87
243
|
## Once it is sent
|
|
88
244
|
|
|
89
245
|
`done` is what the visitor reads in place of the form; without one it says
|
|
@@ -97,7 +253,13 @@ hidden from screen readers and not autofilled. A bot filling in every input
|
|
|
97
253
|
fills that one too, and the submission is answered as though it worked and
|
|
98
254
|
dropped - `handle` never sees it, and the bot learns nothing. It is a filter
|
|
99
255
|
for the crudest spam, not a CAPTCHA; a form worth attacking wants rate
|
|
100
|
-
limiting
|
|
256
|
+
limiting as well.
|
|
257
|
+
|
|
258
|
+
Put that in front of the app, at the CDN or the proxy, where the visitor's
|
|
259
|
+
address is known. A backend sees the renderer's address on an `rpc()`, not the
|
|
260
|
+
visitor's, so `throttle:5,1` on a Laravel method puts every logged-out visitor
|
|
261
|
+
in one bucket. To limit in the backend, key on something the submission carries,
|
|
262
|
+
such as the email address.
|
|
101
263
|
|
|
102
264
|
The trap is named `homepage`, so a form cannot have a field of that name.
|
|
103
265
|
|
|
@@ -296,7 +296,7 @@ answered if you can - post
|
|
|
296
296
|
to that url, `Content-Type: application/json`, at most 100 tags a call, each
|
|
297
297
|
at most 1024 bytes. It needs no token. Only a `202` means it was accepted. A
|
|
298
298
|
429 may be tried once more after its `Retry-After`; anything else is logged
|
|
299
|
-
and never fails the change, since the CDN drops its copy at `
|
|
299
|
+
and never fails the change, since the CDN drops its copy at its `max-age` in `CDN-Cache-Control` anyway.
|
|
300
300
|
Without the variable, do nothing.
|
|
301
301
|
|
|
302
302
|
## Server actions
|
|
@@ -323,6 +323,13 @@ are `optional`, a variadic `rest`, and its `result`, each a JSON Schema;
|
|
|
323
323
|
"defs": { "Order": { "type": "object", "properties": { "id": { "type": "integer" } }, "required": ["id"] } }
|
|
324
324
|
```
|
|
325
325
|
|
|
326
|
+
A function taking one object with `properties` can be [a form with no
|
|
327
|
+
schema](/guides/public-forms#a-laravel-or-go-backend), read from these same
|
|
328
|
+
types: `required`, `format` (`email`, `date`), `minLength`, `maxLength`,
|
|
329
|
+
`minimum`, `maximum` and `enum` become its fields and browser checks. The
|
|
330
|
+
properties are drawn in the order written. If your language's JSON sorts a map,
|
|
331
|
+
say the order with `"x-order": ["name", "email"]` beside them.
|
|
332
|
+
|
|
326
333
|
The build writes `server-actions.generated.ts` in the source directory
|
|
327
334
|
exporting each action, so a client component imports `ordersCancel` and
|
|
328
335
|
calls it, and types `rpc()`'s first argument with the functions. Have Vite
|
|
@@ -440,6 +447,8 @@ It checks the whole contract on this page:
|
|
|
440
447
|
dotted paths and the form's under `""`, redirects, revalidation staying
|
|
441
448
|
with its call, a crash staying a failure, and arguments that do not fit
|
|
442
449
|
failing rather than refusing;
|
|
450
|
+
- **forms** - a function taking one object, its fields in order with the hints
|
|
451
|
+
a browser can check, and a refusal landing on its field;
|
|
443
452
|
- **the request** - `Cookie` and `Authorization` forwarded, a cookie set by a
|
|
444
453
|
call reaching the page, no secret or the wrong one refused with 403, an
|
|
445
454
|
unknown function 404, a malformed body 400;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rsc-kit/mcp",
|
|
3
|
-
"version": "0.30.
|
|
3
|
+
"version": "0.30.48",
|
|
4
4
|
"description": "An MCP server over what an rsc-kit build decided: the routes, why each one is static or not, and what it costs the browser.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
36
|
"@modelcontextprotocol/sdk": "^1.30.0",
|
|
37
|
-
"create-rsc-kit": "^0.30.
|
|
37
|
+
"create-rsc-kit": "^0.30.48",
|
|
38
38
|
"zod": "^3.25"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|