@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,111 +2,146 @@
2
2
 
3
3
  > Serving prerendered pages, PPR shells and kept pages from a CDN, the headers they go out with, and purging by tag.
4
4
 
5
- A prerendered page is a file. So is a PPR shell. Both can sit on a CDN and be
6
- served without touching your origin.
5
+ A page that exports `revalidate` can be kept by a CDN and served to everyone
6
+ without touching your origin:
7
7
 
8
- ## What the build gives you
9
-
10
- ```
11
- build/static/
12
- about.html a whole page, frozen
13
- posts/hello.html likewise
14
- posts/[slug].ppr.html a shell — static parts frozen, holes still empty
15
- about.flight the payload for a client-side navigation
16
- about.seg1.flight the same, for a client already holding one layout
8
+ ```tsx title="src/app/posts/[slug]/page.tsx"
9
+ export const revalidate = 60 // seconds a shared cache may keep it
17
10
  ```
18
11
 
19
- `.html` is a finished page. `.ppr.html` is a shell: the layout, nav and
20
- everything outside a `<Suspense>` boundary, with the fallbacks still in place.
21
- Neither contains per-visitor data — they were rendered at build time, in no
22
- request's context, which is what makes them safe for a shared cache.
23
-
24
- ## Caching them
25
-
26
- The host already sends the right header:
27
-
28
12
  ```
29
- Cache-Control: public, max-age=0, must-revalidate
13
+ Cache-Control: public, max-age=0, s-maxage=60, stale-while-revalidate=86400, stale-if-error=86400
14
+ CDN-Cache-Control: max-age=60, stale-while-revalidate=86400, stale-if-error=86400
15
+ ETag: "…"
30
16
  ```
31
17
 
32
- `public` says a shared cache may store it. `max-age=0, must-revalidate` says
33
- **check with the origin first**. So a CDN in front of this holds the bytes but
34
- still asks every time — which saves bandwidth and nothing else.
18
+ Prerendered pages and PPR shells are files, and can sit on a CDN too. This page
19
+ covers the headers each one goes out with, purging a CDN when data changes, and
20
+ serving shells from the edge. [ISR](/guides/isr) is the how-to for
21
+ `revalidate` itself.
22
+
23
+ ## Caching a page at a CDN
24
+
25
+ Export `revalidate` from the page, in seconds. The page is rendered whole, and
26
+ when the render read nothing that belongs to one visitor, it goes out public:
27
+
28
+ - A CDN keeps it for `revalidate` seconds, then refreshes it in the background.
29
+ A browser never keeps it.
30
+ - While your origin is down, erroring or mid-deploy, a CDN that reads
31
+ `stale-if-error` serves its copy for up to a day rather than the error.
32
+ - A cache revalidating a copy that hasn't changed is answered `304`.
33
+ - A 404, including the `not-found.tsx` a navigation shows, is kept for at most
34
+ a minute, so a scanner walking random URLs can't hold your pages out.
35
+
36
+ The value must be a number literal the build can read. A layout can export it
37
+ too: the smallest value on the way down to the page wins, and `0` anywhere
38
+ means the page is rendered per request. The page is not prerendered:
39
+ `revalidate` says its data changes, so a frozen copy would be wrong from the
40
+ first change.
41
+
42
+ rsc-kit sends `CDN-Cache-Control` (RFC 9213), so a CDN that reads it can cache
43
+ pages. Cloudflare does. Homeport's edge caches on nothing else, and strips it
44
+ before the answer leaves, so you won't see it in a browser there.
45
+
46
+ `CDN-Cache-Control` goes out only when a purge can reach the CDN:
47
+ `CDN_PURGE_URL` is set, or a purge is installed with `installPurge()`. An edge
48
+ that keeps pages on that header alone then never holds a copy `changed()`
49
+ can't take back. Without one, `Cache-Control`'s `s-maxage` still gives a cache
50
+ that reads it the same lifetime. The two headers always agree, and only a
51
+ public answer has either.
52
+
53
+ ### A kept page calls its backend as nobody
54
+
55
+ Its host calls carry no cookie and no `Authorization`, and a cookie the backend
56
+ sets is dropped. `auth()->user()` is null there, as it is for every visitor a
57
+ shared copy is served to. A page that needs to know who is asking is not one to
58
+ keep.
59
+
60
+ A form in a kept page still posts as the visitor. The engine's actions, and
61
+ Laravel 13's forgery check, accept a same-origin post by its `Sec-Fetch-Site`
62
+ header, so it needs no token. A backend that still checks a token tied to the
63
+ session should be asked for it by a component that awaits `connection()`,
64
+ inside `<Suspense>`. In a static page that part is a hole filled per request
65
+ with the visitor's own session. In a `revalidate` page it is a read of the
66
+ visitor, so the page is sent private.
67
+
68
+ ## Why a page was sent private
69
+
70
+ Any of these sends the page `private, no-store`, with a `Cache-Status` header
71
+ naming the reason and one line in the log per page and reason:
35
72
 
36
- A page rendered per request is not one of these files, and says
37
- `private, max-age=0, must-revalidate`: it may have read the visitor's cookies,
38
- and its headers leave before the render could say whether it did.
73
+ | `Cache-Status: rsc-kit; fwd=bypass; detail=` | Why |
74
+ | --- | --- |
75
+ | `cookies`, `headers`, `request`, `connection` | the render read the visitor: in the page, a layout, a Suspense'd child, metadata or `middleware.ts`. The log names the component |
76
+ | `host-middleware` | the route has host middleware, which runs against the visitor's session |
77
+ | `set-cookie` | the answer set a cookie |
78
+ | `nonce` | the answer carries a CSP nonce, which a shared copy would give everyone |
79
+ | `status` | only a 200 or a 404 is kept, never a redirect or a 5xx |
80
+ | `failed-render` | React reported a failure inside the payload |
81
+ | `payload-url` | a navigation's payload was asked for without its `?_rsc=` key, or a document with one; the router always adds it |
39
82
 
40
- To actually serve from the edge, add a cache rule for the paths you want held,
41
- and give the CDN an edge TTL. On Cloudflare that is a Cache Rule with *Edge TTL
42
- → Override origin*.
83
+ ## Purging a CDN when data changes
43
84
 
44
- **Do not blanket-cache the whole site.** A route with middleware — a
45
- `middleware.ts`, or the host's `export const middleware = ['auth']` — is
46
- deliberately sent as:
85
+ A page whose data comes through [`cache(fn, { tags })`](/guides/caching) sends
86
+ the tags of every answer it used:
47
87
 
48
88
  ```
49
- Cache-Control: private, no-store
89
+ Cache-Tag: posts,post:1
50
90
  ```
51
91
 
52
- because middleware runs per visitor — that is a page whose content depends on
53
- who asked. A zone-wide "cache everything" rule overrides that and serves one
54
- person's gated page to everyone. Scope the rule to the paths you know are
55
- static.
92
+ A CDN that purges by tag can drop its copy when one of them changes, rather
93
+ than waiting out `revalidate`. Install a purge, and `changed('posts')` calls it
94
+ with the names that changed, once the action's answer has gone:
56
95
 
57
- Deployments are handled for you: cached responses carry a build version, so a
58
- new deploy does not leave old shells being served against a new payload.
96
+ ```ts
97
+ import { installPurge } from '@rsc-kit/core/purge'
59
98
 
60
- ## Keeping a page that is rendered per request
99
+ installPurge(async ({ tags }) => {
100
+ await fetch(`https://api.cloudflare.com/client/v4/zones/${zone}/purge_cache`, {
101
+ method: 'POST',
102
+ headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' },
103
+ body: JSON.stringify({ tags }),
104
+ })
105
+ })
106
+ ```
61
107
 
62
- A page whose data changes after the build is rendered for each request, and
63
- sent `private`. When the same answer may go to everyone for a while, say how
64
- long. [ISR](/guides/isr) is the how-to; this is what goes over the wire:
108
+ - **A purge that fails** is logged and never fails the change. The CDN still
109
+ drops its copy at the `max-age` in `CDN-Cache-Control`.
110
+ - **To send one yourself**, from a deploy step or a webhook, call
111
+ `purge({ tags, paths, all })`.
112
+ - **A path under a folder named in `subdomains`** is also sent as the path its
113
+ visitors use: `/merchant/orders` purges `/orders` as well.
114
+ - **A tag with a comma or a space** can't go in the header, and is left out
115
+ with a warning.
65
116
 
66
- ```tsx
67
- // app/posts/[slug]/page.tsx
68
- export const revalidate = 60 // seconds a shared cache may keep it
69
- ```
117
+ ### With `CDN_PURGE_URL`
70
118
 
71
- The page is then rendered whole before it is sent, and judged. When the render
72
- read nothing that belongs to one visitor, it goes out as:
119
+ Set `CDN_PURGE_URL` and the purge is `cdnPurge()`, which posts
120
+ `{"tags": [...]}` there, unless you installed another. It is the only name
121
+ read, and it names neither this engine nor a platform, so a host can set it
122
+ for any framework. `RSC_PURGE_URL` and `HOMEPORT_PURGE_URL` are no longer read,
123
+ and `homeportPurge()` is now `cdnPurge()`, with no alias: set `CDN_PURGE_URL`
124
+ and change the import.
73
125
 
74
- ```
75
- Cache-Control: public, max-age=0, s-maxage=60, stale-while-revalidate=86400, stale-if-error=86400
76
- CDN-Cache-Control: max-age=60, stale-while-revalidate=86400, stale-if-error=86400
77
- ETag: "…"
78
- ```
126
+ With a Laravel or Go backend, the change is made there, with `Rsc::changed()`
127
+ or `reg.Changed()`, and never passes through the engine. So the backend posts
128
+ the purge itself. Both do when `CDN_PURGE_URL` is set.
79
129
 
80
- A CDN keeps it for 60 seconds and refreshes it in the background after that;
81
- a browser never keeps it. rsc-kit sends `CDN-Cache-Control` (RFC 9213), so a
82
- CDN that reads it can cache pages: Cloudflare does, and Homeport's edge caches
83
- on nothing else and strips it before the answer leaves, so you will not see it
84
- in a browser there. `CDN-Cache-Control` goes out only when a purge can reach
85
- the CDN - `CDN_PURGE_URL` set, or a purge installed with `installPurge()` - so
86
- an edge that keeps pages on it alone never holds one `changed()` cannot take
87
- back. Without one, `Cache-Control`'s `s-maxage` still says the same lifetime to
88
- a cache that reads it. The two headers always say the same thing, and only a
89
- public answer has either. While your origin is down, erroring or mid-deploy, a
90
- CDN that reads `stale-if-error` serves the copy it has for up to a day rather
91
- than the error. A cache revalidating a copy that has not changed is
92
- answered `304`. A 404 - including the not-found.tsx a navigation shows - is
93
- kept for at most a minute, so a scanner walking random urls cannot hold your
94
- pages out. A layout can export it too: the smallest value on the way down to
95
- the page wins, and `0` anywhere means the page is rendered per request as
96
- before. It must be a number literal the build can read.
130
+ ## Checking whether the CDN answered
97
131
 
98
132
  To see whether the CDN answered, read `Cache-Status` (RFC 9211). A CDN that
99
133
  sends it adds an entry under its own name, with `hit` when its copy answered.
100
134
  `X-RSC-Kit` and `ETag` can't tell you: they were stored with the copy, so a hit
101
135
  repeats them.
102
136
 
103
- Homeport's edge adds this entry from its next engine release, so it isn't there
104
- yet. See [what it will say](#cache-status-on-homeport).
137
+ ```sh
138
+ curl -sI https://example.com/posts/hello | grep -i cache-status
139
+ ```
105
140
 
106
141
  ### `Cache-Status` on Homeport
107
142
 
108
- This arrives with Homeport's next engine release. Until then a response has no
109
- `homeport` entry.
143
+ Homeport's edge adds this entry from its next engine release. Until then a
144
+ response has no `homeport` entry.
110
145
 
111
146
  The entry is named `homeport` and comes last, after any `Cache-Status` the app
112
147
  sends. It is a separate header line, so `curl -I` can show two `Cache-Status`
@@ -132,7 +167,7 @@ On the two "from the app" rows, `fwd` can also be `stale` or `vary-miss`:
132
167
 
133
168
  - **Look for `hit` in the `homeport` entry**, not anywhere in the header: the
134
169
  app's own entry can say `hit` too. The stale copy after a 5xx came from the
135
- edge and does not say `hit`.
170
+ edge and doesn't say `hit`.
136
171
  - **`stored` is not proof of a kept copy.** It is sent before the body, so a
137
172
  body that fails or is too large can say `stored` and keep nothing. The next
138
173
  request's `hit` is the proof.
@@ -140,149 +175,105 @@ On the two "from the app" rows, `fwd` can also be `stale` or `vary-miss`:
140
175
  `X-Homeport-Edge: page`, `shell` or `asset`. Only `X-Homeport-Edge: cached`
141
176
  is gone.
142
177
 
143
- ### Kept by the engine, too
144
-
145
- The host keeps a public `revalidate` page itself, so it is served without a
146
- render on every host - a single server, several pods, a Worker - with a CDN
147
- in front or not. It is Next's ISR, on your own server:
148
-
149
- - A second request for the page is answered from the kept copy, with
150
- `X-RSC-Kit: cached` and an `Age` header. A document and each navigation's
151
- payload are kept apart, and so are hosts and query strings.
152
- - Past `revalidate`, the next visitor still gets the kept copy at once, and
153
- one render replaces it behind them. Nobody waits on an expiry. The stale
154
- copy is served for the same day `stale-while-revalidate` gives a CDN; after
155
- that, the page renders while the visitor waits.
156
- - **`changed(tag)` is seen at once, on every instance.** A kept page
157
- remembers the version of every `cache()` tag it used, and a lookup checks
158
- them. After `changed('posts')`, the next request for any page that read
159
- `posts` renders, on every pod, with no purge sent anywhere - and is never
160
- served stale, because whoever made the change is looking for it. A render
161
- that was running when the change landed is kept, but never served.
178
+ Homeport's edge is stricter than the page's headers alone. A page with more
179
+ than 128 tags, or a tag over 1024 bytes, is served but not kept there. So is
180
+ one that sends `Vary: Cookie` or `Vary: Authorization`, or names more than 16
181
+ headers in `Vary`, which a `revalidate` page only does if your own code adds
182
+ them.
183
+
184
+ ## Kept by the engine, too
185
+
186
+ The host keeps a public `revalidate` page itself, with a CDN in front or not,
187
+ so it is served without a render on every host: a single server, several pods,
188
+ a Worker.
189
+
190
+ - A second request is answered from the kept copy, with `X-RSC-Kit: cached`
191
+ and an `Age` header. A document and each navigation's payload are kept apart,
192
+ and so are hosts and query strings.
193
+ - Past `revalidate`, the next visitor still gets the kept copy at once, and one
194
+ render replaces it behind them. The stale copy is served for the same day
195
+ `stale-while-revalidate` gives a CDN. After that, the page renders while the
196
+ visitor waits.
197
+ - **`changed(tag)` is seen at once, on every instance.** A kept page remembers
198
+ the version of every `cache()` tag it used, and a lookup checks them. After
199
+ `changed('posts')`, the next request for any page that read `posts` renders,
200
+ on every pod, with no purge sent anywhere, and is never served stale. A
201
+ render that was running when the change landed is kept, but never served.
162
202
  - A burst of visitors to a page nobody has kept yet renders it once.
163
203
  - Anything sent private is never kept.
164
204
 
165
- What a page is kept in is the store [`installCacheStore()`](/guides/caching)
166
- installs, shared by every instance, or else this process's memory, 64 MB at
167
- most. On several instances, what keeps a change from being missed is the
168
- version source, not the store: with a Laravel or Go backend, the backend keeps
169
- the versions for every instance, and an app without one installs a shared
170
- source, as it already must for `refreshOn`. Memory per instance then only
171
- means each instance renders its own first copy.
205
+ A kept page lives in the store [`installCacheStore()`](/guides/caching)
206
+ installs, shared by every instance, or else in this process's memory, 64 MB at
207
+ most:
172
208
 
173
209
  | Where it runs | Where a kept page lives | What to install |
174
210
  | --- | --- | --- |
175
211
  | One server, a VPS | this process's memory | nothing |
176
212
  | Several pods, Kubernetes | each pod's memory, or one Redis | nothing; `installCacheStore()` with Redis to render each page once for all pods |
177
- | A Worker | KV | `installCacheStore()` with KV - an isolate's memory is gone in minutes, and the host warns without one |
178
- | Vercel | Redis (Upstash) | `installCacheStore()` - a function's memory lives about as long; Vercel's CDN also keeps the page by `s-maxage` |
179
- | Homeport | Homeport's edge | nothing - the engine keeps no copy there |
213
+ | A Worker | KV | `installCacheStore()` with KV: an isolate's memory is gone in minutes, and the host warns without one |
214
+ | Vercel | Redis (Upstash) | `installCacheStore()`: a function's memory lives about as long. Vercel's CDN also keeps the page by `s-maxage` |
215
+ | Homeport | Homeport's edge | nothing: the engine keeps no copy there |
180
216
 
181
- What tells a pod that a tag changed is never the store: on a Worker or Vercel
182
- with no Laravel or Go backend, that is `sqlVersions()` on D1 or `postgresVersions()`, not
183
- KV, whose writes take up to a minute to reach every location.
217
+ On several instances, what keeps a change from being missed is the version
218
+ source, not the store. With a Laravel or Go backend, the backend keeps the
219
+ versions for every instance. An app without one installs a shared source, as it
220
+ already must for `refreshOn`. On a Worker or Vercel with no backend, that is
221
+ `sqlVersions()` on D1 or `postgresVersions()`, not KV, whose writes take up to a
222
+ minute to reach every location.
184
223
 
185
224
  It is off in development, where a kept page would hide your edits, and on
186
- Homeport, whose edge keeps the page instead. Turn it off yourself with
225
+ Homeport, whose edge keeps the page instead. Turn it off with
187
226
  `pageCache: false` in the host's options; `pageCache: { maxBytes }` sizes the
188
227
  memory store.
189
228
 
190
- A page whose data comes through `cache(fn, { tags })` also sends the tags of
191
- every answer it used:
229
+ ## Serving prerendered pages from a CDN
230
+
231
+ The build writes every page it could prerender as a file:
192
232
 
193
233
  ```
194
- Cache-Tag: posts,post:1
234
+ build/static/
235
+ about.html a whole page
236
+ posts/hello.html likewise
237
+ posts/[slug].ppr.html a shell: static parts rendered, holes still empty
238
+ about.flight the payload for a client-side navigation
239
+ about.seg1.flight the same, for a client already holding one layout
195
240
  ```
196
241
 
197
- A CDN that purges by tag can drop the copy when one of them changes, rather
198
- than waiting out `revalidate`. `changed('posts')` calls the purge you install,
199
- with the names that changed, once the action's answer has gone:
242
+ Neither kind contains per-visitor data. They were rendered at build time, in no
243
+ request's context, which is what makes them safe for a shared cache. They go
244
+ out as:
200
245
 
201
- ```ts
202
- import { installPurge } from '@rsc-kit/core/purge'
203
-
204
- installPurge(async ({ tags }) => {
205
- await fetch(`https://api.cloudflare.com/client/v4/zones/${zone}/purge_cache`, {
206
- method: 'POST',
207
- headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' },
208
- body: JSON.stringify({ tags }),
209
- })
210
- })
246
+ ```
247
+ Cache-Control: public, max-age=0, must-revalidate
211
248
  ```
212
249
 
213
- Every CDN's purge API is its own, so this is one hook rather than a list of
214
- vendors. A purge that fails is logged and never fails the change: the CDN
215
- still drops its copy at its `max-age` in `CDN-Cache-Control`. `purge({ tags, paths, all })` sends
216
- one yourself, from a deploy step or a webhook. A path under a folder named in
217
- `subdomains` is also sent as the path its visitors use: `/merchant/orders`
218
- purges `/orders` as well.
219
-
220
- With `CDN_PURGE_URL` set, the hook is `cdnPurge()`, posting
221
- `{"tags": [...]}` there, unless you installed another. It is the only name
222
- read, and it names neither this engine nor a platform, so a host can set it
223
- for any framework. `RSC_PURGE_URL` and `HOMEPORT_PURGE_URL` were read before
224
- and no longer are, and `homeportPurge()` is now `cdnPurge()`, with no alias
225
- left behind: set `CDN_PURGE_URL` and change the import. With a Laravel or Go
226
- backend, the change is made there - `Rsc::changed()`, `reg.Changed()` - and
227
- never passes through the engine, so the backend posts the purge itself; both
228
- do when `CDN_PURGE_URL` is set. A tag with a comma or a space cannot go in the header, and
229
- is left out with a warning.
230
-
231
- Homeport's edge is stricter about what it keeps than the page's headers alone
232
- say. A page with more than 128 tags, or a tag over 1024 bytes, is served but
233
- not kept there, and so is one that sends `Vary: Cookie` or `Vary: Authorization`
234
- or names more than 16 headers in `Vary`, which a `revalidate` page only does if
235
- your own code adds them.
236
-
237
- **A revalidate page calls its backend as nobody.** Its host calls carry no
238
- cookie and no `Authorization`, and a cookie the backend sets is dropped -
239
- `auth()->user()` is null there, as it is for every visitor a shared copy is
240
- served to. A page that needs to know who is asking is not one to keep.
241
-
242
- A form needs no token for this. The engine's actions, and Laravel 13's
243
- forgery check, accept a same-origin post by its `Sec-Fetch-Site` header, so a
244
- form in a kept page posts as the visitor. A backend that still checks a token
245
- tied to the session should be asked for it by a component that awaits
246
- `connection()`, inside `<Suspense>`. In a static page that part is a hole
247
- filled per request with the visitor's own session; in a revalidate page it is
248
- a read of the visitor, so the page is sent private.
249
-
250
- Any of these sends the page as before, `private, no-store`, with a
251
- `Cache-Status` header naming the reason and one line in the log per page and
252
- reason:
250
+ A CDN in front holds the bytes but still checks with the origin every time. To
251
+ serve from the edge, add a cache rule for the paths you want held, with an edge
252
+ TTL. On Cloudflare that is a Cache Rule with *Edge TTL → Override origin*.
253
+ Cached responses carry a build version, so a new deploy doesn't leave old
254
+ shells served against a new payload.
253
255
 
254
- | `Cache-Status: rsc-kit; fwd=bypass; detail=` | Why |
255
- | --- | --- |
256
- | `cookies`, `headers`, `request`, `connection` | the render read the visitor - in the page, a layout, a Suspense'd child, metadata or `middleware.ts`; the log names the component |
257
- | `host-middleware` | the route has host middleware, which runs against the visitor's session |
258
- | `set-cookie` | the answer set a cookie |
259
- | `nonce` | the answer carries a CSP nonce, which a shared copy would give everyone |
260
- | `status` | only a 200 or a 404 is kept, never a redirect or a 5xx |
261
- | `failed-render` | React reported a failure inside the payload |
262
- | `payload-url` | a navigation's payload was asked for without its `?_rsc=` key, or a document with one; the router always adds it |
256
+ A page rendered per request says `private, max-age=0, must-revalidate`: it may
257
+ have read the visitor's cookies, and its headers leave before the render could
258
+ say whether it did.
263
259
 
264
- The page is not prerendered: `revalidate` says its data changes, so a frozen
265
- copy would be wrong from the first change.
260
+ **Don't cache the whole site.** A route with middleware, a `middleware.ts` or
261
+ the host's `export const middleware = ['auth']`, is sent as
262
+ `Cache-Control: private, no-store`, because its content depends on who asked. A
263
+ zone-wide "cache everything" rule overrides that and serves one person's gated
264
+ page to everyone. Scope the rule to the paths you know are static.
266
265
 
267
266
  ## Finishing a shell at the edge
268
267
 
269
- A shell has holes in it, and something has to fill them. That happens at your
270
- origin, into the same response — the shell is written first, then the
271
- boundaries it could not finish.
272
-
273
- You get this with no configuration: request a PPR route and the document you
274
- receive already contains its dynamic content.
275
-
276
- A small inline script from React moves each hole into place as the HTML parses.
277
- So the content appears without waiting for the app bundle or for hydration — on
278
- a slow connection, the difference between a spinner and a page. It also means
279
- the content is in the HTML a crawler reads.
280
-
281
- This is not the same as working without JavaScript: with scripting off, the
268
+ Request a PPR route and the document already contains its dynamic content: the
269
+ origin writes the shell first, then the boundaries it couldn't finish, into the
270
+ same response. A small inline script from React moves each hole into place as
271
+ the HTML parses, so the content appears without waiting for the app bundle or
272
+ hydration, and is in the HTML a crawler reads. With scripting off, the
282
273
  fallbacks stay.
283
274
 
284
- To serve the shell itself from a CDN, an edge speaks the [edge
285
- contract](/reference/edge-contract), the same one for every framework that
275
+ To serve the shell itself from a CDN, an edge speaks the
276
+ [edge contract](/reference/edge-contract), the same one for every framework that
286
277
  implements it. The build's `.output/edge.json` lists the pages with a shell and
287
278
  the static pages, none of them under middleware. Two endpoints answer only a
288
279
  caller holding the `EDGE_TOKEN` the origin was started with:
@@ -294,99 +285,82 @@ POST /_edge/resume?url=/dashboard a shell's holes, for this visitor
294
285
 
295
286
  A static page goes out whole and never reaches the origin: its scripts and its
296
287
  payload are immutable files under `/assets/`, so an edge that keeps those can
297
- serve the page while the app is asleep. [Pages an edge may serve from its
298
- cache](/hosts/deployment#pages-an-edge-may-serve-from-its-cache) says which
299
- pages rsc-kit lists.
300
-
301
- There is a complete Cloudflare implementation in
302
- [`examples/cloudflare-edge-worker`](https://github.com/rsc-kit/rsc-kit/tree/main/examples/cloudflare-edge-worker),
303
- with no KV and no build step: set the same `EDGE_TOKEN` on the worker and the
304
- origin, and hand it the release's `edge.json`. The cache fills itself from the
305
- page endpoint: a miss goes to the origin while the page warms behind it, a
306
- static hit is the page, and a shell hit streams the shell then pipes the
307
- resumed holes onto the same response.
308
-
309
- ### What the response looks like on the wire
310
-
311
- Measured on a deployed worker, for a page whose hole takes 2.5 s:
312
-
313
- ```
314
- headers 55 ms
315
- first body byte 56 ms
316
- shell heading 56 ms ← the page is on screen here
317
- fallback markup 56 ms
318
- hole content 2549 ms ← same response, no second request
319
- stream complete 2549 ms
320
- ```
288
+ serve the page while the app is asleep.
289
+ [Pages an edge may serve from its cache](/hosts/deployment/build-output/#pages-an-edge-may-serve-from-its-cache)
290
+ says which pages rsc-kit lists.
321
291
 
322
- Before this, the same document finished in 2 ms and contained no hole at all —
323
- the content arrived later, on a separate payload fetch, after React had
324
- hydrated.
292
+ [`examples/cloudflare-edge-worker`](https://github.com/rsc-kit/rsc-kit/tree/main/examples/cloudflare-edge-worker)
293
+ is a complete Cloudflare implementation, with no KV and no build step. Set the
294
+ same `EDGE_TOKEN` on the worker and the origin, and hand it the release's
295
+ `edge.json`. A miss goes to the origin while the page warms behind it, a static
296
+ hit is the page, and a shell hit streams the shell then pipes the resumed holes
297
+ onto the same response.
325
298
 
326
- **The trade is that the response stays open until the holes finish.** Previously
327
- the document closed immediately and `load` fired early; now it fires when the
328
- slowest boundary resolves. Nothing a visitor sees is slower — the shell paints
329
- at the same moment either way — but page-level metrics that key on `load` will
330
- read differently, and a proxy with a short response timeout needs to allow for
331
- the whole render rather than just the shell.
299
+ ### With a Content Security Policy
332
300
 
333
301
  Under a [Content Security Policy](/guides/content-security-policy), the
334
302
  shells move from `shells` to `nonceShells` in `edge.json`. The holes resumed
335
303
  into a shell are React's inline scripts, written for each request, and only a
336
- nonce allows those, so only an edge that makes one per response serves them:
337
- it asks with `Edge-Accept: nonce`, puts its nonce in the page's policy, and
338
- sends it on the resume as `Edge-Nonce`. An edge that does not reads `shells`
339
- alone, and your origin answers those pages. Static pages are listed either
340
- way, each with its policy and its own hashes.
304
+ nonce allows those. So only an edge that makes one per response serves them: it
305
+ asks with `Edge-Accept: nonce`, puts its nonce in the page's policy, and sends
306
+ it on the resume as `Edge-Nonce`. An edge that doesn't reads `shells` alone, and
307
+ your origin answers those pages. Static pages are listed either way, each with
308
+ its policy and its own hashes.
341
309
 
342
- ## Guarded routes are never cached
310
+ ### Guarded routes are never cached
343
311
 
344
- The shell endpoint answers `404` for any route that declares middleware. Such a
345
- page is not cacheable by a shared cache at all, so it never becomes a cache
346
- entry — refused at the source rather than checked at the edge.
312
+ The shell endpoint answers `404` for any route that declares middleware, so a
313
+ guarded page never becomes a cache entry.
347
314
 
348
315
  The resume endpoint runs that route's middleware against **the caller's own
349
- cookies**, and refuses before rendering anything. An edge worker must therefore
350
- forward the visitor's request rather than making one of its own; a resume asked
351
- for with no cookies is an anonymous visitor and gets an anonymous answer.
316
+ cookies**, and refuses before rendering anything. An edge worker must forward
317
+ the visitor's request rather than making one of its own: a resume with no
318
+ cookies is an anonymous visitor and gets an anonymous answer.
352
319
 
353
- ## What stays on your origin
320
+ ## Behind an auth proxy
354
321
 
355
- Next's PPR protocol hands the postponed blob to the CDN and takes it back on the
356
- resume, which means the resume endpoint parses something an attacker can write.
357
- That is the shape of a known denial-of-service against it.
322
+ An auth proxy in front of the app can add a per-visitor response head the app
323
+ never sent. One with sliding expiry re-issues the session on an ordinary `200`,
324
+ so the response leaving your CDN carries a `Set-Cookie` this host never sent.
325
+ Marked `public`, that is one visitor's session handed to the next.
358
326
 
359
- Here the endpoint takes a **url**. The origin reads its own state from disk, and
360
- a body posted to it is ignored. This is only possible because — unlike a generic
361
- CDN — the origin already has the artifact, so there is nothing to hand out and
362
- take back.
327
+ **Give any path your proxy covers a `middleware.ts`**, even an empty one. A
328
+ covered route is excluded from every cache decision: `private, no-store` on the
329
+ page, and refused outright by the shell endpoint.
363
330
 
364
- ## When a CDN owns the response
331
+ You can't detect this by inspecting the response. Nothing added a
332
+ `Vary: Cookie`, and looking for `Set-Cookie` only catches the cookie version: an
333
+ `x-user-id`, a CSRF token or a locale header leaks the same way. Ask instead who
334
+ owns the response head on this route. It fails both ways, too: a session
335
+ leaking to the next visitor, or an anonymous response cached first and served
336
+ to someone who should have had a session, which looks like a flaky login.
365
337
 
366
- Everything above rests on one fact: on this host, a per-visitor response head
367
- can only come from middleware. `responseHeaders()` and `cookies().set()` throw
368
- outside it, so a route that declares no middleware has no way to acquire one —
369
- which is why "declares middleware" is a safe answer to "is this cacheable".
338
+ ## How it works
370
339
 
371
- An auth proxy in front of the app breaks that, and quietly. One with sliding
372
- expiry re-issues the session on an ordinary `200`, so the response leaving your
373
- CDN carries a `Set-Cookie` this host never sent. Marked `public`, that is one
374
- visitor's session handed to the next.
340
+ **Why "declares middleware" is a safe answer to "is this cacheable".** On this
341
+ host, a per-visitor response head can only come from middleware.
342
+ `responseHeaders()` and `cookies().set()` throw outside it, so a route with no
343
+ middleware has no way to acquire one. An auth proxy is the exception above.
375
344
 
376
- **Give any path your proxy covers a `middleware.ts`**, even an empty one. That
377
- is what makes a route covered here, and covered routes are excluded from every
378
- cache decision: `private, no-store` on the page, and refused outright by the
379
- shell endpoint.
345
+ **The resume takes a URL, not state.** Next's PPR protocol hands the postponed
346
+ state to the CDN and takes it back on the resume, so its resume endpoint parses
347
+ something an attacker can write, which is the shape of a known denial of
348
+ service. Here the endpoint takes a URL, and the origin reads its own state from
349
+ disk. A body posted to it is ignored.
380
350
 
381
- Two things worth knowing about this failure if you go looking for it.
351
+ **The response stays open until the holes finish.** Measured on a deployed
352
+ worker, for a page whose hole takes 2.5 s:
382
353
 
383
- You cannot detect this by inspecting the response. Nothing added a
384
- `Vary: Cookie`, and looking for `Set-Cookie` only catches the cookie version —
385
- an `x-user-id`, a CSRF token or a locale header leaks the same way.
386
-
387
- The reliable question is structural: who owns the response head on this route?
354
+ ```
355
+ headers 55 ms
356
+ first body byte 56 ms
357
+ shell heading 56 ms ← the page is on screen here
358
+ fallback markup 56 ms
359
+ hole content 2549 ms ← same response, no second request
360
+ stream complete 2549 ms
361
+ ```
388
362
 
389
- It fails both ways. The loud one is a session leaking to the next visitor. The
390
- quiet one is the reverse — an anonymous response cached first, then served to
391
- someone who should have had a session, silently signing them out. The first gets
392
- reported; the second looks like a flaky login.
363
+ The shell paints at the same moment either way, but `load` fires when the
364
+ slowest boundary resolves. Page metrics that key on `load` read differently,
365
+ and a proxy with a short response timeout needs to allow for the whole render
366
+ rather than just the shell.