@k8ordo/framework 2.0.0 → 3.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.
@@ -72,91 +72,125 @@ schema and lets the pages below it receive the result.
72
72
  ## Routes with parameters
73
73
 
74
74
  Static mode only. A build into files cannot invent parameter values, so it
75
- asks for them:
75
+ asks the parameter's own directory for them — a `paths.ts`, one more route
76
+ file:
76
77
 
77
78
  ```ts
78
- framework({
79
- mode: 'static',
80
- paths: async () => {
81
- const products = await readCatalog();
82
- return products.map((product) => `/products/${product.id}`);
83
- },
84
- });
79
+ // src/routes/products/[id]/paths.ts
80
+ import type { Paths } from '@k8ordo/framework';
81
+
82
+ import { listProducts } from '../../../lib/catalog.server';
83
+
84
+ const paths: Paths<'/products/:id'> = async () =>
85
+ (await listProducts()).map((product) => String(product.id));
86
+
87
+ export default paths;
85
88
  ```
86
89
 
87
- The patterns that still need covering are handed in, so a parameter that takes
88
- the same values everywhere — a locale segment, say — is expanded rather than
89
- listed once per page:
90
+ Its default export is a function returning the values the directory's
91
+ parameter takes: an array of strings, or a promise of one. `Paths<Pattern>`
92
+ is its type — exported from `@k8ordo/framework`, a type only — where the
93
+ pattern is the directory's own, ending in that parameter; the generated
94
+ table checks each file against it.
95
+
96
+ **It runs where the pages run.** The build asks from the built RSC bundle,
97
+ and `vite dev` through the RSC environment's module runner: the module graph
98
+ the pages are in. So a `paths.ts` imports the application's modules by their
99
+ aliases, a `server-only` one included, and shares their instances with the
100
+ pages — a catalog it read and cached is the one the pages then read.
101
+
102
+ **It covers everything below its directory.** The `page.tsx` beside it, and
103
+ every page, `route.ts` and `redirect.ts` in the directories below, are
104
+ written once per value: with `blog/[slug]/paths.ts`, so are
105
+ `blog/[slug]/og.png/route.ts` (`/blog/:slug/og.png`) and
106
+ `blog/[slug]/article.md/route.ts`. No pattern is expanded by hand.
107
+
108
+ **It receives the params above it**, and is called once per combination of
109
+ their values:
90
110
 
91
111
  ```ts
92
- framework({
93
- mode: 'static',
94
- paths: (patterns) =>
95
- patterns.flatMap((pattern) => {
96
- const segments = pattern.split('/');
97
- if (!segments.includes(':locale')) return [pattern];
98
- return ['ja', 'en'].map((locale) =>
99
- segments
100
- .map((segment) => (segment === ':locale' ? locale : segment))
101
- .join('/'),
102
- );
103
- }),
104
- });
105
- ```
112
+ // src/routes/[locale]/paths.ts
113
+ import type { Paths } from '@k8ordo/framework';
106
114
 
107
- A parameterised route with no path supplied **fails the build** — unless its
108
- page has a `fallback.tsx` beside it, whose shell answers the values the build
109
- did not write ([Values the build did not write](#values-the-build-did-not-write)):
115
+ const paths: Paths<'/:locale'> = () => ['ja', 'en'];
110
116
 
117
+ export default paths;
111
118
  ```
112
- static build needs pathnames for /products/:id — supply them with the "paths" option
119
+
120
+ ```ts
121
+ // src/routes/[locale]/posts/[id]/paths.ts
122
+ import type { Paths } from '@k8ordo/framework';
123
+
124
+ import { postsIn } from '../../../../lib/posts.server';
125
+
126
+ const paths: Paths<'/:locale/posts/:id'> = async ({ locale }) =>
127
+ (await postsIn(locale)).map((post) => String(post.id));
128
+
129
+ export default paths;
113
130
  ```
114
131
 
115
- Shipping a site quietly missing half its pages is worse than not shipping one.
116
- Parameterless routes need no declaration; they are taken from the table.
132
+ The params arrive as the strings the files above returned — no
133
+ `paramsSchema` has run. A `paths.ts` below a parameter whose directory has
134
+ none cannot be asked, and the build refuses:
135
+
136
+ ```
137
+ [locale]/posts/[id]/paths.ts is asked for its values once per value of the parameters above it, and /:locale/posts still holds one — give that parameter's directory a paths.ts too
138
+ ```
117
139
 
118
- The other direction counts too — a supplied pathname nothing matched costs
119
- exactly the page it was meant to add:
140
+ **A value is the segment as a visitor reads it** — `café`, `a b` — not
141
+ URL-escaped; the build escapes it for the URL and writes the file under the
142
+ unescaped name. Values are non-empty strings, so an id is spelled
143
+ (`String(product.id)`), and a value repeated is written once:
120
144
 
121
145
  ```
122
- the "paths" option supplied pathnames no route wants: /produtcs/2
146
+ tags/[tag]/paths.ts has to return an array of non-empty strings, the values of :tag
123
147
  ```
124
148
 
125
- A supplied pathname that still holds a parameter (`/ja/blog/:slug` — what
126
- expanding only one of two parameters leaves behind) is a shell location,
127
- which only a page with a `fallback.tsx` beside it takes. For any other route
128
- the build names the file that has none — or, for a `redirect.ts` or
129
- `route.ts`, the file that cannot have one (`… cannot have one`):
149
+ A value that is `..` names a file outside the output, and `!fallback` is the
150
+ segment the build keeps for shells:
130
151
 
131
152
  ```
132
- the "paths" option supplied pathnames that still hold a parameter, and only a page with a fallback.tsx beside it takes one: /ja/blog/:slug ([locale]/blog/[slug]/page.tsx has none)
153
+ a pathname to write leaves the output: /products/..
154
+ a paths.ts returned !fallback, which the build keeps for shells: /posts/!fallback
133
155
  ```
134
156
 
135
- Pathnames are taken as a URL carries them, escapes included
136
- (`/products/caf%C3%A9`) — `href()` output as it is, unless the application is
137
- served under a `base`: these are pathnames in the table's terms, and `href()`
138
- puts the base in front. Only the file name is decoded, and a pathname whose
139
- escapes do not decode, or that decodes to a `..` segment, fails the build —
140
- neither names a file inside the output:
157
+ A parameter with no `paths.ts` in its directory stays open, and an open
158
+ route **fails the build** — unless its page has a `fallback.tsx` beside it,
159
+ whose shell answers the values the build did not write
160
+ ([Values the build did not write](#values-the-build-did-not-write)):
141
161
 
142
162
  ```
143
- the "paths" option supplied a pathname with a malformed escape: /products/%zz
144
- the "paths" option supplied a pathname that leaves the output: /products/..%2F..
163
+ static build needs the values of /tags/:tag — a paths.ts in each parameter's directory returns them, and a fallback.tsx beside a page answers the ones the build does not write
145
164
  ```
146
165
 
147
- A supplied pathname that a `paramsSchema` along its route's stack refuses
166
+ Shipping a site quietly missing half its pages is worse than not shipping one.
167
+ Parameterless routes need no declaration; they are taken from the table.
168
+
169
+ A returned value that a `paramsSchema` along its route's stack refuses
148
170
  fails the build too — it would otherwise be written as a 404 page under a
149
171
  URL the site claims to have — and so does one whose page says `notFound()`:
150
172
 
151
173
  ```
152
- the "paths" option supplied pathnames a params schema refused: /products/shoes
153
- the "paths" option supplied pathnames whose page called notFound(): /products/3
174
+ a paths.ts returned values a params schema refused: /products/shoes
175
+ a paths.ts returned values whose page called notFound(): /products/3
154
176
  ```
155
177
 
156
178
  Neither is written to `dist/client/`. A build that meets both names them in
157
179
  one error, with any page that failed to render, so fixing one does not reveal
158
180
  the next.
159
181
 
182
+ `paths.ts` belongs in a `[param]` directory and nowhere else
183
+ ([What the build refuses](routing.md#what-the-build-refuses)), and
184
+ `framework()` takes no `paths` option — passing one throws when the config
185
+ loads:
186
+
187
+ ```
188
+ framework() takes no paths — a parameter's values come from a paths.ts in its directory under routes/
189
+ ```
190
+
191
+ Under `mode: 'server'` a `paths.ts` is never read and nothing of it is
192
+ built in, so the same `routes/` works in both modes.
193
+
160
194
  ## Values the build did not write
161
195
 
162
196
  Static mode only. A page whose values outgrow the build — a post published
@@ -165,19 +199,21 @@ after the site was deployed, an id in a link nobody listed — keeps a
165
199
 
166
200
  ```
167
201
  src/routes/posts/[id]/
168
- page.tsx the ids paths lists, rendered at build time
202
+ paths.ts the ids the build writes
203
+ page.tsx each of them, rendered at build time
169
204
  fallback.tsx every other id: the shell, its post drawn in the browser
170
205
  ```
171
206
 
172
- The build writes the page for each id `paths` lists, as it always has, and
173
- once more a **shell**: the layouts above rendered as HTML, with the
174
- `fallback.tsx` in the page's place, at `posts/!fallback/`. The host answers
175
- any other id with it, through the rewrites the build writes beside the pages
207
+ The build writes the page for each id `paths.ts` returns, and once more a
208
+ **shell**: the layouts above rendered as HTML, with the `fallback.tsx` in
209
+ the page's place, at `posts/!fallback/`. The host answers any other id with
210
+ it, through the rewrites the build writes beside the pages
176
211
  ([Rewrites to a shell](deploy.md#rewrites-to-a-shell)), and the browser,
177
- which has the id in its URL, reads the post itself:
212
+ which has the id in its URL, reads the post itself. Without the `paths.ts`
213
+ the build writes no post at all, and the shell answers every id.
178
214
 
179
215
  ```tsx
180
- // src/routes/posts/[id]/page.tsx — the ids paths lists
216
+ // src/routes/posts/[id]/page.tsx — the ids paths.ts returns
181
217
  import { notFound } from '@k8ordo/framework';
182
218
  import type { PageProps } from '@k8ordo/framework';
183
219
 
@@ -275,20 +311,20 @@ export const readPost = (id: number): Promise<Post | undefined> => {
275
311
  ```
276
312
 
277
313
  ```ts
278
- // vite.config.ts
279
- framework({
280
- mode: 'static',
281
- paths: async () => {
282
- const response = await fetch('https://api.example.com/posts');
283
- const posts = (await response.json()) as { id: number }[];
284
- return posts.map((post) => `/posts/${String(post.id)}`);
285
- },
286
- });
314
+ // src/routes/posts/[id]/paths.ts
315
+ import type { Paths } from '@k8ordo/framework';
316
+
317
+ import { listPosts } from '../../../lib/posts.server';
318
+
319
+ const paths: Paths<'/posts/:id'> = async () =>
320
+ (await listPosts()).map((post) => String(post.id));
321
+
322
+ export default paths;
287
323
  ```
288
324
 
289
- `vite.config.ts` reads the ids itself rather than through
290
- `posts.server.ts`: it runs in Node outside the server environment, where a
291
- module that imports `server-only` throws. It supplies values only, so the
325
+ `paths.ts` reads the ids through `posts.server.ts`, the module the page
326
+ reads a post through: it runs in the server environment, where a module
327
+ that imports `server-only` loads. No parameter stands above `[id]`, so the
292
328
  shell stands at the pattern itself, `/posts/:id`.
293
329
 
294
330
  ### What a `fallback.tsx` is handed
@@ -361,42 +397,39 @@ the shell for /posts/:id called notFound() while it rendered, and a shell has no
361
397
  ### Where a shell stands
362
398
 
363
399
  A shell stands at a **location**: a pathname whose remaining `:name`
364
- segments are left to the browser. With nothing in `paths` for it, the
365
- location is the pattern itself — `/posts/:id`, one shell for every id. A
366
- location may also fill some parameters and leave the rest, and `paths`
367
- supplies it beside the values:
400
+ segments are left to the browser. Which parameters a location fills is
401
+ decided by the `paths.ts` files above the `fallback.tsx`: a parameter whose
402
+ directory has one is filled, once per value, and a parameter without one is
403
+ left open. With no parameter above, the location is the pattern itself —
404
+ `/posts/:id`, one shell for every id. With one above that lists its values,
405
+ there is a shell per value:
368
406
 
369
- ```ts
370
- paths: async () => {
371
- const ids = await fetchPostIds();
372
- return ['ja', 'en'].flatMap((locale) => [
373
- `/${locale}/posts/:id`, // the shell for this locale
374
- ...ids.map((id) => `/${locale}/posts/${String(id)}`),
375
- ]);
376
- },
407
+ ```
408
+ src/routes/[locale]/
409
+ paths.ts ja, en
410
+ layout.tsx
411
+ posts/[id]/
412
+ page.tsx
413
+ fallback.tsx shells at /ja/posts/:id and /en/posts/:id
377
414
  ```
378
415
 
379
416
  Each location is its own shell, rendered for its own URL — the location
380
417
  with `!fallback` for each parameter left open, `/ja/posts/!fallback`, a
381
- segment no route directory can be named — and written there. Once `paths`
382
- supplies a location for a pattern, the pattern itself is not added:
383
- the locations are the upper values the site has, and a shell at
384
- `/:locale/posts/:id` would answer `/fr/posts/1` too. `@k8ordo/i18n`'s
385
- `locales.paths` gives this shape on its own: it fills the locale and leaves
386
- the rest. A location belongs to the first pattern in the table's order it
387
- fits — the pattern whose URLs a host's rule for it catches.
418
+ segment no route directory can be named — and written there. The filled
419
+ values are the upper values the site has: no shell stands at
420
+ `/:locale/posts/:id`, which would answer `/fr/posts/1` too. A `paths.ts` in
421
+ `posts/[id]/` as well changes no location: the ids it returns are written as
422
+ pages under each locale, and the same two shells answer the rest.
388
423
 
389
424
  **A parameter a layout above receives has to be filled.** A layout above
390
425
  the `fallback.tsx` is written into the shell once, as HTML, for every value
391
426
  the shell answers, so each parameter it receives is one the shell has to
392
427
  know. With `src/routes/[locale]/layout.tsx`, that layout receives `locale`,
393
428
  so `/:locale/posts/:id` cannot be a location — `/ja/posts/:id` and
394
- `/en/posts/:id` can — and the build says which to supply when there is
395
- none, and which one left it out:
429
+ `/en/posts/:id` can — and without a `[locale]/paths.ts` the build says so:
396
430
 
397
431
  ```
398
- static build needs shell locations for /:locale/posts/:id — [locale]/layout.tsx receives :locale, so the "paths" option has to fill it, as in /<locale>/posts/:id
399
- the "paths" option supplied shell locations that leave out a parameter a layout above their fallback.tsx receives: /:locale/posts/:id (:locale, received by [locale]/layout.tsx)
432
+ these shells leave out a parameter a layout above their fallback.tsx receives, so its directory needs a paths.ts: /:locale/posts/:id (:locale, received by [locale]/layout.tsx)
400
433
  ```
401
434
 
402
435
  The parameters a shell may leave are the ones no layout above the
@@ -409,10 +442,11 @@ the layouts above do, over the parameters the location fills. A schema whose
409
442
  work has to reach the shell belongs on a layout — `@k8ordo/i18n`'s, which
410
443
  sets the locale the shell's messages are rendered in, on
411
444
  `[locale]/layout.tsx` (`export const { paramsSchema } = locales;`). A layout
412
- schema that refuses a supplied location stops the build:
445
+ schema that refuses a value a `paths.ts` above the shell returned stops the
446
+ build:
413
447
 
414
448
  ```
415
- the "paths" option supplied shell locations a params schema refused: /xx/posts/:id
449
+ the shells stand where a params schema refuses the values above them: /xx/posts/:id
416
450
  ```
417
451
 
418
452
  **Layouts render with the shell's pathname.** A layout above a shell
@@ -432,16 +466,19 @@ which reads the visitor's URL. React hoists a `<title>`, `<meta>` or
432
466
  `<link>` from either. A parameter no layout receives can still be one a
433
467
  layout derives something from: a root layout that takes `<html lang>` from
434
468
  the first segment of `pathname` reads `!fallback` there at the bare
435
- `/:locale/posts/:id`, and every value gets its default language. Supply
436
- locations that fill it.
469
+ `/:locale/posts/:id`, and every value gets its default language. Give that
470
+ parameter's directory a `paths.ts`, and the shells stand below its values.
437
471
 
438
472
  ### Routes below a fallback's parameter
439
473
 
440
474
  A `fallback.tsx` answers its own page's pattern and nothing below it.
441
475
  `posts/[id]/comments/page.tsx` is `/posts/:id/comments`, a pattern of its
442
- own: it needs its values in `paths`, as any route with parameters does,
443
- unless it keeps a `fallback.tsx` of its own. A `redirect.ts` or `route.ts`
444
- below the parameter cannot have one, so it answers only the values listed.
476
+ own: it is written for the ids `posts/[id]/paths.ts` returns, and for no
477
+ other — unless it keeps a `fallback.tsx` of its own, whose shell stands at
478
+ `/posts/:id/comments`. With no `paths.ts` in `posts/[id]/` and no
479
+ `fallback.tsx` of its own, it stops the build, as any open route does. A
480
+ `redirect.ts` or `route.ts` below the parameter cannot have a
481
+ `fallback.tsx`, so it answers only the values returned.
445
482
 
446
483
  ### What it costs
447
484
 
@@ -451,8 +488,8 @@ below the parameter cannot have one, so it answers only the values listed.
451
488
  `fallback.tsx`.
452
489
  - **Its HTML is the shell's.** No per-value `<title>`, no Open Graph tags:
453
490
  a crawler that runs no JavaScript and a link preview both read the
454
- placeholder. A value that matters to them belongs in `paths`, and a rebuild is
455
- what puts it there.
491
+ placeholder. A value that matters to them is one a `paths.ts` returns,
492
+ and a rebuild is what writes it.
456
493
  - **An unknown value answers `200`.** The host rewrites by shape, so
457
494
  `/posts/999` gets the shell too, and the not-found it shows in place is a
458
495
  soft 404 to a crawler. Give `not-found.tsx` a
@@ -466,14 +503,8 @@ below the parameter cannot have one, so it answers only the values listed.
466
503
 
467
504
  ### What else stops the build
468
505
 
469
- Every refusal of `paths` is said before anything is rendered, with the
470
- others above. A location whose parameter names are not its pattern's, and
471
- a pathname holding the segment the build keeps for shells, escaped or not:
472
-
473
- ```
474
- the "paths" option supplied shell locations whose parameter names are not their pattern's: /en/posts/:slug (/:locale/posts/:id)
475
- the "paths" option supplied pathnames with a segment named !fallback, which the build keeps for shells: /posts/!fallback
476
- ```
506
+ Every refusal of what the `paths.ts` files returned is said before anything
507
+ is rendered, with the others above.
477
508
 
478
509
  A shell's rule catches every URL of its location's shape that no file
479
510
  answers, so a location must not catch URLs that a pattern declared before
@@ -483,8 +514,8 @@ catches infinitely many of them, or the two overlap in one URL the build
483
514
  did not write:
484
515
 
485
516
  ```
486
- the shell for /:lang/:id ([lang]/[id]/fallback.tsx) would also answer URLs /docs/:section (docs/[section]/page.tsx) is declared first for — give docs/[section]/page.tsx a fallback.tsx too, or supply shell locations it cannot match
487
- the shell for /ja/posts/:id ([locale]/posts/[id]/fallback.tsx) would also answer /ja/posts/new, which [locale]/posts/new/page.tsx is declared first for and the build did not write — list /ja/posts/new in "paths", or supply shell locations it cannot match
517
+ the shell for /:lang/:id ([lang]/[id]/fallback.tsx) would also answer URLs /docs/:section (docs/[section]/page.tsx) is declared first for — give docs/[section]/page.tsx a fallback.tsx too, or put the shell where it cannot match, with a paths.ts above it
518
+ the shell for /ja/posts/:id ([locale]/posts/[id]/fallback.tsx) would also answer /ja/posts/new, which [locale]/posts/new/page.tsx is declared first for and the build did not write — return its value from that parameter's paths.ts, or put the shell where it cannot match, with a paths.ts above it
488
519
  ```
489
520
 
490
521
  A URL the build did write is not one of them — its file answers before any
@@ -499,33 +530,41 @@ renders is named by its location, as a page is:
499
530
 
500
531
  ```
501
532
  the shell for /ja/posts/:id ([locale]/posts/[id]/fallback.tsx) was answered by another route, which took "!fallback" as a value
502
- the shell for /en/posts/:id ([locale]/posts/[id]/fallback.tsx) was answered by en/[section]/[slug]/fallback.tsx, which the table tries first — supply shell locations that pattern cannot match
533
+ the shell for /en/posts/:id ([locale]/posts/[id]/fallback.tsx) was answered by en/[section]/[slug]/fallback.tsx, which the table tries first — put the shell where that pattern cannot match, with a paths.ts above it
503
534
  static build could not render /ja/posts/:id — see the error above
504
535
  ```
505
536
 
506
537
  The grammar's refusals — a `fallback.tsx` with no `page.tsx` beside it, or
507
- beside a `layout.tsx`, or with nothing to leave to the browser — are in
538
+ beside a `layout.tsx`, or with nothing to leave to the browser, and a
539
+ `paths.ts` outside a `[param]` directory — are in
508
540
  [What the build refuses](routing.md#what-the-build-refuses).
509
541
 
510
542
  ### Under `vite dev`, and under server mode
511
543
 
512
- `vite dev` answers a value `paths` does not list with the shell, as a host's
513
- rule would, so a shell can be tried without a build. It says so once per
544
+ `vite dev` answers a value as the build's host will, so what works locally
545
+ is what is deployed. A value a `paths.ts` returns is rendered by its
546
+ `page.tsx`. A value none returns is answered with the shell where a
547
+ `fallback.tsx` stands beside the page, and otherwise with the not-found: a
548
+ document is rendered as `not-found.tsx` under a 404, and a payload request
549
+ gets a plain 404, so the browser loads the document; an application with no
550
+ `not-found.tsx` gets a bare 404. It says which, once per pattern or
514
551
  `fallback.tsx`:
515
552
 
516
553
  ```
517
- k8ordo: /posts/3 is not in "paths", so vite dev answers it with posts/[id]/fallback.tsx's shell — list it in "paths" to render page.tsx
554
+ k8ordo: no paths.ts returns /posts/3, so vite dev answers it with the not-found, as the build's host will — return its value from the paths.ts of /posts/:id to render it
555
+ k8ordo: no paths.ts returns /posts/3, so vite dev answers it with posts/[id]/fallback.tsx's shell, as the build's host will
518
556
  ```
519
557
 
520
- It calls `paths` only for an application with a `fallback.tsx`, and again
521
- only when the patterns it hands in change, so a value added to the data
522
- since is one more unlisted value. A URL no fallback's pattern could match
523
- never waits for it. What the build would refuse in `paths` is logged as a
524
- warning, `k8ordo: the build will refuse "paths": …`, and a `paths` that
525
- throws leaves every value to its page:
526
- `k8ordo: the "paths" option failed, so vite dev renders every value with page.tsx: …`.
558
+ It asks the `paths.ts` files once and keeps the answer until a file under
559
+ `routes/` changes, and asks again when a request names a value it does not
560
+ hold — so a value the data has gained since is rendered without a restart.
561
+ A URL no pattern with a parameter could match never waits for them. What
562
+ the build would refuse is logged as a warning,
563
+ `k8ordo: the build will refuse this: …`, and a `paths.ts` that throws
564
+ leaves every value to its page:
565
+ `k8ordo: a paths.ts failed, so vite dev renders every value with page.tsx: …`.
527
566
 
528
567
  Under `mode: 'server'` every value is rendered by its page, per request, and
529
- a `fallback.tsx` is never rendered — nothing of it is built in — so switching
530
- modes needs no change to the files. The grammar's rules for it hold in both
531
- modes.
568
+ neither a `fallback.tsx` nor a `paths.ts` is read — nothing of them is built
569
+ in — so switching modes needs no change to the files. The grammar's rules
570
+ for them hold in both modes.