@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,16 +2,14 @@
2
2
 
3
3
  > Marking work that belongs to the visitor, not to the build.
4
4
 
5
- Most pages can be rendered once, at build time, and served as files. Some work
6
- cannot: a query whose database the build machine cannot reach, or a value that
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
- export default async function OrdersPage() {
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
- At build time it never resolves, so **nothing after it runs** — the query is not
24
- made, and the build needs no database. At request time it resolves immediately
25
- and the component runs normally.
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
- ## Call it once
25
+ ## Putting a boundary above it
28
26
 
29
- This is the mistake worth avoiding:
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
- await connection()
30
+ ```tsx title="src/app/orders/page.tsx"
31
+ import { Suspense } from 'react'
33
32
 
34
- const orders = await db.orders()
35
- await connection() // ← does nothing
36
- const customers = await db.customers()
37
- await connection() // ← does nothing
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
- It is a **barrier, not a wrapper**. Everything after it in that component
41
- belongs to the request, however many calls that turns out to be. The second and
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
- Give it a boundary and the page becomes a shell — the layout and headings are
60
- stored, and the marked part arrives per request:
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
- export default function Page() {
64
- return (
65
- <>
66
- <h1>Orders</h1>
67
- <Suspense fallback={<p>Loading orders…</p>}>
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
- A `loading.tsx` beside the page does the same thing for everything below it.
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 do not need it
74
+ ## When you don't need it
80
75
 
81
- A query the build **can** run, whose answer is the same for every visitor, wants
82
- none of this. Let it freeze — that is the whole benefit of prerendering, and
83
- marking it per-request gives every visitor a render they did not need.
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 it when the build genuinely should not do the work:
81
+ Use `connection()` when:
86
82
 
87
- - the database or API is not reachable from the build machine
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
- ## Same as Next.js
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
- Same name and same behaviour as Next's `connection()`, so there is nothing new
94
- to learn if you have used it.
90
+ ## Coming from Next.js
95
91
 
96
- You will reach for it less often here, though. The build classifies routes by
97
- rendering them rather than asking you to declare, so most dynamic pages are
98
- already understood without a marker.
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 is the best defence a
6
- page has against injected markup turning into injected code. The catch is
7
- that a React page has inline code of its own: the script that boots it, and,
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
- `{}` is a strict default:
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
- To which every page adds a `script-src` and a `style-src` naming its own
26
- inline code. Your own directives replace the default:
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. Add `reportOnly: true`
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
- ## What the engine adds, and when
51
+ ```ts
52
+ 'img-src': "'self' data: https://images.example.com",
53
+ ```
49
54
 
50
- How a page's inline code is allowed depends on whether its body is whole
51
- before its headers leave.
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
- | The page | Allowed by | Why |
54
- | --- | --- | --- |
55
- | 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 |
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
- A nonce is 128 random bits, new for every response, and is only ever put on a
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
- The payload a page hydrates from is not code. It travels in the page as
65
- `<script type="application/json">` data blocks, which a browser never runs and
66
- a policy never judges, so a page's data needs nothing allowed.
65
+ ```ts title="vite.config.ts"
66
+ rscKit({
67
+ contentSecurityPolicy: { reportOnly: true },
68
+ })
69
+ ```
67
70
 
68
- A directive that already has `'unsafe-inline'` is left as you wrote it: a hash
69
- or nonce beside it would switch it off. A policy with no `script-src` and no
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. Inline code you write:
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`. If your app writes `<style>` tags, put
88
- `'unsafe-inline'` in `style-src` and the engine will leave it alone.
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 of their own and take a `nonce` for it:
93
- next-themes' script that sets the theme before the page paints, and the
94
- `<style>` its `disableTransitionOnChange` inserts on every theme change; an
95
- analytics snippet; a CSS-in-JS runtime. `useNonce()` is the nonce to give
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`. It is
117
- a client hook because the nonce that counts is the document's: a navigation's
118
- payload is rendered with a nonce of its own, which the page on screen has
119
- never heard of, so a nonce read on the server and passed down as a prop would
120
- be the wrong one after the first navigation. On the server it is the nonce
121
- the response is written with; in the browser it is read back from the
122
- document.
123
-
124
- It is `undefined` on a page allowed by hash — a stored page, a shell's own
125
- HTML, one a cache keeps — and that is fine for what is in the HTML: the
126
- library's inline script is hashed with the rest of the page and runs without
127
- one. What a library inserts after hydration on such a page has nothing to run
128
- by unless the page has holes, whose nonce it then uses. On a stored page with
129
- none, put `'unsafe-inline'` in `style-src` if the library inserts styles, or
130
- leave the option off — next-themes works without `disableTransitionOnChange`.
131
-
132
- The engine's own components use it too: `PageTransition`'s style is inserted
133
- by React in the browser when it was not in the HTML, as in `vite dev`, and
134
- carries the nonce.
135
-
136
- `style-src-attr 'unsafe-inline'` is in the default because React's
137
- `style={{ ... }}` is a style attribute, which no hash or nonce can allow. An
138
- attribute cannot run code. Leave it out and every `style` prop is refused.
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; nothing depends on the platform.
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 cannot leaves those pages to your app.
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>` cannot carry `frame-ancestors`, `report-uri`,
152
- `report-to` or `sandbox`, and cannot be report-only; set those as headers
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
- - **`vite dev`** sends the same policy, so what it would refuse in production
155
- is refused while you work, plus `'unsafe-eval'` in `script-src`, which
156
- React's development build needs, the local dev server in `connect-src`
157
- (`ws://localhost:*` and `127.0.0.1`, where Vite's HMR client goes when
158
- the page is served from `my-app.test`), and the dev server's origin and
159
- its socket when the page comes from another one (`devOrigin`). Never in a
160
- build. Every page renders per request in dev, so every page is allowed by
161
- its nonce, and Vite's own inline styles (a CSS update, the error overlay)
162
- carry it too. Your own inline `<script>` or `<style>` is refused in dev
163
- even on a page the build would store and hash.
164
-
165
- A middleware that sets its own `Content-Security-Policy` keeps it, and the
166
- engine adds nothing to it. `contentSecurityPolicy` and a
167
- `Content-Security-Policy` in [`headers`](/guides/response-headers) are the
168
- same header twice, so the build refuses both together.
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.