@k8ordo/framework 2.3.1 → 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 (65) hide show
  1. package/README.md +28 -13
  2. package/dist/cli.mjs +3 -3
  3. package/dist/generated.d.mts +13 -13
  4. package/dist/headers-BxB_jJVk.mjs +73 -0
  5. package/dist/index.d.mts +3 -2
  6. package/dist/index.mjs +1 -1
  7. package/dist/paths-type-DXpmjaOr.d.mts +43 -0
  8. package/dist/runtime/app-router.mjs +21 -8
  9. package/dist/runtime/browser-payloads.mjs +1 -1
  10. package/dist/runtime/client-files.d.mts +6 -5
  11. package/dist/runtime/client-files.mjs +17 -9
  12. package/dist/runtime/client-ready.d.mts +7 -1
  13. package/dist/runtime/client-ready.mjs +2 -1
  14. package/dist/runtime/cookies.d.mts +2 -0
  15. package/dist/runtime/cookies.mjs +2 -1
  16. package/dist/runtime/ended.d.mts +7 -0
  17. package/dist/runtime/ended.mjs +14 -0
  18. package/dist/runtime/entry.browser.mjs +1 -1
  19. package/dist/runtime/entry.rsc.d.mts +38 -2
  20. package/dist/runtime/entry.rsc.mjs +97 -31
  21. package/dist/runtime/entry.ssr.d.mts +7 -1
  22. package/dist/runtime/entry.ssr.mjs +12 -6
  23. package/dist/runtime/guard.d.mts +15 -10
  24. package/dist/runtime/guard.mjs +8 -2
  25. package/dist/runtime/mount.mjs +1 -1
  26. package/dist/runtime/page-boundary.mjs +2 -2
  27. package/dist/runtime/params.d.mts +5 -4
  28. package/dist/runtime/params.mjs +2 -2
  29. package/dist/runtime/payload.d.mts +19 -4
  30. package/dist/runtime/payload.mjs +20 -5
  31. package/dist/runtime/prefetch.d.mts +29 -8
  32. package/dist/runtime/prefetch.mjs +95 -24
  33. package/dist/runtime/redirect.d.mts +13 -7
  34. package/dist/runtime/redirect.mjs +25 -9
  35. package/dist/runtime/request-scope.d.mts +37 -2
  36. package/dist/runtime/request-scope.mjs +60 -3
  37. package/dist/runtime/request.d.mts +6 -1
  38. package/dist/runtime/request.mjs +7 -2
  39. package/dist/runtime/route.d.mts +4 -2
  40. package/dist/runtime/route.mjs +13 -5
  41. package/dist/runtime/server.d.mts +5 -5
  42. package/dist/runtime/server.mjs +2 -2
  43. package/dist/runtime/shell.d.mts +2 -1
  44. package/dist/serve.d.mts +4 -0
  45. package/dist/serve.mjs +29 -14
  46. package/dist/static-Be3xIqd8.d.mts +62 -0
  47. package/dist/{static-file-BAjwBMMl.mjs → static-file-BS-WEiwu.mjs} +311 -134
  48. package/dist/{static-host-B-1fAfAw.mjs → static-host-tIDLqV8c.mjs} +1 -1
  49. package/dist/vercel.d.mts +18 -1
  50. package/dist/vercel.mjs +131 -19
  51. package/dist/vite.d.mts +6 -3
  52. package/dist/vite.mjs +366 -156
  53. package/docs/GUIDE.md +173 -46
  54. package/docs/llms.txt +129 -65
  55. package/docs/references/actions.md +14 -5
  56. package/docs/references/boundaries.md +23 -2
  57. package/docs/references/csp.md +91 -2
  58. package/docs/references/deploy.md +154 -25
  59. package/docs/references/errors.md +57 -12
  60. package/docs/references/guards.md +100 -29
  61. package/docs/references/params.md +234 -126
  62. package/docs/references/routing.md +358 -75
  63. package/package.json +18 -13
  64. package/dist/shared-dir-CNXFRZgP.mjs +0 -9
  65. package/dist/static-DnbgGIoy.d.mts +0 -61
package/docs/llms.txt CHANGED
@@ -2,20 +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 `paths`, `site` and `csp`, which server
9
- > mode 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
- > (page/layout/not-found/error/loading/fallback/redirect/guard/route files,
18
- > `[param]` and `(group)` directories; `_` is an ordinary character in
21
+ > (page/layout/not-found/error/loading/fallback/paths/redirect/guard/route
22
+ > files,
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
19
26
  > a segment and a name starting with `.` is not read, so `/.well-known` cannot
20
27
  > be a route; components live outside it in `src/components/` and every
21
28
  > other module in `src/lib/`); a page or layout may
@@ -27,10 +34,13 @@
27
34
  > answers 307/308 (server) or is written as a page that sends the visitor on
28
35
  > (static), and a `route.ts` answers its URL with a `Response` (server: every
29
36
  > method it exports; static: written as the file its `GET` answers). Under
30
- > server mode a Server Action ends with `redirect()`, a `guard.ts` runs before
31
- > whatever answers below it, `cookies()` reads and writes cookies, and a page
32
- > receives `request`. Static mode lists parameter values with `paths` —
33
- > beside a `fallback.tsx`, the values it does not list get a shell: the
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
40
+ > receives `request`. Under static mode a parameter's values come from a
41
+ > `paths.ts` in its directory (a function returning them, typed `Paths`,
42
+ > run with the pages' own modules, applied to every route below it) —
43
+ > beside a `fallback.tsx`, the values it does not return get a shell: the
34
44
  > layouts written as HTML, the page drawn in the browser from the URL, and a
35
45
  > `_redirects` (or `vercel()`'s routes) through which a host reaches it —
36
46
  > writes `404.html` and `sitemap.xml`, and puts a Content-Security-Policy in
@@ -52,14 +62,28 @@ Start with GUIDE.md, then follow the reference most relevant to the task.
52
62
  - [Design Guide](GUIDE.md): choosing a mode and what static mode refuses,
53
63
  setup (install, `vite.config.ts`, `tsconfig.json`, the root layout as the
54
64
  document), `vite dev` and `vite preview` against a static build, the
55
- entries, and the plugin's options — `reactCompiler` among them, React
56
- Compiler on the client components, which needs `oxc-transform-react`
57
- 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.
58
75
 
59
76
  ## References
60
77
 
61
78
  - [Routing](references/routing.md): the `routes/` grammar and every refusal
62
- 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
63
87
  each mode (each page loads only its own client components and CSS, what
64
88
  every page loads comes in at most two files — the entry's and the frame's,
65
89
  which sets the client build's `codeSplitting` — and the browser renders
@@ -74,20 +98,33 @@ Start with GUIDE.md, then follow the reference most relevant to the task.
74
98
  writes them before `tsc` without a build, titles,
75
99
  and fetching the next page ahead (`data-k8ordo-prefetch`; under static
76
100
  mode also the links on screen for a coarse pointer, every page kept for
77
- going back and forward, and `client-files.json` linked with a payload).
101
+ going back and forward, and `client-files.json` linked with a payload),
102
+ MDX (the application's own compiler plugin, `enforce: 'pre'`; the CSS
103
+ an `.mdx` file imports reaches the pages rendering it, with no query),
104
+ and counting page views (`navigatesuccess`, not `usePathname()`).
78
105
  - [Parameters](references/params.md): `paramsSchema` — a refused value is a
79
- pathname the pattern does not answer — and, under static mode, the `paths`
80
- option: supplying pathnames, expanding a parameter every page shares, and
81
- what fails the build; and `fallback.tsx` for the values it does not list —
82
- the shell it renders (no props, the layouts' schemas only, the shell's
83
- `pathname`), shell locations in `paths` and the parameters a layout above
84
- forces them to fill, reading the value in the browser with `useMatch`,
85
- `notFound()` shown in place, routes below its parameter, and what it
86
- costs.
87
- - [Errors and redirects](references/errors.md): `error.tsx`, a Server
88
- Component that throws stopping a static build, `notFound()` and
89
- `not-found.tsx` (a document waits for its page under server mode; under a
90
- `fallback.tsx`, shown in place), and `redirect.ts` in each mode.
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,
113
+ and what fails the build; and `fallback.tsx` for the values it does not
114
+ return — the shell it renders (no props, the layouts' schemas only, the
115
+ shell's `pathname`), where a shell stands (below the values of the
116
+ `paths.ts` files above it, which a layout receiving a parameter forces to
117
+ exist), reading the value in the browser with `useMatch`, `notFound()`
118
+ shown in place, routes below its parameter, what it costs, and how
119
+ `vite dev` answers a value no `paths.ts` returns.
120
+ - [Errors and redirects](references/errors.md): `error.tsx` and reporting
121
+ from it (what no `error.tsx` catches — the root layout's own components —
122
+ and why there is no `global-error.tsx`), a Server Component that throws
123
+ stopping a static build, `notFound()` and `not-found.tsx` (a document
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.
91
128
  - [Execution boundaries](references/boundaries.md): `'use client'`,
92
129
  `use(browser())` for a component that needs a browser (and `useMatch` /
93
130
  `usePathname` waiting for it on their own under a `fallback.tsx`),
@@ -95,61 +132,79 @@ Start with GUIDE.md, then follow the reference most relevant to the task.
95
132
  page that exports `search` under server mode — and forms.
96
133
  - [Server Actions](references/actions.md): server mode only. `'use server'`
97
134
  against `server-only`, `cookies()` / `responseHeaders()` /
98
- `requestHeaders()` in an action, the page context an action runs in, forms
99
- 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()`.
100
137
  - [Guards, cookies and the request](references/guards.md): server mode only.
101
- `guard.ts` (outer first; a `Response` ends the request),
102
- `responseHeaders()`, `cookies()` and its defaults, and the read-only
103
- `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.
104
145
  - [Content Security Policy](references/csp.md): `nonce()` in a header a
105
146
  guard writes under server mode, in `style-src` too for the framework's
106
- one `<style>`; the `csp` option and `colorSchemeScriptHash()` under static
107
- 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.
108
152
  - [Running and deploying](references/deploy.md): a static build's
109
153
  `dist/client/` (HTML, `index.rsc` payloads, shells, `404.html`,
110
154
  `client-files.json`, `sitemap.xml`, `_redirects`), the rewrites that reach a shell on Netlify,
111
155
  Cloudflare Workers static assets (`html_handling`, the 2,000/100 rule
112
156
  limits) and Pages, Vercel and any other host, and `serve` and
113
- `vite preview` serving a static build as a static host;
157
+ `vite preview` serving a static build as a static host; `headers`, the
158
+ headers every response carries in either mode (`vite dev`,
159
+ `vite preview`, `serve`, the handler, Vercel's routes, `_headers`);
114
160
  under server mode, a `dist/` that runs on its own with
115
161
  `node dist/server.js` (`PORT`, `HOST`, every dependency bundled in, and
116
162
  `resolve.external` for one that cannot be), `serve` (compression, `ETag`,
117
- `Range`), the request handler under Node.js, Bun, Deno and Cloudflare
118
- 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()`),
119
169
  with routes of your own under static mode;
120
170
  a tab opened before a deploy, and Vite's `base`.
121
171
 
122
172
  ## Entry points
123
173
 
124
174
  - `@k8ordo/framework` — what application code imports, re-exported from
125
- `@k8ordo/router` (which stays a peer, one copy): `href`, `navigateTo`,
175
+ `@k8ordo/router` (which stays a peer, one copy): `href`,
126
176
  `bindParams`, `notFound`, `isNotFound`, `usePathname`,
127
177
  `usePendingPathname`, `useMatch`, `matchPath`, `normalizePathname`,
128
178
  `withBase`, and the types `PageProps`, `LayoutProps`, `RouteContext`,
129
- `ErrorProps`, `BoundLinks`, `BoundParams`, `MatchablePattern`,
130
- `MatchOptions`, `NavigateToOptions` and the `Registered*` types. Not
131
- `useParams` or `useRoute`: a page receives `params` as a prop.
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.
183
+ Not `useParams` or `useRoute`: a page receives `params` as a prop.
132
184
  - `@k8ordo/framework/vite` — `framework(options)` returns the Vite plugins;
133
- `options.mode` (`'static' | 'server'`) is required, `options.routesDir`
134
- moves the route directory (default `src/routes`). Only with
135
- `mode: 'static'`: `options.paths` supplies concrete pathnames for routes
136
- with parameters (a parameterised route left uncovered fails the build,
137
- unless its page has a `fallback.tsx`), and for such a page also shell
138
- locations, pathnames that still hold the parameters left to the browser
139
- (`/ja/posts/:id`), `options.site` (the origin) makes the build also write
140
- `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
141
191
  `options.csp` (directives → sources, `ContentSecurityPolicy`) is the
142
192
  Content-Security-Policy written into each page's `<meta>`, first in
143
193
  `<head>` after the `<meta charSet>`, with the hashes of the framework's own inline scripts on that
144
194
  page added to `script-src` (or `default-src` made into it) and
145
195
  `script-src-elem`; a policy with `'strict-dynamic'`, `frame-ancestors`,
146
196
  `report-uri`, `report-to` or `sandbox` is refused. `FrameworkOptions` is
147
- `StaticOptions | ServerOptions`. This entry loads Vite and is for
148
- `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.
149
202
  - `@k8ordo/framework/server` — server mode only; a static build refuses any
150
203
  module that imports a value from it. What code inside the request handler
151
- imports, needing nothing from Node but `AsyncLocalStorage`: `redirect(to)`
152
- 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()`
153
208
  is the `Headers` the final response carries, `cookies()` the request's
154
209
  cookies to read and write (`get`, `has`, `set(name, value,
155
210
  CookieOptions)`, `delete(name, CookieScope)`; defaults `path: '/'`,
@@ -159,15 +214,22 @@ Start with GUIDE.md, then follow the reference most relevant to the task.
159
214
  a `guard.ts`, a `route.ts` or a Server Action, and they throw anywhere
160
215
  else; `nonce()` is the nonce this answer's inline scripts carry, new per
161
216
  request and readable anywhere the request is answered, the render
162
- 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
163
224
  (`({ request, params }) => Response | void`) and `GuardContext<P>` its
164
- argument; `RedirectTarget` is the type of what a `redirect.ts`
165
- default-exports, and `RouteRequest` the type of the `request` prop.
225
+ argument; `RouteRequest` is the type of the `request` prop.
166
226
  - `@k8ordo/framework/serve` — the Node.js server for a build.
167
227
  `serve({ dist?, port?, host? })` (`ServeOptions`: `dist` defaults to
168
228
  `dist` under the working directory, `port` to `3000`, `host` to
169
229
  `localhost`) serves a build and returns a `Server`, `{ port, url, close }`
170
- (`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
171
233
  files as they are and everything else through the request handler; for a
172
234
  static build, as a static host does — files, the `_redirects` rewrites,
173
235
  `404.html` under 404, never the handler. `dist/server.js` runs it under
@@ -175,14 +237,16 @@ Start with GUIDE.md, then follow the reference most relevant to the task.
175
237
  `dependencies`.
176
238
  - `dist/server.js` — under server mode, what the build writes to start
177
239
  `serve` on the build it sits in: `node dist/server.js`, listening on
178
- `PORT` (default `3000`) and `HOST` (default `localhost`), closing and
179
- 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
180
243
  dependency of the handler are bundled into `dist/`, with
181
244
  `dist/package.json` (written in either mode) marking it ES modules, so
182
245
  `dist/` runs with nothing installed and the framework is a dev
183
246
  dependency.
184
247
  - `dist/rsc/index.js` — under server mode, the built request handler, a
185
- 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
186
250
  dependency bundled in, that needs nothing from its runtime but
187
251
  `AsyncLocalStorage` (`node:async_hooks`), so Node.js, Bun, Deno and
188
252
  Cloudflare Workers with `nodejs_compat` run it directly; the host serves
@@ -207,11 +271,11 @@ Start with GUIDE.md, then follow the reference most relevant to the task.
207
271
  - `k8ordo generate` — the package's one command (its `bin`), run in the
208
272
  application's directory: resolves the Vite config there as `vite build`
209
273
  does and writes `.k8ordo/` as the build does when it begins, then stops —
210
- no environment built, no page rendered, `paths` never called — so CI
274
+ no environment built, no page rendered, no `paths.ts` read — so CI
211
275
  runs it before `tsc` without the data a build reads. A `routes/` with
212
276
  the problems the build names under
213
277
  `routes/ is not a valid pathname space:` fails it the same way (exit
214
278
  code 1), with nothing written. What `mode: 'static'` refuses on top of
215
- those — a `guard.ts`, a `route.ts` answering more than `GET`, a page
216
- reading `search`, an `export *`, a Server Action, whatever `paths`
279
+ those — a `[...name]` directory, a `guard.ts`, a `route.ts` answering more than `GET`, a page
280
+ reading `search`, an `export *`, a Server Action, whatever a `paths.ts`
217
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
 
@@ -95,6 +95,7 @@ export default defineConfig({
95
95
  plugins: [
96
96
  framework({
97
97
  mode: 'static',
98
+ site: 'https://example.com',
98
99
  csp: {
99
100
  'script-src': ["'self'", await colorSchemeScriptHash()],
100
101
  'object-src': ["'none'"],
@@ -111,8 +112,11 @@ made from `default-src` — unless the policy lets it in already with
111
112
  `'unsafe-inline'`: a hash beside that would turn `'unsafe-inline'` off for
112
113
  every `style` attribute as well.
113
114
 
114
- An inline script of the application's is allowed by its hash in that policy,
115
- as `colorSchemeScriptHash()` gives `@k8ordo/color-scheme`'s (pass it the
115
+ An inline script of the application's is allowed by its hash in that policy:
116
+ `scriptHash(text)` from `@k8ordo/framework/vite` resolves to the source,
117
+ `'sha256-…'`, for the text its `<script>` is given
118
+ ([A script of the application's that runs before the first paint](#a-script-of-the-applications-that-runs-before-the-first-paint)),
119
+ as `colorSchemeScriptHash()` does for `@k8ordo/color-scheme`'s (pass it the
116
120
  props `ColorSchemeScript` is given, `nonce` aside); any other inline script
117
121
  on the page — one that reached it from content — is refused. The framework's module
118
122
  script is allowed by where it comes from (`'self'`), since nothing in a file
@@ -139,3 +143,88 @@ browser fetches
139
143
  so a policy that restricts `connect-src`, or `default-src` in its place,
140
144
  names the API's origin there:
141
145
  `'connect-src': ["'self'", 'https://api.example.com']`.
146
+
147
+ ## A script of the application's that runs before the first paint
148
+
149
+ A preference only the browser applies — a density kept with
150
+ `@k8ordo/state`'s `defineLocalState`, say — reaches `<html>` before the
151
+ first paint through an inline script in the root layout's `<head>`, which
152
+ reads the stored value with `inlineRead()`
153
+ ([Routing](routing.md#how-a-page-is-sent) says why `<head>`). Write it the
154
+ way `@k8ordo/color-scheme` writes its own, under either mode:
155
+
156
+ - **A `'use client'` component, not a `<script>` in the layout.** The text of
157
+ a `<script>` a Server Component renders goes into every page's payload —
158
+ each `.rsc`, and the copy written into the HTML for hydration — though a
159
+ script that arrives with a navigation never runs. A client component's
160
+ payload carries only its props, and the server render and hydration both
161
+ build the text from its module.
162
+ - **The nonce as a prop.** The root layout passes `nonce()` under server
163
+ mode. Under static mode it passes nothing, and `csp` names the hash of the
164
+ same text, `scriptHash(densityScript)`, which a module without a directive
165
+ exports for both the component and `vite.config.ts`.
166
+
167
+ ```ts
168
+ // src/lib/density-script.ts
169
+ import { densityState } from '../state';
170
+
171
+ export const densityScript = `(() => {
172
+ const s = ${densityState.inlineRead()};
173
+ if (s && s.density === 'compact') {
174
+ document.documentElement.dataset.density = 'compact';
175
+ }
176
+ })();`;
177
+ ```
178
+
179
+ ```tsx
180
+ // src/components/density-script.tsx
181
+ 'use client';
182
+
183
+ import { densityScript } from '../lib/density-script';
184
+
185
+ export function DensityScript({ nonce }: { nonce?: string }) {
186
+ return <script nonce={nonce}>{densityScript}</script>;
187
+ }
188
+ ```
189
+
190
+ ```tsx
191
+ // src/routes/layout.tsx — server mode; under static mode, no nonce
192
+ <head>
193
+ <ColorSchemeScript nonce={nonce()} />
194
+ <DensityScript nonce={nonce()} />
195
+ </head>
196
+ ```
197
+
198
+ ```ts
199
+ // vite.config.ts — static mode
200
+ import { colorSchemeScriptHash } from '@k8ordo/color-scheme';
201
+ import { framework, scriptHash } from '@k8ordo/framework/vite';
202
+ import { defineConfig } from 'vite';
203
+
204
+ import { densityScript } from './src/lib/density-script';
205
+
206
+ export default defineConfig({
207
+ plugins: [
208
+ framework({
209
+ mode: 'static',
210
+ site: 'https://example.com',
211
+ csp: {
212
+ 'script-src': [
213
+ "'self'",
214
+ await colorSchemeScriptHash(),
215
+ await scriptHash(densityScript),
216
+ ],
217
+ },
218
+ }),
219
+ ],
220
+ });
221
+ ```
222
+
223
+ `scriptHash()` hashes the text as it is, so give it the very string the
224
+ `<script>` is given: a character more or less, a space included, is another
225
+ script, and the policy refuses it. A line break counts as the browser parses
226
+ the page, a CR LF as one LF, so a text read from a file checked out with
227
+ CR LF hashes as the script that runs.
228
+
229
+ The script changes `<html>` before React hydrates it, so `<html>` carries
230
+ `suppressHydrationWarning`, as it does for `@k8ordo/color-scheme`.