@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/GUIDE.md CHANGED
@@ -24,19 +24,21 @@ called for each route at build time instead of once per request.
24
24
 
25
25
  Switching takes more than changing `mode`. Towards `mode: 'server'`:
26
26
 
27
- - drop `paths`, `site` and `csp`, which it refuses — the policy becomes a
28
- header a `guard.ts` writes, and no `sitemap.xml` is written,
27
+ - drop `site` and `csp`, which it refuses — the policy becomes a header a
28
+ `guard.ts` writes, and no `sitemap.xml` is written,
29
29
  - run the build with `node dist/server.js`, or hand its request handler to
30
30
  a host of your own ([Running and deploying](references/deploy.md)).
31
31
 
32
- A `fallback.tsx` may stay: server mode renders every value with its page
33
- and never renders it. Its placement is still checked, so a misplaced one
34
- fails either build.
32
+ A `fallback.tsx` and a `paths.ts` may stay: server mode renders every value
33
+ with its page and reads neither. Their placement is still checked, so a
34
+ misplaced one fails either build.
35
35
 
36
36
  Towards `mode: 'static'`:
37
37
 
38
- - list the values of every route with parameters in `paths` — and, for
39
- values the build cannot know, put a `fallback.tsx` beside the page
38
+ - add `site`, the origin the site is served from,
39
+ - give every parameter's directory a `paths.ts` that returns its values —
40
+ and, for values the build cannot know, put a `fallback.tsx` beside the
41
+ page
40
42
  ([Values the build did not write](references/params.md#values-the-build-did-not-write)),
41
43
  - remove what static mode refuses (below), and every read of the `request`
42
44
  prop.
@@ -44,8 +46,8 @@ Towards `mode: 'static'`:
44
46
  | | `mode: 'static'` | `mode: 'server'` |
45
47
  | ------------------------------- | ------------------------------------------------ | --------------------------------------- |
46
48
  | What ships | `dist/client/`, files any static host serves | `dist/`, run with `node dist/server.js` |
47
- | A route with parameters | its values listed by the `paths` option | any value, arriving with the request |
48
- | A value `paths` does not list | `404.html`, or a `fallback.tsx`'s shell | rendered by its page |
49
+ | A route with parameters | its values returned by a `paths.ts` | any value, arriving with the request |
50
+ | A value no `paths.ts` returns | `404.html`, or a `fallback.tsx`'s shell | rendered by its page |
49
51
  | A URL nothing answers | the host serves `404.html` | the application answers with a real 404 |
50
52
  | `redirect.ts` | written as a page that sends the visitor on | answered with `307` / `308` |
51
53
  | `route.ts` | written as the file its `GET` answers | answers every method it exports |
@@ -61,8 +63,8 @@ Towards `mode: 'static'`:
61
63
  Under `mode: 'static'` there is no request, so nothing that answers one can
62
64
  be built. The build does not leave such code to fail quietly in production:
63
65
  it refuses it by name, every offending file of a kind at once. What the route
64
- files declare — a `guard.ts`, a page's `search`, a `route.ts`'s methods — is
65
- 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
66
68
  and `'use server'` modules only after compiling, which is what finds them.
67
69
  So a build stopped by the first can stop again on the second.
68
70
 
@@ -80,6 +82,8 @@ The same refusal, with the same last line, is said for:
80
82
  refused),
81
83
  - a `'use server'` module,
82
84
  - a `guard.ts`,
85
+ - a `[...name]` directory, whose pathnames only a request names
86
+ ([Rest parameters](references/routing.md#rest-parameters)),
83
87
  - a page that exports `search`,
84
88
  - a `route.ts` that exports any method but `GET`.
85
89
 
@@ -92,7 +96,8 @@ this application wants mode: 'server'
92
96
  The build names the application's own modules. A dependency it leaves
93
97
  external — one installed in `node_modules` — is not compiled, so its import
94
98
  of `@k8ordo/framework/server` is not seen; what is refused there is the call
95
- — `cookies()`, `requestHeaders()`, `responseHeaders()`, `nonce()` — and the
99
+ — `cookies()`, `requestHeaders()`, `responseHeaders()`, `nonce()`,
100
+ `waitUntil()` — and the
96
101
  page or `route.ts` that made it fails to build. A page reading the `request`
97
102
  prop fails the same way, with the same last line.
98
103
 
@@ -129,7 +134,9 @@ exception, and puts the framework in `dependencies`
129
134
  import { framework } from '@k8ordo/framework/vite';
130
135
  import { defineConfig } from 'vite';
131
136
 
132
- export default defineConfig({ plugins: [framework({ mode: 'static' })] });
137
+ export default defineConfig({
138
+ plugins: [framework({ mode: 'static', site: 'https://example.com' })],
139
+ });
133
140
  // or framework({ mode: 'server' })
134
141
  ```
135
142
 
@@ -205,18 +212,19 @@ the same origin rather than linking them from another one.
205
212
  ### `vite dev` under static mode
206
213
 
207
214
  `vite dev` runs the handler per request in either mode, and writes no files.
208
- Under `mode: 'static'` that makes it not quite the build: a parameter value
209
- `paths` does not list still renders with its page, a `redirect.ts` answers
210
- with a `307` (`308` when `permanent`) rather than a page that sends the
211
- visitor on, an unknown URL gets the handler's 404 rather than the host's
215
+ Under `mode: 'static'` that makes it not quite the build: a `redirect.ts`
216
+ answers with a `307` (`308` when `permanent`) rather than a page that sends
217
+ the visitor on, an unknown URL gets the handler's 404 rather than the host's
212
218
  `404.html`, and a page whose Server Component throws answers a `500`
213
219
  carrying the thrown message, where the build would stop.
214
220
 
215
- Beside a `fallback.tsx`, a value `paths` does not list gets the shell
216
- instead, as a host's rewrite would answer it, and `vite dev` says so once
217
- per `fallback.tsx`. It calls `paths` only for an application with a
218
- `fallback.tsx`, and again only when the patterns it hands in change, so a
219
- value added to the data since is answered as an unlisted one.
221
+ A parameter's value is answered as the build's host will answer it. One a
222
+ `paths.ts` returns renders with its page. One none returns gets the shell
223
+ where a `fallback.tsx` stands beside the page, and the not-found otherwise,
224
+ and `vite dev` says so once per pattern. It asks the `paths.ts` files again
225
+ when a request names a value it does not hold, so a value the data has
226
+ gained since renders without a restart
227
+ ([Parameters](references/params.md#under-vite-dev-and-under-server-mode)).
220
228
 
221
229
  `vite preview` after a static build serves it as a static host does: the
222
230
  files in `dist/client/`, the rewrites in its `_redirects`, and `404.html`
@@ -224,23 +232,48 @@ under a `404`. It never calls the handler, so a page is the file the build
224
232
  wrote, with its Content-Security-Policy `<meta>`
225
233
  ([Serving a static build](references/deploy.md#serving-a-static-build)).
226
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
+
227
256
  ## The entries
228
257
 
229
258
  The package is split by where the code runs.
230
259
 
231
260
  - `@k8ordo/framework` — what the application's own code imports: `href`,
232
- `navigateTo`, `bindParams`, `notFound`, `usePathname`,
261
+ `bindParams`, `notFound`, `usePathname`,
233
262
  `usePendingPathname`, `useMatch`, `matchPath`, `withBase`, and the types
234
263
  `PageProps`, `LayoutProps`, `RouteContext` and `ErrorProps`, re-exported
235
- 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
236
267
  read a `<Router>` the framework never renders, and a page receives
237
268
  `params` as a prop instead.
238
- - `@k8ordo/framework/vite` — `framework()`, for `vite.config.ts` only. It
239
- 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.
240
273
  - `@k8ordo/framework/server` — server mode only. What code inside the
241
274
  request handler imports: `cookies()`, `responseHeaders()`,
242
- `requestHeaders()`, `redirect()`, `nonce()`, and the types `Guard`,
243
- `GuardContext`, `RouteRequest` and `RedirectTarget`.
275
+ `requestHeaders()`, `redirect()`, `nonce()`, `waitUntil()`, and the types
276
+ `Guard`, `GuardContext`, `RouteRequest` and `HandlerContext`.
244
277
  - `@k8ordo/framework/serve` — `serve`, the Node.js server for a build: what
245
278
  `dist/server.js` runs, for a server of your own to import. A static build
246
279
  it serves as a static host does, its `_redirects` applied.
@@ -262,8 +295,16 @@ dev dependencies.
262
295
 
263
296
  ## The options
264
297
 
265
- `framework()` takes `mode`, which is required, `routesDir`, the route
266
- 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
+ ```
267
308
 
268
309
  `reactCompiler: true` compiles the client components with React Compiler —
269
310
  the Rust one, `oxc-transform-react`, through `@vitejs/plugin-react`'s
@@ -276,21 +317,106 @@ read the config, with React's plugin's own error
276
317
  what runs in the browser is compiled; Server Components, and the HTML a
277
318
  client component renders on the server, are left as written.
278
319
 
279
- Three more describe files, so only `mode: 'static'` takes them — passing
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
+
328
+ Two more describe files, so only `mode: 'static'` takes them — passing
280
329
  them with `mode: 'server'` is a type error:
281
330
 
282
- - `paths` — the pathnames of routes with parameters, and where a
283
- `fallback.tsx`'s shell stands
284
- ([Parameters](references/params.md#routes-with-parameters)).
285
- - `site` — the origin the site is served from, `https://example.com`. With
286
- it the build also writes `sitemap.xml`, listing every page it rendered;
287
- without it there is no sitemap, because a sitemap of relative URLs is not
288
- one. A `route.ts` that writes `sitemap.xml` as well stops the build,
289
- naming both — keep one
290
- ([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.
291
340
  - `csp` — the Content-Security-Policy each page carries
292
341
  ([Content Security Policy](references/csp.md#static-mode-hashes-in-a-meta)).
293
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
+
294
420
  ## References
295
421
 
296
422
  Each topic has a reference of its own. Read the one the task needs:
@@ -300,9 +426,9 @@ Each topic has a reference of its own. Read the one the task needs:
300
426
  before the first paint goes, `loading.tsx`, `route.ts`, the generated
301
427
  `.k8ordo/` files, titles, and fetching the next page ahead.
302
428
  - [Parameters](references/params.md): typing parameters with `paramsSchema`,
303
- what a refused value becomes, and, under static mode, supplying the
304
- pathnames with `paths` and answering the values it does not list with a
305
- `fallback.tsx`.
429
+ what a refused value becomes, and, under static mode, returning a
430
+ parameter's values from the `paths.ts` in its directory and answering the
431
+ values it does not return with a `fallback.tsx`.
306
432
  - [Errors and redirects](references/errors.md): `error.tsx`, what fails a
307
433
  static build, `notFound()` and `not-found.tsx`, and `redirect.ts`.
308
434
  - [Execution boundaries](references/boundaries.md): `'use client'`,
@@ -312,10 +438,11 @@ Each topic has a reference of its own. Read the one the task needs:
312
438
  what an action may read and write, the page context it runs in, forms
313
439
  before JavaScript, and ending with `redirect()`.
314
440
  - [Guards, cookies and the request](references/guards.md): server mode
315
- only. `guard.ts`, `responseHeaders()`, `cookies()`, and the `request` a
316
- page reads.
441
+ only. `guard.ts`, `responseHeaders()`, `cookies()`, `waitUntil()` for
442
+ work after the answer, and the `request` a page reads.
317
443
  - [Content Security Policy](references/csp.md): `nonce()` under server
318
- 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.
319
446
  - [Running and deploying](references/deploy.md): what a static build
320
447
  writes, the rewrites that reach its shells on each host, `serve` and
321
448
  `vite preview`, the request handler on other runtimes, `vercel()`, a tab