@k8ordo/framework 3.0.0 → 4.0.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 (55) hide show
  1. package/README.md +6 -4
  2. package/dist/cli.mjs +3 -3
  3. package/dist/generated.d.mts +12 -13
  4. package/dist/index.d.mts +3 -3
  5. package/dist/index.mjs +1 -1
  6. package/dist/paths-type-DXpmjaOr.d.mts +43 -0
  7. package/dist/runtime/app-router.mjs +1 -1
  8. package/dist/runtime/browser-payloads.mjs +1 -1
  9. package/dist/runtime/cookies.d.mts +2 -0
  10. package/dist/runtime/cookies.mjs +2 -1
  11. package/dist/runtime/ended.d.mts +7 -0
  12. package/dist/runtime/ended.mjs +14 -0
  13. package/dist/runtime/entry.browser.mjs +1 -1
  14. package/dist/runtime/entry.rsc.d.mts +17 -7
  15. package/dist/runtime/entry.rsc.mjs +42 -33
  16. package/dist/runtime/entry.ssr.d.mts +1 -0
  17. package/dist/runtime/entry.ssr.mjs +4 -3
  18. package/dist/runtime/guard.d.mts +15 -10
  19. package/dist/runtime/guard.mjs +8 -2
  20. package/dist/runtime/mount.mjs +1 -1
  21. package/dist/runtime/page-boundary.mjs +2 -2
  22. package/dist/runtime/params.d.mts +5 -4
  23. package/dist/runtime/params.mjs +2 -2
  24. package/dist/runtime/redirect.d.mts +13 -7
  25. package/dist/runtime/redirect.mjs +25 -9
  26. package/dist/runtime/request-scope.d.mts +37 -2
  27. package/dist/runtime/request-scope.mjs +60 -3
  28. package/dist/runtime/request.d.mts +6 -1
  29. package/dist/runtime/request.mjs +7 -2
  30. package/dist/runtime/route.d.mts +4 -2
  31. package/dist/runtime/route.mjs +13 -5
  32. package/dist/runtime/server.d.mts +5 -5
  33. package/dist/runtime/server.mjs +2 -2
  34. package/dist/runtime/shell.d.mts +2 -1
  35. package/dist/serve.d.mts +4 -0
  36. package/dist/serve.mjs +26 -13
  37. package/dist/{static-c-nX9Juk.d.mts → static-Be3xIqd8.d.mts} +19 -12
  38. package/dist/{static-file-BEk_xO5y.mjs → static-file-BS-WEiwu.mjs} +180 -136
  39. package/dist/{static-host-CyAmsL_7.mjs → static-host-tIDLqV8c.mjs} +1 -1
  40. package/dist/vercel.d.mts +18 -1
  41. package/dist/vercel.mjs +104 -7
  42. package/dist/vite.d.mts +6 -3
  43. package/dist/vite.mjs +155 -65
  44. package/docs/GUIDE.md +149 -21
  45. package/docs/llms.txt +102 -47
  46. package/docs/references/actions.md +14 -5
  47. package/docs/references/boundaries.md +23 -2
  48. package/docs/references/csp.md +91 -2
  49. package/docs/references/deploy.md +85 -16
  50. package/docs/references/errors.md +18 -10
  51. package/docs/references/guards.md +100 -29
  52. package/docs/references/params.md +91 -22
  53. package/docs/references/routing.md +171 -31
  54. package/package.json +15 -10
  55. package/dist/paths-type-DtsL8JuC.d.mts +0 -19
package/docs/GUIDE.md CHANGED
@@ -35,6 +35,7 @@ misplaced one fails either build.
35
35
 
36
36
  Towards `mode: 'static'`:
37
37
 
38
+ - add `site`, the origin the site is served from,
38
39
  - give every parameter's directory a `paths.ts` that returns its values —
39
40
  and, for values the build cannot know, put a `fallback.tsx` beside the
40
41
  page
@@ -62,8 +63,8 @@ Towards `mode: 'static'`:
62
63
  Under `mode: 'static'` there is no request, so nothing that answers one can
63
64
  be built. The build does not leave such code to fail quietly in production:
64
65
  it refuses it by name, every offending file of a kind at once. What the route
65
- files declare — a `guard.ts`, a page's `search`, a `route.ts`'s methods — is
66
- refused before anything is compiled; the `@k8ordo/framework/server` imports
66
+ files declare — a `[...name]` directory, a `guard.ts`, a page's `search`, a
67
+ `route.ts`'s methods — is refused before anything is compiled; the `@k8ordo/framework/server` imports
67
68
  and `'use server'` modules only after compiling, which is what finds them.
68
69
  So a build stopped by the first can stop again on the second.
69
70
 
@@ -81,6 +82,8 @@ The same refusal, with the same last line, is said for:
81
82
  refused),
82
83
  - a `'use server'` module,
83
84
  - a `guard.ts`,
85
+ - a `[...name]` directory, whose pathnames only a request names
86
+ ([Rest parameters](references/routing.md#rest-parameters)),
84
87
  - a page that exports `search`,
85
88
  - a `route.ts` that exports any method but `GET`.
86
89
 
@@ -93,7 +96,8 @@ this application wants mode: 'server'
93
96
  The build names the application's own modules. A dependency it leaves
94
97
  external — one installed in `node_modules` — is not compiled, so its import
95
98
  of `@k8ordo/framework/server` is not seen; what is refused there is the call
96
- — `cookies()`, `requestHeaders()`, `responseHeaders()`, `nonce()` — and the
99
+ — `cookies()`, `requestHeaders()`, `responseHeaders()`, `nonce()`,
100
+ `waitUntil()` — and the
97
101
  page or `route.ts` that made it fails to build. A page reading the `request`
98
102
  prop fails the same way, with the same last line.
99
103
 
@@ -130,7 +134,9 @@ exception, and puts the framework in `dependencies`
130
134
  import { framework } from '@k8ordo/framework/vite';
131
135
  import { defineConfig } from 'vite';
132
136
 
133
- export default defineConfig({ plugins: [framework({ mode: 'static' })] });
137
+ export default defineConfig({
138
+ plugins: [framework({ mode: 'static', site: 'https://example.com' })],
139
+ });
134
140
  // or framework({ mode: 'server' })
135
141
  ```
136
142
 
@@ -226,23 +232,48 @@ under a `404`. It never calls the handler, so a page is the file the build
226
232
  wrote, with its Content-Security-Policy `<meta>`
227
233
  ([Serving a static build](references/deploy.md#serving-a-static-build)).
228
234
 
235
+ ## Browser targets
236
+
237
+ A build is aimed at the browsers that ship every feature Baseline has newly
238
+ available. The framework gives their versions to the client environment's
239
+ `build.target` and to the CSS's `build.cssTarget` and
240
+ `css.lightningcss.targets`. It reads them from `baseline-browser-mapping` as
241
+ browserslist's `baseline newly available` does, so they move with the data
242
+ each release of the framework carries.
243
+
244
+ Vite's own default is an older, fixed set (Chrome 111, Safari 16.4), for
245
+ which Lightning CSS rewrites what those browsers lack: `light-dark()` into
246
+ variables resolved where each custom property is declared, so a `.dark`
247
+ below `<html>` no longer darkens what it holds, and `:dir(rtl)` into
248
+ `:lang()` selectors, which do not follow `dir`.
249
+
250
+ An application that sets `build.target`, `build.cssTarget` or
251
+ `css.lightningcss.targets` keeps what it set: the framework fills in only
252
+ what is unset, and `build.target` stands for the CSS too, as it does in
253
+ Vite. The code the `rsc` and `ssr` environments build keeps Vite's own
254
+ target, since a server reads it.
255
+
229
256
  ## The entries
230
257
 
231
258
  The package is split by where the code runs.
232
259
 
233
260
  - `@k8ordo/framework` — what the application's own code imports: `href`,
234
- `navigateTo`, `bindParams`, `notFound`, `usePathname`,
261
+ `bindParams`, `notFound`, `usePathname`,
235
262
  `usePendingPathname`, `useMatch`, `matchPath`, `withBase`, and the types
236
263
  `PageProps`, `LayoutProps`, `RouteContext` and `ErrorProps`, re-exported
237
- from `@k8ordo/router`. `useParams` and `useRoute` are not among them: they
264
+ from `@k8ordo/router`, with the two types of its own a route file of
265
+ either mode is written against: `Paths` (a `paths.ts`) and
266
+ `RedirectTarget` (a `redirect.ts`). `useParams` and `useRoute` are not among them: they
238
267
  read a `<Router>` the framework never renders, and a page receives
239
268
  `params` as a prop instead.
240
- - `@k8ordo/framework/vite` — `framework()`, for `vite.config.ts` only. It
241
- loads Vite.
269
+ - `@k8ordo/framework/vite` — `framework()`, and `scriptHash()`, the hash
270
+ of an inline script of the application's for the `csp` option
271
+ ([Content Security Policy](references/csp.md#static-mode-hashes-in-a-meta)).
272
+ For `vite.config.ts` only: it loads Vite.
242
273
  - `@k8ordo/framework/server` — server mode only. What code inside the
243
274
  request handler imports: `cookies()`, `responseHeaders()`,
244
- `requestHeaders()`, `redirect()`, `nonce()`, and the types `Guard`,
245
- `GuardContext`, `RouteRequest` and `RedirectTarget`.
275
+ `requestHeaders()`, `redirect()`, `nonce()`, `waitUntil()`, and the types
276
+ `Guard`, `GuardContext`, `RouteRequest` and `HandlerContext`.
246
277
  - `@k8ordo/framework/serve` — `serve`, the Node.js server for a build: what
247
278
  `dist/server.js` runs, for a server of your own to import. A static build
248
279
  it serves as a static host does, its `_redirects` applied.
@@ -264,8 +295,16 @@ dev dependencies.
264
295
 
265
296
  ## The options
266
297
 
267
- `framework()` takes `mode`, which is required, `routesDir`, the route
268
- directory (default `src/routes`), and `reactCompiler`, in either mode.
298
+ `framework()` takes `mode`, which is required, and `reactCompiler` and
299
+ `headers`, in either mode. The route files always live in `src/routes/`.
300
+ Any other key
301
+ stops the config as it loads, naming the keys the mode takes — a
302
+ `vite.config.js` is never type-checked, and a key spelt wrong would
303
+ otherwise do nothing:
304
+
305
+ ```
306
+ framework() takes no reactcompiler — under mode: 'static' it takes mode, reactCompiler, headers, site, csp
307
+ ```
269
308
 
270
309
  `reactCompiler: true` compiles the client components with React Compiler —
271
310
  the Rust one, `oxc-transform-react`, through `@vitejs/plugin-react`'s
@@ -278,18 +317,106 @@ read the config, with React's plugin's own error
278
317
  what runs in the browser is compiled; Server Components, and the HTML a
279
318
  client component renders on the server, are left as written.
280
319
 
320
+ `headers` is one set of response headers every answer carries, whatever
321
+ answered it — a page, a file of the build, a `404`, a redirect: sent by
322
+ `vite dev`, `vite preview` and `serve`, added by the handler to its own
323
+ answers under `mode: 'server'`, and written into `_headers` (and `vercel()`'s
324
+ routes) for a static host. A header a `guard.ts` or a `route.ts` set under
325
+ the same name wins
326
+ ([Headers on every response](references/deploy.md#headers-on-every-response)).
327
+
281
328
  Two more describe files, so only `mode: 'static'` takes them — passing
282
329
  them with `mode: 'server'` is a type error:
283
330
 
284
- - `site` — the origin the site is served from, `https://example.com`. With
285
- it the build also writes `sitemap.xml`, listing every page it rendered;
286
- without it there is no sitemap, because a sitemap of relative URLs is not
287
- one. A `route.ts` that writes `sitemap.xml` is the application's own
288
- list, and the build leaves the file to it
289
- ([Routing](references/routing.md#under-static-mode)).
331
+ - `site`, required — the origin the site is served from,
332
+ `https://example.com`, with no path (Vite's `base` says where under it).
333
+ The build asks for every page and `route.ts` under it, so it is the
334
+ origin of the `request.url` a `route.ts` reads, and writes
335
+ `sitemap.xml`, listing every page it rendered at that origin. A
336
+ `route.ts` that writes `sitemap.xml` is the application's own list, and
337
+ the build leaves the file to it
338
+ ([Routing](references/routing.md#under-static-mode)). A value that is not
339
+ an origin stops the config as it loads.
290
340
  - `csp` — the Content-Security-Policy each page carries
291
341
  ([Content Security Policy](references/csp.md#static-mode-hashes-in-a-meta)).
292
342
 
343
+ ## Preferences
344
+
345
+ Where a visitor's preference is kept follows from who acts on it. There are
346
+ three cases, and each has one place:
347
+
348
+ - **It only changes how the page looks** — a colour scheme, a writing mode.
349
+ The browser applies it, and the server never needs it: keep it with
350
+ `@k8ordo/state`'s `defineLocalState`, and put it on `<html>` before the
351
+ first paint with an inline script that reads it through `inlineRead()`
352
+ ([A script of the application's that runs before the first paint](references/csp.md#a-script-of-the-applications-that-runs-before-the-first-paint)).
353
+ The same code runs under both modes. `@k8ordo/color-scheme` is this case.
354
+ - **The server answers by it** — a guard that sends the visitor elsewhere, a
355
+ form that sets it without JavaScript, HTML that differs with it. The
356
+ density in `@k8ordo/state`'s cookie examples is here for the second
357
+ reason: it only changes the look, but a form without JavaScript sets it,
358
+ and the Server Action receiving that form can write a cookie, not
359
+ localStorage. Keep it in a cookie with `defineCookieState`, under
360
+ `mode: 'server'`. A render
361
+ reads it with `def.parseCookies(request.cookies)` and hands that to
362
+ `useAppState` as `initialCookie`, so the default never flashes; a guard,
363
+ a `route.ts` or a Server Action reads `def.parseCookies(cookies())` and
364
+ writes `cookies().set(...def.cookie(patch, cookies()))`; the browser
365
+ writes through `useAppState`'s `update()`
366
+ ([Guards, cookies and the request](references/guards.md)). Under
367
+ `mode: 'static'` there is no request, and the cookie renders its default
368
+ until the page hydrates.
369
+ - **The language.** A page's language is its URL's; what is kept is the
370
+ choice for `/`, the one URL without a locale. It is a plain cookie of the
371
+ application's own, which the language switcher writes with
372
+ `cookieStore.set` and `/` hands to `@k8ordo/i18n` as `chosen` — a
373
+ `guard.ts` under server mode, an effect in front of
374
+ `navigator.languages` under static mode (`@k8ordo/i18n`'s guide, "The `/`
375
+ page"):
376
+
377
+ ```ts
378
+ // src/routes/(home)/guard.ts — server mode
379
+ const guard: Guard<'/'> = ({ request }) => {
380
+ redirect(
381
+ href('/:locale', {
382
+ locale: locales.negotiateRequest(request, {
383
+ chosen: cookies().get('locale'),
384
+ }),
385
+ }),
386
+ );
387
+ };
388
+ ```
389
+
390
+ It is not a cookie state: nothing renders it, and the tag needs no schema
391
+ — negotiation checks it against the set and falls through when it is not
392
+ one.
393
+
394
+ ## Testing
395
+
396
+ What needs no request — a `paramsSchema`, a module under `src/lib/`, a
397
+ client component — is an ordinary module, tested as one. What answers a
398
+ request is tested through the handler the build wrote, the same function a
399
+ host calls:
400
+
401
+ ```ts
402
+ // after `vite build` under mode: 'server'
403
+ import handler from '../dist/rsc/index.js';
404
+
405
+ const response = await handler(new Request('https://example.test/account'));
406
+ expect(response.status).toBe(303); // the guard sent the visitor to sign in
407
+ expect(response.headers.get('location')).toBe('/login');
408
+ ```
409
+
410
+ A guard, a `route.ts` and a Server Action are tested this way rather than
411
+ called by hand: `cookies()`, `responseHeaders()`, `requestHeaders()`,
412
+ `nonce()` and `waitUntil()` belong to a request in progress, and outside
413
+ one they throw `… needs a request`. For a real HTTP round trip — the
414
+ `Set-Cookie` a browser would store, a compressed answer — `serve()` from
415
+ `@k8ordo/framework/serve` runs the build on `port: 0`, a free port, and
416
+ `close()` stops it ([Running the build](references/deploy.md#running-the-build)).
417
+ A static build is tested by the files it wrote under `dist/client/`, or
418
+ served the same way, as a static host serves them.
419
+
293
420
  ## References
294
421
 
295
422
  Each topic has a reference of its own. Read the one the task needs:
@@ -311,10 +438,11 @@ Each topic has a reference of its own. Read the one the task needs:
311
438
  what an action may read and write, the page context it runs in, forms
312
439
  before JavaScript, and ending with `redirect()`.
313
440
  - [Guards, cookies and the request](references/guards.md): server mode
314
- only. `guard.ts`, `responseHeaders()`, `cookies()`, and the `request` a
315
- page reads.
441
+ only. `guard.ts`, `responseHeaders()`, `cookies()`, `waitUntil()` for
442
+ work after the answer, and the `request` a page reads.
316
443
  - [Content Security Policy](references/csp.md): `nonce()` under server
317
- mode, the `csp` option and hashes under static mode.
444
+ mode, the `csp` option and hashes under static mode, and an application's
445
+ own script that runs before the first paint, under both.
318
446
  - [Running and deploying](references/deploy.md): what a static build
319
447
  writes, the rewrites that reach its shells on each host, `serve` and
320
448
  `vite preview`, the request handler on other runtimes, `vercel()`, a tab
package/docs/llms.txt CHANGED
@@ -2,21 +2,27 @@
2
2
 
3
3
  > Turns `src/routes/` into an application of React Server Components, built
4
4
  > with Vite, in one of two modes chosen in `vite.config.ts`:
5
- > `framework({ mode: 'static' })` renders every route at build time into
5
+ > `framework({ mode: 'static', site })` renders every route at build time into
6
6
  > files a static host serves, with no server, and
7
7
  > `framework({ mode: 'server' })` renders per request on a server. Switching
8
- > is changing `mode` (and dropping `site` and `csp`, which server mode
9
- > refuses); nothing else in the application
10
- > changes. Pick `server` when the application needs the request — Server
8
+ > takes more than `mode`: towards server, drop `site` and `csp` (refused
9
+ > there; the policy becomes a header a `guard.ts` writes) and run
10
+ > `dist/server.js` or hand the handler to a host; towards static, add
11
+ > `site`, give every parameter's directory a `paths.ts` (or its page a
12
+ > `fallback.tsx`), and remove what static mode refuses and every read of the
13
+ > `request` prop — a layout's `nonce()` from `/server` included. The
14
+ > route grammar, the boundaries and the handler stay the same. Pick `server` when the application needs the request — Server
11
15
  > Actions, `guard.ts`, the `request` prop, cookies, a page that exports
12
16
  > `search`, parameter values with no list rendered on the server (static
13
- > mode has only a `fallback.tsx`'s shell for those, drawn in the browser) —
14
- > and `static` otherwise; static mode refuses all of those by name, in the
17
+ > mode has only a `fallback.tsx`'s shell for those, drawn in the browser), a
18
+ > `[...name]` directory — and `static` otherwise; static mode refuses all of those by name, in the
15
19
  > build and in `vite dev` (`this application wants mode: 'server'`).
16
20
  > `src/routes/` is the pathname space and holds nothing else
17
21
  > (page/layout/not-found/error/loading/fallback/paths/redirect/guard/route
18
22
  > files,
19
- > `[param]` and `(group)` directories; `_` is an ordinary character in
23
+ > `[param]` and `(group)` directories, and under server mode `[...name]`,
24
+ > which takes every remaining segment — one or more — as an array
25
+ > (`/files/:path+`); `_` is an ordinary character in
20
26
  > a segment and a name starting with `.` is not read, so `/.well-known` cannot
21
27
  > be a route; components live outside it in `src/components/` and every
22
28
  > other module in `src/lib/`); a page or layout may
@@ -28,8 +34,9 @@
28
34
  > answers 307/308 (server) or is written as a page that sends the visitor on
29
35
  > (static), and a `route.ts` answers its URL with a `Response` (server: every
30
36
  > method it exports; static: written as the file its `GET` answers). Under
31
- > server mode a Server Action ends with `redirect()`, a `guard.ts` runs before
32
- > whatever answers below it, `cookies()` reads and writes cookies, and a page
37
+ > server mode a Server Action, a `guard.ts` and a `route.ts` end the request
38
+ > with `redirect(href(…))` (a `303` from a guard or a `route.ts`) or, the
39
+ > last two, `notFound()`, a `guard.ts` runs before whatever answers below it, `cookies()` reads and writes cookies, and a page
33
40
  > receives `request`. Under static mode a parameter's values come from a
34
41
  > `paths.ts` in its directory (a function returning them, typed `Paths`,
35
42
  > run with the pages' own modules, applied to every route below it) —
@@ -55,14 +62,28 @@ Start with GUIDE.md, then follow the reference most relevant to the task.
55
62
  - [Design Guide](GUIDE.md): choosing a mode and what static mode refuses,
56
63
  setup (install, `vite.config.ts`, `tsconfig.json`, the root layout as the
57
64
  document), `vite dev` and `vite preview` against a static build, the
58
- entries, and the plugin's options — `reactCompiler` among them, React
59
- Compiler on the client components, which needs `oxc-transform-react`
60
- installed.
65
+ browsers a build is aimed at (Baseline newly available, unless the config
66
+ sets its own targets), the entries, the plugin's options —
67
+ `reactCompiler` (React Compiler on the client components, which needs
68
+ `oxc-transform-react` installed), `headers` on every answer, and
69
+ static mode's `site` and `csp` — where a visitor's preference is kept
70
+ (look-only: `defineLocalState` and `inlineRead()`; one the server answers
71
+ by: `defineCookieState` under server mode; the language: a plain cookie
72
+ passed to `negotiateRequest` as `chosen`) — and testing: what answers a request
73
+ through the built handler or `serve({ port: 0 })`, since the request API
74
+ throws outside a request.
61
75
 
62
76
  ## References
63
77
 
64
78
  - [Routing](references/routing.md): the `routes/` grammar and every refusal
65
- the build names, where a `fallback.tsx` may sit, how a page is sent in
79
+ the build names, rest parameters (`[...name]`, server mode only: the
80
+ pattern `/files/:path+`, an array of decoded segments for the page, layout,
81
+ `route.ts` and `guard.ts`, never zero segments, always the last directory,
82
+ tried after a literal and a `[name]` beside it and before the
83
+ `not-found.tsx`), where a `fallback.tsx` may sit, a layout's current link
84
+ (`matchPath(pattern, pathname)` on the server, `useMatch` in a client) and
85
+ the one way to write each URL (a table shared with `@k8ordo/router` and
86
+ `@k8ordo/state`), how a page is sent in
66
87
  each mode (each page loads only its own client components and CSS, what
67
88
  every page loads comes in at most two files — the entry's and the frame's,
68
89
  which sets the client build's `codeSplitting` — and the browser renders
@@ -82,9 +103,13 @@ Start with GUIDE.md, then follow the reference most relevant to the task.
82
103
  an `.mdx` file imports reaches the pages rendering it, with no query),
83
104
  and counting page views (`navigatesuccess`, not `usePathname()`).
84
105
  - [Parameters](references/params.md): `paramsSchema` — a refused value is a
85
- pathname the pattern does not answer — and, under static mode, `paths.ts`:
86
- the values of its directory's parameter (`Paths<Pattern>`), given the
87
- params above it, for every page, `route.ts` and `redirect.ts` below it,
106
+ pathname the pattern does not answer; a `[...name]` directory's receives
107
+ the array of segments — and, under static mode, `paths.ts`:
108
+ the values of its directory's parameter (`Paths<Pattern>`, typed as a
109
+ link takes them, spelled as `href` spells them), given the params above
110
+ it as the layouts above its directory read them (their `paramsSchema`
111
+ exports; values one of them refuses fail the build), for every page,
112
+ `route.ts` and `redirect.ts` below it,
88
113
  and what fails the build; and `fallback.tsx` for the values it does not
89
114
  return — the shell it renders (no props, the layouts' schemas only, the
90
115
  shell's `pathname`), where a shell stands (below the values of the
@@ -96,8 +121,10 @@ Start with GUIDE.md, then follow the reference most relevant to the task.
96
121
  from it (what no `error.tsx` catches — the root layout's own components —
97
122
  and why there is no `global-error.tsx`), a Server Component that throws
98
123
  stopping a static build, `notFound()` and `not-found.tsx` (a document
99
- waits for its page under server mode; under a `fallback.tsx`, shown in
100
- place), and `redirect.ts` in each mode.
124
+ waits for its page under server mode; from a `guard.ts` or a `route.ts`,
125
+ the same 404; under a `fallback.tsx`, shown in place), and `redirect.ts`
126
+ in each mode — its `to` a pattern in the table's terms, where
127
+ `redirect()` takes a URL.
101
128
  - [Execution boundaries](references/boundaries.md): `'use client'`,
102
129
  `use(browser())` for a component that needs a browser (and `useMatch` /
103
130
  `usePathname` waiting for it on their own under a `fallback.tsx`),
@@ -105,16 +132,23 @@ Start with GUIDE.md, then follow the reference most relevant to the task.
105
132
  page that exports `search` under server mode — and forms.
106
133
  - [Server Actions](references/actions.md): server mode only. `'use server'`
107
134
  against `server-only`, `cookies()` / `responseHeaders()` /
108
- `requestHeaders()` in an action, the page context an action runs in, forms
109
- before JavaScript, and ending with `redirect()`.
135
+ `requestHeaders()` / `waitUntil()` in an action, the page context an
136
+ action runs in, forms before JavaScript, and ending with `redirect()`.
110
137
  - [Guards, cookies and the request](references/guards.md): server mode only.
111
- `guard.ts` (outer first; a `Response` ends the request),
112
- `responseHeaders()`, `cookies()` and its defaults, and the read-only
113
- `request` a page receives.
138
+ `guard.ts` (outer first; `redirect(href(…))` ends the request with a
139
+ `303`, `notFound()` with the nearest `not-found.tsx` under 404, a returned
140
+ `Response` — a `401` — as it is),
141
+ `responseHeaders()`, `cookies()` and its defaults, `waitUntil(promise)`
142
+ for work that goes on after the answer (what it means, where it may be
143
+ called, a rejection logged once), and the read-only `request` a page
144
+ receives.
114
145
  - [Content Security Policy](references/csp.md): `nonce()` in a header a
115
146
  guard writes under server mode, in `style-src` too for the framework's
116
- one `<style>`; the `csp` option and `colorSchemeScriptHash()` under static
117
- mode, and `connect-src` for the data a `fallback.tsx`'s shell fetches.
147
+ one `<style>`; the `csp` option, `scriptHash()` and
148
+ `colorSchemeScriptHash()` under static mode, and `connect-src` for the
149
+ data a `fallback.tsx`'s shell fetches; an application's own pre-paint
150
+ script (`inlineRead()`) as a `'use client'` component taking the nonce,
151
+ its text hashed with `scriptHash()` for static mode.
118
152
  - [Running and deploying](references/deploy.md): a static build's
119
153
  `dist/client/` (HTML, `index.rsc` payloads, shells, `404.html`,
120
154
  `client-files.json`, `sitemap.xml`, `_redirects`), the rewrites that reach a shell on Netlify,
@@ -126,39 +160,51 @@ Start with GUIDE.md, then follow the reference most relevant to the task.
126
160
  under server mode, a `dist/` that runs on its own with
127
161
  `node dist/server.js` (`PORT`, `HOST`, every dependency bundled in, and
128
162
  `resolve.external` for one that cannot be), `serve` (compression, `ETag`,
129
- `Range`), the request handler under Node.js, Bun, Deno and Cloudflare
130
- Workers, the same-origin rule for a `POST`, and `vercel()` in either mode,
163
+ `Range`, `close()` waiting for what `waitUntil()` was handed), the request
164
+ handler under Node.js, Bun, Deno and Cloudflare Workers and the
165
+ `HandlerContext` it takes as a second argument, the same-origin rule for a
166
+ `POST`, and `vercel()` in either mode (under server mode its function's
167
+ `maxDuration` and `regions`, and `@vercel/functions` installed for
168
+ `waitUntil()`),
131
169
  with routes of your own under static mode;
132
170
  a tab opened before a deploy, and Vite's `base`.
133
171
 
134
172
  ## Entry points
135
173
 
136
174
  - `@k8ordo/framework` — what application code imports, re-exported from
137
- `@k8ordo/router` (which stays a peer, one copy): `href`, `navigateTo`,
175
+ `@k8ordo/router` (which stays a peer, one copy): `href`,
138
176
  `bindParams`, `notFound`, `isNotFound`, `usePathname`,
139
177
  `usePendingPathname`, `useMatch`, `matchPath`, `normalizePathname`,
140
178
  `withBase`, and the types `PageProps`, `LayoutProps`, `RouteContext`,
141
- `ErrorProps`, `BoundLinks`, `BoundParams`, `MatchablePattern`,
142
- `MatchOptions`, `NavigateToOptions` and the `Registered*` types; and one
143
- type of its own, `Paths<Pattern>`, what a `paths.ts` exports by default.
179
+ `ErrorProps`, `BoundParams`, `MatchablePattern`,
180
+ `MatchOptions` and the `Registered*` types; and two
181
+ types of its own, `Paths<Pattern>`, what a `paths.ts` exports by default,
182
+ and `RedirectTarget`, what a `redirect.ts` does.
144
183
  Not `useParams` or `useRoute`: a page receives `params` as a prop.
145
184
  - `@k8ordo/framework/vite` — `framework(options)` returns the Vite plugins;
146
- `options.mode` (`'static' | 'server'`) is required, `options.routesDir`
147
- moves the route directory (default `src/routes`). Only with
148
- `mode: 'static'`: `options.site` (the origin) makes the build also write
149
- `sitemap.xml`, and
185
+ `options.mode` (`'static' | 'server'`) is required; the route files
186
+ always live in `src/routes/`, and a key the mode does not take throws
187
+ as the config loads. Only with
188
+ `mode: 'static'`: `options.site`, required, the origin the site is
189
+ served from (no path) — the origin of every request the build makes, a
190
+ `route.ts`'s included, and of `sitemap.xml` — and
150
191
  `options.csp` (directives → sources, `ContentSecurityPolicy`) is the
151
192
  Content-Security-Policy written into each page's `<meta>`, first in
152
193
  `<head>` after the `<meta charSet>`, with the hashes of the framework's own inline scripts on that
153
194
  page added to `script-src` (or `default-src` made into it) and
154
195
  `script-src-elem`; a policy with `'strict-dynamic'`, `frame-ancestors`,
155
196
  `report-uri`, `report-to` or `sandbox` is refused. `FrameworkOptions` is
156
- `StaticOptions | ServerOptions`. This entry loads Vite and is for
157
- `vite.config.ts` only.
197
+ `StaticOptions | ServerOptions`. `scriptHash(text)` resolves to the CSP
198
+ source, `'sha256-…'`, of an inline script of the application's — the
199
+ text its `<script>` is given, a CR LF hashed as the LF the browser
200
+ parses — for `csp`, which adds only the framework's own hashes. This
201
+ entry loads Vite and is for `vite.config.ts` only.
158
202
  - `@k8ordo/framework/server` — server mode only; a static build refuses any
159
203
  module that imports a value from it. What code inside the request handler
160
- imports, needing nothing from Node but `AsyncLocalStorage`: `redirect(to)`
161
- ends a Server Action by sending the visitor to `to`; `responseHeaders()`
204
+ imports, needing nothing from Node but `AsyncLocalStorage`: `redirect(url)`
205
+ ends what answers the request — a Server Action, a `guard.ts` or a
206
+ `route.ts` (the last two with a `303`) — by sending the visitor to the URL
207
+ `href()` built, and throws by name while a page renders; `responseHeaders()`
162
208
  is the `Headers` the final response carries, `cookies()` the request's
163
209
  cookies to read and write (`get`, `has`, `set(name, value,
164
210
  CookieOptions)`, `delete(name, CookieScope)`; defaults `path: '/'`,
@@ -168,15 +214,22 @@ Start with GUIDE.md, then follow the reference most relevant to the task.
168
214
  a `guard.ts`, a `route.ts` or a Server Action, and they throw anywhere
169
215
  else; `nonce()` is the nonce this answer's inline scripts carry, new per
170
216
  request and readable anywhere the request is answered, the render
171
- included; `Guard<P>` is the type of what a `guard.ts` default-exports
217
+ included; `waitUntil(promise)` keeps the host from stopping until
218
+ `promise` settles, for work that goes on after the answer — callable
219
+ anywhere the request is in progress, the render included, a rejection
220
+ logged once and never left unhandled; `HandlerContext`
221
+ (`{ waitUntil?: (promise: Promise<unknown>) => void }`) is the type of
222
+ the second argument the built handler takes from a runtime that calls it;
223
+ `Guard<P>` is the type of what a `guard.ts` default-exports
172
224
  (`({ request, params }) => Response | void`) and `GuardContext<P>` its
173
- argument; `RedirectTarget` is the type of what a `redirect.ts`
174
- default-exports, and `RouteRequest` the type of the `request` prop.
225
+ argument; `RouteRequest` is the type of the `request` prop.
175
226
  - `@k8ordo/framework/serve` — the Node.js server for a build.
176
227
  `serve({ dist?, port?, host? })` (`ServeOptions`: `dist` defaults to
177
228
  `dist` under the working directory, `port` to `3000`, `host` to
178
229
  `localhost`) serves a build and returns a `Server`, `{ port, url, close }`
179
- (`port: 0` takes a free port): for a server build, the client build's
230
+ (`port: 0` takes a free port; `close()` resolves once the answers under
231
+ way are written and what `waitUntil()` was handed has settled): for a
232
+ server build, the client build's
180
233
  files as they are and everything else through the request handler; for a
181
234
  static build, as a static host does — files, the `_redirects` rewrites,
182
235
  `404.html` under 404, never the handler. `dist/server.js` runs it under
@@ -184,14 +237,16 @@ Start with GUIDE.md, then follow the reference most relevant to the task.
184
237
  `dependencies`.
185
238
  - `dist/server.js` — under server mode, what the build writes to start
186
239
  `serve` on the build it sits in: `node dist/server.js`, listening on
187
- `PORT` (default `3000`) and `HOST` (default `localhost`), closing and
188
- exiting with `0` on `SIGTERM` or `SIGINT`. `serve` and every
240
+ `PORT` (default `3000`) and `HOST` (default `localhost`), closing —
241
+ after the work handed to `waitUntil()` — and exiting with `0` on `SIGTERM`
242
+ or `SIGINT`. `serve` and every
189
243
  dependency of the handler are bundled into `dist/`, with
190
244
  `dist/package.json` (written in either mode) marking it ES modules, so
191
245
  `dist/` runs with nothing installed and the framework is a dev
192
246
  dependency.
193
247
  - `dist/rsc/index.js` — under server mode, the built request handler, a
194
- plain `(request: Request) => Promise<Response>` default export with every
248
+ `(request: Request, context?: HandlerContext) => Promise<Response>`
249
+ default export (`context.waitUntil` is the host's own) with every
195
250
  dependency bundled in, that needs nothing from its runtime but
196
251
  `AsyncLocalStorage` (`node:async_hooks`), so Node.js, Bun, Deno and
197
252
  Cloudflare Workers with `nodejs_compat` run it directly; the host serves
@@ -221,6 +276,6 @@ Start with GUIDE.md, then follow the reference most relevant to the task.
221
276
  the problems the build names under
222
277
  `routes/ is not a valid pathname space:` fails it the same way (exit
223
278
  code 1), with nothing written. What `mode: 'static'` refuses on top of
224
- those — a `guard.ts`, a `route.ts` answering more than `GET`, a page
279
+ those — a `[...name]` directory, a `guard.ts`, a `route.ts` answering more than `GET`, a page
225
280
  reading `search`, an `export *`, a Server Action, whatever a `paths.ts`
226
281
  returns — it does not check, so its success is not the build's.
@@ -47,7 +47,9 @@ An action answers the request as much as a [guard](guards.md) does, so it
47
47
  has the same API: `cookies()` to read and write the cookies,
48
48
  `responseHeaders()` to add to the answer, and `requestHeaders()` for the
49
49
  headers the request arrived with — an action is handed its arguments, not the
50
- request.
50
+ request. `waitUntil(promise)` keeps the host running for work the action
51
+ leaves going after it returns
52
+ ([Work after the answer](guards.md#work-after-the-answer)).
51
53
 
52
54
  ```ts
53
55
  'use server';
@@ -65,8 +67,11 @@ export async function signIn(_previous: FormState, formData: FormData) {
65
67
 
66
68
  What an action writes goes on its answer: the page it re-rendered, the `303`
67
69
  to where it redirected, or the payload the client runtime applies. The page
68
- re-rendered after it sees the cookies the request carried in `request`, not
69
- what the action wrote.
70
+ re-rendered after it reads what the action wrote: `request.cookies` is the
71
+ request's cookies with every write of the guards and the action laid over
72
+ them, so a preference changed by a form posted without JavaScript is drawn
73
+ with its new value in that very answer. `request.headers.get('cookie')`
74
+ stays the header as it arrived.
70
75
 
71
76
  **An action runs in the context of the page it was posted to.** A call
72
77
  posts to the URL of the page on screen, whose params schemas run for that
@@ -109,5 +114,9 @@ posted by the client runtime is answered with a payload that tells the
109
114
  router to navigate there. The target is a URL, sent as given — build it with
110
115
  `href()`, which carries Vite's `base` when the application is served under
111
116
  one, where a `redirect.ts` target is a pattern in the table's terms and gets
112
- the base put in front. `redirect()` is for actions: a page that should
113
- send the visitor elsewhere is a `redirect.ts`, where the build can see it.
117
+ the base put in front. `redirect()` ends what answers a request — a Server
118
+ Action, a [`guard.ts`](guards.md#guards) or a
119
+ [`route.ts`](routing.md#under-server-mode), the last two with a `303`. A page
120
+ is a render, not an answer: called while one renders, `redirect()` throws by
121
+ name, and a page that should send the visitor elsewhere is a `redirect.ts`,
122
+ where the build can see it, or a guard.
@@ -101,14 +101,16 @@ be wrapped in one of these to come under the same check.
101
101
 
102
102
  ## The search
103
103
 
104
- `@k8ordo/state` owns the search params, and the framework generates its
105
- `Register` for you, so a filter is typed against the same routes:
104
+ `@k8ordo/state` owns the search params. A link that carries them is built on
105
+ the URL `href` returns, so the pattern is checked against the same routes:
106
106
 
107
107
  ```tsx
108
108
  'use client';
109
+ import { href } from '@k8ordo/framework';
109
110
  import { useAppState } from '@k8ordo/state';
110
111
 
111
112
  const [{ q }, update] = useAppState(listState, ['q']);
113
+ <a href={listState.href(href('/products'), { q, page: 2 })}>next</a>;
112
114
  ```
113
115
 
114
116
  Changing the search does not change the page: the router leaves the route tree
@@ -156,6 +158,25 @@ is on screen. The generated table reads it through
156
158
  `@k8ordo/state`, so the application depends on that. A layout never
157
159
  receives it.
158
160
 
161
+ So a pager below the list leaves the reader at the bottom of the new page,
162
+ a link to `?page=2` and an `update()` alike — a fragment does not scroll a
163
+ same-pathname navigation either. `update().finished` settles once the page
164
+ loaded again is on screen, so the pager moves the focus to the results there:
165
+
166
+ ```tsx
167
+ 'use client';
168
+ const results = useRef<HTMLHeadingElement>(null);
169
+
170
+ const turn = async (next: number) => {
171
+ await update({ page: next }, { history: 'push' }).finished;
172
+ results.current?.focus(); // the scroll follows the focus
173
+ };
174
+
175
+ <h2 ref={results} tabIndex={-1}>
176
+ Results
177
+ </h2>;
178
+ ```
179
+
159
180
  Under `mode: 'static'` the export is refused by name: a file is the same
160
181
  whatever the search holds.
161
182