@rsc-kit/mcp 0.41.0 → 0.42.1

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 +18 -2
  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 -252
  7. package/guides/bap-vs-inertia.md +67 -25
  8. package/guides/blueprints.md +300 -169
  9. package/guides/bun.md +42 -25
  10. package/guides/caching.md +46 -49
  11. package/guides/coming-from-inertia.md +2 -2
  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 -542
  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 +270 -216
  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 +269 -147
  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 -539
  59. package/guides/laravel.md +0 -787
  60. package/guides/your-own-backend.md +0 -602
package/guides/images.md CHANGED
@@ -2,18 +2,23 @@
2
2
 
3
3
  > Responsive images with no optimizer to run — unpic for a CDN, imagetools for files in the repo.
4
4
 
5
- There is no image component and no image server. `next/image` is two things
6
- glued together: a component that writes `srcset` and `sizes` for you, and an
7
- optimizer that resizes on the fly — a process with sharp in it, a cache to
8
- manage, and a CPU bill on every cold hit. The first half is worth having. The
9
- second belongs to whatever already serves your images.
5
+ There is no image component and no image server. Images on a CDN get their
6
+ `srcset` from [unpic](https://unpic.pics), and images in your repository are
7
+ resized once, at build time:
8
+
9
+ ```tsx title="src/app/page.tsx"
10
+ import { Image } from '@unpic/react';
11
+
12
+ <Image src="https://res.cloudinary.com/demo/image/upload/sample.jpg" width={800} height={600} alt="A sample" />
13
+ ```
14
+
15
+ Neither runs at request time, and neither adds JavaScript to the page.
10
16
 
11
17
  ## Images on a CDN
12
18
 
13
- If the file lives on Cloudinary, imgix, Cloudflare Images, Bunny, Vercel,
14
- Netlify or any of the [other providers unpic knows](https://unpic.pics/img/react/),
15
- it already has a resizing url. [unpic](https://unpic.pics) writes the `srcset`
16
- against it:
19
+ If the file is on Cloudinary, imgix, Cloudflare Images, Bunny, Vercel, Netlify
20
+ or any of the [other providers unpic knows](https://unpic.pics/img/react/), it
21
+ already has a resizing URL. unpic writes the `srcset` against it:
17
22
 
18
23
  ```tsx title="src/app/page.tsx"
19
24
  import { Image } from '@unpic/react';
@@ -28,18 +33,52 @@ import { Image } from '@unpic/react';
28
33
  ```
29
34
 
30
35
  It is a plain component, so it renders in a server component and the browser
31
- receives an `<img>` — `srcset` from 640w up, `sizes`, `aspect-ratio`,
32
- `loading="lazy"`, `decoding="async"` — and none of unpic's code. The route's
33
- javascript does not change. The CDN is detected from the url; nothing to
34
- configure.
36
+ receives only an `<img>`:
37
+
38
+ - a `srcset` from 640w up, and `sizes`;
39
+ - an `aspect-ratio`, so the layout doesn't jump;
40
+ - `loading="lazy"` and `decoding="async"`.
41
+
42
+ None of unpic's code reaches the browser. The CDN is detected from the URL, so
43
+ there is nothing to configure. TanStack Start and Astro use the same library.
44
+
45
+ ### A hero image
46
+
47
+ The image at the top of a page should load at once. Turn lazy loading off and
48
+ raise its priority:
49
+
50
+ ```tsx
51
+ <Image
52
+ src="https://res.cloudinary.com/demo/image/upload/hero.jpg"
53
+ layout="fullWidth"
54
+ height={480}
55
+ priority
56
+ alt="…"
57
+ />
58
+ ```
59
+
60
+ `layout="fullWidth"` fills the container's width. `priority` sets
61
+ `loading="eager"` and `fetchpriority="high"`.
35
62
 
36
- TanStack Start and Astro point at the same library, for the same reason.
63
+ ### An uploaded image
64
+
65
+ A user's upload saved to a CDN is the same: store the URL, then pass it to
66
+ `Image`:
67
+
68
+ ```tsx title="src/app/users/[id]/page.tsx"
69
+ import { Image } from '@unpic/react';
70
+
71
+ export default async function Profile({ params }: { params: Promise<{ id: string }> }) {
72
+ const user = await findUser((await params).id);
73
+
74
+ return <Image src={user.avatarUrl} width={96} height={96} alt={user.name} />;
75
+ }
76
+ ```
37
77
 
38
78
  ## Images in the repository
39
79
 
40
- A file under `src/` is resized once, at build time, by
41
- [vite-imagetools](https://github.com/JonasKruckenberg/imagetools). Query
42
- parameters on the import say what you want:
80
+ [vite-imagetools](https://github.com/JonasKruckenberg/imagetools) resizes a file
81
+ under `src/` once, when the app builds. Add the plugin:
43
82
 
44
83
  ```ts title="vite.config.ts"
45
84
  import { imagetools } from 'vite-imagetools';
@@ -49,6 +88,8 @@ export default defineConfig({
49
88
  });
50
89
  ```
51
90
 
91
+ Then say what you want in the import's query:
92
+
52
93
  ```tsx title="src/app/page.tsx"
53
94
  import hero from '../hero.png?w=400;800;1200&format=webp&as=srcset';
54
95
  import heroSrc from '../hero.png?w=800&format=webp';
@@ -56,14 +97,17 @@ import heroSrc from '../hero.png?w=800&format=webp';
56
97
  <img srcSet={hero} src={heroSrc} sizes="(min-width: 800px) 800px, 100vw" width={800} height={600} alt="…" />
57
98
  ```
58
99
 
59
- The build emits `hero-<hash>.webp` at each width into `assets/`, hashed and
60
- cacheable forever, and a page that is frozen carries the urls. It costs the
61
- build what resizing costs, once per image per width, and nothing at request
62
- time. It is opt-in for that reason: a hundred hero images at four widths is a
100
+ The build writes `hero-<hash>.webp` at each width into `assets/`, hashed and
101
+ cacheable forever. A stored page carries the URLs.
102
+
103
+ Resizing costs build time, once per image per width, and nothing at request
104
+ time. That is why it is opt-in: a hundred hero images at four widths is a
63
105
  noticeable build, and most of them belong on a CDN.
64
106
 
65
- Declare the queries so TypeScript stops asking — and the build's typecheck
66
- lets them through:
107
+ ### Typing the imports
108
+
109
+ Declare the queries, so TypeScript accepts them and the build's typecheck lets
110
+ them through:
67
111
 
68
112
  ```ts title="src/images.d.ts"
69
113
  // A pattern may hold one `*`, so each matches on the tail of the query.
@@ -77,13 +121,28 @@ declare module '*&format=webp' {
77
121
  }
78
122
  ```
79
123
 
80
- ## What to pick
124
+ ## Icons and logos
81
125
 
82
- | the image is | use |
126
+ A small image that doesn't need resizing is a plain `<img>`, or an SVG inlined
127
+ as a component:
128
+
129
+ ```tsx
130
+ <img src="/logo.svg" width={120} height={32} alt="Acme" />
131
+ ```
132
+
133
+ ## Choosing one
134
+
135
+ | The image is | Use |
83
136
  | --- | --- |
84
- | user-uploaded, or on a CDN already | unpic |
137
+ | uploaded by users, or already on a CDN | unpic |
85
138
  | in the repo, a handful | imagetools |
86
139
  | in the repo, hundreds | put them on a CDN and use unpic |
87
- | an icon or a logo | `<img>`, or inline the svg |
140
+ | an icon or a logo | `<img>`, or an inline SVG |
141
+
142
+ ## Why there is no image server
88
143
 
89
- Neither one runs at request time, and neither is on the page's javascript.
144
+ `next/image` is two things together: a component that writes `srcset` and
145
+ `sizes`, and an optimizer that resizes on the fly. The optimizer is a process
146
+ with sharp in it, a cache to manage, and CPU spent on every cold hit. The
147
+ component is worth having, and unpic is that. The resizing belongs to whatever
148
+ already serves your images.
package/guides/index.json CHANGED
@@ -1,13 +1,8 @@
1
1
  [
2
- {
3
- "slug": "admin",
4
- "title": "Admin panels",
5
- "description": "An admin panel over any data - a database, an ORM, a REST API, a Laravel or Go backend - with server or client tables, tenants and plugins."
6
- },
7
2
  {
8
3
  "slug": "admin-plugins",
9
4
  "title": "Admin plugins",
10
- "description": "Writing an @rsc-kit/press plugin, publishing it as a package, and selling it."
5
+ "description": "Adding to an @rsc-kit/press panel from a package, or from a plugin installed while the panel runs - writing one, publishing it and selling it."
11
6
  },
12
7
  {
13
8
  "slug": "api-routes",
@@ -69,11 +64,6 @@
69
64
  "title": "Content Security Policy",
70
65
  "description": "A strict policy on every page, with the engine allowing its own inline code by hash or nonce."
71
66
  },
72
- {
73
- "slug": "deployment",
74
- "title": "Deploying",
75
- "description": "What to ship, and the two settings that fail quietly."
76
- },
77
67
  {
78
68
  "slug": "domains",
79
69
  "title": "Domains and subdomains",
@@ -107,7 +97,7 @@
107
97
  {
108
98
  "slug": "fonts",
109
99
  "title": "Fonts",
110
- "description": "Self-hosted fonts from npm, and what next/font was doing for you."
100
+ "description": "Self-hosted fonts from npm, preloading the first one, and porting from next/font."
111
101
  },
112
102
  {
113
103
  "slug": "forms",
@@ -119,11 +109,6 @@
119
109
  "title": "Getting started",
120
110
  "description": "Serve React Server Components from any JavaScript backend."
121
111
  },
122
- {
123
- "slug": "go",
124
- "title": "Go",
125
- "description": "A Go process behind the renderer — functions, guards and actions in Go."
126
- },
127
112
  {
128
113
  "slug": "images",
129
114
  "title": "Images",
@@ -149,16 +134,16 @@
149
134
  "title": "ISR",
150
135
  "description": "A page rendered once and served to everyone, refreshed on a timer or the moment its data changes. On one server, several pods or a Worker, with Laravel, Go or no backend."
151
136
  },
152
- {
153
- "slug": "laravel",
154
- "title": "Laravel",
155
- "description": "React Server Components in front, a Laravel application behind them."
156
- },
157
137
  {
158
138
  "slug": "live-data",
159
139
  "title": "Live data",
160
140
  "description": "Keep what a page shows current as the data changes - refreshOn, events, polling."
161
141
  },
142
+ {
143
+ "slug": "live-data-in-production",
144
+ "title": "Live data in production",
145
+ "description": "Where refreshOn's versions are kept, and running live data on several servers, Workers and poolers."
146
+ },
162
147
  {
163
148
  "slug": "mcp",
164
149
  "title": "Working with an AI agent",
@@ -172,7 +157,7 @@
172
157
  {
173
158
  "slug": "navigation",
174
159
  "title": "Navigation",
175
- "description": "What happens when someone clicks a link, and why state survives it."
160
+ "description": "Moving between pages without a reload - visit, prefetch, progress, refresh, and measuring a click."
176
161
  },
177
162
  {
178
163
  "slug": "no-javascript",
@@ -222,12 +207,12 @@
222
207
  {
223
208
  "slug": "redirects",
224
209
  "title": "Redirects",
225
- "description": "Leaving a page from inside the render, and what that costs."
210
+ "description": "Sending the visitor somewhere else from a page, a middleware or a server action."
226
211
  },
227
212
  {
228
213
  "slug": "response-headers",
229
214
  "title": "Headers and cookies",
230
- "description": "Setting response headers and cookies during a render."
215
+ "description": "Reading request headers and cookies, setting response headers and cookies, and security headers."
231
216
  },
232
217
  {
233
218
  "slug": "route-interception",
@@ -237,7 +222,7 @@
237
222
  {
238
223
  "slug": "routing",
239
224
  "title": "Routing",
240
- "description": "File-based routes, layouts, loading states and parallel slots."
225
+ "description": "File-based routes, route parameters, layouts, loading states and parallel slots."
241
226
  },
242
227
  {
243
228
  "slug": "sections",
@@ -262,7 +247,7 @@
262
247
  {
263
248
  "slug": "testing",
264
249
  "title": "Testing",
265
- "description": "Three tiers, and the one thing that still needs a browser."
250
+ "description": "Call actions and routes as functions, fetch the whole app without a server, and keep a browser for the round trip."
266
251
  },
267
252
  {
268
253
  "slug": "third-party-scripts",
@@ -282,7 +267,7 @@
282
267
  {
283
268
  "slug": "validation",
284
269
  "title": "Validation",
285
- "description": "Surfacing server-side validation errors in a form."
270
+ "description": "Checking input before submit, and showing the server's validation errors in a form."
286
271
  },
287
272
  {
288
273
  "slug": "view-transitions",
@@ -293,10 +278,5 @@
293
278
  "slug": "where-it-runs",
294
279
  "title": "Where it runs",
295
280
  "description": "A host is a Nitro preset, not a server you write."
296
- },
297
- {
298
- "slug": "your-own-backend",
299
- "title": "Your own backend",
300
- "description": "The one endpoint a backend in any language answers."
301
281
  }
302
282
  ]
@@ -2,85 +2,71 @@
2
2
 
3
3
  > instrumentation.ts runs once, before anything else.
4
4
 
5
- Some setup belongs to the process, not to a page: validating the
6
- environment, configuring a shared package, warming a connection. Put it in
7
- `src/instrumentation.ts` and it runs once — before any page module
8
- evaluates, and before the first request.
5
+ Some setup belongs to the process, not to a page: validating the environment,
6
+ configuring a shared package, opening a connection. Put it in
7
+ `src/instrumentation.ts`, and it runs once, before any page module evaluates
8
+ and before the first request:
9
9
 
10
10
  ```ts title="src/instrumentation.ts"
11
11
  // Runs at import, before any page: a bad variable stops the server here.
12
12
  import './env'
13
13
 
14
- // Anything asynchronous the app needs before it serves. Called once per
15
- // process, at startup; the first render waits for it, later ones do not.
14
+ // Called once per process, at startup. The first render waits for it.
16
15
  export async function register() {
17
16
  await db.connect()
18
17
  }
19
18
  ```
20
19
 
21
- The name is Next's, so a reader arriving from there knows what it is. Both
22
- halves are optional: a file with only imports is a bootstrap, a file with
23
- only `register()` is a hook.
20
+ The name is Next's. Both halves are optional: a file with only imports is a
21
+ bootstrap, and a file with only `register()` is a hook.
24
22
 
25
- ## Why it is a framework file
23
+ ## Validating the environment
26
24
 
27
- Two things make this a file the framework has to know about rather than a
28
- module the app imports.
25
+ Import the module that checks your variables. The scaffold writes this line
26
+ when the project validates its environment:
29
27
 
30
- **Import order.** A page that configures a shared package at import time —
31
- a logger, an ORM, a metrics client — runs before any module the app could
32
- put in front of it, because the generated entry imports the pages. The entry
33
- imports `instrumentation.ts` *first*, so whatever it sets up is set up when
34
- the first page module evaluates. Importing a bootstrap module from every
35
- file that needs it works until someone forgets, and the failure is a page
36
- that throws "not configured" for the one visitor who reached it first.
37
-
38
- **Before the first request.** `register()` is **called once**, and every
39
- entry point a render can come through — the built server, the dev server,
40
- the prerender, a route's middleware check, a server action, an api route —
41
- waits on that one call rather than making its own. After startup it has long
42
- since finished, so a request waits on nothing: it checks a promise that has
43
- already settled. That is how "before the first render" holds without the app
44
- knowing the list of entry points, and without a request ever paying for it.
28
+ ```ts title="src/instrumentation.ts"
29
+ import './env'
30
+ ```
45
31
 
46
- ## When it runs
32
+ `src/env.ts` refuses at import, so a missing variable is the server's failure
33
+ at startup rather than a visitor's, three calls later.
47
34
 
48
- | where | when |
49
- | --- | --- |
50
- | a server (`bun`, `node`, any long-lived preset) | at startup, before the server reports itself up |
51
- | a Worker | at the isolate's first request — there is no startup, and a binding is only readable once a request has arrived |
52
- | `vite dev` | when the dev server first evaluates the app, and again when the file or anything it imports changes |
53
- | `vite build` | before the first page is prerendered |
54
-
55
- **Once per process, not once per request.** Measured on a built server and on
56
- a compiled binary, with a `register()` that logs: one call before the first
57
- request, and still one after a stream of them.
58
-
59
- A `register()` that resolved is not called again. One that *rejected* is the
60
- exception: the next request calls it again, so a database that was not up yet
61
- is asked again rather than leaving the process permanently refusing. On a
62
- server that case is rare in practice — a startup failure exits the process,
63
- below.
64
-
65
- On a server, a failure at startup — the file throwing at import, or
66
- `register()` rejecting — is reported and the process exits. A server that
67
- could not bootstrap has nothing correct to serve, and a health check that
68
- passed on a server about to fail its first visitor is worse than one that
69
- never passed. The dev server stays up and reports the error on the page.
35
+ The build prerenders pages, so it runs this file too. A build machine without
36
+ the production variables sets `SKIP_ENV_VALIDATION=1`, which the generated
37
+ schema honours. The server that runs the build still validates at startup.
70
38
 
71
- ## In a command
39
+ ## Configuring a shared package
72
40
 
73
- A [command](/guides/commands/) does not import this file unless it exports
74
- `register = true`. Without it the command runs from a bundle that leaves this
75
- file out, so neither its environment checks nor anything else at its top level
76
- run: a queue drain should not fail on the web's secrets, and validates what it
77
- imports itself. With it the file is imported, `register()` runs before the
78
- command, and `shutdown()` after it returns.
41
+ Configure a logger, an ORM or a metrics client at the top of the file. It runs
42
+ before the first page module evaluates, so no page finds it unconfigured:
43
+
44
+ ```ts title="src/instrumentation.ts"
45
+ import { configure } from '@/lib/logger'
46
+
47
+ configure({ level: process.env.LOG_LEVEL ?? 'info' })
48
+ ```
49
+
50
+ ## Connecting before the first request
51
+
52
+ Do anything asynchronous in `register()`. Every way a render can arrive waits
53
+ on that one call, and after startup it has long since finished:
54
+
55
+ ```ts title="src/instrumentation.ts"
56
+ export async function register() {
57
+ await db.connect()
58
+ await cache.warm()
59
+ }
60
+ ```
79
61
 
80
- ## Shutting down
62
+ A `register()` that resolved isn't called again. One that rejected is: the
63
+ next request calls it again, so a database that wasn't up yet is asked again
64
+ rather than leaving the process refusing for good.
81
65
 
82
- Export `shutdown()` beside `register()` to close what `register()` opened.
83
- A built server calls it when it is stopped, after in-flight requests have
66
+ ## Closing connections on shutdown
67
+
68
+ Export `shutdown()` beside `register()` to close what `register()` opened. A
69
+ built server calls it when it is stopped, after in-flight requests have
84
70
  drained, and then exits:
85
71
 
86
72
  ```ts title="src/instrumentation.ts"
@@ -93,15 +79,17 @@ export async function shutdown() {
93
79
  }
94
80
  ```
95
81
 
96
- It is bounded by `RSC_SHUTDOWN_TIMEOUT` (10 seconds), and one that throws is
97
- reported rather than holding the process. The server exits whether or not
98
- you export it; see [Stopping gracefully](/hosts/deployment/#stopping-gracefully).
99
- Not called on a Worker, which has no process to stop, or by the dev server.
82
+ - It is bounded by `RSC_SHUTDOWN_TIMEOUT` (10 seconds).
83
+ - One that throws is reported rather than holding the process.
84
+ - The server exits whether or not you export it; see
85
+ [Stopping gracefully](/hosts/deployment/#stopping-gracefully).
86
+ - It isn't called on a Worker, which has no process to stop, or by the dev
87
+ server.
100
88
 
101
89
  ## Logging requests
102
90
 
103
- Export `onRequest()` to hear about every request the app answers - a page, a
104
- navigation's payload, an action, a `route.ts` - once its response is over:
91
+ Export `onRequest()` to hear about every request the app answers (a page, a
92
+ navigation's payload, an action, a `route.ts`) once its response is over:
105
93
 
106
94
  ```ts title="src/instrumentation.ts"
107
95
  import type { ServedRequest } from '@rsc-kit/core/request'
@@ -113,50 +101,101 @@ export function onRequest({ method, pathname, status, durationMs, aborted, heade
113
101
  }
114
102
  ```
115
103
 
116
- "Over" is what middleware cannot tell you: a page streams, so the last byte
117
- leaves long after anything in front of the render has returned. `onRequest()`
118
- is called after it, with `durationMs` up to that byte. A visitor who closes the
119
- tab or navigates away mid-stream ends the response early, and `onRequest()` is
120
- called then with `aborted: true`. A render that fails, before its first byte
121
- or partway through, comes with the `error`.
104
+ To report failed renders, read `error`:
105
+
106
+ ```ts title="src/instrumentation.ts"
107
+ export function onRequest({ pathname, status, error }: ServedRequest) {
108
+ if (error) reportError(error, { pathname, status })
109
+ }
110
+ ```
111
+
112
+ What it is told:
122
113
 
123
- It is told less than the request on purpose. `pathname` has no query string,
124
- because a query carries what people typed. `headers` holds only the names
125
- `onRequestHeaders` lists, by lowercase name, and only those the request had,
126
- so a cookie or an `Authorization` header reaches it only if you name it.
114
+ - **When the response is over.** A page streams, so the last byte leaves long
115
+ after anything in front of the render has returned, which middleware can't
116
+ see. `durationMs` runs up to that byte.
117
+ - **`aborted: true`** when a visitor closes the tab or navigates away
118
+ mid-stream.
119
+ - **`error`** when a render fails, before its first byte or partway through.
120
+ - **`pathname` without the query string**, because a query carries what
121
+ people typed.
122
+ - **Only the headers `onRequestHeaders` lists**, by lowercase name, and only
123
+ those the request had. A cookie or an `Authorization` header reaches it
124
+ only if you name it.
127
125
 
128
126
  It runs beside the response, never in front of it. The body is passed through
129
- as it streams, `onRequest()` is not awaited, and one that throws or rejects is
127
+ as it streams, `onRequest()` isn't awaited, and one that throws or rejects is
130
128
  reported rather than failing the page. Static files are served before the app
131
- and are not reported. It is called in development too.
129
+ and aren't reported. It is called in development too.
130
+
131
+ ## Using it in a command
132
132
 
133
- ## Environment validation
133
+ A [command](/guides/commands/) doesn't import this file unless it exports
134
+ `register = true`:
134
135
 
135
- The scaffold writes this file when the project validates its environment,
136
- and the only line it needs is the import: `src/env.ts` refuses at import, and
137
- importing it here is what makes a missing variable the server's failure at
138
- startup rather than a visitor's three calls later.
136
+ ```ts title="src/commands/queue-drain.ts"
137
+ export const register = true
138
+ ```
139
139
 
140
- The build prerenders pages, so it runs the bootstrap too. A build machine
141
- without the production variables sets `SKIP_ENV_VALIDATION=1`, which the
142
- generated schema honours; the server that runs the build validates at
143
- startup regardless.
140
+ Without it, the command runs from a bundle that leaves this file out, so
141
+ neither its environment checks nor anything else at its top level run. A queue
142
+ drain shouldn't fail on the web's secrets; it validates what it imports itself.
143
+ With it, the file is imported, `register()` runs before the command, and
144
+ `shutdown()` after it returns.
144
145
 
145
146
  ## On a Worker
146
147
 
147
- Read bindings inside `register()`, not at the top of the module. A Worker
148
- evaluates the module before any request exists, and `process.env` is empty
149
- until Nitro maps the first request's bindings onto it — so a top-level
150
- `process.env.DATABASE_URL` is `undefined` at exactly the moment it looks
151
- like it should not be. Inside `register()`, which runs at the first request,
152
- it is there.
148
+ Read bindings inside `register()`, not at the top of the module:
153
149
 
154
- ```ts
150
+ ```ts title="src/instrumentation.ts"
155
151
  export async function register() {
156
152
  const url = process.env.DATABASE_URL // readable here on every host
157
153
  await db.connect(url)
158
154
  }
159
155
  ```
160
156
 
161
- The same file works unchanged on a server, where both moments have the
157
+ A Worker evaluates the module before any request exists, and `process.env` is
158
+ empty until Nitro maps the first request's bindings onto it. A top-level
159
+ `process.env.DATABASE_URL` is `undefined` at exactly the moment it looks like
160
+ it shouldn't be. Inside `register()`, which runs at the first request, it is
161
+ there. The same file works unchanged on a server, where both moments have the
162
162
  environment.
163
+
164
+ ## When it runs
165
+
166
+ | Where | When |
167
+ | --- | --- |
168
+ | A server (`bun`, `node`, any long-lived preset) | At startup, before the server reports itself up |
169
+ | A Worker | At the isolate's first request. There is no startup, and a binding is only readable once a request has arrived. |
170
+ | `vite dev` | When the dev server first evaluates the app, and again when the file or anything it imports changes |
171
+ | `vite build` | Before the first page is prerendered |
172
+
173
+ It runs **once per process, not once per request**. Measured on a built server
174
+ and on a compiled binary, with a `register()` that logs: one call before the
175
+ first request, and still one after a stream of them.
176
+
177
+ On a server, a failure at startup (the file throwing at import, or
178
+ `register()` rejecting) is reported and the process exits. A server that
179
+ couldn't bootstrap has nothing correct to serve, and a health check that
180
+ passed on a server about to fail its first visitor is worse than one that
181
+ never passed. The dev server stays up and reports the error on the page.
182
+
183
+ ## Why it is a framework file
184
+
185
+ Two things make this a file the framework has to know about, rather than a
186
+ module the app imports.
187
+
188
+ **Import order.** A page that configures a shared package at import time runs
189
+ before any module the app could put in front of it, because the generated
190
+ entry imports the pages. The entry imports `instrumentation.ts` *first*.
191
+ Importing a bootstrap module from every file that needs it works until someone
192
+ forgets, and the failure is a page that throws "not configured" for the one
193
+ visitor who reached it first.
194
+
195
+ **Before the first request.** `register()` is **called once**, and every entry
196
+ point a render can come through (the built server, the dev server, the
197
+ prerender, a route's middleware check, a server action, an API route) waits on
198
+ that one call rather than making its own. After startup, a request checks a
199
+ promise that has already settled. That is how "before the first render" holds
200
+ without the app knowing the list of entry points, and without a request ever
201
+ paying for it.
package/guides/isr.md CHANGED
@@ -32,7 +32,7 @@ turns it off. With Laravel the page lives under `resources/js/app`.
32
32
  A 404 is kept for a minute at most, so a scanner walking random urls cannot
33
33
  hold your pages out.
34
34
 
35
- ## See it working
35
+ ## Checking that a page is kept
36
36
 
37
37
  Pages are never kept in development, so build and start the app first. Ask for
38
38
  a page twice:
@@ -151,14 +151,14 @@ goes to every visitor. A page that shows who is signed in is not one to keep.
151
151
  A form in a kept page still works: a same-origin post needs no token. See
152
152
  [blueprints](/guides/blueprints#on-a-kept-page).
153
153
 
154
- ## On several instances
154
+ ## Running on several instances
155
155
 
156
156
  Nothing changes in the page. Each instance keeps its own copy in memory, 64 MB
157
157
  at most, and `changed()` reaches them all, because a kept page checks its tags'
158
158
  versions on each lookup.
159
159
 
160
160
  - **With Laravel or Go,** the backend holds the versions, so there is nothing to
161
- set up beyond [a shared store for Go](/hosts/go#saying-data-changed).
161
+ set up beyond [a shared store for Go](/hosts/go/data-changed).
162
162
  - **With no backend,** install a shared
163
163
  [version source](/guides/live-data), as `refreshOn` already needs.
164
164
  - **On a Worker or Vercel,** memory does not last, so give the page cache a