@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/connection.md
CHANGED
|
@@ -2,16 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
> Marking work that belongs to the visitor, not to the build.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
has to differ per visitor.
|
|
8
|
-
|
|
9
|
-
`connection()` marks that work.
|
|
5
|
+
The build renders every page once and stores what it can. `connection()` marks
|
|
6
|
+
the work it shouldn't do: a query whose database the build can't reach, or a
|
|
7
|
+
value that has to differ per visitor.
|
|
10
8
|
|
|
11
9
|
```tsx title="src/app/orders/page.tsx"
|
|
12
10
|
import { connection } from '@rsc-kit/core/request'
|
|
13
11
|
|
|
14
|
-
|
|
12
|
+
async function Orders() {
|
|
15
13
|
await connection()
|
|
16
14
|
|
|
17
15
|
const rows = await db.query('select * from orders')
|
|
@@ -20,33 +18,32 @@ export default async function OrdersPage() {
|
|
|
20
18
|
}
|
|
21
19
|
```
|
|
22
20
|
|
|
23
|
-
|
|
24
|
-
made, and the build needs no database.
|
|
25
|
-
and the component runs
|
|
21
|
+
During the build, `connection()` never resolves, so **nothing after it runs**:
|
|
22
|
+
the query isn't made, and the build needs no database. On a request it
|
|
23
|
+
resolves at once and the component runs as usual.
|
|
26
24
|
|
|
27
|
-
##
|
|
25
|
+
## Putting a boundary above it
|
|
28
26
|
|
|
29
|
-
|
|
27
|
+
Wrap the component that calls `connection()` in `<Suspense>`. The page around
|
|
28
|
+
it is stored, and the marked part arrives per request:
|
|
30
29
|
|
|
31
|
-
```tsx
|
|
32
|
-
|
|
30
|
+
```tsx title="src/app/orders/page.tsx"
|
|
31
|
+
import { Suspense } from 'react'
|
|
33
32
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
33
|
+
export default function OrdersPage() {
|
|
34
|
+
return (
|
|
35
|
+
<>
|
|
36
|
+
<h1>Orders</h1>
|
|
37
|
+
<Suspense fallback={<p>Loading orders…</p>}>
|
|
38
|
+
<Orders />
|
|
39
|
+
</Suspense>
|
|
40
|
+
</>
|
|
41
|
+
)
|
|
42
|
+
}
|
|
38
43
|
```
|
|
39
44
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
third calls are already covered by the first.
|
|
43
|
-
|
|
44
|
-
One call, as early as the work begins. That is the whole API.
|
|
45
|
-
|
|
46
|
-
## It needs a boundary above it
|
|
47
|
-
|
|
48
|
-
The build stores what it *can* paint. If `connection()` is reached before
|
|
49
|
-
anything has painted, there is nothing to store:
|
|
45
|
+
If `connection()` is reached before anything has painted, there is nothing to
|
|
46
|
+
store, and the build refuses the route and says which fix to make:
|
|
50
47
|
|
|
51
48
|
```tsx
|
|
52
49
|
export default async function Page() {
|
|
@@ -56,43 +53,42 @@ export default async function Page() {
|
|
|
56
53
|
}
|
|
57
54
|
```
|
|
58
55
|
|
|
59
|
-
|
|
60
|
-
|
|
56
|
+
A `loading.tsx` beside the page also works: it puts the whole page in one
|
|
57
|
+
boundary. See [Showing a loading state](/guides/routing#showing-a-loading-state).
|
|
58
|
+
|
|
59
|
+
## Calling it once
|
|
60
|
+
|
|
61
|
+
`connection()` is a barrier, not a wrapper. Everything after it in that
|
|
62
|
+
component belongs to the request, so one call is enough:
|
|
61
63
|
|
|
62
64
|
```tsx
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
<Orders /> {/* calls connection() inside */}
|
|
69
|
-
</Suspense>
|
|
70
|
-
</>
|
|
71
|
-
)
|
|
72
|
-
}
|
|
65
|
+
await connection()
|
|
66
|
+
|
|
67
|
+
const orders = await db.orders()
|
|
68
|
+
await connection() // does nothing
|
|
69
|
+
const customers = await db.customers()
|
|
73
70
|
```
|
|
74
71
|
|
|
75
|
-
|
|
76
|
-
Without either, the build refuses the route rather than storing a blank page,
|
|
77
|
-
and tells you which one to add.
|
|
72
|
+
Call it once, before the work begins.
|
|
78
73
|
|
|
79
|
-
## When you
|
|
74
|
+
## When you don't need it
|
|
80
75
|
|
|
81
|
-
A query the build **can** run, whose answer is the same for every visitor,
|
|
82
|
-
|
|
83
|
-
marking it per
|
|
76
|
+
A query the build **can** run, whose answer is the same for every visitor,
|
|
77
|
+
doesn't need it. Let the build store the answer: that is the point of
|
|
78
|
+
prerendering, and marking it per request gives every visitor a render they
|
|
79
|
+
didn't need.
|
|
84
80
|
|
|
85
|
-
Use
|
|
81
|
+
Use `connection()` when:
|
|
86
82
|
|
|
87
|
-
- the database or API
|
|
83
|
+
- the database or API isn't reachable from the build machine
|
|
88
84
|
- the value must differ per visitor
|
|
89
85
|
- the data changes faster than you deploy
|
|
90
86
|
|
|
91
|
-
|
|
87
|
+
A page that reads `params`, `searchParams`, `headers()` or `cookies()` already
|
|
88
|
+
waits for the request, and needs no `connection()` for that part.
|
|
92
89
|
|
|
93
|
-
|
|
94
|
-
to learn if you have used it.
|
|
90
|
+
## Coming from Next.js
|
|
95
91
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
92
|
+
It has the same name and behaviour as Next's `connection()`. You'll need it
|
|
93
|
+
less often here: the build decides how to store each route by rendering it, so
|
|
94
|
+
most dynamic pages are understood without a marker.
|
|
@@ -2,13 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
> A strict policy on every page, with the engine allowing its own inline code by hash or nonce.
|
|
4
4
|
|
|
5
|
-
A Content-Security-Policy that refuses inline scripts
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
where a page streams, the scripts React writes to move each finished part into
|
|
9
|
-
place. Those have to be allowed without allowing everything.
|
|
10
|
-
|
|
11
|
-
Name the policy, and the engine adds what each page's own inline code needs:
|
|
5
|
+
A Content-Security-Policy that refuses inline scripts stops injected markup
|
|
6
|
+
from turning into injected code. Turn it on in the plugin, and the engine
|
|
7
|
+
allows the inline code each page needs, by hash or by nonce:
|
|
12
8
|
|
|
13
9
|
```ts title="vite.config.ts"
|
|
14
10
|
rscKit({
|
|
@@ -16,14 +12,23 @@ rscKit({
|
|
|
16
12
|
})
|
|
17
13
|
```
|
|
18
14
|
|
|
19
|
-
|
|
15
|
+
That is a strict policy on every page, with no `'unsafe-inline'` for scripts.
|
|
20
16
|
|
|
21
|
-
|
|
17
|
+
## Turning it on
|
|
18
|
+
|
|
19
|
+
`{}` sends this default:
|
|
20
|
+
|
|
21
|
+
```text
|
|
22
22
|
default-src 'self'; base-uri 'self'; object-src 'none'; style-src-attr 'unsafe-inline'
|
|
23
23
|
```
|
|
24
24
|
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
Every page then adds a `script-src` and a `style-src` naming its own inline
|
|
26
|
+
code: the script that boots it, and the scripts React writes to put each
|
|
27
|
+
streamed part in place.
|
|
28
|
+
|
|
29
|
+
## Writing your own directives
|
|
30
|
+
|
|
31
|
+
Your directives replace the default:
|
|
27
32
|
|
|
28
33
|
```ts title="vite.config.ts"
|
|
29
34
|
rscKit({
|
|
@@ -41,37 +46,34 @@ rscKit({
|
|
|
41
46
|
})
|
|
42
47
|
```
|
|
43
48
|
|
|
44
|
-
A directive takes a list or a space-separated string
|
|
45
|
-
to send it as `Content-Security-Policy-Report-Only` while you find out what
|
|
46
|
-
it would refuse.
|
|
49
|
+
A directive takes a list or a space-separated string:
|
|
47
50
|
|
|
48
|
-
|
|
51
|
+
```ts
|
|
52
|
+
'img-src': "'self' data: https://images.example.com",
|
|
53
|
+
```
|
|
49
54
|
|
|
50
|
-
|
|
51
|
-
|
|
55
|
+
Keep `style-src-attr 'unsafe-inline'`. React's `style={{ ... }}` is a style
|
|
56
|
+
attribute, which no hash or nonce can allow. An attribute can't run code, and
|
|
57
|
+
without it every `style` prop is refused.
|
|
52
58
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
| [Kept](/guides/isr) by `revalidate` | the same, from the body that is kept | rendered with no nonce, because the copy is shared |
|
|
57
|
-
| A [shell](/guides/ppr) with its holes resumed | the shell's hashes, and a nonce for the holes | the holes are rendered for this request |
|
|
58
|
-
| Rendered per request | a nonce | it streams: its last script is not known when the header goes |
|
|
59
|
+
A directive that already has `'unsafe-inline'` is left as you wrote it, since
|
|
60
|
+
a hash or nonce beside it would switch it off. A policy with no `script-src`
|
|
61
|
+
and no `default-src` restricts no scripts, and isn't given one.
|
|
59
62
|
|
|
60
|
-
|
|
61
|
-
response no cache may share. React puts it on every script it writes, and on
|
|
62
|
-
the styles it hoists.
|
|
63
|
+
## Trying it before enforcing it
|
|
63
64
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
65
|
+
```ts title="vite.config.ts"
|
|
66
|
+
rscKit({
|
|
67
|
+
contentSecurityPolicy: { reportOnly: true },
|
|
68
|
+
})
|
|
69
|
+
```
|
|
67
70
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
`default-src` restricts no scripts, and is not given one.
|
|
71
|
+
The policy goes out as `Content-Security-Policy-Report-Only`, so the browser
|
|
72
|
+
reports what it would refuse and runs it anyway.
|
|
71
73
|
|
|
72
74
|
## Your own inline code
|
|
73
75
|
|
|
74
|
-
The engine allows the inline code it writes.
|
|
76
|
+
The engine allows the inline code it writes. For inline code you write:
|
|
75
77
|
|
|
76
78
|
- **A `<script>` or `<style>` in a page the build stores**, a shell included,
|
|
77
79
|
is hashed with everything else, and runs.
|
|
@@ -82,20 +84,41 @@ The engine allows the inline code it writes. Inline code you write:
|
|
|
82
84
|
- **One brought by a navigation** is refused on every page: the browser
|
|
83
85
|
inserts it into a document whose policy was decided before it existed.
|
|
84
86
|
|
|
85
|
-
So write CSS in a stylesheet and code in a module, which `'self'` allows
|
|
87
|
+
So write CSS in a stylesheet and code in a module, which `'self'` allows:
|
|
88
|
+
|
|
89
|
+
```tsx title="src/app/layout.tsx"
|
|
90
|
+
import './styles.css';
|
|
91
|
+
|
|
92
|
+
export default function RootLayout({ children }: { children: React.ReactNode }) {
|
|
93
|
+
return (
|
|
94
|
+
<html lang="en">
|
|
95
|
+
<body>
|
|
96
|
+
{children}
|
|
97
|
+
<script type="module" src="/analytics.js" />
|
|
98
|
+
</body>
|
|
99
|
+
</html>
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
|
|
86
104
|
[Third-party scripts](/guides/third-party-scripts) loaded by `src` need their
|
|
87
|
-
origin in `script-src
|
|
88
|
-
|
|
105
|
+
origin in `script-src`:
|
|
106
|
+
|
|
107
|
+
```ts
|
|
108
|
+
'script-src': ["'self'", 'https://plausible.io'],
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
If your app writes `<style>` tags, put `'unsafe-inline'` in `style-src` and
|
|
112
|
+
the engine leaves it alone.
|
|
89
113
|
|
|
90
114
|
### A library that asks for a nonce
|
|
91
115
|
|
|
92
|
-
Some libraries write inline code
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
them:
|
|
116
|
+
Some libraries write inline code and take a `nonce` for it: next-themes'
|
|
117
|
+
script that sets the theme before the page paints, and the `<style>` its
|
|
118
|
+
`disableTransitionOnChange` inserts on every theme change; an analytics
|
|
119
|
+
snippet; a CSS-in-JS runtime. `useNonce()` is the nonce to give them:
|
|
97
120
|
|
|
98
|
-
```tsx title="app/Theme.tsx"
|
|
121
|
+
```tsx title="src/app/Theme.tsx"
|
|
99
122
|
'use client';
|
|
100
123
|
|
|
101
124
|
import { ThemeProvider } from 'next-themes';
|
|
@@ -113,56 +136,82 @@ export function Theme({ children }: { children: ReactNode }) {
|
|
|
113
136
|
}
|
|
114
137
|
```
|
|
115
138
|
|
|
116
|
-
Render `<Theme>` from the root layout in place of the `ThemeProvider`.
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
document.
|
|
123
|
-
|
|
124
|
-
It is `undefined` on a page allowed by hash
|
|
125
|
-
HTML, one a cache keeps
|
|
126
|
-
library's inline script is hashed with the rest of the page
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
The engine's own components use it too: `PageTransition`'s style
|
|
133
|
-
by React in the browser when it
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
+
Render `<Theme>` from the root layout in place of the `ThemeProvider`.
|
|
140
|
+
|
|
141
|
+
It is a client hook because the nonce that counts is the document's. A
|
|
142
|
+
navigation's payload is rendered with a nonce of its own, which the page on
|
|
143
|
+
screen has never seen, so a nonce read on the server and passed down as a prop
|
|
144
|
+
would be wrong after the first navigation. On the server it is the nonce the
|
|
145
|
+
response is written with; in the browser it is read back from the document.
|
|
146
|
+
|
|
147
|
+
It is `undefined` on a page allowed by hash: a stored page, a shell's own
|
|
148
|
+
HTML, one a cache keeps. That is fine for what is in the HTML, because the
|
|
149
|
+
library's inline script is hashed with the rest of the page. What a library
|
|
150
|
+
inserts after hydration on such a page has nothing to run by, unless the page
|
|
151
|
+
has holes, whose nonce it then uses. On a stored page with none, put
|
|
152
|
+
`'unsafe-inline'` in `style-src` if the library inserts styles, or leave the
|
|
153
|
+
option off — next-themes works without `disableTransitionOnChange`.
|
|
154
|
+
|
|
155
|
+
The engine's own components use it too: `PageTransition`'s style, inserted
|
|
156
|
+
by React in the browser when it wasn't in the HTML, as in `vite dev`, carries
|
|
157
|
+
the nonce.
|
|
158
|
+
|
|
159
|
+
## Setting it from a middleware
|
|
160
|
+
|
|
161
|
+
Don't. A middleware that sets its own `Content-Security-Policy` keeps it, and
|
|
162
|
+
the engine adds nothing to it, so React never learns a nonce and every
|
|
163
|
+
streamed page is refused. `contentSecurityPolicy` and a
|
|
164
|
+
`Content-Security-Policy` in [`headers`](/guides/response-headers) are the
|
|
165
|
+
same header twice, so the build refuses both together.
|
|
166
|
+
|
|
167
|
+
## In development
|
|
168
|
+
|
|
169
|
+
**`vite dev`** sends the same policy, so what production would refuse is
|
|
170
|
+
refused while you work. It adds:
|
|
171
|
+
|
|
172
|
+
- `'unsafe-eval'` in `script-src`, which React's development build needs;
|
|
173
|
+
- the local dev server in `connect-src` (`ws://localhost:*` and
|
|
174
|
+
`127.0.0.1`, where Vite's HMR client goes when the page is served from
|
|
175
|
+
`my-app.test`);
|
|
176
|
+
- the dev server's origin and its socket, when the page comes from another
|
|
177
|
+
one (`devOrigin`).
|
|
178
|
+
|
|
179
|
+
None of these is ever in a build. Every page renders per request in dev, so
|
|
180
|
+
every page is allowed by its nonce, and Vite's own inline styles (a CSS
|
|
181
|
+
update, the error overlay) carry it too. Your own inline `<script>` or
|
|
182
|
+
`<style>` is refused in dev even on a page the build would store and hash.
|
|
139
183
|
|
|
140
184
|
## Where it is sent
|
|
141
185
|
|
|
142
186
|
- **A server, anywhere**: Node, Bun, Docker, a Worker, a function. The host
|
|
143
|
-
sets the header on each page
|
|
187
|
+
sets the header on each page, and nothing depends on the platform.
|
|
144
188
|
- **An edge cache** that implements the
|
|
145
189
|
[edge contract](/reference/edge-contract) sends a stored page's policy with
|
|
146
190
|
its copy. A shell goes only to an edge that makes a nonce for each
|
|
147
191
|
response (`nonceShells`), which it puts in the policy and sends to your app
|
|
148
|
-
for the holes. An edge that
|
|
192
|
+
for the holes. An edge that can't leaves those pages to your app.
|
|
149
193
|
- **A static export** has no server to send a header, so each page carries its
|
|
150
194
|
policy as a `<meta http-equiv="Content-Security-Policy">` with its own
|
|
151
|
-
hashes. A `<meta>`
|
|
152
|
-
`report-to` or `sandbox`, and
|
|
195
|
+
hashes. A `<meta>` can't carry `frame-ancestors`, `report-uri`,
|
|
196
|
+
`report-to` or `sandbox`, and can't be report-only; set those as headers
|
|
153
197
|
on the host if it can.
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
198
|
+
|
|
199
|
+
## How it works
|
|
200
|
+
|
|
201
|
+
How a page's inline code is allowed depends on whether its body is whole
|
|
202
|
+
before its headers leave.
|
|
203
|
+
|
|
204
|
+
| The page | Allowed by | Why |
|
|
205
|
+
| --- | --- | --- |
|
|
206
|
+
| Stored at build | the hash of each inline script and style in it | the same bytes for everyone, so one header for everyone, and caches keep sharing it |
|
|
207
|
+
| [Kept](/guides/isr) by `revalidate` | the same, from the body that is kept | rendered with no nonce, because the copy is shared |
|
|
208
|
+
| A [shell](/guides/ppr) with its holes resumed | the shell's hashes, and a nonce for the holes | the holes are rendered for this request |
|
|
209
|
+
| Rendered per request | a nonce | it streams: its last script isn't known when the header goes |
|
|
210
|
+
|
|
211
|
+
A nonce is 128 random bits, new for every response, and is only ever put on a
|
|
212
|
+
response no cache may share. React puts it on every script it writes, and on
|
|
213
|
+
the styles it hoists.
|
|
214
|
+
|
|
215
|
+
The payload a page hydrates from isn't code. It travels in the page as
|
|
216
|
+
`<script type="application/json">` data blocks, which a browser never runs and
|
|
217
|
+
a policy never judges, so a page's data needs nothing allowed.
|