@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.
Files changed (60) hide show
  1. package/dist/recipes.js +12 -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 +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 +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
@@ -2,10 +2,8 @@
2
2
 
3
3
  > A release step or a worker is the same app started differently — ./app migrate.
4
4
 
5
- A server is not the only thing an app runs. A deploy runs migrations before the
6
- new version takes traffic; a queue is drained by a process that serves nothing.
7
- Those are your code, with your dependencies, started differently. A file in
8
- `src/commands` is one of them:
5
+ A command is your app's code started without the server: a migration before a
6
+ deploy, a queue worker, a nightly cleanup. Put a file in `src/commands`:
9
7
 
10
8
  ```ts title="src/commands/migrate.ts"
11
9
  import { db } from '@repo/database'
@@ -21,21 +19,45 @@ export default async function (args: string[]) {
21
19
  ```
22
20
 
23
21
  ```sh
24
- ./app migrate --dry-run # the compiled binary
22
+ ./app migrate --dry-run # the compiled binary
25
23
  node .output/server/commands.mjs migrate --dry-run # a plain build, node or bun
26
24
  ```
27
25
 
28
- `./app` with no arguments starts the server, as it always did. Anything else
29
- is a command, and the server is not started: a release step run beside the
30
- version that is serving does not try to bind its port.
26
+ `./app` with no arguments starts the server. Anything else runs a command, and
27
+ the server is not started, so a release step beside the running version doesn't
28
+ try to bind its port.
31
29
 
32
- The command is built into the same bundle as the pages, so `@repo/database`
33
- and the logger are the packages the pages use. There is no second build, and
34
- a binary carries the commands inside it.
30
+ Commands are built into the same bundle as the pages. `@repo/database` and the
31
+ logger are the same packages the pages use, and a binary carries the commands
32
+ inside it.
35
33
 
36
- ## What a command is
34
+ A Laravel app's commands are artisan's. See [Laravel commands](/hosts/laravel/commands).
37
35
 
38
- A file directly in `src/commands`, with a default export:
36
+ ## Writing a command
37
+
38
+ A command is a file directly in `src/commands` with a default export. Wrap it in
39
+ `defineCommand` to get the types without writing them:
40
+
41
+ ```ts title="src/commands/seed.ts"
42
+ import { parseArgs } from 'node:util'
43
+ import { defineCommand } from '@rsc-kit/core/commands'
44
+ import { db } from '@repo/database'
45
+
46
+ export const description = 'Fill the database with sample data'
47
+ export const register = true
48
+
49
+ export default defineCommand(async (args) => {
50
+ const { values } = parseArgs({ args, options: { count: { type: 'string', default: '10' } } })
51
+
52
+ await db.seed({ count: Number(values.count) })
53
+ })
54
+ ```
55
+
56
+ ```sh
57
+ ./app seed --count 50
58
+ ```
59
+
60
+ The full signature, if you prefer to annotate it yourself:
39
61
 
40
62
  ```ts
41
63
  import type { CommandContext } from '@rsc-kit/core/commands'
@@ -43,30 +65,43 @@ import type { CommandContext } from '@rsc-kit/core/commands'
43
65
  export default async function (args: string[], context: CommandContext): Promise<number | void>
44
66
  ```
45
67
 
46
- `defineCommand` types a command without writing the annotations, and returns
47
- what it is given: `export default defineCommand(async (args, context) => {})`.
48
- Both come from `@rsc-kit/core/commands`.
49
-
50
68
  - **`args`** is everything after the name, untouched. `./app migrate --help`
51
- gives `migrate` the `--help`; parsing is yours.
52
- - **The return value** is the exit code. Nothing is `0`; a number from `0` to
53
- `255` is itself. A command that throws exits `1` with the error and every
54
- `cause` under it, each on a `[cause]:` line - a library that wraps an error,
55
- as Drizzle does a failed query, keeps the reason there. One that returns
56
- anything else (a string, `256`) exits `1` and says what it returned, rather
57
- than reporting success for a mistake.
58
- - **`context`** is `{ signal, deadline }`: `signal` is aborted by `SIGTERM` and
59
- `SIGINT`, and `deadline` says when the command has to be done. See
69
+ gives `migrate` the `--help`. Parsing is yours.
70
+ - **`context`** is `{ signal, deadline }`, for knowing when to stop. See
60
71
  [a worker](#a-worker).
61
- - **`description`**, an optional exported string, is what `./app --help` lists
62
- beside the name. It is read from the source as a plain string literal, so
63
- listing never loads a command.
72
+ - **`description`**, an optional exported string, is what `./app --help` shows
73
+ beside the name. It must be a plain string literal: the build reads it from
74
+ the source, so listing never loads a command.
75
+
76
+ The name is the file name without its extension. These are not commands:
77
+
78
+ - declaration files and tests (`*.test.ts`, `*.spec.ts`);
79
+ - anything starting with `_` or `.`, so `_shared.ts` is how two commands share
80
+ a helper;
81
+ - a file in a folder. `src/commands/db/seed.ts` is not `db:seed`, and the build
82
+ says so rather than leaving it out silently.
83
+
84
+ ## Exit codes
85
+
86
+ The return value is the exit code:
87
+
88
+ ```ts title="src/commands/check-links.ts"
89
+ export default async function () {
90
+ const broken = await findBrokenLinks()
64
91
 
65
- The name is the file's name without its extension. Declaration files, tests
66
- (`*.test.ts`, `*.spec.ts`) and anything starting with `_` or `.` are not
67
- commands — `_shared.ts` is how two commands share a helper. A folder is not
68
- one either: `src/commands/db/seed.ts` is not `db:seed`, and the build says so
69
- rather than leaving it out silently.
92
+ for (const link of broken) console.error(link)
93
+
94
+ return broken.length > 0 ? 1 : 0
95
+ }
96
+ ```
97
+
98
+ - Returning nothing exits `0`. A number from `0` to `255` exits with itself.
99
+ - A command that throws exits `1`, printing the error and every `cause` under
100
+ it, each on a `[cause]:` line. A library that wraps an error, as Drizzle does
101
+ a failed query, keeps the reason there.
102
+ - Returning anything else (a string, `256`) exits `1` and says what was
103
+ returned, rather than reporting success for a mistake.
104
+ - An unknown name exits `2`, as any misused tool does.
70
105
 
71
106
  ```
72
107
  $ ./app --help
@@ -83,35 +118,74 @@ $ echo $?
83
118
  2
84
119
  ```
85
120
 
86
- `2` is an unknown name, as it is for any tool that was misused. `1` is a
87
- command that failed. A deploy that runs `./app migrate` and stops on a
88
- non-zero exit is therefore right in both cases.
121
+ A deploy that runs `./app migrate` and stops on a non-zero exit is right in
122
+ both failure cases.
123
+
124
+ ## Running one
125
+
126
+ Anywhere that can run the binary or the build. One image serves the server and
127
+ every command:
128
+
129
+ ```dockerfile
130
+ FROM oven/bun:1
131
+ COPY dist/app /app
132
+ ENTRYPOINT ["/app"]
133
+ CMD []
134
+ ```
135
+
136
+ ```sh
137
+ docker run --rm myapp # the server
138
+ docker run --rm myapp migrate # a release step
139
+ docker run --rm myapp work # a worker
140
+ ```
89
141
 
90
- ## `register()` is yours to ask for
142
+ The same shape is a Kubernetes Job's `args: ["migrate"]`, a platform's release
143
+ command, or a process type. Nothing in `src/commands` names a platform.
144
+
145
+ From a plain build, run the commands entry:
146
+
147
+ ```sh
148
+ node .output/server/commands.mjs migrate
149
+ bun .output/server/commands.mjs migrate
150
+ ```
151
+
152
+ <Aside type="caution" title="Not .output/server/index.mjs">
153
+ `index.mjs` is the server, and it does not read its arguments:
154
+ `node .output/server/index.mjs migrate` starts the server and migrates
155
+ nothing. `commands.mjs` is the entry for a command. The binary has both
156
+ behind one file because it knows from `argv` which was meant.
157
+ </Aside>
158
+
159
+ The build lists the commands in `.output/rsc-kit.json` as `commands`, for a
160
+ deploy that wants to check before running one.
161
+
162
+ ## Using the server's setup
91
163
 
92
164
  `src/instrumentation.ts` [validates the environment at import and connects in
93
- `register()`](/guides/instrumentation/). That is the server's environment, and
94
- a command does not load the file unless it asks:
95
-
96
- - **By default it is not imported.** A command runs from a bundle of its own,
97
- without instrumentation.ts, so a queue drain does not fail on the web's
98
- Stripe key. What the command imports still runs: one that imports
99
- `@repo/env/worker` validates the worker's schema, and only that one.
100
- - **`export const register = true`** runs the command from the server's
101
- bundle: instrumentation.ts is imported, so its environment is validated, and
102
- `register()` runs before the command. It has to be the literal `true` - the
103
- build reads it off the source to choose the bundle, and a computed one is
104
- refused when it is true.
105
-
106
- A command that opted in gets `register()` first, and `shutdown()` after it
107
- returns — also after it throws — so the pool `register()` opened is closed.
108
- Both are bounded by `RSC_SHUTDOWN_TIMEOUT` (10 seconds), and a `shutdown()`
109
- that fails is reported without changing the command's exit code.
165
+ `register()`](/guides/instrumentation/). A command doesn't load it unless it
166
+ asks:
167
+
168
+ ```ts
169
+ export const register = true
170
+ ```
171
+
172
+ - **By default it is not imported.** The command runs from a bundle of its own,
173
+ without instrumentation.ts, so a queue drain doesn't fail on the web's Stripe key.
174
+ What the command imports still runs: one that imports `@repo/env/worker`
175
+ validates the worker's schema, and only that one.
176
+ - **With `export const register = true`**, the command runs from the server's bundle. instrumentation.ts is
177
+ imported, so its environment is validated, and `register()` runs before the
178
+ command. It has to be the literal `true`: the build reads it from the source
179
+ to choose the bundle, and refuses a computed one that is true.
180
+
181
+ A command that opted in gets `register()` first and `shutdown()` after it
182
+ returns, or after it throws, so the pool `register()` opened is closed. Both are
183
+ bounded by `RSC_SHUTDOWN_TIMEOUT` (10 seconds). A `shutdown()` that fails is
184
+ reported without changing the command's exit code.
110
185
 
111
186
  ## A worker
112
187
 
113
- A command that does not return until it is told to is a worker, and `signal` is
114
- how it is told:
188
+ A command that runs until it is told to stop is a worker. `signal` tells it:
115
189
 
116
190
  ```ts title="src/commands/work.ts"
117
191
  import { defineCommand } from '@rsc-kit/core/commands'
@@ -125,29 +199,21 @@ export default defineCommand(async (_args, { signal }) => {
125
199
  ```
126
200
 
127
201
  The first `SIGTERM` or `SIGINT` aborts `signal` and gives the command
128
- `RSC_SHUTDOWN_TIMEOUT` seconds to return; if it does, the process runs
129
- `shutdown()` and exits with the command's code, `0` for a clean stop. A
130
- command that does not stop in time exits `1`, and says so. A second signal does
131
- not wait.
132
-
133
- `signal.aborted` is the state; the `abort` event is the moment it changed, and
134
- it happens once. A stop reaches a command one of two ways, so a command that
135
- waits needs both:
136
-
137
- - **Before it starts.** A stop that arrives while the app loads (tens of
138
- milliseconds) or while `register()` runs finds `signal` already aborted, and
139
- the event has fired before the command could listen for it. The runner gives
140
- the event loop one turn before it calls the command, so a stop that was
141
- waiting is delivered first, the same on Node, on Bun and in a binary.
142
- - **After it starts.** The usual case: `signal.aborted` turns true and the
143
- `abort` event fires while the command is running.
144
-
145
- So check `signal.aborted` as the command starts, and listen for `abort` for the
146
- rest, or pass `signal` to something that does (`sleep(ms, undefined, { signal })`
147
- rejects at once). Checking alone misses a stop that arrives during a long
148
- `await`; listening alone waits for ever on a stop that came first. The worker
149
- above does both: the loop tests `signal.aborted`, and `queue.processNext({ signal })`
150
- returns when the signal aborts. A command that only waits does it by hand:
202
+ `RSC_SHUTDOWN_TIMEOUT` seconds to return. If it does, the process runs
203
+ `shutdown()` and exits with the command's code, `0` for a clean stop. A command
204
+ that doesn't stop in time exits `1` and says so. A second signal doesn't wait.
205
+
206
+ ### Catching a stop that came early
207
+
208
+ A stop can arrive before the command starts, while the app loads or while
209
+ `register()` runs. Then `signal` is already aborted, and the `abort` event fired
210
+ before the command could listen. So a command that waits needs both: it checks
211
+ `signal.aborted` as it starts, and listens for `abort` after that, or passes
212
+ `signal` to something that does.
213
+
214
+ The worker above does both: the loop tests `signal.aborted`, and
215
+ `queue.processNext({ signal })` returns when the signal aborts. A command that
216
+ only waits does it by hand:
151
217
 
152
218
  ```ts
153
219
  export default defineCommand(async (_args, { signal }) => {
@@ -156,10 +222,26 @@ export default defineCommand(async (_args, { signal }) => {
156
222
  })
157
223
  ```
158
224
 
159
- `deadline` is when the runner stops waiting, as a `Date.now()` time, and is
160
- `undefined` until the command has been told to stop. A worker finishing its
161
- current job reads `context.deadline - Date.now()` instead of working out
162
- `RSC_SHUTDOWN_TIMEOUT` and its default for itself.
225
+ Checking alone misses a stop during a long `await`. Listening alone waits for
226
+ ever on a stop that came first. The runner gives the event loop one turn before
227
+ it calls the command, so a waiting stop is delivered first, on Node, on Bun and
228
+ in a binary.
229
+
230
+ ### Finishing the current job
231
+
232
+ `deadline` is when the runner stops waiting, as a `Date.now()` time. It is
233
+ `undefined` until the command has been told to stop:
234
+
235
+ ```ts
236
+ export default defineCommand(async (_args, context) => {
237
+ while (!context.signal.aborted) {
238
+ const job = await queue.next()
239
+ const budget = context.deadline ? context.deadline - Date.now() : Infinity
240
+
241
+ await job.run({ timeout: budget })
242
+ }
243
+ })
244
+ ```
163
245
 
164
246
  <Aside type="caution" title="Do not destructure deadline">
165
247
  `deadline` is a getter, because what it answers changes. Destructuring it,
@@ -168,152 +250,121 @@ current job reads `context.deadline - Date.now()` instead of working out
168
250
  moment you need it. `signal` is a plain property and can be destructured.
169
251
  </Aside>
170
252
 
171
- The process is kept alive for as long as the command runs, whether or not it is
172
- holding a socket. A command waiting only for its signal waits, on Node as on
173
- Bun.
253
+ The process stays alive for as long as the command runs, whether or not it holds
254
+ a socket, on Node as on Bun.
174
255
 
175
- ## Where a stop is held from
256
+ ## Running on a schedule
176
257
 
177
- The entry holds `SIGTERM` and `SIGINT` from its first line, however long the app
178
- takes to load after that, and hands them to the command. Before its first line
179
- a stop is the operating system's, and ends the process with `143`.
258
+ A scheduler is a worker that runs whatever is due every minute, the way
259
+ Laravel's `schedule:work` does:
180
260
 
181
- In a plain build that is nearly nothing: `commands.mjs` is a few lines and
182
- loads the app after it holds the signals. In a compiled binary there is one
183
- more step first. Bun parses every module in the file before it runs the
184
- first line of any, so a stop that arrives while a large app is being parsed
185
- (a production app measured about 170 ms, for 580 modules and 119 MB) ends the
186
- process.
187
-
188
- `--splitting` makes the entry its own chunk, which runs first and loads the rest
189
- after it. The scaffold's `compile` script has it:
190
-
191
- ```sh
192
- bun build --compile --splitting .output/server/compile.mjs --outfile dist/app
193
- ```
194
-
195
- An app made before it was added keeps its old script until you add the flag. An
196
- app with no `src/commands` has nothing to split, so the flag changes nothing
197
- there until it gets one. `dist/app` is still the only file the binary needs. A Bun that does not accept the pair fails the compile
198
- loudly: 1.2 says `cannot use --compile with --splitting`.
199
-
200
- <Aside type="note" title="--sourcemap writes files beside the binary">
201
- With `--compile`, `--sourcemap` embeds the map in the binary, and Bun also
202
- writes `.map` files next to the output: one for a plain compile, and one per
203
- chunk with `--splitting` (46 for the example, about 224 for a large app, and a
204
- `dist` that grew to 201 MB). The binary does not read them. A stack trace is
205
- the same with them deleted, so ship the binary alone and remove them
206
- (`rm -f dist/*.map`), or leave `--sourcemap` off, which is what the scaffold
207
- does. `--sourcemap=inline` does not prevent them under `--compile`. What
208
- a trace shows either way is in [reading a stack trace](/hosts/where-it-runs/#reading-a-stack-trace).
209
- </Aside>
210
-
211
- The same stop, 250 ms into starting a binary of 80 MB, exits `143` without it
212
- and `0` with it, and `./app --help` answers without loading the app.
213
- Nothing else about the binary changes: the server, the frozen pages and the
214
- commands run from it as they do without.
215
-
216
- ## Asking needs no environment
217
-
218
- `./app --help` and a name that is not a command are answered before the app is
219
- loaded, from a list the build wrote into the entry. A build machine, or a CI step
220
- that has no database url, can list the commands. A real command loads the app,
221
- and so validates the environment, and fails there with the schema's own message.
261
+ ```ts title="src/commands/schedule.ts"
262
+ import { setTimeout as sleep } from 'node:timers/promises'
263
+ import { defineCommand } from '@rsc-kit/core/commands'
222
264
 
223
- ## Running one
265
+ export const register = true
224
266
 
225
- Anywhere that can run the binary or the build:
267
+ export default defineCommand(async (_args, { signal }) => {
268
+ while (!signal.aborted) {
269
+ await runDueJobs()
226
270
 
227
- ```dockerfile
228
- FROM oven/bun:1
229
- COPY dist/app /app
230
- ENTRYPOINT ["/app"]
231
- CMD []
271
+ // Wakes at once when told to stop, instead of finishing the minute.
272
+ await sleep(60_000, undefined, { signal }).catch(() => {})
273
+ }
274
+ })
232
275
  ```
233
276
 
234
277
  ```sh
235
- docker run --rm myapp # the server
236
- docker run --rm myapp migrate # a release step
237
- docker run --rm myapp work # a worker
278
+ ./app schedule
238
279
  ```
239
280
 
240
- The same shape is a Kubernetes Job's `args: ["migrate"]`, a release command, or
241
- a process type. It does not matter to the app which: an image whose entrypoint
242
- is the binary starts a server or a command depending on the argument, and
243
- nothing in `src/commands` names a platform.
281
+ Run **one** of it. Two would run every job twice, which is also why the web
282
+ server is not the place for a schedule: it runs as many copies as you scale it
283
+ to.
284
+
285
+ A job that runs once a day doesn't need a process waiting for it. Let the
286
+ platform's cron run an ordinary command, such as `./app cleanup`, and let it
287
+ exit.
244
288
 
245
- From a plain build, run the commands entry, not the server's:
289
+ ## While developing
290
+
291
+ `vite dev` has no build to run a command from. Run the file instead:
246
292
 
247
293
  ```sh
248
- node .output/server/commands.mjs migrate
249
- bun .output/server/commands.mjs migrate
294
+ bun src/commands/migrate.ts
250
295
  ```
251
296
 
252
- <Aside type="caution" title="Not .output/server/index.mjs">
253
- `index.mjs` is the server, and it does not read its arguments:
254
- `node .output/server/index.mjs migrate` starts the server and migrates
255
- nothing. `commands.mjs` is the entry for a command. The binary has both
256
- behind one file because it knows from `argv` which was meant.
257
- </Aside>
297
+ Bun runs TypeScript and follows the app's `tsconfig` paths.
258
298
 
259
- The build lists them in `.output/rsc-kit.json` as `commands`, for a deploy that
260
- wants to know before running one.
299
+ ## Where commands aren't built
261
300
 
262
- ## Where there are no commands
301
+ Commands are built for the `bun` and `node` presets, whose output is a process.
263
302
 
264
- - **`vite dev`.** There is no build to run one from. While developing, run the
265
- file: `bun src/commands/migrate.ts` (Bun runs TypeScript, and the app's
266
- `tsconfig` paths).
267
303
  - **A Worker.** The bundle targets workerd, where a database driver or
268
- `node:child_process` is a different build of the package or not there at all,
269
- so a command run from it would run something other than what you wrote. The
270
- build leaves `src/commands` out and says so. A Worker's release step is the
271
- file itself, run in CI before the deploy, or `wrangler` for D1.
272
- - **Any other preset** (Vercel, Netlify, Deno): commands are built for the
273
- `bun` and `node` presets, whose output is a process. Anywhere else the build
274
- says so and leaves `src/commands` out; run the file from CI.
304
+ `node:child_process` is a different build of the package or not there at all.
305
+ The build leaves `src/commands` out and says so. Run the file in CI before the
306
+ deploy, or use `wrangler` for D1.
307
+ - **Any other preset** (Vercel, Netlify, Deno): the build says so and leaves
308
+ `src/commands` out. Run the file from CI.
275
309
 
276
- ## Recurring work
310
+ ## Common mistakes
277
311
 
278
- A scheduler is a command that keeps running, the way Laravel's `schedule:work`
279
- is. Start one instance of it; every minute it runs whatever is due, and it
280
- stops when it is told to:
312
+ **`process.exit()` inside a command skips `shutdown()`.** Return the code
313
+ instead. The runner exits for you, after closing what `register()` opened.
281
314
 
282
- ```ts title="src/commands/schedule.ts"
283
- import { setTimeout as sleep } from 'node:timers/promises'
284
- import { defineCommand } from '@rsc-kit/core/commands'
315
+ **A package with an import side effect runs it for the server too, if a page
316
+ imports it.** The command's own module runs only when it is named. What it
317
+ imports is the bundle's, shared with the pages.
285
318
 
286
- export const register = true
319
+ **A command is not a route.** It isn't prerendered and has no request, so
320
+ `headers()` and `cookies()` have nothing to read.
287
321
 
288
- export default defineCommand(async (_args, { signal }) => {
289
- while (!signal.aborted) {
290
- await runDueJobs()
322
+ ## How it works
291
323
 
292
- // Wakes at once when told to stop, instead of finishing the minute.
293
- await sleep(60_000, undefined, { signal }).catch(() => {})
294
- }
295
- })
296
- ```
324
+ ### Listing needs no environment
297
325
 
298
- ```sh
299
- ./app schedule
300
- ```
326
+ `./app --help` and a name that isn't a command are answered before the app
327
+ loads, from a list the build wrote into the entry. A build machine or a CI step
328
+ with no database URL can list the commands. A real command loads the app,
329
+ validates the environment, and fails there with the schema's own message.
330
+
331
+ ### Where a stop is held from
332
+
333
+ The entry holds `SIGTERM` and `SIGINT` from its first line, however long the app
334
+ takes to load, and hands them to the command. Before that line, a stop is the
335
+ operating system's and ends the process with `143`.
301
336
 
302
- Run **one** of it. Two would run every job twice, which is why the web server
303
- is not the place for a schedule: it runs as many copies as you scale it to.
337
+ In a plain build that window is tiny: `commands.mjs` is a few lines and loads
338
+ the app after it holds the signals. A compiled binary has one more step. Bun
339
+ parses every module in the file before running the first line of any, so a stop
340
+ while a large app is parsed (about 170 ms for a production app of 580 modules
341
+ and 119 MB) ends the process.
304
342
 
305
- A job that runs once a day does not need a process waiting for it. Let the
306
- platform's cron run an ordinary command (`./app cleanup`) and let it exit.
343
+ `--splitting` makes the entry its own chunk, which runs first and loads the rest
344
+ after. The scaffold's `compile` script has it:
307
345
 
308
- ## Things that fail quietly
346
+ ```sh
347
+ bun build --compile --splitting .output/server/compile.mjs --outfile dist/app
348
+ ```
309
349
 
310
- **A `process.exit()` inside a command skips `shutdown()`.** Return the code
311
- instead; the runner exits for you, after it has closed what `register()` opened.
350
+ - An app made before the flag was added keeps its old script until you add it.
351
+ - An app with no `src/commands` has nothing to split, so the flag changes
352
+ nothing there.
353
+ - `dist/app` is still the only file the binary needs.
354
+ - A Bun that doesn't accept the pair fails the compile loudly. 1.2 says
355
+ `cannot use --compile with --splitting`.
312
356
 
313
- **A command that imports a package with a side effect at import runs it for the
314
- server too, if a page imports that package.** Commands are loaded only when
315
- named, so the command's own module never runs in a server. What it imports is
316
- the bundle's, shared with the pages.
357
+ The same stop, 250 ms into starting an 80 MB binary, exits `143` without the
358
+ flag and `0` with it, and `./app --help` answers without loading the app.
359
+ Nothing else about the binary changes.
317
360
 
318
- **A command is not a route.** It is not prerendered, has no request, and
319
- `headers()` and `cookies()` have nothing to read. It is your code, not a page's.
361
+ <Aside type="note" title="--sourcemap writes files beside the binary">
362
+ With `--compile`, `--sourcemap` embeds the map in the binary, and Bun also
363
+ writes `.map` files next to the output: one for a plain compile, and one per
364
+ chunk with `--splitting` (46 for the example, about 224 for a large app, and a
365
+ `dist` that grew to 201 MB). The binary does not read them. A stack trace is
366
+ the same with them deleted, so ship the binary alone and remove them
367
+ (`rm -f dist/*.map`), or leave `--sourcemap` off, which is what the scaffold
368
+ does. `--sourcemap=inline` does not prevent them under `--compile`. What
369
+ a trace shows either way is in [reading a stack trace](/hosts/where-it-runs/#reading-a-stack-trace).
370
+ </Aside>