@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.
- package/README.md +28 -13
- package/dist/cli.mjs +3 -3
- package/dist/generated.d.mts +13 -13
- package/dist/headers-BxB_jJVk.mjs +73 -0
- package/dist/index.d.mts +3 -2
- package/dist/index.mjs +1 -1
- package/dist/paths-type-DXpmjaOr.d.mts +43 -0
- package/dist/runtime/app-router.mjs +21 -8
- package/dist/runtime/browser-payloads.mjs +1 -1
- package/dist/runtime/client-files.d.mts +6 -5
- package/dist/runtime/client-files.mjs +17 -9
- package/dist/runtime/client-ready.d.mts +7 -1
- package/dist/runtime/client-ready.mjs +2 -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 +38 -2
- package/dist/runtime/entry.rsc.mjs +97 -31
- package/dist/runtime/entry.ssr.d.mts +7 -1
- package/dist/runtime/entry.ssr.mjs +12 -6
- 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/payload.d.mts +19 -4
- package/dist/runtime/payload.mjs +20 -5
- package/dist/runtime/prefetch.d.mts +29 -8
- package/dist/runtime/prefetch.mjs +95 -24
- 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 +29 -14
- package/dist/static-Be3xIqd8.d.mts +62 -0
- package/dist/{static-file-BAjwBMMl.mjs → static-file-BS-WEiwu.mjs} +311 -134
- package/dist/{static-host-B-1fAfAw.mjs → static-host-tIDLqV8c.mjs} +1 -1
- package/dist/vercel.d.mts +18 -1
- package/dist/vercel.mjs +131 -19
- package/dist/vite.d.mts +6 -3
- package/dist/vite.mjs +366 -156
- package/docs/GUIDE.md +173 -46
- package/docs/llms.txt +129 -65
- 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 +154 -25
- package/docs/references/errors.md +57 -12
- package/docs/references/guards.md +100 -29
- package/docs/references/params.md +234 -126
- package/docs/references/routing.md +358 -75
- package/package.json +18 -13
- package/dist/shared-dir-CNXFRZgP.mjs +0 -9
- package/dist/static-DnbgGIoy.d.mts +0 -61
|
@@ -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
|
|
@@ -29,11 +31,13 @@ src/lib/ every other module, flat
|
|
|
29
31
|
```
|
|
30
32
|
|
|
31
33
|
- `page.tsx`, `layout.tsx`, `not-found.tsx`, `error.tsx`, `loading.tsx`,
|
|
32
|
-
`fallback.tsx`, `redirect.ts`, `guard.ts` and `route.ts` are
|
|
33
|
-
filenames the grammar accepts. `mode: 'static'` refuses
|
|
34
|
+
`fallback.tsx`, `paths.ts`, `redirect.ts`, `guard.ts` and `route.ts` are
|
|
35
|
+
the only filenames the grammar accepts. `mode: 'static'` refuses
|
|
36
|
+
`guard.ts`, and a `[...name]` directory with whatever it holds
|
|
34
37
|
([Choosing a mode](../GUIDE.md#what-static-mode-refuses)), and
|
|
35
|
-
`mode: 'server'` never renders `fallback.tsx
|
|
36
|
-
|
|
38
|
+
`mode: 'server'` never renders `fallback.tsx` and never reads `paths.ts`,
|
|
39
|
+
though it checks where each sits
|
|
40
|
+
([Values the build did not write](#values-the-build-did-not-write)). Any
|
|
37
41
|
other file is refused: components live outside `routes/` in
|
|
38
42
|
`src/components/` and every other module — Server Actions, schemas, state
|
|
39
43
|
definitions, data, helpers — in `src/lib/`, and a route file imports them
|
|
@@ -41,7 +45,8 @@ src/lib/ every other module, flat
|
|
|
41
45
|
skipped, so a `.DS_Store` or an editor's swap file does not break the
|
|
42
46
|
build.
|
|
43
47
|
- A directory name is a literal URL segment of letters, digits, `.`, `_`, `~`
|
|
44
|
-
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
|
|
45
50
|
ordinary character, so `_drafts/page.tsx` answers `/_drafts`. A name that
|
|
46
51
|
starts with `.` is never read, so `/.well-known` cannot be declared as a
|
|
47
52
|
route.
|
|
@@ -53,9 +58,10 @@ src/lib/ every other module, flat
|
|
|
53
58
|
receive `request` ([Reading the request](guards.md#reading-the-request)).
|
|
54
59
|
- A parameter's values arrive with the request under `mode: 'server'`, and
|
|
55
60
|
need no list. Under `mode: 'static'` the build has no request to take them
|
|
56
|
-
from, and
|
|
61
|
+
from, and a `paths.ts` in the parameter's directory returns them, for
|
|
62
|
+
every route below it
|
|
57
63
|
([Routes with parameters](params.md#routes-with-parameters)); a
|
|
58
|
-
`fallback.tsx` beside the page answers the ones it does not
|
|
64
|
+
`fallback.tsx` beside the page answers the ones it does not return.
|
|
59
65
|
|
|
60
66
|
```tsx
|
|
61
67
|
// src/routes/products/[id]/page.tsx
|
|
@@ -71,30 +77,88 @@ export default async function ProductPage({
|
|
|
71
77
|
}
|
|
72
78
|
```
|
|
73
79
|
|
|
74
|
-
`PageProps<P>`
|
|
75
|
-
|
|
76
|
-
with only `comments/page.tsx` below it —
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
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:
|
|
80
85
|
|
|
81
86
|
```tsx
|
|
82
87
|
// src/routes/blog/[slug]/layout.tsx
|
|
83
|
-
import type {
|
|
88
|
+
import type { LayoutProps } from '@k8ordo/framework';
|
|
84
89
|
|
|
85
90
|
export default function PostLayout({
|
|
86
91
|
params,
|
|
87
92
|
children,
|
|
88
|
-
}: {
|
|
89
|
-
params: { slug: string };
|
|
90
|
-
children: ReactNode;
|
|
91
|
-
}) {
|
|
93
|
+
}: LayoutProps<'/blog/:slug'>) {
|
|
92
94
|
return <article data-slug={params.slug}>{children}</article>;
|
|
93
95
|
}
|
|
94
96
|
```
|
|
95
97
|
|
|
96
|
-
The generated table checks those props against the pattern at
|
|
97
|
-
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.
|
|
98
162
|
|
|
99
163
|
## How a page is sent
|
|
100
164
|
|
|
@@ -194,8 +258,9 @@ Such a script runs before any of the page's CSS has loaded: it can set a
|
|
|
194
258
|
class or an attribute on `<html>`, but a style it computes reads none of
|
|
195
259
|
that CSS. A script with a `src` or a `type` stays where React wrote it, and
|
|
196
260
|
so does one inside a `<noscript>` or a `<template>`, and every script in
|
|
197
|
-
`<body>`. Under `mode: 'static'` the policy's `<meta>`
|
|
198
|
-
`<
|
|
261
|
+
`<body>`. Under `mode: 'static'` the policy's `<meta>` comes right after
|
|
262
|
+
the `<meta charSet>` that opens `<head>` (first in `<head>` when there is
|
|
263
|
+
none), ahead of the moved scripts, so the policy covers them
|
|
199
264
|
([Content Security Policy](csp.md#static-mode-hashes-in-a-meta)).
|
|
200
265
|
|
|
201
266
|
## What the build refuses
|
|
@@ -204,7 +269,7 @@ Every problem is reported, not just the first, and each names the file:
|
|
|
204
269
|
|
|
205
270
|
| routes/ contains | error |
|
|
206
271
|
| ------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
207
|
-
| `products/helper.ts` | `routes/ holds only page.tsx, layout.tsx, not-found.tsx, error.tsx, redirect.ts, guard.ts, route.ts, loading.tsx, fallback.tsx — move "helper.ts" out of routes/`
|
|
272
|
+
| `products/helper.ts` | `routes/ holds only page.tsx, layout.tsx, not-found.tsx, error.tsx, redirect.ts, guard.ts, route.ts, loading.tsx, fallback.tsx, paths.ts — move "helper.ts" out of routes/` |
|
|
208
273
|
| `[123]/page.tsx` | `"[123]" is not a valid param directory — use [name] with a letter or underscore first` |
|
|
209
274
|
| `pro ducts/page.tsx` | `"pro ducts" cannot be a URL segment — use letters, digits, . _ ~ or -` |
|
|
210
275
|
| `[id]/things/[id]/page.tsx` | `":id" is already taken by an ancestor — params must be unique within a path` |
|
|
@@ -221,24 +286,109 @@ Every problem is reported, not just the first, and each names the file:
|
|
|
221
286
|
| `feed/[id]/route.ts` and `feed/[id]/fallback.tsx` | `fallback.tsx stands in for the page.tsx beside it, and "feed/[id]" has none` |
|
|
222
287
|
| `posts/[id]/layout.tsx` beside `posts/[id]/fallback.tsx` | `fallback.tsx cannot sit beside layout.tsx — that layout receives every parameter of "posts/[id]", so no shell could leave one to the browser; move the layout one directory up, or into page.tsx and fallback.tsx` |
|
|
223
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` |
|
|
224
|
-
| `[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
|
|
225
|
-
|
|
226
|
-
|
|
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` |
|
|
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` |
|
|
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
|
|
228
300
|
both kinds under one key and the table cannot interleave across it, which is
|
|
229
301
|
the one shape where a declared route can still be shadowed — so it is reported
|
|
230
302
|
rather than shipped.
|
|
231
303
|
|
|
232
|
-
The `fallback.tsx` rows hold in both modes, so switching
|
|
233
|
-
change to the files. `mode: 'static'` refuses more on top of
|
|
234
|
-
needs a request
|
|
235
|
-
|
|
304
|
+
The `fallback.tsx` and `paths.ts` rows hold in both modes, so switching
|
|
305
|
+
modes needs no change to the files. `mode: 'static'` refuses more on top of
|
|
306
|
+
these: whatever needs a request
|
|
307
|
+
([What static mode refuses](../GUIDE.md#what-static-mode-refuses)), a
|
|
308
|
+
parameter no `paths.ts` gives values to
|
|
309
|
+
([Routes with parameters](params.md#routes-with-parameters)), and a shell
|
|
310
|
+
standing where it cannot
|
|
236
311
|
([What else stops the build](params.md#what-else-stops-the-build)).
|
|
237
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
|
+
|
|
368
|
+
## The values of a parameter
|
|
369
|
+
|
|
370
|
+
Under `mode: 'static'`, a `paths.ts` is what the build asks for the values
|
|
371
|
+
a parameter takes: its default export, a function typed `Paths<Pattern>`,
|
|
372
|
+
returns them, and every page, `route.ts` and `redirect.ts` below its
|
|
373
|
+
directory is written once per value. What it is handed, what a value is and
|
|
374
|
+
where it runs are in
|
|
375
|
+
[Routes with parameters](params.md#routes-with-parameters); the grammar's
|
|
376
|
+
part is where it may sit.
|
|
377
|
+
|
|
378
|
+
- **In a `[param]` directory**, whose parameter it gives values to. In the
|
|
379
|
+
root, a literal directory or a group it is refused.
|
|
380
|
+
- **With or without a `page.tsx` beside it.** The values are for whatever
|
|
381
|
+
sits below: `[locale]/paths.ts` beside only a `layout.tsx` gives the
|
|
382
|
+
locale of every page under it.
|
|
383
|
+
- **With or without a `fallback.tsx` beside it.** With both, the values
|
|
384
|
+
returned are written as pages and the shell answers the rest.
|
|
385
|
+
|
|
386
|
+
Under `mode: 'server'` it is not read, and nothing of it is built.
|
|
387
|
+
|
|
238
388
|
## Values the build did not write
|
|
239
389
|
|
|
240
390
|
Under `mode: 'static'`, a `fallback.tsx` beside a `page.tsx` is what the
|
|
241
|
-
values `paths`
|
|
391
|
+
values no `paths.ts` returns are answered with: a shell, the layouts above
|
|
242
392
|
written as HTML with the `fallback.tsx` in the page's place, which a host
|
|
243
393
|
serves for any such value and the browser fills in from the URL. What it
|
|
244
394
|
renders, how it reads the value and what it costs are in
|
|
@@ -250,7 +400,8 @@ the grammar's part is where it may sit.
|
|
|
250
400
|
- **Under a pattern with a parameter**, at least one of which no layout
|
|
251
401
|
above it receives. A layout above is written into the shell once, as
|
|
252
402
|
HTML, for every value the shell answers, so a parameter it receives is one
|
|
253
|
-
the shell has to know —
|
|
403
|
+
the shell has to know — given by a `paths.ts` in that parameter's
|
|
404
|
+
directory, which puts a shell below each of its values
|
|
254
405
|
(`/ja/posts/:id`). The parameters left to the browser are the rest.
|
|
255
406
|
- **With no `layout.tsx` beside it.** That layout would receive every
|
|
256
407
|
parameter, and leave the browser none. Move it one directory up, where it
|
|
@@ -259,9 +410,9 @@ the grammar's part is where it may sit.
|
|
|
259
410
|
|
|
260
411
|
It receives no props, and an `error.tsx` or `loading.tsx` beside it or above
|
|
261
412
|
wraps it as it wraps the page. It answers its own page's pattern and nothing
|
|
262
|
-
below it: `posts/[id]/comments/page.tsx`
|
|
263
|
-
a `fallback.tsx` of its own, and a
|
|
264
|
-
|
|
413
|
+
below it: `posts/[id]/comments/page.tsx` is written for the values
|
|
414
|
+
`posts/[id]/paths.ts` returns, or keeps a `fallback.tsx` of its own, and a
|
|
415
|
+
`redirect.ts` or `route.ts` there answers only the values returned.
|
|
265
416
|
|
|
266
417
|
Under `mode: 'server'` every value is rendered by its page, per request, and
|
|
267
418
|
nothing of a `fallback.tsx` is built.
|
|
@@ -344,7 +495,9 @@ A directory answers from a `route.ts` or renders a `page.tsx` (or redirects),
|
|
|
344
495
|
never two of them, and the layouts above a `route.ts` do not wrap it —
|
|
345
496
|
nothing renders. It takes its place in the table's order the way a page does,
|
|
346
497
|
literals before params, so `api/[id]/route.ts` beside `api/latest/page.tsx`
|
|
347
|
-
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,
|
|
348
501
|
and loads the document instead.
|
|
349
502
|
|
|
350
503
|
### Under server mode
|
|
@@ -355,16 +508,23 @@ method it does not export gets a `405` whose `Allow` names the ones it does.
|
|
|
355
508
|
The guards above it run first. A `POST` to a `route.ts` is not held to the
|
|
356
509
|
same-origin rule a Server Action is — what posts to one, a webhook, is not a
|
|
357
510
|
form on this site — so it checks what it needs itself. It answers the request
|
|
358
|
-
as much as a guard does:
|
|
359
|
-
`
|
|
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.
|
|
360
519
|
|
|
361
520
|
### Under static mode
|
|
362
521
|
|
|
363
522
|
The build calls its `GET` once for each pathname the site has — a pattern
|
|
364
|
-
with params
|
|
365
|
-
([Parameters](params.md#routes-with-parameters)) — with a request whose
|
|
366
|
-
origin is `site`
|
|
367
|
-
absolute), and writes what it answered as the
|
|
523
|
+
with params takes its values from the `paths.ts` files above it, as a
|
|
524
|
+
page's does ([Parameters](params.md#routes-with-parameters)) — with a request whose
|
|
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:
|
|
368
528
|
`dist/client/feed.xml`. A file keeps no type of its own, so a static host
|
|
369
529
|
serves it with the type its extension says — `vite preview` and `serve` too,
|
|
370
530
|
which send a file with no extension as `application/octet-stream`. On
|
|
@@ -388,20 +548,21 @@ A `route.ts` at `/` cannot be a file (the host serves `/` from `index.html`),
|
|
|
388
548
|
nor one with pages written below it, which would need a directory of the same
|
|
389
549
|
name. What it writes is not a page, so `sitemap.xml` leaves it out.
|
|
390
550
|
|
|
391
|
-
Nor can a `route.ts` write a file the build writes itself — `
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
551
|
+
Nor can a `route.ts` write a file the build writes itself — `404.html`
|
|
552
|
+
beside a `not-found.tsx`, `_redirects` when there are shells,
|
|
553
|
+
`client-files.json`, a page's `index.html` or `index.rsc` — since one would
|
|
554
|
+
replace the other. It stops before it renders anything, naming every such
|
|
555
|
+
`route.ts` in one error, each beside what is in its way:
|
|
396
556
|
|
|
397
557
|
```
|
|
398
558
|
static build writes a route.ts as the file its GET answers, and these cannot be written — remove the route.ts, or what else is written there:
|
|
399
|
-
|
|
559
|
+
client-files.json: src/routes/client-files.json/route.ts, and the build lists there what each page loads beyond its payload
|
|
400
560
|
```
|
|
401
561
|
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
562
|
+
`sitemap.xml` is the exception: a `route.ts` there is the application's own
|
|
563
|
+
list of the site, with the dates and priorities only it knows, so the build
|
|
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
|
|
405
566
|
`public/_redirects`, which the build adds to its own. `vite dev` does not
|
|
406
567
|
stop: only the build writes the files.
|
|
407
568
|
|
|
@@ -457,6 +618,8 @@ export const redirects = {} as const;
|
|
|
457
618
|
|
|
458
619
|
export const fallbacks = {} as const;
|
|
459
620
|
|
|
621
|
+
export const paths = {} as const;
|
|
622
|
+
|
|
460
623
|
export const routes = defineRoutes({
|
|
461
624
|
'/': {
|
|
462
625
|
layout: layout satisfies Layout<'/'>,
|
|
@@ -491,12 +654,15 @@ page once the walk has imported it — `routeModules` what imports the
|
|
|
491
654
|
`searchReaders` what reads the search for a page that exports `search`, and
|
|
492
655
|
`fallbacks`, under `mode: 'static'`, what imports the `fallback.tsx` beside a
|
|
493
656
|
page and the parameters its shell may leave to the browser (under
|
|
494
|
-
`mode: 'server'` it is always empty, and no `fallback.tsx` is imported)
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
657
|
+
`mode: 'server'` it is always empty, and no `fallback.tsx` is imported), and
|
|
658
|
+
`paths`, under `mode: 'static'`, what imports each `paths.ts`, by its file,
|
|
659
|
+
each checked against `Paths` of its directory's pattern in a `PathsChecks`
|
|
660
|
+
type (under `mode: 'server'` it too is empty); all seven are empty here
|
|
661
|
+
because no route file declares any.
|
|
662
|
+
|
|
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
|
|
500
666
|
route file: under `mode: 'server'` it registers the `request`, which is how
|
|
501
667
|
`PageProps` and `LayoutProps` gain that field there and not under
|
|
502
668
|
`mode: 'static'`:
|
|
@@ -536,6 +702,12 @@ unchecked; an application that keeps its set elsewhere writes the
|
|
|
536
702
|
goes, or gains or loses the export. A hand-written augmentation left in the
|
|
537
703
|
application still compiles beside it, since both declare the same type.
|
|
538
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
|
+
|
|
539
711
|
Neither file names `@k8ordo/router`: they import from
|
|
540
712
|
`@k8ordo/framework/generated`, so the application's code, generated or not,
|
|
541
713
|
reads only the framework.
|
|
@@ -544,17 +716,17 @@ reads only the framework.
|
|
|
544
716
|
clone — a CI checkout among them — has neither until something writes them.
|
|
545
717
|
`k8ordo generate`, the package's command, writes them and stops there: it
|
|
546
718
|
reads the Vite config in the working directory as `vite build` does, so the
|
|
547
|
-
`mode`
|
|
548
|
-
builds nothing and renders no page — `paths` is
|
|
719
|
+
`mode` given to `framework()` is the one it uses, then
|
|
720
|
+
builds nothing and renders no page — no `paths.ts` is read, and nothing a
|
|
549
721
|
page reads is touched. A `routes/` with the problems of
|
|
550
722
|
[What the build refuses](#what-the-build-refuses) fails it as it fails the
|
|
551
723
|
build, with `routes/ is not a valid pathname space:` and every problem
|
|
552
724
|
named, and nothing is written. What `mode: 'static'` refuses on top of
|
|
553
725
|
those — a `guard.ts`, a `route.ts` answering more than `GET`, a page
|
|
554
|
-
reading `search`, an `export *`, a Server Action, whatever `paths`
|
|
726
|
+
reading `search`, an `export *`, a Server Action, whatever a `paths.ts`
|
|
555
727
|
returns — it does not check, so its success is not the build's. Run it
|
|
556
728
|
before `tsc`; it is also how a CI job without the data a build reads — the
|
|
557
|
-
database behind `paths`, say — has the types:
|
|
729
|
+
database behind a `paths.ts`, say — has the types:
|
|
558
730
|
|
|
559
731
|
```yaml
|
|
560
732
|
# .github/workflows/ci.yml, in the application's directory
|
|
@@ -591,6 +763,99 @@ Keep one `<title>` on screen at a time: the root layout renders none, each
|
|
|
591
763
|
page renders its own, and `not-found.tsx` renders one too. Two titles at once
|
|
592
764
|
is not a fallback chain — React renders both.
|
|
593
765
|
|
|
766
|
+
## Counting page views
|
|
767
|
+
|
|
768
|
+
A page change here is not a document load, and it is not `pushState`
|
|
769
|
+
either — the router commits it through the Navigation API — so an analytics
|
|
770
|
+
script that counts only loads, or that watches the History API, misses
|
|
771
|
+
it. The platform says when a page change is done: `navigatesuccess` fires
|
|
772
|
+
once the new page is on screen, its `<title>` in place.
|
|
773
|
+
|
|
774
|
+
```tsx
|
|
775
|
+
'use client';
|
|
776
|
+
|
|
777
|
+
import { useEffect } from 'react';
|
|
778
|
+
|
|
779
|
+
export function PageViews() {
|
|
780
|
+
useEffect(() => {
|
|
781
|
+
let counted = location.pathname;
|
|
782
|
+
const count = () => {
|
|
783
|
+
// a search or a fragment that moved is a navigation too
|
|
784
|
+
if (location.pathname === counted) return;
|
|
785
|
+
counted = location.pathname;
|
|
786
|
+
gtag('event', 'page_view', {
|
|
787
|
+
page_location: location.href,
|
|
788
|
+
page_title: document.title,
|
|
789
|
+
});
|
|
790
|
+
};
|
|
791
|
+
navigation.addEventListener('navigatesuccess', count);
|
|
792
|
+
return () => {
|
|
793
|
+
navigation.removeEventListener('navigatesuccess', count);
|
|
794
|
+
};
|
|
795
|
+
}, []);
|
|
796
|
+
return null;
|
|
797
|
+
}
|
|
798
|
+
```
|
|
799
|
+
|
|
800
|
+
Render it from the root layout; the same listener works from a plain
|
|
801
|
+
script with no React at all. `usePathname()` is not the moment to count
|
|
802
|
+
from: it follows the URL, which the router commits before the next page
|
|
803
|
+
arrives, so the title read then is still the page being left. Turn off the
|
|
804
|
+
tool's own counting of history changes as well — going back and forward
|
|
805
|
+
still fires `popstate`, and those views would be counted twice.
|
|
806
|
+
|
|
807
|
+
## MDX
|
|
808
|
+
|
|
809
|
+
The framework compiles no MDX; an application adds a compiler as a Vite
|
|
810
|
+
plugin, ahead of `framework()` and before the framework's own transforms see
|
|
811
|
+
the file:
|
|
812
|
+
|
|
813
|
+
```ts
|
|
814
|
+
// vite.config.ts
|
|
815
|
+
import { framework } from '@k8ordo/framework/vite';
|
|
816
|
+
import mdx from '@mdx-js/rollup';
|
|
817
|
+
import { defineConfig } from 'vite';
|
|
818
|
+
|
|
819
|
+
export default defineConfig({
|
|
820
|
+
plugins: [
|
|
821
|
+
{ enforce: 'pre', ...mdx() },
|
|
822
|
+
framework({ mode: 'static', site: 'https://example.com' }),
|
|
823
|
+
],
|
|
824
|
+
});
|
|
825
|
+
```
|
|
826
|
+
|
|
827
|
+
An `.mdx` file is then a module whose default export is a Server Component,
|
|
828
|
+
imported like any other — statically, or late through `import.meta.glob` so
|
|
829
|
+
each page reads only its own:
|
|
830
|
+
|
|
831
|
+
```tsx
|
|
832
|
+
// src/routes/posts/[slug]/page.tsx
|
|
833
|
+
const posts = import.meta.glob<{ default: ComponentType }>(
|
|
834
|
+
'../../../content/*.mdx',
|
|
835
|
+
);
|
|
836
|
+
|
|
837
|
+
export default async function PostPage({ params }: PageProps<'/posts/:slug'>) {
|
|
838
|
+
const load = posts[`../../../content/${params.slug}.mdx`];
|
|
839
|
+
if (load === undefined) notFound();
|
|
840
|
+
const { default: Post } = await load();
|
|
841
|
+
return <Post />;
|
|
842
|
+
}
|
|
843
|
+
```
|
|
844
|
+
|
|
845
|
+
**CSS an `.mdx` file imports reaches the pages that render it**, as a `.tsx`
|
|
846
|
+
module's does: `import './post.module.css'` or `import 'katex/dist/katex.css'`
|
|
847
|
+
at the top of the file is linked in the HTML of the pages rendering that
|
|
848
|
+
file's default export, and of no other. No query on the import is needed.
|
|
849
|
+
(`?vite-rsc-css-export=default`, the RSC plugin's own, still works and is
|
|
850
|
+
left alone.)
|
|
851
|
+
|
|
852
|
+
The stylesheets ride on the component the module exports, in an `.mdx` as in
|
|
853
|
+
a `.tsx`: a module that imports CSS and exports only a table of components
|
|
854
|
+
(`export const components = { h1: Heading }`) for another module to render
|
|
855
|
+
carries none of it to the page. Export a component that renders with the
|
|
856
|
+
table — `export function Article({ children })` — from the module that
|
|
857
|
+
imports the CSS.
|
|
858
|
+
|
|
594
859
|
## Fetching the next page ahead
|
|
595
860
|
|
|
596
861
|
The client runtime listens on the whole document for a pointer moving onto a
|
|
@@ -605,10 +870,14 @@ with them their stylesheets, which apply to the page on screen from then on
|
|
|
605
870
|
A touch screen has no hover, and a tap's press and click come together,
|
|
606
871
|
leaving nothing to fetch ahead in. So under `mode: 'static'`, where the
|
|
607
872
|
primary pointer is coarse (`(pointer: coarse)`), the runtime fetches the
|
|
608
|
-
links on screen instead:
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
873
|
+
links on screen instead: once the document's `load` event has fired, each
|
|
874
|
+
link that has stayed on screen for a second, one at a time in the order
|
|
875
|
+
they appeared, at low fetch priority, passing over a link that left the
|
|
876
|
+
screen before its turn. While a page change loads it fetches nothing more,
|
|
877
|
+
and cancels a prefetch on its way to another page, so the page being
|
|
878
|
+
loaded has the connection to itself; that link is fetched first once the
|
|
879
|
+
new page shows. Under `mode: 'server'` it does not — every link a visitor
|
|
880
|
+
scrolls past would be a render on the server.
|
|
612
881
|
|
|
613
882
|
Only a link a click would load in place is fetched: the same origin and below
|
|
614
883
|
Vite's `base`, no `download`, no `target` other than `_self`, and not the
|
|
@@ -652,15 +921,29 @@ navigation when another overtakes it, unless a later navigation is waiting
|
|
|
652
921
|
for it too — the same as a fetch the navigation had started itself.
|
|
653
922
|
|
|
654
923
|
Under `mode: 'static'` the build also writes `client-files.json`: for each
|
|
655
|
-
page, the scripts and stylesheets its client components need
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
924
|
+
page, the scripts and stylesheets its client components need and the
|
|
925
|
+
stylesheets its Server Components link, less those every page loads. The
|
|
926
|
+
runtime reads it at low priority once the document runs, and whenever it
|
|
927
|
+
asks for a page's payload — a prefetch or a navigation — it links that
|
|
928
|
+
page's files at the same moment and at the payload's priority
|
|
929
|
+
(`<link rel="modulepreload">` for a script, `<link rel="preload"
|
|
930
|
+
as="style">` for a stylesheet) instead of waiting for the payload to name
|
|
931
|
+
them, a round trip later. The links on screen are fetched only once the
|
|
932
|
+
list has been read. A stylesheet still applies only once the page that
|
|
933
|
+
needs it is shown, and the page waits for it: a client component's once
|
|
934
|
+
that component loads, a Server Component's once it has arrived. A page the
|
|
935
|
+
list leaves out — a shell's value, or a host that lost the file — loads as
|
|
936
|
+
it would without one, its Server Components' stylesheets fetched as its
|
|
937
|
+
payload names them.
|
|
938
|
+
|
|
939
|
+
A page fetched ahead has its stylesheets fetched too, and they apply only
|
|
940
|
+
once the page is visited. Until then Chromium logs that the file "was
|
|
941
|
+
preloaded using link preload but not used within a few seconds", once per
|
|
942
|
+
file — more of them under a touch screen, where the links on screen are
|
|
943
|
+
fetched ahead. The warning is harmless: a visit uses what was fetched.
|
|
944
|
+
`<link rel="prefetch">`, which would not warn, is not in Safari, so it is
|
|
945
|
+
not used. Mark a long list `data-k8ordo-prefetch="false"` to fetch, and
|
|
946
|
+
warn, less.
|
|
664
947
|
|
|
665
948
|
Under `mode: 'server'` a prefetch is a render, as a navigation is — the
|
|
666
949
|
reason to mark a link to an expensive page. The platform's Speculation
|