@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/edge-caching.md
CHANGED
|
@@ -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
|
|
6
|
-
|
|
5
|
+
A page that exports `revalidate` can be kept by a CDN and served to everyone
|
|
6
|
+
without touching your origin:
|
|
7
7
|
|
|
8
|
-
|
|
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,
|
|
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
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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
|
-
|
|
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
|
-
|
|
45
|
-
|
|
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-
|
|
89
|
+
Cache-Tag: posts,post:1
|
|
50
90
|
```
|
|
51
91
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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
|
-
|
|
58
|
-
|
|
96
|
+
```ts
|
|
97
|
+
import { installPurge } from '@rsc-kit/core/purge'
|
|
59
98
|
|
|
60
|
-
|
|
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
|
|
63
|
-
|
|
64
|
-
|
|
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
|
-
|
|
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
|
-
|
|
72
|
-
|
|
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
|
-
|
|
76
|
-
|
|
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
|
-
|
|
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
|
-
|
|
104
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
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
|
-
|
|
166
|
-
installs, shared by every instance, or else this process's memory, 64 MB at
|
|
167
|
-
most
|
|
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
|
|
178
|
-
| Vercel | Redis (Upstash) | `installCacheStore()
|
|
179
|
-
| Homeport | Homeport's edge | nothing
|
|
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
|
-
|
|
182
|
-
|
|
183
|
-
|
|
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
|
|
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
|
-
|
|
191
|
-
|
|
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
|
-
|
|
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
|
-
|
|
198
|
-
|
|
199
|
-
|
|
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
|
-
```
|
|
202
|
-
|
|
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
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
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
|
-
|
|
255
|
-
|
|
256
|
-
|
|
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
|
-
|
|
265
|
-
|
|
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
|
-
|
|
270
|
-
origin
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
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
|
|
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.
|
|
298
|
-
cache](/hosts/deployment
|
|
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
|
-
|
|
323
|
-
|
|
324
|
-
|
|
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
|
-
|
|
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
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
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
|
-
|
|
310
|
+
### Guarded routes are never cached
|
|
343
311
|
|
|
344
|
-
The shell endpoint answers `404` for any route that declares middleware
|
|
345
|
-
page
|
|
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
|
|
350
|
-
|
|
351
|
-
|
|
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
|
-
##
|
|
320
|
+
## Behind an auth proxy
|
|
354
321
|
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
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
|
-
|
|
360
|
-
|
|
361
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
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
|
-
**
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
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
|
-
|
|
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
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
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
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
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.
|