@rsc-kit/mcp 0.40.0 → 0.42.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/dist/recipes.js +37 -20
  2. package/dist/recipes.js.map +1 -1
  3. package/guides/admin-plugins.md +206 -6
  4. package/guides/api-routes.md +159 -215
  5. package/guides/authorization.md +103 -114
  6. package/guides/backend-answered-pages.md +301 -250
  7. package/guides/bap-vs-inertia.md +67 -25
  8. package/guides/blueprints.md +307 -174
  9. package/guides/bun.md +42 -25
  10. package/guides/caching.md +46 -49
  11. package/guides/coming-from-inertia.md +5 -5
  12. package/guides/commands.md +251 -200
  13. package/guides/connection.md +50 -54
  14. package/guides/content-security-policy.md +133 -84
  15. package/guides/domains.md +190 -149
  16. package/guides/edge-caching.md +242 -268
  17. package/guides/emails.md +96 -45
  18. package/guides/errors.md +116 -137
  19. package/guides/feature-flags.md +100 -30
  20. package/guides/file-uploads.md +101 -26
  21. package/guides/fonts.md +139 -110
  22. package/guides/forms.md +520 -538
  23. package/guides/images.md +87 -28
  24. package/guides/index.json +13 -33
  25. package/guides/instrumentation.md +134 -95
  26. package/guides/isr.md +3 -3
  27. package/guides/live-data-in-production.md +695 -0
  28. package/guides/live-data.md +170 -839
  29. package/guides/mcp.md +83 -59
  30. package/guides/metadata.md +124 -179
  31. package/guides/navigation.md +118 -147
  32. package/guides/no-javascript.md +49 -57
  33. package/guides/offline.md +118 -153
  34. package/guides/openapi.md +100 -35
  35. package/guides/pages.md +160 -50
  36. package/guides/ppr.md +178 -256
  37. package/guides/pwa.md +251 -218
  38. package/guides/queries.md +171 -300
  39. package/guides/react-compiler.md +90 -50
  40. package/guides/redirects.md +141 -177
  41. package/guides/response-headers.md +76 -74
  42. package/guides/route-interception.md +112 -111
  43. package/guides/routing.md +216 -322
  44. package/guides/sections.md +63 -66
  45. package/guides/seo-files.md +127 -65
  46. package/guides/server-actions.md +285 -357
  47. package/guides/static-generation.md +131 -199
  48. package/guides/testing.md +272 -150
  49. package/guides/third-party-scripts.md +94 -47
  50. package/guides/typed-routes.md +115 -113
  51. package/guides/url-validation.md +113 -61
  52. package/guides/validation.md +53 -62
  53. package/guides/view-transitions.md +159 -132
  54. package/guides/where-it-runs.md +226 -162
  55. package/package.json +2 -2
  56. package/guides/admin.md +0 -520
  57. package/guides/deployment.md +0 -598
  58. package/guides/go.md +0 -538
  59. package/guides/laravel.md +0 -786
  60. package/guides/your-own-backend.md +0 -587
@@ -2,11 +2,8 @@
2
2
 
3
3
  > Checking and typing params, query strings and request bodies with a schema you already write.
4
4
 
5
- A url is input from a stranger. `params` arrive as strings, `searchParams` as
6
- more strings, and `?page=banana` reaches your database as `NaN` unless
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
- Both are ordinary exports, read by the build the same way `metadata` and
34
- `generateStaticParams` are. Any [Standard Schema](https://standardschema.dev)
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 is no `undefined` branch
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 surviving as `NaN`
44
- into whatever the page does next.
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
- Export neither and nothing changes: `params` is the record of strings it always
47
- was, and `searchParams` is a `URLSearchParams`.
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
- ## They fail differently, on purpose
67
+ ### Repeated keys
50
68
 
51
- This is the part worth reading twice.
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
- | what was wrong | answer | why |
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 url does not describe a page |
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
- `/posts/` with a slug your schema refuses is not a broken page, it is an
59
- absent one — and a `500` tells a crawler to come back later while a `404` tells
60
- it the thing is gone. Meanwhile refusing a bad `?page=` as a 404 would let one
61
- bad link make a real page look deleted.
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 say which one:
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
- ## Repeated keys
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
- Plus the body, which is the one that matters for a `POST`:
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 — each schema's output behind a
109
- promise, and with none, `RouteContext<'/api/posts/[id]'>` types `params` from
110
- the segments. Awaited, the same way a page awaits its props. That is not only symmetry: a
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
- Statuses follow the same reasoning as a page, plus one:
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
- | what was wrong | answer |
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` because that is what a [server action](/guides/validation/) already
129
- returns for a refused field, so a client has one shape to handle rather than
130
- two.
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
- JSON and form encodings both parse, since a url is posted to by `fetch` and by
133
- `<form>` alike. A body that is not valid JSON is a `422` about the body as a
134
- whole rather than a crash.
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
- will find it already read. Use the parsed value — that is the point of it.
167
+ finds it already read. Use the parsed value instead.
139
168
 
140
- Only when you export a `body` schema, and only for `POST`, `PUT`, `PATCH` and
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
- ## What this is not
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
- A schema here checks the **shape** of a request, not whether the person making
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
- Authorise on identity, in the handler, after the shape is known good. See
151
- [Authorization](/guides/authorization/).
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.
@@ -1,19 +1,18 @@
1
1
  # Validation
2
2
 
3
- > Surfacing server-side validation errors in a form.
3
+ > Checking input before submit, and showing the server's validation errors in a form.
4
4
 
5
- Validation errors have one shape everywhere: a field name mapped to the
6
- messages for it.
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
- `ServerValidationError` carries that shape, and `<Form>`
13
- catch it and put it in `errors`. Nothing in a component has to know where the
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
- ## In a form
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
- No `try`/`catch`, and no state of your own. A successful submit clears
44
- `errors`; a failed one fills them.
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
- ## Outside a form
45
+ ## Checking before submit
47
46
 
48
- Catch it yourself, wherever the call rejects with one:
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
- A failure fills the same `errors` you were already rendering and the action is
111
- never called — so a mistake costs no round trip, and an optimistic update never
112
- shows a row the server was going to refuse. Async schemas are awaited, so a
113
- uniqueness check works the same way.
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
- Field names follow the schema's paths: a nested field is `address.city`, and an
116
- issue belonging to no field — a cross-field rule like "these must match" — is
117
- under the empty string, so it has somewhere to be shown rather than being
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="A schema here is a courtesy, not a control">
121
- It runs in the browser. The action is a public endpoint reachable without
122
- this form — with `curl`, with the wrong values, in any order — so the server
123
- still has to check. See [Authorization](/guides/authorization).
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
- ### In a JavaScript action
97
+ ## Validating in a server action
130
98
 
131
- The same schema, on the side that decides. An action **returns** its failure
132
- rather than throwing one, so one line turns that into the error the form
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 same mapping the client-side check uses, so the field
168
- names match whichever side produced them.
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
- ## ServerValidationError
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
  | --- | --- | --- |