@rsc-kit/mcp 0.41.0 → 0.42.1
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 -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 +270 -216
- package/guides/queries.md +171 -300
- package/guides/react-compiler.md +90 -50
- package/guides/redirects.md +141 -177
- package/guides/response-headers.md +76 -74
- package/guides/route-interception.md +112 -111
- package/guides/routing.md +216 -322
- package/guides/sections.md +63 -66
- package/guides/seo-files.md +127 -65
- package/guides/server-actions.md +285 -357
- package/guides/static-generation.md +131 -199
- package/guides/testing.md +269 -147
- package/guides/third-party-scripts.md +94 -47
- package/guides/typed-routes.md +115 -113
- package/guides/url-validation.md +113 -61
- package/guides/validation.md +53 -62
- package/guides/view-transitions.md +159 -132
- package/guides/where-it-runs.md +226 -162
- package/package.json +2 -2
- package/guides/admin.md +0 -520
- package/guides/deployment.md +0 -598
- package/guides/go.md +0 -539
- package/guides/laravel.md +0 -787
- package/guides/your-own-backend.md +0 -602
package/guides/url-validation.md
CHANGED
|
@@ -2,11 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
> Checking and typing params, query strings and request bodies with a schema you already write.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
something stopped it.
|
|
8
|
-
|
|
9
|
-
Export a schema beside the page and it stops there instead:
|
|
5
|
+
Export a schema beside a page, and its `params` and `searchParams` arrive
|
|
6
|
+
checked and typed:
|
|
10
7
|
|
|
11
8
|
```tsx title="src/app/posts/[slug]/page.tsx"
|
|
12
9
|
import { z } from 'zod'
|
|
@@ -30,37 +27,66 @@ export default async function PostPage({ params: p, searchParams: q }: PageProps
|
|
|
30
27
|
}
|
|
31
28
|
```
|
|
32
29
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
works — Zod, Valibot, ArkType — because the schema is asked to validate itself
|
|
36
|
-
and nothing here imports one.
|
|
37
|
-
|
|
38
|
-
Three things you get that types alone cannot give you:
|
|
30
|
+
A URL is input from a stranger. Without a schema, `?page=banana` reaches your
|
|
31
|
+
database as `NaN`. With one:
|
|
39
32
|
|
|
40
33
|
- **`?page=3` arrives as `3`**, the number, not `"3"`.
|
|
41
|
-
- **A missing value arrives as its default**, so there
|
|
34
|
+
- **A missing value arrives as its default**, so there's no `undefined` branch
|
|
42
35
|
to write.
|
|
43
|
-
- **A bad value is refused once**, at the edge, instead of
|
|
44
|
-
|
|
36
|
+
- **A bad value is refused once**, at the edge, instead of travelling on into
|
|
37
|
+
whatever the page does next.
|
|
38
|
+
|
|
39
|
+
Any [Standard Schema](https://standardschema.dev) works: Zod, Valibot or
|
|
40
|
+
ArkType. The schema is asked to validate itself, and nothing here imports one.
|
|
41
|
+
Both are ordinary exports, read by the build the same way `metadata` and
|
|
42
|
+
`generateStaticParams` are.
|
|
43
|
+
|
|
44
|
+
Export neither and nothing changes: `params` is a record of strings, and
|
|
45
|
+
`searchParams` is a `URLSearchParams`.
|
|
46
|
+
|
|
47
|
+
## Validating a query string
|
|
48
|
+
|
|
49
|
+
```tsx title="src/app/products/page.tsx"
|
|
50
|
+
export const searchParams = z.object({
|
|
51
|
+
q: z.string().default(''),
|
|
52
|
+
sort: z.enum(['price', 'newest']).default('newest'),
|
|
53
|
+
page: z.coerce.number().int().min(1).default(1),
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
export default async function Products({ searchParams: q }: PageProps<never, typeof searchParams>) {
|
|
57
|
+
const { q: query, sort, page } = await q
|
|
58
|
+
|
|
59
|
+
return <ProductList query={query} sort={sort} page={page} />
|
|
60
|
+
}
|
|
61
|
+
```
|
|
45
62
|
|
|
46
|
-
|
|
47
|
-
|
|
63
|
+
`/products?sort=cheapest` is refused, and `/products` gets the defaults. Links
|
|
64
|
+
to the page are typed from the same schema; see [Typed
|
|
65
|
+
routes](/guides/typed-routes#typed-search-params).
|
|
48
66
|
|
|
49
|
-
|
|
67
|
+
### Repeated keys
|
|
50
68
|
|
|
51
|
-
|
|
69
|
+
`?tag=red&tag=blue` arrives as an array, and `?q=shoes` as a string. The
|
|
70
|
+
schema doesn't need to know which shape the URL happened to take, so
|
|
71
|
+
`z.array(z.string())` and `z.string()` both work on the key you'd expect.
|
|
52
72
|
|
|
53
|
-
|
|
73
|
+
## What happens when a value is wrong
|
|
74
|
+
|
|
75
|
+
A bad `params` and a bad `searchParams` fail differently:
|
|
76
|
+
|
|
77
|
+
| What was wrong | Answer | Why |
|
|
54
78
|
| --- | --- | --- |
|
|
55
|
-
| `params` | **404**, `not-found.tsx` | the
|
|
79
|
+
| `params` | **404**, `not-found.tsx` | the URL doesn't describe a page |
|
|
56
80
|
| `searchParams` | nearest `error.tsx` | the page exists, the query was wrong |
|
|
57
81
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
82
|
+
A slug your schema refuses isn't a broken page, it's an absent one. A `500`
|
|
83
|
+
tells a crawler to come back later, while a `404` tells it the page is gone.
|
|
84
|
+
Refusing a bad `?page=` as a 404, on the other hand, would let one bad link
|
|
85
|
+
make a real page look deleted.
|
|
86
|
+
|
|
87
|
+
### Saying which field was wrong
|
|
62
88
|
|
|
63
|
-
The error carries the fields, so a boundary can
|
|
89
|
+
The error carries the fields, so a boundary can name them:
|
|
64
90
|
|
|
65
91
|
```tsx title="src/app/posts/[slug]/error.tsx"
|
|
66
92
|
'use client'
|
|
@@ -76,15 +102,9 @@ export default function Error({ error }) {
|
|
|
76
102
|
}
|
|
77
103
|
```
|
|
78
104
|
|
|
79
|
-
##
|
|
80
|
-
|
|
81
|
-
`?tag=red&tag=blue` arrives as an array and `?q=shoes` as a string, without the
|
|
82
|
-
schema having to know which shape the url happened to take. So
|
|
83
|
-
`z.array(z.string())` and `z.string()` both work on the key you would expect.
|
|
84
|
-
|
|
85
|
-
## API routes take the same three
|
|
105
|
+
## Validating an API route
|
|
86
106
|
|
|
87
|
-
|
|
107
|
+
A `route.ts` takes the same `params` and `searchParams`, plus `body`:
|
|
88
108
|
|
|
89
109
|
```ts title="src/app/api/posts/[id]/route.ts"
|
|
90
110
|
import { z } from 'zod'
|
|
@@ -105,47 +125,79 @@ export async function POST(
|
|
|
105
125
|
}
|
|
106
126
|
```
|
|
107
127
|
|
|
108
|
-
`RouteContext` is `PageProps` for a route
|
|
109
|
-
promise
|
|
110
|
-
the segments.
|
|
111
|
-
route that never awaits `searchParams` provably does not vary by it, so the
|
|
112
|
-
build can store one answer and serve it for `?utm_source=anything`. Resolved
|
|
113
|
-
eagerly, that fact is unknowable and every tracking link misses the stored
|
|
114
|
-
answer.
|
|
115
|
-
|
|
116
|
-
The handler still takes a real `Request` and still returns a real `Response` —
|
|
117
|
-
the schemas add a second argument and take nothing away. A route that exports
|
|
118
|
-
none behaves exactly as it did before any of this existed.
|
|
128
|
+
`RouteContext` is `PageProps` for a route: each schema's output behind a
|
|
129
|
+
promise. With no schemas, `RouteContext<'/api/posts/[id]'>` types `params`
|
|
130
|
+
from the segments.
|
|
119
131
|
|
|
120
|
-
|
|
132
|
+
The handler still takes a real `Request` and returns a real `Response`. The
|
|
133
|
+
schemas add a second argument and take nothing away. A route that exports none
|
|
134
|
+
behaves exactly as before.
|
|
121
135
|
|
|
122
|
-
|
|
|
136
|
+
| What was wrong | Answer |
|
|
123
137
|
| --- | --- |
|
|
124
138
|
| `params` | `404` |
|
|
125
139
|
| `searchParams` | `400` with `{ message, errors }` |
|
|
126
140
|
| `body` | `422` with `{ message, errors }` |
|
|
127
141
|
|
|
128
|
-
`422`
|
|
129
|
-
|
|
130
|
-
|
|
142
|
+
`422` is what a [server action](/guides/validation/) already returns for a
|
|
143
|
+
refused field, so a client has one shape to handle rather than two.
|
|
144
|
+
|
|
145
|
+
### JSON and forms
|
|
146
|
+
|
|
147
|
+
Both encodings parse, since a URL is posted to by `fetch` and by `<form>` alike:
|
|
148
|
+
|
|
149
|
+
```ts
|
|
150
|
+
await fetch('/api/posts/1', {
|
|
151
|
+
method: 'POST',
|
|
152
|
+
headers: { 'Content-Type': 'application/json' },
|
|
153
|
+
body: JSON.stringify({ title: 'Hello' }),
|
|
154
|
+
})
|
|
155
|
+
```
|
|
131
156
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
157
|
+
```html
|
|
158
|
+
<form method="post" action="/api/posts/1">
|
|
159
|
+
<input name="title" />
|
|
160
|
+
</form>
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
A body that isn't valid JSON is a `422` about the body as a whole, not a crash.
|
|
135
164
|
|
|
136
165
|
:::caution[Exporting `body` reads the request]
|
|
137
166
|
The schema consumes the stream, so `await request.json()` inside the handler
|
|
138
|
-
|
|
167
|
+
finds it already read. Use the parsed value instead.
|
|
139
168
|
|
|
140
|
-
|
|
141
|
-
`DELETE`. A `GET` handler is never touched.
|
|
169
|
+
This happens only when you export a `body` schema, and only for `POST`, `PUT`,
|
|
170
|
+
`PATCH` and `DELETE`. A `GET` handler is never touched.
|
|
142
171
|
:::
|
|
143
172
|
|
|
144
|
-
##
|
|
173
|
+
## A schema doesn't check who is asking
|
|
174
|
+
|
|
175
|
+
A schema checks the **shape** of a request, not whether the person making it
|
|
176
|
+
may. `params.id` being a number says nothing about whose row it is. The caller
|
|
177
|
+
chooses the id, and trusting it is the whole of an IDOR.
|
|
145
178
|
|
|
146
|
-
|
|
147
|
-
it may. `params.id` being a number says nothing about whose row it is — the
|
|
148
|
-
caller chooses the id, and trusting it is the whole of an IDOR.
|
|
179
|
+
Check access on identity, in the handler, once the shape is known good:
|
|
149
180
|
|
|
150
|
-
|
|
151
|
-
|
|
181
|
+
```ts title="src/app/api/orders/[id]/route.ts"
|
|
182
|
+
export async function GET(request: Request, { params }: RouteContext<'/api/orders/[id]'>) {
|
|
183
|
+
const { id } = await params
|
|
184
|
+
const order = await db.orders.find(id)
|
|
185
|
+
|
|
186
|
+
if (order.userId !== (await currentUser()).id) {
|
|
187
|
+
return new Response(null, { status: 404 })
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return Response.json(order)
|
|
191
|
+
}
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
See [Authorization](/guides/authorization/).
|
|
195
|
+
|
|
196
|
+
## Why the props are promises
|
|
197
|
+
|
|
198
|
+
A page or route awaits `params` and `searchParams` rather than receiving them
|
|
199
|
+
resolved. That isn't only for symmetry. A route that never awaits
|
|
200
|
+
`searchParams` provably doesn't vary by it, so the build can store one answer
|
|
201
|
+
and serve it for `?utm_source=anything`. If they were resolved eagerly, the
|
|
202
|
+
build couldn't know that, and every tracking link would miss the stored
|
|
203
|
+
answer.
|
package/guides/validation.md
CHANGED
|
@@ -1,19 +1,18 @@
|
|
|
1
1
|
# Validation
|
|
2
2
|
|
|
3
|
-
>
|
|
3
|
+
> Checking input before submit, and showing the server's validation errors in a form.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
A validation error is a field name mapped to its messages, wherever it came
|
|
6
|
+
from:
|
|
7
7
|
|
|
8
8
|
```ts
|
|
9
9
|
{ title: ['The title must be at least 3 characters.'], body: ['The body is required.'] }
|
|
10
10
|
```
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
validation ran.
|
|
12
|
+
`<Form>` shows these under its fields, whether they came from a schema in the
|
|
13
|
+
browser or from the server.
|
|
15
14
|
|
|
16
|
-
##
|
|
15
|
+
## Showing errors in a form
|
|
17
16
|
|
|
18
17
|
```tsx title="src/components/PostForm.tsx"
|
|
19
18
|
"use client";
|
|
@@ -40,41 +39,13 @@ export function PostForm() {
|
|
|
40
39
|
}
|
|
41
40
|
```
|
|
42
41
|
|
|
43
|
-
|
|
44
|
-
`errors
|
|
42
|
+
There's no `try`/`catch` and no state of your own. A failed submit fills
|
|
43
|
+
`errors`, and a successful one clears them.
|
|
45
44
|
|
|
46
|
-
##
|
|
45
|
+
## Checking before submit
|
|
47
46
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
```tsx
|
|
51
|
-
"use client";
|
|
52
|
-
|
|
53
|
-
import { ServerValidationError } from '@rsc-kit/core/errors';
|
|
54
|
-
import { createPost } from '../actions';
|
|
55
|
-
|
|
56
|
-
try {
|
|
57
|
-
await createPost(title, body);
|
|
58
|
-
} catch (error) {
|
|
59
|
-
if (error instanceof ServerValidationError) {
|
|
60
|
-
setErrors(error.errors);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
## Producing one
|
|
66
|
-
|
|
67
|
-
<Aside type="caution" title="The server half is the host's">
|
|
68
|
-
There is no validation library here, and no rule syntax to learn. What the
|
|
69
|
-
engine defines is the error the client understands — deciding that input is
|
|
70
|
-
invalid is your code's job, or your host's.
|
|
71
|
-
</Aside>
|
|
72
|
-
|
|
73
|
-
### Before it is submitted
|
|
74
|
-
|
|
75
|
-
`<Form>` takes any [Standard Schema](https://standardschema.dev) —
|
|
76
|
-
Zod, Valibot, ArkType, or anything else implementing it. There is no adapter
|
|
77
|
-
per library and no import of one: the schema is asked to validate itself.
|
|
47
|
+
Pass `<Form>` a schema. Any [Standard Schema](https://standardschema.dev)
|
|
48
|
+
works - Zod, Valibot, ArkType - with no adapter:
|
|
78
49
|
|
|
79
50
|
```tsx title="src/components/PostForm.tsx"
|
|
80
51
|
"use client";
|
|
@@ -107,30 +78,26 @@ export function PostForm() {
|
|
|
107
78
|
}
|
|
108
79
|
```
|
|
109
80
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
81
|
+
When the schema fails, the errors appear the same way and the action is never
|
|
82
|
+
called. That saves a round trip, and an optimistic update never shows a row the
|
|
83
|
+
server would refuse. Async schemas are awaited, so a uniqueness check works
|
|
84
|
+
too.
|
|
114
85
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
dropped.
|
|
86
|
+
- A nested field is named by its path: `address.city`.
|
|
87
|
+
- An error that belongs to no field, such as "these must match", is under the
|
|
88
|
+
empty string `''`.
|
|
119
89
|
|
|
120
|
-
<Aside type="danger" title="
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
Use the same schema on both sides if you like; the point is that the one
|
|
126
|
-
running on the server is the one that decides.
|
|
90
|
+
<Aside type="danger" title="The server still has to check">
|
|
91
|
+
A schema in the browser is a convenience, not a control. The action is a
|
|
92
|
+
public endpoint anyone can call without this form, so the server must
|
|
93
|
+
validate too. Using the same schema on both sides is fine. See
|
|
94
|
+
[Authorization](/guides/authorization).
|
|
127
95
|
</Aside>
|
|
128
96
|
|
|
129
|
-
|
|
97
|
+
## Validating in a server action
|
|
130
98
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
already understands:
|
|
99
|
+
Validate with the same schema in the action, and **return** the failure rather
|
|
100
|
+
than throwing it:
|
|
134
101
|
|
|
135
102
|
```ts title="src/actions.ts"
|
|
136
103
|
'use server'
|
|
@@ -148,6 +115,9 @@ export async function createPost(title: string, body: string) {
|
|
|
148
115
|
}
|
|
149
116
|
```
|
|
150
117
|
|
|
118
|
+
Then turn the issues into the error `<Form>` understands with
|
|
119
|
+
`issuesToErrors`:
|
|
120
|
+
|
|
151
121
|
```tsx title="src/components/PostForm.tsx"
|
|
152
122
|
import { ServerValidationError } from '@rsc-kit/core/errors';
|
|
153
123
|
import { issuesToErrors } from '@rsc-kit/core/standardSchema';
|
|
@@ -164,10 +134,31 @@ async function submit(formData: FormData) {
|
|
|
164
134
|
}
|
|
165
135
|
```
|
|
166
136
|
|
|
167
|
-
`issuesToErrors` is the
|
|
168
|
-
|
|
137
|
+
`issuesToErrors` is the mapping the browser-side check uses, so field names
|
|
138
|
+
match whichever side found the error.
|
|
139
|
+
|
|
140
|
+
rsc-kit has no validation rules of its own. Deciding what is invalid is your
|
|
141
|
+
code's job, or your backend's: a Laravel `FormRequest`'s 422 arrives as the
|
|
142
|
+
same errors.
|
|
143
|
+
|
|
144
|
+
## Catching errors outside a form
|
|
145
|
+
|
|
146
|
+
Anywhere else, catch `ServerValidationError`:
|
|
169
147
|
|
|
170
|
-
|
|
148
|
+
```tsx
|
|
149
|
+
"use client";
|
|
150
|
+
|
|
151
|
+
import { ServerValidationError } from '@rsc-kit/core/errors';
|
|
152
|
+
import { createPost } from '../actions';
|
|
153
|
+
|
|
154
|
+
try {
|
|
155
|
+
await createPost(title, body);
|
|
156
|
+
} catch (error) {
|
|
157
|
+
if (error instanceof ServerValidationError) {
|
|
158
|
+
setErrors(error.errors);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
```
|
|
171
162
|
|
|
172
163
|
| Member | Type | What it holds |
|
|
173
164
|
| --- | --- | --- |
|