@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.
- package/dist/recipes.js +37 -20
- 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 -250
- package/guides/bap-vs-inertia.md +67 -25
- package/guides/blueprints.md +307 -174
- package/guides/bun.md +42 -25
- package/guides/caching.md +46 -49
- package/guides/coming-from-inertia.md +5 -5
- 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 -538
- 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 +272 -150
- 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 -538
- package/guides/laravel.md +0 -786
- package/guides/your-own-backend.md +0 -587
package/guides/blueprints.md
CHANGED
|
@@ -2,18 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
> A form defined once - fields, checks and saves - and drawn anywhere it is needed - a contact page, an edit page, an admin panel - from a Standard Schema or a backend's own actions.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
`<profile.Form id={user.id} values={user} />`, and an
|
|
9
|
-
[admin panel](/guides/admin/) draws its create and edit pages from it with
|
|
10
|
-
`defineResource({ form: profile })` - the same fields, the same browser
|
|
11
|
-
checks, the same refusals under the same fields. A
|
|
12
|
-
[theme](/guides/admin/#themes) draws it; the `shadcn` theme draws both.
|
|
13
|
-
|
|
14
|
-
On a Laravel or Go backend there is no schema to write: hand it the backend's
|
|
15
|
-
actions, and the form is drawn from what they take. See
|
|
16
|
-
[A Laravel or Go backend](#a-laravel-or-go-backend).
|
|
5
|
+
A **blueprint** is a form defined once: its fields, its checks and what saving
|
|
6
|
+
it does. `defineBlueprint` from `@rsc-kit/press/forms` builds one from a
|
|
7
|
+
Standard Schema, or from a Laravel or Go backend's actions:
|
|
17
8
|
|
|
18
9
|
```ts title="src/forms/contact.ts"
|
|
19
10
|
import * as z from 'zod'
|
|
@@ -52,16 +43,60 @@ export default function ContactPage() {
|
|
|
52
43
|
}
|
|
53
44
|
```
|
|
54
45
|
|
|
55
|
-
The form reads nothing from the request, so the
|
|
56
|
-
|
|
46
|
+
The form reads nothing from the request, so the build stores the page around
|
|
47
|
+
it like any other static page. Only sending it runs on the server.
|
|
48
|
+
|
|
49
|
+
The same blueprint draws an edit page with
|
|
50
|
+
`<profile.Form id={user.id} values={user} />`, and an
|
|
51
|
+
[admin panel](/guides/admin/)'s create and edit pages with
|
|
52
|
+
`defineResource({ form: profile })`. Each place gets the same fields, the same
|
|
53
|
+
browser checks, and the same refusals under the same fields. A theme draws it;
|
|
54
|
+
the `shadcn` theme draws both the form and the panel.
|
|
55
|
+
|
|
56
|
+
On a Laravel or Go backend there is no schema to write. Hand it the backend's
|
|
57
|
+
actions, and the form is drawn from what they take. See
|
|
58
|
+
[A Laravel or Go backend](#a-laravel-or-go-backend).
|
|
59
|
+
|
|
60
|
+
## Installing it
|
|
61
|
+
|
|
62
|
+
```sh
|
|
63
|
+
bun add @rsc-kit/press
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
```css title="src/app/styles.css"
|
|
67
|
+
@import "@rsc-kit/press/shadcn.css";
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
The two lines are the panel's too. [Admin panels](/guides/admin/) has the rest
|
|
71
|
+
of its setup.
|
|
72
|
+
|
|
73
|
+
## Checking what was sent
|
|
74
|
+
|
|
75
|
+
The schema's constraints become the browser's: `required`, `minlength`,
|
|
76
|
+
`maxlength`, `type="email"` and a `pattern`. A visitor hears about a missing
|
|
77
|
+
field before anything is sent, under the field, in their own language.
|
|
78
|
+
|
|
79
|
+
The schema then checks every submission on the server. That is the check that
|
|
80
|
+
counts, because anyone can post without a browser. What it refuses comes back
|
|
81
|
+
under each field, with what was typed kept.
|
|
57
82
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
83
|
+
`handle` gets the values only once they have passed. To refuse something the
|
|
84
|
+
schema can't know, such as an address already signed up, call `fieldErrors`:
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
import { fieldErrors } from '@rsc-kit/core/action'
|
|
88
|
+
|
|
89
|
+
handle: async ({ email }) => {
|
|
90
|
+
if (await subscribed(email)) fieldErrors({ email: 'That address is already on the list.' })
|
|
91
|
+
await subscribe(email)
|
|
92
|
+
},
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
The message goes under its field, the same way the schema's do.
|
|
61
96
|
|
|
62
97
|
## Editing a record
|
|
63
98
|
|
|
64
|
-
Give the blueprint an `update
|
|
99
|
+
Give the blueprint an `update`. Drawn with an `id`, the form then edits that
|
|
65
100
|
record instead of creating one, starting from `values`:
|
|
66
101
|
|
|
67
102
|
```ts title="src/forms/profile.ts"
|
|
@@ -85,102 +120,107 @@ export default async function Settings() {
|
|
|
85
120
|
}
|
|
86
121
|
```
|
|
87
122
|
|
|
88
|
-
Only the blueprint's own fields of `values` reach the browser, so handing it
|
|
89
|
-
a whole row does not put its password hash in the page.
|
|
123
|
+
- Only the blueprint's own fields of `values` reach the browser, so handing it
|
|
124
|
+
a whole row does not put its password hash in the page.
|
|
125
|
+
- **The key comes back from the browser**, like every field, and anyone can
|
|
126
|
+
post another. `update` must check the visitor may change that record: by
|
|
127
|
+
comparing it with the session's user, or in Laravel, in the `FormRequest`'s
|
|
128
|
+
`authorize()`.
|
|
129
|
+
- A blueprint may have `handle`, `update` or both. Drawing it for one it
|
|
130
|
+
doesn't have says so.
|
|
131
|
+
- `honeypot: false` drops the [bot trap](#stopping-bots) for a form only
|
|
132
|
+
signed-in people reach.
|
|
90
133
|
|
|
91
|
-
|
|
92
|
-
another. `update` must check the visitor may change that record - here, by
|
|
93
|
-
comparing it with the session's user; in Laravel, the `FormRequest`'s
|
|
94
|
-
`authorize()`. A blueprint may have `handle`, `update` or both, and drawing it
|
|
95
|
-
for one it does not have says so.
|
|
134
|
+
## Fields that depend on others
|
|
96
135
|
|
|
97
|
-
`
|
|
136
|
+
`fields` can say two things about another field, as plain data. The browser
|
|
137
|
+
applies them while the visitor types, and the server applies them again to
|
|
138
|
+
what was sent.
|
|
98
139
|
|
|
99
|
-
|
|
140
|
+
```ts
|
|
141
|
+
fields: {
|
|
142
|
+
company: { visible: { when: 'topic', is: 'sales' } },
|
|
143
|
+
slug: { from: { field: 'title' } },
|
|
144
|
+
},
|
|
145
|
+
```
|
|
100
146
|
|
|
101
|
-
|
|
102
|
-
|
|
147
|
+
**`visible`** shows a field only while another has one of the values in `is`,
|
|
148
|
+
one value or a list:
|
|
103
149
|
|
|
104
150
|
```ts
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
table: (col) => [col.text('name'), col.text('email')],
|
|
111
|
-
form: profile,
|
|
112
|
-
})
|
|
151
|
+
fields: {
|
|
152
|
+
company: { visible: { when: 'topic', is: ['sales', 'partners'] } },
|
|
153
|
+
reason: { visible: { when: 'topic', not: 'sales' } },
|
|
154
|
+
vat: { visible: { when: 'business' } },
|
|
155
|
+
},
|
|
113
156
|
```
|
|
114
157
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
158
|
+
- `not` turns it round.
|
|
159
|
+
- With neither, the field shows once the other is filled in, or a switch is on.
|
|
160
|
+
- A hidden field is not sent, and the browser doesn't check it;
|
|
161
|
+
if a post sends one anyway, the server drops it before the schema sees it.
|
|
162
|
+
- So a field asked only sometimes is optional to the schema,
|
|
163
|
+
`z.string().min(2).optional()`, and checked whenever it is there.
|
|
120
164
|
|
|
121
|
-
|
|
165
|
+
**`from`** fills a field with the slug of another as the other is typed:
|
|
166
|
+
"Café & Bar" becomes `cafe-bar`. Once someone edits it by hand it is left
|
|
167
|
+
alone, and one left empty is filled the same way on the server.
|
|
122
168
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
field before anything is sent, under the field, in their own language. The
|
|
126
|
-
schema then checks every submission on the server, which is the check that
|
|
127
|
-
counts: anyone can post without a browser. What it refuses comes back under
|
|
128
|
-
each field, with what was typed kept.
|
|
169
|
+
`when` and `field` name another of the form's fields, and one that doesn't
|
|
170
|
+
exist fails at startup.
|
|
129
171
|
|
|
130
|
-
|
|
131
|
-
an address already signed up - goes under its field the same way:
|
|
172
|
+
## Once it is sent
|
|
132
173
|
|
|
133
|
-
|
|
134
|
-
|
|
174
|
+
`done` is what the visitor reads in place of the form. Without one, it says the
|
|
175
|
+
message was sent. `redirect` sends them somewhere instead, such as a thank-you
|
|
176
|
+
page or the account they just made:
|
|
135
177
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
},
|
|
178
|
+
```ts
|
|
179
|
+
export const signup = defineBlueprint({
|
|
180
|
+
name: 'signup',
|
|
181
|
+
form: z.object({ email: z.email() }),
|
|
182
|
+
theme: shadcn,
|
|
183
|
+
redirect: '/welcome',
|
|
184
|
+
handle: (values) => createAccount(values),
|
|
185
|
+
})
|
|
140
186
|
```
|
|
141
187
|
|
|
142
|
-
|
|
188
|
+
A form has `done` or `redirect`, not both.
|
|
189
|
+
|
|
190
|
+
## In an admin panel
|
|
143
191
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
to what was sent.
|
|
192
|
+
A resource takes the blueprint as its `form`, and its create and edit pages are
|
|
193
|
+
that blueprint's form:
|
|
147
194
|
|
|
148
195
|
```ts
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
196
|
+
export const people = defineResource<User>()({
|
|
197
|
+
name: 'people',
|
|
198
|
+
key: 'id',
|
|
199
|
+
title: 'name',
|
|
200
|
+
source: { list, find, delete: remove },
|
|
201
|
+
table: (col) => [col.text('name'), col.text('email')],
|
|
202
|
+
form: profile,
|
|
203
|
+
})
|
|
153
204
|
```
|
|
154
205
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
schema - `z.string().min(2).optional()` - and checked whenever it is there.
|
|
161
|
-
|
|
162
|
-
**`from`** fills a field with the slug of another - "Café & Bar" is
|
|
163
|
-
`cafe-bar` - as the other is typed. Once someone edits it by hand it is left
|
|
164
|
-
alone, and one left empty is filled the same way on the server.
|
|
165
|
-
|
|
166
|
-
`when` and `field` name another of the form's fields, and one that does not
|
|
167
|
-
exist fails at startup.
|
|
206
|
+
A blueprint that saves (a backend's `create` and `update`, or a schema's
|
|
207
|
+
`handle` and `update`) saves the panel's records too, so the source only lists,
|
|
208
|
+
finds and deletes. One that doesn't save leaves that to a whole
|
|
209
|
+
[source](/guides/admin/). Say `fields` and `actions` on the blueprint, not again
|
|
210
|
+
on the resource.
|
|
168
211
|
|
|
169
212
|
## A Laravel or Go backend
|
|
170
213
|
|
|
171
|
-
Give it the backend's actions, and the form needs no schema of its own
|
|
172
|
-
backend already says what each takes, so the fields and the browser's checks
|
|
173
|
-
come from there, and every check that counts is the backend's. The actions are
|
|
174
|
-
the ones the build generated, imported like any other:
|
|
214
|
+
Give it the backend's actions, and the form needs no schema of its own:
|
|
175
215
|
|
|
176
216
|
```ts title="resources/js/forms/contact.ts"
|
|
177
217
|
import { defineBlueprint } from '@rsc-kit/press/forms'
|
|
178
218
|
import { shadcn } from '@rsc-kit/press/shadcn'
|
|
179
|
-
import {
|
|
219
|
+
import { send } from '../server-actions/contact'
|
|
180
220
|
|
|
181
221
|
export const contact = defineBlueprint({
|
|
182
222
|
name: 'contact',
|
|
183
|
-
actions: { create:
|
|
223
|
+
actions: { create: send },
|
|
184
224
|
fields: { message: { type: 'textarea' } },
|
|
185
225
|
theme: shadcn,
|
|
186
226
|
submit: 'Send message',
|
|
@@ -188,48 +228,78 @@ export const contact = defineBlueprint({
|
|
|
188
228
|
})
|
|
189
229
|
```
|
|
190
230
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
$user)` in Laravel, where `$this->route('user')` reads it in `rules()` and
|
|
196
|
-
`authorize()`, and `func(ctx, in ProfileInput, id string)` in Go.
|
|
231
|
+
The backend already says what each action takes, so the fields and the
|
|
232
|
+
browser's checks come from there, and every check that counts is the backend's.
|
|
233
|
+
The actions are the ones the build generated, one module per backend class,
|
|
234
|
+
imported like any other.
|
|
197
235
|
|
|
198
|
-
You need not write it
|
|
199
|
-
|
|
236
|
+
You need not write it. This writes the file from the action, and it is yours to
|
|
237
|
+
edit from then on:
|
|
200
238
|
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
function. Nothing reaches the browser but the form, and no validation library
|
|
205
|
-
is added to the server.
|
|
239
|
+
```sh
|
|
240
|
+
bunx rsc-kit make:form Contact.send
|
|
241
|
+
```
|
|
206
242
|
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
does one given a function the build did not generate. So
|
|
210
|
-
does a field it cannot draw, such as a file: an action's values travel as JSON,
|
|
211
|
-
so an upload needs `form` and `handle`. Give an open-typed field a control in
|
|
212
|
-
`fields`.
|
|
243
|
+
It writes `forms/contact.ts` in the source directory. `--name` picks the form's
|
|
244
|
+
name, and `--force` writes over a file that is there.
|
|
213
245
|
|
|
214
|
-
To
|
|
215
|
-
a schema and call the backend yourself:
|
|
246
|
+
To edit records too, add `update`:
|
|
216
247
|
|
|
217
248
|
```ts
|
|
218
|
-
|
|
219
|
-
|
|
249
|
+
import * as profiles from '../server-actions/profiles'
|
|
250
|
+
|
|
251
|
+
export const profile = defineBlueprint({
|
|
252
|
+
name: 'profile',
|
|
253
|
+
actions: { create: profiles.store, update: profiles.update },
|
|
254
|
+
theme: shadcn,
|
|
255
|
+
})
|
|
220
256
|
```
|
|
221
257
|
|
|
222
|
-
|
|
223
|
-
|
|
258
|
+
- `actions` is `{ create, update }`, either or both.
|
|
259
|
+
- The create form is drawn from what `create` takes and the edit form from what
|
|
260
|
+
`update` takes, so a password required to sign up can be optional to edit.
|
|
261
|
+
- `update` is called with the record's key after the values:
|
|
262
|
+
`update(UpdateProfile $request, string $user)` in Laravel, where
|
|
263
|
+
`$this->route('user')` reads it in `rules()` and `authorize()`, and
|
|
264
|
+
`func(ctx, in ProfileInput, id string)` in Go.
|
|
265
|
+
- An action that is not there fails the import, and a field `fields` names that
|
|
266
|
+
the action does not take fails the typecheck.
|
|
267
|
+
- `fields` still sets a label or a control, and the bot trap, `done` and
|
|
268
|
+
`redirect` work as on any form.
|
|
269
|
+
- There is no `handle`: the values go to the function. Nothing reaches the
|
|
270
|
+
browser but the form, and no validation library is added to the server.
|
|
224
271
|
|
|
225
272
|
The values arrive as one object, keyed by field. What the backend refuses comes
|
|
226
273
|
back under the field of the same name, with what was typed kept, and there is
|
|
227
274
|
nothing to catch.
|
|
228
275
|
|
|
276
|
+
### What an action must take
|
|
277
|
+
|
|
278
|
+
The action must take one object with named fields: a `FormRequest` in Laravel,
|
|
279
|
+
a struct in Go.
|
|
280
|
+
|
|
281
|
+
- A form given anything else says so when it first renders, as does one given a
|
|
282
|
+
function the build did not generate.
|
|
283
|
+
- So does a field it cannot draw, such as a file. An action's values travel as
|
|
284
|
+
JSON, so an upload needs `form` and `handle`.
|
|
285
|
+
- Give an open-typed field a control in `fields`.
|
|
286
|
+
|
|
287
|
+
To check something in TypeScript first, or to shape the values on the way, keep
|
|
288
|
+
a schema and call the backend yourself:
|
|
289
|
+
|
|
290
|
+
```ts
|
|
291
|
+
form: z.object({ email: z.email() }),
|
|
292
|
+
handle: (values) => send(values),
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
The schema's checks then run first, so a submission it refuses never reaches
|
|
296
|
+
the backend.
|
|
297
|
+
|
|
229
298
|
### Laravel
|
|
230
299
|
|
|
231
|
-
|
|
232
|
-
before the method runs, as on a route
|
|
300
|
+
Type-hint a `FormRequest` on the method. It is built from the values and
|
|
301
|
+
validated before the method runs, as on a route, and its failure is the 422 the
|
|
302
|
+
form reads:
|
|
233
303
|
|
|
234
304
|
```php title="app/Http/Requests/SendContact.php"
|
|
235
305
|
namespace App\Http\Requests;
|
|
@@ -248,6 +318,8 @@ class SendContact extends FormRequest
|
|
|
248
318
|
return [
|
|
249
319
|
'name' => ['required', 'string', 'min:2'],
|
|
250
320
|
'email' => ['required', 'email', 'unique:subscribers,email'],
|
|
321
|
+
'topic' => ['required', 'in:sales,support'],
|
|
322
|
+
'order' => ['required_if:topic,support', 'string'],
|
|
251
323
|
'message' => ['required', 'string', 'min:10', 'max:2000'],
|
|
252
324
|
];
|
|
253
325
|
}
|
|
@@ -269,40 +341,57 @@ class Contact
|
|
|
269
341
|
}
|
|
270
342
|
```
|
|
271
343
|
|
|
272
|
-
The method takes the request alone. Without a `FormRequest`, take
|
|
273
|
-
|
|
274
|
-
`ValidationException::withMessages(['email' => 'That address is blocked.'])`.
|
|
275
|
-
Either lands the same way.
|
|
344
|
+
The method takes the request alone. Without a `FormRequest`, take
|
|
345
|
+
`array $values` and validate it yourself. Either lands the same way:
|
|
276
346
|
|
|
277
|
-
|
|
278
|
-
|
|
347
|
+
```php
|
|
348
|
+
public function send(array $values): void
|
|
349
|
+
{
|
|
350
|
+
$data = Validator::make($values, ['email' => ['required', 'email']])->validate();
|
|
279
351
|
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
check alone, and its field a plain input: `regex` (PCRE is not the browser's
|
|
284
|
-
pattern), `Rule::enum()`, a closure. `rules()` runs with no request behind it,
|
|
285
|
-
so one that reads the request cannot be described: list the fields in `fields`.
|
|
352
|
+
if (Blocked::has($data['email'])) {
|
|
353
|
+
throw ValidationException::withMessages(['email' => 'That address is blocked.']);
|
|
354
|
+
}
|
|
286
355
|
|
|
287
|
-
|
|
288
|
-
|
|
356
|
+
Message::create($data);
|
|
357
|
+
}
|
|
358
|
+
```
|
|
289
359
|
|
|
290
|
-
|
|
291
|
-
|
|
360
|
+
Keep the class under `app/Rsc/Actions`, where every public method is an action:
|
|
361
|
+
`Contact::send` is `send` in `server-actions/contact`, generated at the next
|
|
362
|
+
build. `php artisan make:rsc-action` writes one; see
|
|
363
|
+
[Commands](/hosts/laravel/commands).
|
|
364
|
+
|
|
365
|
+
**How the fields are read.** The form's fields come from `rules()`, in order,
|
|
366
|
+
at build time:
|
|
367
|
+
|
|
368
|
+
- Read: `required`, the type rules, `email`, `url`, `date`, `min`, `max`,
|
|
369
|
+
`size`, `between`, and `in` or `Rule::in()`, which draws a select.
|
|
370
|
+
- `required_if:topic,support` is read as `visible`: the field shows while
|
|
371
|
+
`topic` is `support`, or any of the values listed after it, and is required
|
|
372
|
+
then. In the request above, `order` shows only for support.
|
|
373
|
+
- Anything else stays the server's check alone, and its field a plain input:
|
|
374
|
+
`regex` (PCRE is not the browser's pattern), `Rule::enum()`, a closure.
|
|
375
|
+
- `rules()` runs with no request behind it, so one that reads the request
|
|
376
|
+
cannot be described. List those fields in `fields`.
|
|
377
|
+
|
|
378
|
+
`rsc:install` does not set up Tailwind or shadcn, so the `shadcn` theme comes
|
|
379
|
+
out unstyled until the app has both.
|
|
292
380
|
|
|
293
381
|
### Go
|
|
294
382
|
|
|
295
383
|
Register an action that takes the values as a struct. Its `json` tags are the
|
|
296
|
-
form's field names, and `rsckit.InvalidField` or `rsckit.Invalid` is the 422
|
|
384
|
+
form's field names, and `rsckit.InvalidField` or `rsckit.Invalid` is the 422:
|
|
297
385
|
|
|
298
386
|
```go
|
|
299
387
|
type ContactInput struct {
|
|
300
|
-
Name string `json:"name"`
|
|
301
|
-
Email string `json:"email"`
|
|
302
|
-
|
|
388
|
+
Name string `json:"name" validate:"required,min=2"`
|
|
389
|
+
Email string `json:"email" validate:"required,email"`
|
|
390
|
+
Topic string `json:"topic" validate:"required,oneof=sales support"`
|
|
391
|
+
Message string `json:"message" validate:"required,min=10,max=2000"`
|
|
303
392
|
}
|
|
304
393
|
|
|
305
|
-
reg.HandleAction("
|
|
394
|
+
reg.HandleAction("Contact.send", func(ctx context.Context, in ContactInput) error {
|
|
306
395
|
if blocked(in.Email) {
|
|
307
396
|
return rsckit.InvalidField("email", "That address is blocked.")
|
|
308
397
|
}
|
|
@@ -321,24 +410,17 @@ return rsckit.Invalid(map[string][]string{
|
|
|
321
410
|
```
|
|
322
411
|
|
|
323
412
|
`reg.WriteManifest("rsc-host.json")` puts it in the manifest, and the build
|
|
324
|
-
generates `
|
|
413
|
+
generates `send` in `server-actions/contact` from it, as for any action.
|
|
325
414
|
|
|
326
|
-
The form's fields are the struct's, in its order
|
|
327
|
-
its `json` tag says `omitempty`. A `validate` tag, in go-playground/validator's
|
|
328
|
-
spelling, adds what the browser can check: `required`, `omitempty`, `email`,
|
|
329
|
-
`url`, `min`, `max`, `len`, `gte`, `lte`, `oneof` (a select) and
|
|
330
|
-
`datetime=2006-01-02`. `required_if=Topic support` is read as `visible`: the
|
|
331
|
-
field shows while `topic` is `support`, and is required then. Only one field and
|
|
332
|
-
one value - several pairs must all hold, which a form cannot show as one.
|
|
415
|
+
**How the fields are read.** The form's fields are the struct's, in its order:
|
|
333
416
|
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
```
|
|
417
|
+
- A field is required unless its `json` tag says `omitempty`.
|
|
418
|
+
- A `validate` tag, in go-playground/validator's spelling, adds what the
|
|
419
|
+
browser can check: `required`, `omitempty`, `email`, `url`, `min`, `max`,
|
|
420
|
+
`len`, `gte`, `lte`, `oneof` (a select) and `datetime=2006-01-02`.
|
|
421
|
+
- `required_if=Topic support` is read as `visible`: the field shows while
|
|
422
|
+
`topic` is `support`, and is required then. Only one field and one value -
|
|
423
|
+
several pairs must all hold, which a form cannot show as one.
|
|
342
424
|
|
|
343
425
|
**The tag is only read, never run.** `rsc-kit/go` depends on no validator, so
|
|
344
426
|
the function still checks what it was sent, with validator or by hand: a post
|
|
@@ -346,43 +428,94 @@ need not come from a browser.
|
|
|
346
428
|
|
|
347
429
|
### On a kept page
|
|
348
430
|
|
|
349
|
-
A blueprint's form can sit in an [ISR](/guides/isr) page
|
|
350
|
-
everyone, and the post is the visitor's own: it needs no token, because the
|
|
351
|
-
engine and Laravel 13 accept a same-origin post by its `Sec-Fetch-Site` header.
|
|
352
|
-
The call to the backend runs in that post, so it carries the visitor's cookie,
|
|
353
|
-
unlike the calls the kept page made while rendering.
|
|
431
|
+
A blueprint's form can sit in an [ISR](/guides/isr) page:
|
|
354
432
|
|
|
355
|
-
|
|
433
|
+
```tsx title="resources/js/app/contact/page.tsx"
|
|
434
|
+
import { contact } from '../../forms/contact'
|
|
435
|
+
|
|
436
|
+
export const revalidate = 3600
|
|
437
|
+
|
|
438
|
+
export default function ContactPage() {
|
|
439
|
+
return <contact.Form />
|
|
440
|
+
}
|
|
441
|
+
```
|
|
442
|
+
|
|
443
|
+
The page is kept for everyone, and the post is the visitor's own. It needs no
|
|
444
|
+
token, because the engine and Laravel 13 accept a same-origin post by its
|
|
445
|
+
`Sec-Fetch-Site` header. The call to the backend runs in that post, so it
|
|
446
|
+
carries the visitor's cookie, unlike the calls the kept page made while
|
|
447
|
+
rendering.
|
|
448
|
+
|
|
449
|
+
## Stopping bots
|
|
356
450
|
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
451
|
+
Every form carries a [`<Honeypot />`](/guides/forms/#stopping-bots): a field
|
|
452
|
+
people never see. A bot filling in every input fills that one too, and the
|
|
453
|
+
submission is answered as though it worked and dropped: `handle` never sees
|
|
454
|
+
it, and the bot learns nothing.
|
|
360
455
|
|
|
361
|
-
|
|
456
|
+
It is a filter for the crudest spam, not a CAPTCHA. A form worth attacking
|
|
457
|
+
wants rate limiting as well. A backend is told the visitor's address on every
|
|
458
|
+
call, so a throttle on the action limits each visitor on their own:
|
|
459
|
+
|
|
460
|
+
```php title="app/Rsc/Actions/Contact.php"
|
|
461
|
+
use Illuminate\Routing\Attributes\Controllers\Middleware;
|
|
362
462
|
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
463
|
+
#[Middleware('throttle:5,1')]
|
|
464
|
+
class Contact
|
|
465
|
+
{
|
|
466
|
+
public function send(SendContact $request): void { … }
|
|
467
|
+
}
|
|
468
|
+
```
|
|
369
469
|
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
balancer. Limiting at the CDN as well spares the backend the work.
|
|
470
|
+
- The address is `$request->ip()` in Laravel, and `rsckit.ClientIP(ctx)` in Go.
|
|
471
|
+
- Behind a load balancer or Cloudflare, set `RSC_TRUST_FORWARDED=1` on the
|
|
472
|
+
renderer, or every visitor arrives as the balancer.
|
|
473
|
+
- Limiting at the CDN as well spares the backend the work.
|
|
375
474
|
|
|
376
|
-
The trap is named `homepage`, so a
|
|
475
|
+
The trap is named `homepage`, so a blueprint with the trap cannot have a field
|
|
476
|
+
of that name. Set `honeypot: false` to use one.
|
|
377
477
|
|
|
378
478
|
## Drawing it yourself
|
|
379
479
|
|
|
380
480
|
The theme's part is one component, `BlueprintForm`, typed `FormsTheme` from
|
|
381
|
-
`@rsc-kit/press/forms`.
|
|
382
|
-
starting `values`, the `submit` words, `done`, and the `honeypot` field's
|
|
383
|
-
name to render hidden - absent when the blueprint turned it off. Replace it
|
|
384
|
-
as you would a panel page:
|
|
481
|
+
`@rsc-kit/press/forms`. Replace it as you would a panel page:
|
|
385
482
|
|
|
386
483
|
```ts
|
|
387
484
|
theme: { ...shadcn, BlueprintForm: MyForm },
|
|
388
485
|
```
|
|
486
|
+
|
|
487
|
+
It may be a client component, and it is given these props, all data, a bound
|
|
488
|
+
action or a string:
|
|
489
|
+
|
|
490
|
+
| Prop | What it is |
|
|
491
|
+
| --- | --- |
|
|
492
|
+
| `action` | The bound server action. A refusal is a 422 whose messages belong under their fields. |
|
|
493
|
+
| `fields` | The fields to draw, with their types, options and browser constraints. |
|
|
494
|
+
| `values` | What each field starts as: the record being edited, or nothing. |
|
|
495
|
+
| `submit` | The submit button's words. |
|
|
496
|
+
| `done` | What to show once it is sent. Absent when the form redirects. |
|
|
497
|
+
| `honeypot` | The bot trap's field name: render `<Honeypot name={honeypot} />`. Absent when the blueprint turned it off. |
|
|
498
|
+
|
|
499
|
+
```tsx title="src/forms/MyForm.tsx"
|
|
500
|
+
"use client";
|
|
501
|
+
import type { FormProps } from '@rsc-kit/press/forms'
|
|
502
|
+
import { Honeypot } from '@rsc-kit/core/honeypot'
|
|
503
|
+
|
|
504
|
+
export function MyForm({ action, fields, values, submit, honeypot }: FormProps) {
|
|
505
|
+
return (
|
|
506
|
+
<form action={async (form) => { await action(form) }}>
|
|
507
|
+
{fields.map((field) => (
|
|
508
|
+
<label key={field.name}>
|
|
509
|
+
{field.label}
|
|
510
|
+
<input name={field.name} required={field.required} defaultValue={String(values[field.name] ?? '')} />
|
|
511
|
+
</label>
|
|
512
|
+
))}
|
|
513
|
+
{honeypot && <Honeypot name={honeypot} />}
|
|
514
|
+
<button>{submit}</button>
|
|
515
|
+
</form>
|
|
516
|
+
)
|
|
517
|
+
}
|
|
518
|
+
```
|
|
519
|
+
|
|
520
|
+
This sketch leaves out showing refusals and `done`. The `shadcn` theme's form
|
|
521
|
+
is the complete one to start from.
|