@rsc-kit/mcp 0.41.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 +12 -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 +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 +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/authorization.md
CHANGED
|
@@ -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
|
|
6
|
-
|
|
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
|
|
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
|
|
24
|
-
|
|
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
|
-
|
|
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
|
-
|
|
31
|
-
|
|
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
|
|
48
|
-
directory's own part of the chain.
|
|
62
|
+
`src/app/middleware.ts` above it still runs first.
|
|
49
63
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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="
|
|
64
|
-
It
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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
|
-
|
|
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
|
-
|
|
103
|
-
|
|
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
|
-
###
|
|
117
|
+
### Writing the check once
|
|
107
118
|
|
|
108
|
-
|
|
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
|
|
140
|
-
|
|
141
|
-
|
|
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
|
-
`.
|
|
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
|
-
|
|
157
|
-
|
|
158
|
-
the
|
|
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
|
|
163
|
-
would
|
|
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**
|
|
173
|
-
the destination in `X-RSC-Redirect` if the middleware
|
|
174
|
-
would follow
|
|
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
|
-
##
|
|
198
|
+
## Reading the session
|
|
191
199
|
|
|
192
|
-
|
|
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
|
|
207
|
-
actions
|
|
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
|
|
212
|
-
|
|
213
|
-
|
|
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
|
-
###
|
|
221
|
+
### Looking the user up once
|
|
217
222
|
|
|
218
|
-
Middleware
|
|
219
|
-
|
|
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
|
|
232
|
-
|
|
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
|
-
###
|
|
239
|
+
### Typing the signed-in user
|
|
236
240
|
|
|
237
|
-
|
|
238
|
-
`
|
|
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
|
|
271
|
-
|
|
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
|
-
##
|
|
274
|
+
## Writing to the response
|
|
275
275
|
|
|
276
|
-
Middleware runs before the response exists,
|
|
277
|
-
|
|
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
|
-
|
|
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`
|
|
322
|
-
|
|
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
|
|
326
|
-
|
|
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
|
-
##
|
|
328
|
+
## Storing a guarded page
|
|
331
329
|
|
|
332
|
-
Whether
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
and the middleware decides who gets
|
|
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
|
|
340
|
-
| Guarded, rendered
|
|
341
|
-
| Unguarded,
|
|
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
|
|
346
|
-
**A static export
|
|
347
|
-
running code, so exporting a guarded route publishes it to
|
|
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
|
-
**
|
|
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 |
|