@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.
- package/README.md +6 -4
- package/dist/cli.mjs +3 -3
- package/dist/generated.d.mts +12 -13
- package/dist/index.d.mts +3 -3
- package/dist/index.mjs +1 -1
- package/dist/paths-type-DXpmjaOr.d.mts +43 -0
- package/dist/runtime/app-router.mjs +1 -1
- package/dist/runtime/browser-payloads.mjs +1 -1
- package/dist/runtime/cookies.d.mts +2 -0
- package/dist/runtime/cookies.mjs +2 -1
- package/dist/runtime/ended.d.mts +7 -0
- package/dist/runtime/ended.mjs +14 -0
- package/dist/runtime/entry.browser.mjs +1 -1
- package/dist/runtime/entry.rsc.d.mts +17 -7
- package/dist/runtime/entry.rsc.mjs +42 -33
- package/dist/runtime/entry.ssr.d.mts +1 -0
- package/dist/runtime/entry.ssr.mjs +4 -3
- package/dist/runtime/guard.d.mts +15 -10
- package/dist/runtime/guard.mjs +8 -2
- package/dist/runtime/mount.mjs +1 -1
- package/dist/runtime/page-boundary.mjs +2 -2
- package/dist/runtime/params.d.mts +5 -4
- package/dist/runtime/params.mjs +2 -2
- package/dist/runtime/redirect.d.mts +13 -7
- package/dist/runtime/redirect.mjs +25 -9
- package/dist/runtime/request-scope.d.mts +37 -2
- package/dist/runtime/request-scope.mjs +60 -3
- package/dist/runtime/request.d.mts +6 -1
- package/dist/runtime/request.mjs +7 -2
- package/dist/runtime/route.d.mts +4 -2
- package/dist/runtime/route.mjs +13 -5
- package/dist/runtime/server.d.mts +5 -5
- package/dist/runtime/server.mjs +2 -2
- package/dist/runtime/shell.d.mts +2 -1
- package/dist/serve.d.mts +4 -0
- package/dist/serve.mjs +26 -13
- package/dist/{static-c-nX9Juk.d.mts → static-Be3xIqd8.d.mts} +19 -12
- package/dist/{static-file-BEk_xO5y.mjs → static-file-BS-WEiwu.mjs} +180 -136
- package/dist/{static-host-CyAmsL_7.mjs → static-host-tIDLqV8c.mjs} +1 -1
- package/dist/vercel.d.mts +18 -1
- package/dist/vercel.mjs +104 -7
- package/dist/vite.d.mts +6 -3
- package/dist/vite.mjs +155 -65
- package/docs/GUIDE.md +149 -21
- package/docs/llms.txt +102 -47
- package/docs/references/actions.md +14 -5
- package/docs/references/boundaries.md +23 -2
- package/docs/references/csp.md +91 -2
- package/docs/references/deploy.md +85 -16
- package/docs/references/errors.md +18 -10
- package/docs/references/guards.md +100 -29
- package/docs/references/params.md +91 -22
- package/docs/references/routing.md +171 -31
- package/package.json +15 -10
- package/dist/paths-type-DtsL8JuC.d.mts +0 -19
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
What a route parameter arrives as, and — under `mode: 'static'` — how the
|
|
4
4
|
build learns which values to render, and what answers the ones it did not.
|
|
5
5
|
Under `mode: 'server'` a value comes with the request and needs no list.
|
|
6
|
+
A `[...name]` directory's parameter arrives as an array of segments
|
|
7
|
+
([Rest parameters](#rest-parameters)), under `mode: 'server'` only.
|
|
6
8
|
|
|
7
9
|
## Parameters with a schema
|
|
8
10
|
|
|
@@ -69,6 +71,50 @@ be a lie, and a not-found receives strings for the same reason. A layout that
|
|
|
69
71
|
wants the parsed value beside the page's parses it itself, or declares the
|
|
70
72
|
schema and lets the pages below it receive the result.
|
|
71
73
|
|
|
74
|
+
## Rest parameters
|
|
75
|
+
|
|
76
|
+
Server mode only. The parameter of a `[...name]` directory
|
|
77
|
+
([Rest parameters](routing.md#rest-parameters)) arrives as the segments it
|
|
78
|
+
took, one or more, each decoded — `readonly string[]`, where a `[name]`
|
|
79
|
+
gives a string:
|
|
80
|
+
|
|
81
|
+
```tsx
|
|
82
|
+
// src/routes/docs/[...path]/page.tsx
|
|
83
|
+
import type { PageProps } from '@k8ordo/framework';
|
|
84
|
+
|
|
85
|
+
export default function DocPage({ params }: PageProps<'/docs/:path+'>) {
|
|
86
|
+
return <h1>{params.path.join(' / ')}</h1>; // /docs/guide/intro → guide / intro
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
A `paramsSchema` receives that array and validates it as any other value: a
|
|
91
|
+
refusal is a pathname the directory does not answer, and what the schema
|
|
92
|
+
returns is what the page receives and what a link takes.
|
|
93
|
+
|
|
94
|
+
```tsx
|
|
95
|
+
// src/routes/docs/[...path]/page.tsx
|
|
96
|
+
import type { PageProps } from '@k8ordo/framework';
|
|
97
|
+
import * as z from 'zod/mini';
|
|
98
|
+
|
|
99
|
+
const SLUG = /^[a-z0-9-]+$/u;
|
|
100
|
+
|
|
101
|
+
export const paramsSchema = z.object({
|
|
102
|
+
path: z.array(z.string().check(z.regex(SLUG))).check(z.maxLength(4)),
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
export default function DocPage({ params }: PageProps<'/docs/:path+'>) {
|
|
106
|
+
return <h1>{params.path.join(' / ')}</h1>;
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
`/docs/Guide` and `/docs/a/b/c/d/e` are then pathnames nothing answers here,
|
|
111
|
+
and go on to the nearest `not-found.tsx`. The layout in the directory, a
|
|
112
|
+
`guard.ts` and the `not-found.tsx` above receive the array as the pathname
|
|
113
|
+
carried it, as they receive every parameter.
|
|
114
|
+
|
|
115
|
+
`mode: 'static'` refuses the directory itself, so none of the sections below
|
|
116
|
+
— `paths.ts`, `fallback.tsx` — applies to one.
|
|
117
|
+
|
|
72
118
|
## Routes with parameters
|
|
73
119
|
|
|
74
120
|
Static mode only. A build into files cannot invent parameter values, so it
|
|
@@ -82,16 +128,19 @@ import type { Paths } from '@k8ordo/framework';
|
|
|
82
128
|
import { listProducts } from '../../../lib/catalog.server';
|
|
83
129
|
|
|
84
130
|
const paths: Paths<'/products/:id'> = async () =>
|
|
85
|
-
(await listProducts()).map((product) =>
|
|
131
|
+
(await listProducts()).map((product) => product.id);
|
|
86
132
|
|
|
87
133
|
export default paths;
|
|
88
134
|
```
|
|
89
135
|
|
|
90
136
|
Its default export is a function returning the values the directory's
|
|
91
|
-
parameter takes: an array
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
137
|
+
parameter takes: an array, or a promise of one. `Paths<Pattern>` is its
|
|
138
|
+
type — exported from `@k8ordo/framework`, a type only — where the pattern
|
|
139
|
+
is the directory's own, ending in that parameter; the generated table checks
|
|
140
|
+
each file against it. **A value is what a link to the page takes**: the
|
|
141
|
+
`paramsSchema` output where the page's stack declares one — a number for the
|
|
142
|
+
`id` above, so the catalog's ids go in as they are — and anything with one
|
|
143
|
+
spelling (a string, a number, a bigint or a boolean) where none does.
|
|
95
144
|
|
|
96
145
|
**It runs where the pages run.** The build asks from the built RSC bundle,
|
|
97
146
|
and `vite dev` through the RSC environment's module runner: the module graph
|
|
@@ -124,28 +173,54 @@ import type { Paths } from '@k8ordo/framework';
|
|
|
124
173
|
import { postsIn } from '../../../../lib/posts.server';
|
|
125
174
|
|
|
126
175
|
const paths: Paths<'/:locale/posts/:id'> = async ({ locale }) =>
|
|
127
|
-
(await postsIn(locale)).map((post) =>
|
|
176
|
+
(await postsIn(locale)).map((post) => post.id);
|
|
128
177
|
|
|
129
178
|
export default paths;
|
|
130
179
|
```
|
|
131
180
|
|
|
132
|
-
The params arrive as the
|
|
133
|
-
|
|
134
|
-
|
|
181
|
+
**The params arrive as the layouts above read them.** Each value above is
|
|
182
|
+
spelled as the build writes it, then run through the `paramsSchema` exports
|
|
183
|
+
of the layouts in the directories above this `paths.ts`'s, outer first. The
|
|
184
|
+
page beside it and a layout in its own directory take no part: they may read
|
|
185
|
+
the value this file is being asked for, which nobody has yet. Their type is
|
|
186
|
+
exactly what those schemas produce, and the string for a parameter none of
|
|
187
|
+
them reads: `locale` is the locale `[locale]/layout.tsx`'s schema accepted.
|
|
188
|
+
When two `paths.ts` share a pattern under different layouts, as sibling route
|
|
189
|
+
groups can, the type is what both of them receive: a `year` one reads as a
|
|
190
|
+
number and the other leaves alone is `number | string`.
|
|
191
|
+
A directory needs no page of its own for its values to be read: with
|
|
192
|
+
`[year]/layout.tsx` reading `year` through
|
|
193
|
+
`z.object({ year: z.coerce.number() })` and no page at `[year]/`,
|
|
194
|
+
`[year]/paths.ts` may return `'2024'`, and `[year]/[slug]/paths.ts`
|
|
195
|
+
receives `year` as the number `2024`.
|
|
196
|
+
|
|
197
|
+
Values above that one of those schemas refuses are never handed over: the
|
|
198
|
+
`paths.ts` below is not asked under them, and the build refuses, as it does
|
|
199
|
+
for a shell standing there:
|
|
200
|
+
|
|
201
|
+
```
|
|
202
|
+
the paths.ts files stand where a params schema refuses the values above them, so they were not asked: /last/:slug ([year]/[slug]/paths.ts)
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
A `paths.ts` below a parameter whose directory has none cannot be asked,
|
|
206
|
+
and the build refuses:
|
|
135
207
|
|
|
136
208
|
```
|
|
137
209
|
[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
210
|
```
|
|
139
211
|
|
|
140
|
-
**
|
|
141
|
-
URL
|
|
142
|
-
unescaped name.
|
|
143
|
-
|
|
212
|
+
**The build spells a value as `href` does** — `String(value)`, escaped for
|
|
213
|
+
the URL — so `café` and `a b` are returned as a visitor reads them, and the
|
|
214
|
+
file is written under the unescaped name. A string must not be empty, and a
|
|
215
|
+
value spelled twice is written once:
|
|
144
216
|
|
|
145
217
|
```
|
|
146
|
-
tags/[tag]/paths.ts has to return an array of non-empty
|
|
218
|
+
tags/[tag]/paths.ts has to return an array of the values of :tag, each a non-empty string, a number, a bigint or a boolean
|
|
147
219
|
```
|
|
148
220
|
|
|
221
|
+
The spelling is the one the page's `paramsSchema` reads back, as for a
|
|
222
|
+
link; a value it refuses stops the build (below).
|
|
223
|
+
|
|
149
224
|
A value that is `..` names a file outside the output, and `!fallback` is the
|
|
150
225
|
segment the build keeps for shells:
|
|
151
226
|
|
|
@@ -180,13 +255,7 @@ one error, with any page that failed to render, so fixing one does not reveal
|
|
|
180
255
|
the next.
|
|
181
256
|
|
|
182
257
|
`paths.ts` belongs in a `[param]` directory and nowhere else
|
|
183
|
-
([What the build refuses](routing.md#what-the-build-refuses))
|
|
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
|
-
```
|
|
258
|
+
([What the build refuses](routing.md#what-the-build-refuses)).
|
|
190
259
|
|
|
191
260
|
Under `mode: 'server'` a `paths.ts` is never read and nothing of it is
|
|
192
261
|
built in, so the same `routes/` works in both modes.
|
|
@@ -317,7 +386,7 @@ import type { Paths } from '@k8ordo/framework';
|
|
|
317
386
|
import { listPosts } from '../../../lib/posts.server';
|
|
318
387
|
|
|
319
388
|
const paths: Paths<'/posts/:id'> = async () =>
|
|
320
|
-
(await listPosts()).map((post) =>
|
|
389
|
+
(await listPosts()).map((post) => post.id);
|
|
321
390
|
|
|
322
391
|
export default paths;
|
|
323
392
|
```
|
|
@@ -21,6 +21,8 @@ src/routes/
|
|
|
21
21
|
page.tsx /products
|
|
22
22
|
[id]/page.tsx /products/:id
|
|
23
23
|
[id]/fallback.tsx the ids a static build did not write (static mode)
|
|
24
|
+
files/
|
|
25
|
+
[...path]/route.ts /files/a, /files/a/b, … every segment left (server mode)
|
|
24
26
|
(docs)/ a route group: its own layout, no URL segment
|
|
25
27
|
layout.tsx
|
|
26
28
|
guide/page.tsx /guide
|
|
@@ -31,7 +33,8 @@ src/lib/ every other module, flat
|
|
|
31
33
|
- `page.tsx`, `layout.tsx`, `not-found.tsx`, `error.tsx`, `loading.tsx`,
|
|
32
34
|
`fallback.tsx`, `paths.ts`, `redirect.ts`, `guard.ts` and `route.ts` are
|
|
33
35
|
the only filenames the grammar accepts. `mode: 'static'` refuses
|
|
34
|
-
`guard.ts
|
|
36
|
+
`guard.ts`, and a `[...name]` directory with whatever it holds
|
|
37
|
+
([Choosing a mode](../GUIDE.md#what-static-mode-refuses)), and
|
|
35
38
|
`mode: 'server'` never renders `fallback.tsx` and never reads `paths.ts`,
|
|
36
39
|
though it checks where each sits
|
|
37
40
|
([Values the build did not write](#values-the-build-did-not-write)). Any
|
|
@@ -42,7 +45,8 @@ src/lib/ every other module, flat
|
|
|
42
45
|
skipped, so a `.DS_Store` or an editor's swap file does not break the
|
|
43
46
|
build.
|
|
44
47
|
- A directory name is a literal URL segment of letters, digits, `.`, `_`, `~`
|
|
45
|
-
and `-`; `[name]` is a parameter
|
|
48
|
+
and `-`; `[name]` is a parameter, `[...name]` a rest parameter
|
|
49
|
+
([Rest parameters](#rest-parameters)) and `(name)` is a group. `_` is an
|
|
46
50
|
ordinary character, so `_drafts/page.tsx` answers `/_drafts`. A name that
|
|
47
51
|
starts with `.` is never read, so `/.well-known` cannot be declared as a
|
|
48
52
|
route.
|
|
@@ -73,30 +77,88 @@ export default async function ProductPage({
|
|
|
73
77
|
}
|
|
74
78
|
```
|
|
75
79
|
|
|
76
|
-
`PageProps<P>`
|
|
77
|
-
|
|
78
|
-
with only `comments/page.tsx` below it —
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
request:
|
|
80
|
+
`PageProps<P>` takes a pattern the table has a page at. `LayoutProps<P>`
|
|
81
|
+
takes any pattern, so a layout whose directory holds no `page.tsx` of its
|
|
82
|
+
own — `blog/[slug]/` with only `comments/page.tsx` below it — names its own
|
|
83
|
+
pattern all the same. Its params are strings, and under `mode: 'server'` it
|
|
84
|
+
receives `request` too:
|
|
82
85
|
|
|
83
86
|
```tsx
|
|
84
87
|
// src/routes/blog/[slug]/layout.tsx
|
|
85
|
-
import type {
|
|
88
|
+
import type { LayoutProps } from '@k8ordo/framework';
|
|
86
89
|
|
|
87
90
|
export default function PostLayout({
|
|
88
91
|
params,
|
|
89
92
|
children,
|
|
90
|
-
}: {
|
|
91
|
-
params: { slug: string };
|
|
92
|
-
children: ReactNode;
|
|
93
|
-
}) {
|
|
93
|
+
}: LayoutProps<'/blog/:slug'>) {
|
|
94
94
|
return <article data-slug={params.slug}>{children}</article>;
|
|
95
95
|
}
|
|
96
96
|
```
|
|
97
97
|
|
|
98
|
-
The generated table checks those props against the pattern at
|
|
99
|
-
as it checks every route file's
|
|
98
|
+
The generated table checks those props against the layout's own pattern at
|
|
99
|
+
the import, as it checks every route file's, so a pattern that names other
|
|
100
|
+
params fails there.
|
|
101
|
+
|
|
102
|
+
**A layout marks the current link with `matchPath`.** It receives `pathname`
|
|
103
|
+
in the table's terms, and `matchPath` is a pure function a Server Component
|
|
104
|
+
calls, so the navigation renders on the server with its `aria-current`
|
|
105
|
+
already right. `aria-current="page"` goes only on the link whose own URL is
|
|
106
|
+
the page on screen, an exact match; a link to a section the page is inside
|
|
107
|
+
gets `aria-current="true"`, an inclusive match:
|
|
108
|
+
|
|
109
|
+
```tsx
|
|
110
|
+
// src/routes/layout.tsx
|
|
111
|
+
import { href, matchPath } from '@k8ordo/framework';
|
|
112
|
+
import type { LayoutProps } from '@k8ordo/framework';
|
|
113
|
+
|
|
114
|
+
export default function RootLayout({ children, pathname }: LayoutProps<'/'>) {
|
|
115
|
+
const onProducts = matchPath('/products', pathname) !== null;
|
|
116
|
+
const inProducts =
|
|
117
|
+
matchPath('/products/*', pathname, { inclusive: true }) !== null;
|
|
118
|
+
return (
|
|
119
|
+
<>
|
|
120
|
+
<nav>
|
|
121
|
+
<a
|
|
122
|
+
aria-current={onProducts ? 'page' : inProducts ? 'true' : undefined}
|
|
123
|
+
href={href('/products')}
|
|
124
|
+
>
|
|
125
|
+
Products
|
|
126
|
+
</a>
|
|
127
|
+
</nav>
|
|
128
|
+
{children}
|
|
129
|
+
</>
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
A Client Component asks `useMatch('/products')` and
|
|
135
|
+
`useMatch('/products/*', { inclusive: true })` instead. Never compare `pathname === href(…)`: `pathname` is in the table's
|
|
136
|
+
terms and `href` returns the URL with Vite's `base` in front, so under a base
|
|
137
|
+
the two never agree.
|
|
138
|
+
|
|
139
|
+
## One way per URL
|
|
140
|
+
|
|
141
|
+
Each thing an application does with a URL has one spelling, the same under
|
|
142
|
+
`@k8ordo/router`, `@k8ordo/state` and `@k8ordo/framework`:
|
|
143
|
+
|
|
144
|
+
| to | write |
|
|
145
|
+
| ----------------------------------------------------------- | ------------------------------------------------------------------------------------- |
|
|
146
|
+
| link to a page | `href(pattern, params)` |
|
|
147
|
+
| link to a page with its state | `def.href(href(pattern, params), values)` |
|
|
148
|
+
| link to the page itself with other state | `def.href('', values)` |
|
|
149
|
+
| ask which page is showing | `matchPath(pattern, pathname)` in a Server Component, `useMatch(pattern)` in a client |
|
|
150
|
+
| change the page from code | `navigation.navigate(href(pattern, params))`, the link's own URL |
|
|
151
|
+
| change the state | `update(values)` |
|
|
152
|
+
| move a page for good | `redirect.ts`, its `to` a table pattern (`@k8ordo/framework`) |
|
|
153
|
+
| redirect from a Server Action, a `guard.ts` or a `route.ts` | `redirect(href(pattern, params))` (`@k8ordo/framework/server`) |
|
|
154
|
+
| show the page on screen in another locale | `withBase(locales.localize(pathname, locale))` |
|
|
155
|
+
|
|
156
|
+
The last row is the one URL built from a pathname rather than a pattern: the
|
|
157
|
+
page showing may be any page. Nothing else compares or combines `pathname` and
|
|
158
|
+
`href()` — `pathname` is in the table's terms and `href()` carries Vite's
|
|
159
|
+
`base`. In a `[locale]` application, import `href` only from the module that
|
|
160
|
+
binds the locale (`export const href = bindParams(…)`): the router's own
|
|
161
|
+
`href` has the same name and type-checks too.
|
|
100
162
|
|
|
101
163
|
## How a page is sent
|
|
102
164
|
|
|
@@ -226,9 +288,15 @@ Every problem is reported, not just the first, and each names the file:
|
|
|
226
288
|
| `about/page.tsx` and `about/fallback.tsx` | `fallback.tsx stands in for values the build did not write, and "/about" has no parameter; remove it` |
|
|
227
289
|
| `[locale]/layout.tsx` above `[locale]/about/fallback.tsx` | `fallback.tsx has nothing to leave to the browser — [locale]/layout.tsx receives every parameter of "/:locale/about"; remove fallback.tsx and return the values from a paths.ts` |
|
|
228
290
|
| `posts/paths.ts` | `paths.ts gives the values of its directory's parameter, and "posts" is not a [param] directory — move it into the parameter's directory` |
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
291
|
+
| `files/[...path]/raw/page.tsx` | `"[...path]" takes the rest of the pathname, so it is the last directory of its path — move "raw" out of "files/[...path]"` |
|
|
292
|
+
| `files/[...a]/page.tsx` and `files/[...b]/page.tsx` | `"[...b]" cannot sit beside "[...a]" — each takes the rest of the pathname below "files"; keep one` |
|
|
293
|
+
| `[...1]/page.tsx` | `"[...1]" is not a valid rest directory — use [...name] with a letter or underscore first` |
|
|
294
|
+
| `files/[...path]/not-found.tsx` | `not-found.tsx answers what nothing below its directory did, and nothing is below "files/[...path]" — [...path] takes the rest of the pathname; move it one directory up` |
|
|
295
|
+
| `files/[...path]/fallback.tsx` | `fallback.tsx is the shell a build into files answers a parameter's unwritten values with, and "files/[...path]" is a [...name] directory, which no build into files writes; remove it` |
|
|
296
|
+
|
|
297
|
+
The generated table lists literal segments before parameters, and a
|
|
298
|
+
parameter before a rest parameter, so `about/` beside `[slug]/` — and
|
|
299
|
+
`[slug]/` beside `[...path]/` — is reachable without saying anything. A route group holds
|
|
232
300
|
both kinds under one key and the table cannot interleave across it, which is
|
|
233
301
|
the one shape where a declared route can still be shadowed — so it is reported
|
|
234
302
|
rather than shipped.
|
|
@@ -242,6 +310,61 @@ parameter no `paths.ts` gives values to
|
|
|
242
310
|
standing where it cannot
|
|
243
311
|
([What else stops the build](params.md#what-else-stops-the-build)).
|
|
244
312
|
|
|
313
|
+
## Rest parameters
|
|
314
|
+
|
|
315
|
+
Server mode only. A `[...name]` directory takes every segment left of the
|
|
316
|
+
pathname — one or more — where `[name]` takes exactly one:
|
|
317
|
+
|
|
318
|
+
```ts
|
|
319
|
+
// src/routes/files/[...path]/route.ts
|
|
320
|
+
import type { RouteContext } from '@k8ordo/framework';
|
|
321
|
+
|
|
322
|
+
export function GET({ params }: RouteContext<'/files/:path+'>) {
|
|
323
|
+
return Response.json({ name: params.path.at(-1) });
|
|
324
|
+
}
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
`/files/reports/2026/summary.pdf` reaches it with
|
|
328
|
+
`params.path` as `['reports', '2026', 'summary.pdf']`.
|
|
329
|
+
|
|
330
|
+
- **Its pattern is `/files/:path+`** — what `PageProps`, `LayoutProps`,
|
|
331
|
+
`RouteContext`, `Guard` and `href` name it by.
|
|
332
|
+
- **The value is an array of segments**, each decoded on its own:
|
|
333
|
+
`/files/a%2Fb/c` is `['a/b', 'c']`. A `page.tsx`, a `layout.tsx`, a
|
|
334
|
+
`route.ts` and a `guard.ts` in the directory all receive it, and a
|
|
335
|
+
`paramsSchema` there validates the array and may replace it
|
|
336
|
+
([Parameters](params.md#rest-parameters)).
|
|
337
|
+
- **A link takes the same array**: `href('/files/:path+', { path: ['a/b', 'c'] })`
|
|
338
|
+
is `/files/a%2Fb/c`. An empty array, or an empty segment, throws.
|
|
339
|
+
- **Zero segments is not its URL.** `/files` is answered by `files/page.tsx`
|
|
340
|
+
(or `route.ts`, or `redirect.ts`) when there is one and by the nearest
|
|
341
|
+
`not-found.tsx` when there is not. There is no optional form:
|
|
342
|
+
`[[...name]]` is not a directory name the grammar reads.
|
|
343
|
+
- **It is the last directory of its path.** Nothing sits below it, a route
|
|
344
|
+
group included, and one parent holds one. It may hold `page.tsx`,
|
|
345
|
+
`layout.tsx`, `error.tsx`, `loading.tsx`, `guard.ts`, `route.ts` and
|
|
346
|
+
`redirect.ts`; not `not-found.tsx`, which would have nothing below it to
|
|
347
|
+
answer for, nor `fallback.tsx` or `paths.ts`.
|
|
348
|
+
- **It is tried after what is more specific.** Beside it, a literal
|
|
349
|
+
directory answers first, then a `[name]`, then the `[...name]`, and the
|
|
350
|
+
`not-found.tsx` of the directory above only after all of them. So
|
|
351
|
+
`files/readme/page.tsx` keeps `/files/readme`, and `files/[id]/page.tsx`
|
|
352
|
+
keeps every single segment its `paramsSchema` accepts, leaving the rest —
|
|
353
|
+
and the ones it refused — to `files/[...path]/`.
|
|
354
|
+
- **A `redirect.ts` moves it with the same spelling.**
|
|
355
|
+
`old/[...path]/redirect.ts` exporting `'/files/:path+'` sends
|
|
356
|
+
`/old/a/b` to `/files/a/b`, every segment as the URL spelled it.
|
|
357
|
+
|
|
358
|
+
`mode: 'static'` refuses a `[...name]` directory, in the build before
|
|
359
|
+
anything is compiled and in `vite dev` as a file in it is compiled: it
|
|
360
|
+
answers a pathname of any depth, which only a request names.
|
|
361
|
+
|
|
362
|
+
```
|
|
363
|
+
static build cannot write a [...name] directory — it answers a pathname of any depth, which only a request names, and this is one:
|
|
364
|
+
src/routes/files/[...path]
|
|
365
|
+
this application wants mode: 'server'
|
|
366
|
+
```
|
|
367
|
+
|
|
245
368
|
## The values of a parameter
|
|
246
369
|
|
|
247
370
|
Under `mode: 'static'`, a `paths.ts` is what the build asks for the values
|
|
@@ -372,7 +495,9 @@ A directory answers from a `route.ts` or renders a `page.tsx` (or redirects),
|
|
|
372
495
|
never two of them, and the layouts above a `route.ts` do not wrap it —
|
|
373
496
|
nothing renders. It takes its place in the table's order the way a page does,
|
|
374
497
|
literals before params, so `api/[id]/route.ts` beside `api/latest/page.tsx`
|
|
375
|
-
leaves `/api/latest` to the page.
|
|
498
|
+
leaves `/api/latest` to the page. An `api/[...rest]/route.ts` answers every
|
|
499
|
+
URL below `/api` that nothing more specific does
|
|
500
|
+
([Rest parameters](#rest-parameters)). A client navigation to it gets no payload,
|
|
376
501
|
and loads the document instead.
|
|
377
502
|
|
|
378
503
|
### Under server mode
|
|
@@ -383,16 +508,23 @@ method it does not export gets a `405` whose `Allow` names the ones it does.
|
|
|
383
508
|
The guards above it run first. A `POST` to a `route.ts` is not held to the
|
|
384
509
|
same-origin rule a Server Action is — what posts to one, a webhook, is not a
|
|
385
510
|
form on this site — so it checks what it needs itself. It answers the request
|
|
386
|
-
as much as a guard does:
|
|
387
|
-
`
|
|
511
|
+
as much as a guard does, and ends it the same ways: `redirect(url)` answers
|
|
512
|
+
with a `303`, and `notFound()` with the nearest `not-found.tsx` under a 404
|
|
513
|
+
— a `route.ts` that wants a 404 of another shape, JSON say, returns its own
|
|
514
|
+
`Response`. [`cookies()`](guards.md#cookies),
|
|
515
|
+
`responseHeaders()` and `requestHeaders()` work inside it, and
|
|
516
|
+
[`waitUntil()`](guards.md#work-after-the-answer) keeps the host running for
|
|
517
|
+
what it goes on with after answering — from inside a stream it answered
|
|
518
|
+
with, too.
|
|
388
519
|
|
|
389
520
|
### Under static mode
|
|
390
521
|
|
|
391
522
|
The build calls its `GET` once for each pathname the site has — a pattern
|
|
392
523
|
with params takes its values from the `paths.ts` files above it, as a
|
|
393
524
|
page's does ([Parameters](params.md#routes-with-parameters)) — with a request whose
|
|
394
|
-
origin is `site`
|
|
395
|
-
absolute), and writes what it answered as the
|
|
525
|
+
origin is `site`, the origin `framework()` requires under static mode (an
|
|
526
|
+
RSS feed's links are absolute, as above), and writes what it answered as the
|
|
527
|
+
file at that pathname:
|
|
396
528
|
`dist/client/feed.xml`. A file keeps no type of its own, so a static host
|
|
397
529
|
serves it with the type its extension says — `vite preview` and `serve` too,
|
|
398
530
|
which send a file with no extension as `application/octet-stream`. On
|
|
@@ -429,9 +561,8 @@ static build writes a route.ts as the file its GET answers, and these cannot be
|
|
|
429
561
|
|
|
430
562
|
`sitemap.xml` is the exception: a `route.ts` there is the application's own
|
|
431
563
|
list of the site, with the dates and priorities only it knows, so the build
|
|
432
|
-
writes none of its own and leaves the file to it
|
|
433
|
-
|
|
434
|
-
is handed, which a sitemap's absolute URLs are built from. Rules of your own for a host go in
|
|
564
|
+
writes none of its own and leaves the file to it, building its absolute URLs
|
|
565
|
+
from the `request.url` it is handed, whose origin is `site`. Rules of your own for a host go in
|
|
435
566
|
`public/_redirects`, which the build adds to its own. `vite dev` does not
|
|
436
567
|
stop: only the build writes the files.
|
|
437
568
|
|
|
@@ -529,9 +660,9 @@ each checked against `Paths` of its directory's pattern in a `PathsChecks`
|
|
|
529
660
|
type (under `mode: 'server'` it too is empty); all seven are empty here
|
|
530
661
|
because no route file declares any.
|
|
531
662
|
|
|
532
|
-
`.k8ordo/register.gen.ts` registers that table with the framework
|
|
533
|
-
|
|
534
|
-
|
|
663
|
+
`.k8ordo/register.gen.ts` registers that table with the framework, so typed
|
|
664
|
+
links work everywhere without a line of ceremony — a page state's link too,
|
|
665
|
+
since it is built on what `href` returns. It also says what the mode hands a
|
|
535
666
|
route file: under `mode: 'server'` it registers the `request`, which is how
|
|
536
667
|
`PageProps` and `LayoutProps` gain that field there and not under
|
|
537
668
|
`mode: 'static'`:
|
|
@@ -571,6 +702,12 @@ unchecked; an application that keeps its set elsewhere writes the
|
|
|
571
702
|
goes, or gains or loses the export. A hand-written augmentation left in the
|
|
572
703
|
application still compiles beside it, since both declare the same type.
|
|
573
704
|
|
|
705
|
+
Running it is the framework's part too: `src/i18n.ts` runs before any route
|
|
706
|
+
file, in the browser and in both server renders (the Server Components' and
|
|
707
|
+
the HTML's), whether `@k8ordo/i18n` is a dependency or not. The set is
|
|
708
|
+
registered wherever a message is called, even in a module that never imports
|
|
709
|
+
it.
|
|
710
|
+
|
|
574
711
|
Neither file names `@k8ordo/router`: they import from
|
|
575
712
|
`@k8ordo/framework/generated`, so the application's code, generated or not,
|
|
576
713
|
reads only the framework.
|
|
@@ -579,7 +716,7 @@ reads only the framework.
|
|
|
579
716
|
clone — a CI checkout among them — has neither until something writes them.
|
|
580
717
|
`k8ordo generate`, the package's command, writes them and stops there: it
|
|
581
718
|
reads the Vite config in the working directory as `vite build` does, so the
|
|
582
|
-
`mode`
|
|
719
|
+
`mode` given to `framework()` is the one it uses, then
|
|
583
720
|
builds nothing and renders no page — no `paths.ts` is read, and nothing a
|
|
584
721
|
page reads is touched. A `routes/` with the problems of
|
|
585
722
|
[What the build refuses](#what-the-build-refuses) fails it as it fails the
|
|
@@ -680,7 +817,10 @@ import mdx from '@mdx-js/rollup';
|
|
|
680
817
|
import { defineConfig } from 'vite';
|
|
681
818
|
|
|
682
819
|
export default defineConfig({
|
|
683
|
-
plugins: [
|
|
820
|
+
plugins: [
|
|
821
|
+
{ enforce: 'pre', ...mdx() },
|
|
822
|
+
framework({ mode: 'static', site: 'https://example.com' }),
|
|
823
|
+
],
|
|
684
824
|
});
|
|
685
825
|
```
|
|
686
826
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@k8ordo/framework",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"description": "The k8ordo framework: routes/ compiled into React Server Components, run with or without a server.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"k8ordo",
|
|
@@ -64,41 +64,46 @@
|
|
|
64
64
|
"registry": "https://registry.npmjs.org"
|
|
65
65
|
},
|
|
66
66
|
"dependencies": {
|
|
67
|
-
"@vitejs/plugin-react": "6.
|
|
67
|
+
"@vitejs/plugin-react": "6.3.0",
|
|
68
68
|
"@vitejs/plugin-rsc": "0.5.39",
|
|
69
|
+
"baseline-browser-mapping": "2.11.39",
|
|
69
70
|
"compressible": "2.0.18",
|
|
70
71
|
"fresh": "2.0.0",
|
|
71
72
|
"mime-types": "3.0.2",
|
|
72
73
|
"negotiator": "1.1.0",
|
|
73
|
-
"range-parser": "
|
|
74
|
+
"range-parser": "2.0.0",
|
|
74
75
|
"rsc-html-stream": "0.0.8"
|
|
75
76
|
},
|
|
76
77
|
"devDependencies": {
|
|
77
78
|
"@arethetypeswrong/cli": "0.18.5",
|
|
78
|
-
"@k8ordo/framework-engine": "^0.1.
|
|
79
|
-
"@k8ordo/router": "^
|
|
79
|
+
"@k8ordo/framework-engine": "^0.1.4",
|
|
80
|
+
"@k8ordo/router": "^4.0.0",
|
|
80
81
|
"@types/compressible": "2.0.3",
|
|
81
82
|
"@types/fresh": "0.5.3",
|
|
82
83
|
"@types/mime-types": "3.0.1",
|
|
83
|
-
"@types/range-parser": "1.2.7",
|
|
84
84
|
"@types/react": "19.3.0",
|
|
85
|
+
"@vercel/functions": "3.11.3",
|
|
85
86
|
"oxc-transform-react": "0.156.0",
|
|
86
87
|
"publint": "0.3.25",
|
|
87
88
|
"react": "19.3.1",
|
|
88
89
|
"react-dom": "19.3.1",
|
|
89
90
|
"typescript": "7.0.2",
|
|
90
|
-
"vite": "npm:@voidzero-dev/vite-plus-core@
|
|
91
|
-
"vite-plus": "1.
|
|
92
|
-
"vitest": "5.1.
|
|
91
|
+
"vite": "npm:@voidzero-dev/vite-plus-core@8.3.8000",
|
|
92
|
+
"vite-plus": "1.7.0",
|
|
93
|
+
"vitest": "5.1.1"
|
|
93
94
|
},
|
|
94
95
|
"peerDependencies": {
|
|
95
|
-
"@k8ordo/router": "^
|
|
96
|
+
"@k8ordo/router": "^4.0.0",
|
|
97
|
+
"@vercel/functions": "^3.0.0",
|
|
96
98
|
"oxc-transform-react": ">=0.152.0 <0.157.0",
|
|
97
99
|
"react": ">=19.3.0",
|
|
98
100
|
"react-dom": ">=19.3.0",
|
|
99
101
|
"vite": ">=8.1.0"
|
|
100
102
|
},
|
|
101
103
|
"peerDependenciesMeta": {
|
|
104
|
+
"@vercel/functions": {
|
|
105
|
+
"optional": true
|
|
106
|
+
},
|
|
102
107
|
"oxc-transform-react": {
|
|
103
108
|
"optional": true
|
|
104
109
|
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { ParamsOf } from "@k8ordo/router";
|
|
2
|
-
/** The parameter a pattern ends in: `id` of `/:locale/posts/:id`. */
|
|
3
|
-
type LastParam<Pattern extends string> = Pattern extends `${string}/:${infer Rest}` ? Rest extends `${string}/:${string}` ? LastParam<`/${Rest}`> : Rest : never;
|
|
4
|
-
/**
|
|
5
|
-
* What a `paths.ts` exports by default, under `mode: 'static'`: the values
|
|
6
|
-
* its directory's parameter takes, given the params of the directories above
|
|
7
|
-
* it — one call per combination of theirs. `Pattern` is the directory's own,
|
|
8
|
-
* ending in that parameter. A value is the segment as a visitor reads it
|
|
9
|
-
* (`café`, not `caf%C3%A9`); the build escapes it for the URL.
|
|
10
|
-
*
|
|
11
|
-
* ```ts
|
|
12
|
-
* // src/routes/[locale]/posts/[id]/paths.ts
|
|
13
|
-
* const paths: Paths<'/:locale/posts/:id'> = async ({ locale }) =>
|
|
14
|
-
* (await postsIn(locale)).map((post) => String(post.id));
|
|
15
|
-
* export default paths;
|
|
16
|
-
* ```
|
|
17
|
-
*/
|
|
18
|
-
type Paths<Pattern extends string> = (params: Omit<ParamsOf<Pattern>, LastParam<Pattern>>) => readonly string[] | Promise<readonly string[]>;
|
|
19
|
-
export { Paths as t };
|