@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,12 +2,27 @@
2
2
 
3
3
  > Protecting pages, server actions and API routes.
4
4
 
5
- There are three ways into your app — a page, a server action, an API route —
6
- and each needs its own check. Guarding one does not guard the others.
5
+ There are three ways into your app: a page, a server action and an API route.
6
+ Each needs its own check, and guarding one doesn't guard the others.
7
+
8
+ ```ts title="src/app/admin/middleware.ts"
9
+ import { redirect } from '@rsc-kit/core/redirect';
10
+
11
+ export default async function middleware() {
12
+ if (!(await currentUser())?.isAdmin) redirect('/login');
13
+ }
14
+ ```
15
+
16
+ | Question | Where it goes |
17
+ | --- | --- |
18
+ | May this person see this part of the site? | `middleware.ts` in its folder |
19
+ | May this caller run this action? | Inside the action |
20
+ | May this caller use this endpoint? | Inside the route handler, or a `middleware.ts` above it |
21
+ | Should the page show different things to different people? | The page, which renders per request anyway |
7
22
 
8
23
  ## Protect a page
9
24
 
10
- Put a `middleware.ts` in the directory you want to protect:
25
+ Put a `middleware.ts` in the folder you want to protect:
11
26
 
12
27
  ```ts title="src/app/admin/middleware.ts"
13
28
  import { redirect } from '@rsc-kit/core/redirect';
@@ -20,16 +35,16 @@ export default async function middleware() {
20
35
  }
21
36
  ```
22
37
 
23
- It runs before anything in that directory or below it renders. Return nothing
24
- to allow; redirect or throw to refuse.
38
+ It runs before anything in that folder or below it renders. Return nothing to
39
+ allow the request; redirect or throw to refuse it.
40
+
41
+ Middleware files nest like layouts, outermost first, and **every** request runs
42
+ the whole chain: a full page load, a navigation, a prefetch, a revalidation.
25
43
 
26
- Middleware compose up the tree like layouts — outermost first — and **every**
27
- request runs the whole chain: a full page load, a navigation, a prefetch, a
28
- revalidation. There is no flag to remember; the file is the declaration.
44
+ ### Several checks
29
45
 
30
- Several checks in one directory are a list, run in the order written and
31
- stopping at the first refusal — which is how a check written once is reused
32
- where it is needed:
46
+ Export a list to run several checks in order. The first refusal stops the
47
+ rest:
33
48
 
34
49
  ```ts title="src/app/admin/middleware.ts"
35
50
  import { signedIn, verified } from '../../guards';
@@ -44,13 +59,12 @@ export default [
44
59
  ];
45
60
  ```
46
61
 
47
- `src/app/middleware.ts` above it still runs first; the list is this
48
- directory's own part of the chain.
62
+ `src/app/middleware.ts` above it still runs first.
49
63
 
50
- A guard can also say the page is not there. `notFound()` in middleware.ts
51
- answers **404** with `not-found.tsx`, before anything is rendered — for an
52
- admin area that should not admit to existing, rather than sending strangers
53
- somewhere else:
64
+ ### Answering 404 instead
65
+
66
+ `notFound()` in a `middleware.ts` answers **404** with `not-found.tsx` before
67
+ anything renders. Use it for an area that shouldn't admit to existing:
54
68
 
55
69
  ```ts title="src/app/admin/middleware.ts"
56
70
  import { notFound } from '@rsc-kit/core/not-found';
@@ -60,29 +74,16 @@ export default async function adminsOnly() {
60
74
  }
61
75
  ```
62
76
 
63
- <Aside type="caution" title="Do not put the check in a layout">
64
- It looks like it works, and it does on a full page load. But a navigation
65
- tells the server which layouts the browser already has, and the server skips
66
- them — that is what makes navigation fast. A request can claim to have your
67
- layout, and then the check never runs.
68
-
69
- `middleware.ts` is never skipped.
77
+ <Aside type="caution" title="Don't put the check in a layout">
78
+ It works on a full page load. But a navigation tells the server which layouts
79
+ the browser already has, and the server skips them. A request can claim to
80
+ have your layout, and then the check never runs. `middleware.ts` is never
81
+ skipped.
70
82
  </Aside>
71
83
 
72
84
  ## Protect a server action
73
85
 
74
- An action is a public endpoint. Anyone can call it directly:
75
-
76
- ```bash
77
- curl -X POST /_rsc/action \
78
- -H 'X-RSC-Action: 0339292364be#placeOrder' \
79
- -H 'X-RSC-Content-Type: text/plain;charset=UTF-8' \
80
- --data-binary '["a rubber duck"]'
81
- ```
82
-
83
- That is not a hole to plug — an action *is* an RPC endpoint and its id is not a
84
- secret. It means the check goes **inside the action**, not in the component that
85
- renders the button:
86
+ Check inside the action:
86
87
 
87
88
  ```ts title="src/actions.ts"
88
89
  'use server'
@@ -98,16 +99,24 @@ export async function placeOrder(item: string) {
98
99
  }
99
100
  ```
100
101
 
102
+ An action is a public endpoint, and its id isn't a secret. Anyone can call it
103
+ without your page:
104
+
105
+ ```bash
106
+ curl -X POST /_rsc/action \
107
+ -H 'X-RSC-Action: 0339292364be#placeOrder' \
108
+ -H 'X-RSC-Content-Type: text/plain;charset=UTF-8' \
109
+ --data-binary '["a rubber duck"]'
110
+ ```
111
+
101
112
  <Aside type="caution" title="Middleware does not cover actions">
102
- Middleware runs when a route renders. An action renders no route, so none of
103
- them run. Each entry point defends itself.
113
+ `middleware.ts` runs when a route renders. An action renders no route, so
114
+ none of it runs.
104
115
  </Aside>
105
116
 
106
- ### Write the check once
117
+ ### Writing the check once
107
118
 
108
- One action is fine. Twenty is twenty chances to forget, and the one you forget
109
- is the one that matters. Put the check on a client and build every action from
110
- it:
119
+ Put the check on an action client, and build every action from it:
111
120
 
112
121
  ```ts title="src/server/client.ts"
113
122
  'use server'
@@ -136,14 +145,11 @@ export const placeOrder = client.input(schema).handler(async ({ input, ctx }) =>
136
145
  export const listOrders = client.query(async ({ ctx }) => orders.forUser(ctx.user.id))
137
146
  ```
138
147
 
139
- `ctx.user` is typed and non-null inside the handler, because the only way to get
140
- there was through the middleware that put it in. **An action cannot be added
141
- without the check** — not because a rule says so, but because there is no other
142
- constructor to reach for.
148
+ `ctx.user` is typed and never null inside the handler, and an action built
149
+ from `client` can't skip the check. `.handler()` makes an action and
150
+ `.query()` makes a [read](/guides/queries/); both run the same chain.
143
151
 
144
- `.handler()` makes an action, `.query()` makes a [read](/guides/queries/), and
145
- both run the same chain. Add a second `.use()` for a role check and it applies
146
- to everything built from that client:
152
+ Add another `.use()` for a role, and it applies to everything built from it:
147
153
 
148
154
  ```ts
149
155
  export const admin = client.use(async ({ ctx, next }) => {
@@ -153,14 +159,16 @@ export const admin = client.use(async ({ ctx, next }) => {
153
159
  })
154
160
  ```
155
161
 
156
- Authorise on **identity, not arguments**. `cancelOrder(id)` that trusts the id
157
- is the whole of an IDOR — the caller chooses the id, so the handler has to check
158
- the row belongs to `ctx.user`.
162
+ ### Checking the record, not only the user
163
+
164
+ Authorize on **identity, not arguments**. The caller chooses the id they send
165
+ to `cancelOrder(id)`, so the handler has to check that the order belongs to
166
+ `ctx.user`. Trusting the id is an IDOR.
159
167
 
160
168
  ## Protect an API route
161
169
 
162
- A [route](/guides/api-routes/) runs the same middleware a page in that directory
163
- would. Put it under a guarded path and it is guarded:
170
+ A [route](/guides/api-routes/) runs the same middleware a page in its folder
171
+ would:
164
172
 
165
173
  ```text
166
174
  src/app/admin/
@@ -169,9 +177,9 @@ src/app/admin/
169
177
  api/export/route.ts ← guarded too
170
178
  ```
171
179
 
172
- A refused route answers **401** or **403** rather than redirecting, and names
173
- the destination in `X-RSC-Redirect` if the middleware wanted one. A `fetch`
174
- would follow a redirect and hand back a login page as though it were your data.
180
+ A refused route answers **401** or **403** instead of redirecting, and names
181
+ the destination in `X-RSC-Redirect` if the middleware redirected. A `fetch`
182
+ would otherwise follow the redirect and return a login page as your data.
175
183
 
176
184
  For a route with no middleware above it, check inside the handler:
177
185
 
@@ -187,9 +195,9 @@ export async function GET(): Promise<Response> {
187
195
  }
188
196
  ```
189
197
 
190
- ## Read the session
198
+ ## Reading the session
191
199
 
192
- There is no request object. `headers()` and `cookies()` read the one in flight:
200
+ `headers()` and `cookies()` read the request in flight:
193
201
 
194
202
  ```ts title="src/app/[locale]/middleware.ts"
195
203
  import { redirect } from '@rsc-kit/core/redirect';
@@ -203,20 +211,17 @@ export default async function middleware() {
203
211
  }
204
212
  ```
205
213
 
206
- They work anywhere a request is in flight — middleware, server components,
207
- actions, API routes — so whatever you already use for locale, feature flags or
208
- tenants works inside a plain async function. `request()` gives you the whole
209
- `Request` for anything the two do not cover.
214
+ They work anywhere a request is in flight: middleware, server components,
215
+ actions and API routes. `request()` returns the whole `Request`.
210
216
 
211
- They are async for a reason worth knowing: at build time there is no request, so
212
- a read *suspends*. React freezes the shell above it and only the part that
213
- wanted a header renders per visitor. A synchronous read would force the whole
214
- page to re-render for everyone.
217
+ They're async because at build time there is no request: the read waits, the
218
+ rest of the page is stored, and only the part that read a header renders per
219
+ visitor.
215
220
 
216
- ### Ask once
221
+ ### Looking the user up once
217
222
 
218
- Middleware wants to know who you are; the layout wants their name; the page
219
- wants their permissions. Wrap the lookup in `cache()` and that is one query:
223
+ Middleware, the layout and the page all want the current user. Wrap the lookup
224
+ in `cache()` and it runs once per request:
220
225
 
221
226
  ```ts title="src/session.ts"
222
227
  import { cache } from '@rsc-kit/core/cache';
@@ -228,17 +233,13 @@ export const currentUser = cache(async () => {
228
233
  });
229
234
  ```
230
235
 
231
- The scope is one request. Two requests never see each other's answers, and
232
- nothing survives between them. Outside a request it just calls through, so
233
- shared code does not need to know where it is running.
236
+ The cache lasts one request, and two requests never see each other's answers.
237
+ Outside a request it just calls through.
234
238
 
235
- ### The page knows what the guard decided
239
+ ### Typing the signed-in user
236
240
 
237
- Middleware runs inside the same request, so a page under it that calls
238
- `currentUser()` gets the cached answer, not a second query — and because the
239
- guard redirected everyone with `null`, that answer is a user. TypeScript
240
- cannot see the guard, though: the type is still `User | null`. Put the
241
- guarantee in a helper both sides call:
241
+ A page under the guard gets the cached user, but its type is still
242
+ `User | null`. Put the guarantee in a helper both call:
242
243
 
243
244
  ```ts title="src/session.ts"
244
245
  import { redirect } from '@rsc-kit/core/redirect';
@@ -267,14 +268,13 @@ export default async function Account() {
267
268
  }
268
269
  ```
269
270
 
270
- `redirect()` returns `never`, which is what narrows the type. And a page
271
- rendered from somewhere the guard does not cover redirects too, instead of
272
- handing a `null` to a component.
271
+ `redirect()` returns `never`, which narrows the type. A page rendered somewhere
272
+ the guard doesn't cover redirects too, instead of passing `null` on.
273
273
 
274
- ## Set a cookie
274
+ ## Writing to the response
275
275
 
276
- Middleware runs before the response exists, which makes it the place to put a
277
- header or a cookie on it:
276
+ Middleware runs before the response exists, so it can set a header or a cookie
277
+ on it:
278
278
 
279
279
  ```ts title="src/app/account/middleware.ts"
280
280
  import { cookies, responseHeaders } from '@rsc-kit/core/request'
@@ -297,8 +297,7 @@ export default async function middleware() {
297
297
  }
298
298
  ```
299
299
 
300
- Actions can write too, which is the case that matters — signing someone in is a
301
- mutation that has to leave a cookie behind:
300
+ An action can set a cookie too, which is how signing in works:
302
301
 
303
302
  ```ts title="src/app/login/actions.ts"
304
303
  'use server';
@@ -318,43 +317,33 @@ export const login = action.input(credentials).handler(async ({ input }) => {
318
317
  });
319
318
  ```
320
319
 
321
- `get`, `set` and `delete` over the request in flight is the whole surface an
322
- auth library needs, so you can wire in whichever one you use.
320
+ `get`, `set` and `delete` on the request in flight are all an auth library
321
+ needs, so you can wire in whichever one you use.
323
322
 
324
323
  <Aside type="caution" title="A component is too late">
325
- By the time a page renders, the status and headers are already on the wire.
326
- Writing from a component throws and tells you to move it to middleware —
327
- rather than accepting the call and quietly dropping it.
324
+ By the time a page renders, the status and headers have been sent. Writing
325
+ from a component throws and tells you to move it to middleware.
328
326
  </Aside>
329
327
 
330
- ## A guarded page can still be frozen
328
+ ## Storing a guarded page
331
329
 
332
- Whether the content is the same for everyone, and whether *you* may see it, are
333
- different questions. The build answers the first; middleware answers the second,
334
- per request. So an internal page whose bytes never vary is frozen at build time
335
- and the middleware decides who gets the file:
330
+ Whether a page is the same for everyone and whether *you* may see it are
331
+ separate questions. The build answers the first, and middleware the second, on
332
+ every request. So a guarded page whose content never varies is still stored at
333
+ build time, and the middleware decides who gets it:
336
334
 
337
335
  | Route | Navigation |
338
336
  | --- | --- |
339
- | Guarded and frozen — check, then serve from disk | 6.7 ms |
340
- | Guarded, rendered on demand | 2626.7 ms |
341
- | Unguarded, frozen | 0.6 ms |
337
+ | Guarded and stored: check, then serve from disk | 6.7 ms |
338
+ | Guarded, rendered per request | 2626.7 ms |
339
+ | Unguarded, stored | 0.6 ms |
342
340
 
343
341
  The check runs before the file is read, so a refusal never touches it.
344
342
 
345
- <Aside type="caution" title="Two places this does not hold">
346
- **A static export cannot guard anything.** A static host serves files without
347
- running code, so exporting a guarded route publishes it to whoever asks.
343
+ <Aside type="caution" title="Two places this doesn't hold">
344
+ **A static export can't guard anything.** A static host serves files without
345
+ running code, so exporting a guarded route publishes it to anyone.
348
346
 
349
- **Do not put a guarded page in a shared cache.** Your origin runs the check; a
350
- CDN holding the response serves it to the next caller without asking.
347
+ **Don't put a guarded page in a shared cache.** Your server runs the check,
348
+ but a CDN holding the response serves it to the next caller without asking.
351
349
  </Aside>
352
-
353
- ## Where each check goes
354
-
355
- | Question | Where it goes |
356
- | --- | --- |
357
- | May this person see this section? | `middleware.ts` in its directory |
358
- | May this caller run this action? | Inside the action |
359
- | May this caller use this endpoint? | Inside the route handler |
360
- | Should the page show different things to different people? | The page — it renders per request anyway |