@k8ordo/framework 1.2.0 → 1.3.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 +2 -2
- package/dist/runtime/app-router.d.mts +3 -2
- package/dist/runtime/app-router.mjs +31 -21
- package/dist/runtime/browser-payloads.mjs +3 -2
- package/dist/runtime/client-ready.d.mts +12 -8
- package/dist/runtime/client-ready.mjs +25 -14
- package/dist/runtime/entry.browser.mjs +1 -1
- package/dist/runtime/entry.rsc.mjs +17 -4
- package/dist/runtime/entry.ssr.d.mts +2 -1
- package/dist/runtime/entry.ssr.mjs +67 -13
- package/dist/runtime/payload.d.mts +37 -0
- package/dist/runtime/payload.mjs +27 -1
- package/dist/runtime/tree-pathname.d.mts +4 -0
- package/dist/runtime/tree-pathname.mjs +12 -0
- package/dist/vite.mjs +259 -51
- package/docs/llms.txt +9 -5
- package/docs/references/boundaries.md +5 -2
- package/docs/references/csp.md +29 -2
- package/docs/references/errors.md +34 -0
- package/docs/references/routing.md +41 -2
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -35,10 +35,10 @@ dependencies in either mode: the build bundles what the application runs.
|
|
|
35
35
|
|
|
36
36
|
| Package | Version | Required | Needed for |
|
|
37
37
|
| --------------------- | -------- | -------- | ------------------------------------------ |
|
|
38
|
-
| `@k8ordo/router` | ^1.
|
|
38
|
+
| `@k8ordo/router` | ^1.3.0 | yes | routing, re-exported by the framework |
|
|
39
39
|
| `react` | ≥19.3.0 | yes | rendering |
|
|
40
40
|
| `react-dom` | ≥19.3.0 | yes | rendering |
|
|
41
|
-
| `vite` | ≥8.
|
|
41
|
+
| `vite` | ≥8.1.0 | yes | the build (`framework()` is a Vite plugin) |
|
|
42
42
|
| `oxc-transform-react` | ^0.152.0 | optional | `reactCompiler: true` (React Compiler) |
|
|
43
43
|
|
|
44
44
|
<!-- /peers -->
|
|
@@ -6,9 +6,10 @@ export declare const setDocumentClient: (client: string) => void;
|
|
|
6
6
|
export declare const navigationsApplied: () => number;
|
|
7
7
|
/**
|
|
8
8
|
* What a Server Action's answer does to the document, once it is read.
|
|
9
|
-
* `sent` is `navigationsApplied()` as the action was sent
|
|
9
|
+
* `sent` is `navigationsApplied()` as the action was sent, and `url` where
|
|
10
|
+
* it was posted: the answer is that page.
|
|
10
11
|
*/
|
|
11
|
-
export declare const answerAction: (payload: Payload, sent: number) => Promise<unknown>;
|
|
12
|
+
export declare const answerAction: (payload: Payload, sent: number, url: string) => Promise<unknown>;
|
|
12
13
|
export declare const setPayloadReader: (read: (stream: ReadableStream<Uint8Array>) => Promise<Payload>) => void;
|
|
13
14
|
/**
|
|
14
15
|
* The client half under the framework: the tree comes from the server, so
|
|
@@ -5,7 +5,8 @@ import { ShowNotFound } from "./fallback-boundary.mjs";
|
|
|
5
5
|
import { isPayload } from "./is-payload.mjs";
|
|
6
6
|
import { createPrefetchCache, prefetchTargetOf } from "./prefetch.mjs";
|
|
7
7
|
import { Recover, markNavigated, reloadInstead } from "./recover.mjs";
|
|
8
|
-
import {
|
|
8
|
+
import { TreePathname } from "./tree-pathname.mjs";
|
|
9
|
+
import { NavigationGeneration, PathnameProvider, normalizePathname, useInterceptedNavigation, withoutBase } from "@k8ordo/router";
|
|
9
10
|
import { startTransition, useDeferredValue, useEffect, useRef, useState } from "react";
|
|
10
11
|
import { jsx } from "react/jsx-runtime";
|
|
11
12
|
//#region src/runtime/app-router.tsx
|
|
@@ -43,7 +44,7 @@ const canRender = (payload) => payload.client === documentClient;
|
|
|
43
44
|
* except under a `<Suspense>`, whose fallback stands in for it as a
|
|
44
45
|
* transition leaves it.
|
|
45
46
|
*/
|
|
46
|
-
const WHEN_APPLIED = {
|
|
47
|
+
const WHEN_APPLIED = { rows: "leave" };
|
|
47
48
|
/**
|
|
48
49
|
* How many trees navigations have applied — the router's, and `vite dev`'s
|
|
49
50
|
* reload of the page on screen. A Server Action's answer is not counted:
|
|
@@ -54,9 +55,10 @@ let navigations = 0;
|
|
|
54
55
|
const navigationsApplied = () => navigations;
|
|
55
56
|
/**
|
|
56
57
|
* What a Server Action's answer does to the document, once it is read.
|
|
57
|
-
* `sent` is `navigationsApplied()` as the action was sent
|
|
58
|
+
* `sent` is `navigationsApplied()` as the action was sent, and `url` where
|
|
59
|
+
* it was posted: the answer is that page.
|
|
58
60
|
*/
|
|
59
|
-
const answerAction = async (payload, sent) => {
|
|
61
|
+
const answerAction = async (payload, sent, url) => {
|
|
60
62
|
mounted?.forgetPrefetched();
|
|
61
63
|
if (payload.redirect !== void 0) {
|
|
62
64
|
await navigation.navigate(payload.redirect).finished;
|
|
@@ -64,7 +66,7 @@ const answerAction = async (payload, sent) => {
|
|
|
64
66
|
}
|
|
65
67
|
if (!canRender(payload)) return reloadInstead();
|
|
66
68
|
await whenClientReady([payload.tree, payload.notFound], WHEN_APPLIED);
|
|
67
|
-
if (navigations === sent) mounted?.apply(payload);
|
|
69
|
+
if (navigations === sent) mounted?.apply(payload, normalizePathname(new URL(url).pathname));
|
|
68
70
|
return payload.returnValue;
|
|
69
71
|
};
|
|
70
72
|
let readPayload = () => Promise.reject(/* @__PURE__ */ new Error("k8ordo: payloads are read in the browser only"));
|
|
@@ -115,7 +117,7 @@ let applies = 0;
|
|
|
115
117
|
* Takes over the request of a payload whose tree is being applied, and
|
|
116
118
|
* returns what applying it puts in state.
|
|
117
119
|
*/
|
|
118
|
-
const hold = (streaming, payload) => {
|
|
120
|
+
const hold = (streaming, payload, pathname) => {
|
|
119
121
|
applies += 1;
|
|
120
122
|
const stream = streams.get(payload);
|
|
121
123
|
if (stream !== void 0) {
|
|
@@ -129,7 +131,8 @@ const hold = (streaming, payload) => {
|
|
|
129
131
|
return {
|
|
130
132
|
tree: payload.tree,
|
|
131
133
|
notFound: payload.notFound,
|
|
132
|
-
order: applies
|
|
134
|
+
order: applies,
|
|
135
|
+
pathname
|
|
133
136
|
};
|
|
134
137
|
};
|
|
135
138
|
/**
|
|
@@ -164,7 +167,8 @@ function AppRouter({ pathname, search, tree, notFound }) {
|
|
|
164
167
|
const [latest, setLatest] = useState(() => ({
|
|
165
168
|
tree,
|
|
166
169
|
notFound,
|
|
167
|
-
order: 0
|
|
170
|
+
order: 0,
|
|
171
|
+
pathname: typeof location === "undefined" ? null : normalizePathname(location.pathname)
|
|
168
172
|
}));
|
|
169
173
|
const current = useDeferredValue(latest);
|
|
170
174
|
const prefetched = useRef(null);
|
|
@@ -181,9 +185,9 @@ function AppRouter({ pathname, search, tree, notFound }) {
|
|
|
181
185
|
}, [latest, current]);
|
|
182
186
|
useEffect(() => {
|
|
183
187
|
mounted = {
|
|
184
|
-
apply: (payload) => {
|
|
188
|
+
apply: (payload, postedTo) => {
|
|
185
189
|
searchApplied.current = payload.search;
|
|
186
|
-
const next = hold(streaming.current, payload);
|
|
190
|
+
const next = hold(streaming.current, payload, postedTo);
|
|
187
191
|
startTransition(() => {
|
|
188
192
|
setLatest(next);
|
|
189
193
|
});
|
|
@@ -216,7 +220,7 @@ function AppRouter({ pathname, search, tree, notFound }) {
|
|
|
216
220
|
if (!canRender(payload)) return reloadInstead();
|
|
217
221
|
markNavigated();
|
|
218
222
|
navigations += 1;
|
|
219
|
-
const next = hold(streaming.current, payload);
|
|
223
|
+
const next = hold(streaming.current, payload, normalizePathname(new URL(url).pathname));
|
|
220
224
|
searchApplied.current = payload.search;
|
|
221
225
|
startTransition(() => {
|
|
222
226
|
setLatest(next);
|
|
@@ -260,16 +264,19 @@ function AppRouter({ pathname, search, tree, notFound }) {
|
|
|
260
264
|
signal.throwIfAborted();
|
|
261
265
|
if (payload === null) return await reloadInstead();
|
|
262
266
|
if (!canRender(payload)) return await reloadInstead();
|
|
263
|
-
return
|
|
267
|
+
return {
|
|
268
|
+
payload,
|
|
269
|
+
pathname: normalizePathname(url.pathname)
|
|
270
|
+
};
|
|
264
271
|
} finally {
|
|
265
272
|
loading.current -= 1;
|
|
266
273
|
}
|
|
267
274
|
},
|
|
268
|
-
apply: (
|
|
275
|
+
apply: (loaded) => {
|
|
269
276
|
markNavigated();
|
|
270
277
|
navigations += 1;
|
|
271
|
-
searchApplied.current =
|
|
272
|
-
setLatest(hold(streaming.current,
|
|
278
|
+
searchApplied.current = loaded.payload.search;
|
|
279
|
+
setLatest(hold(streaming.current, loaded.payload, loaded.pathname));
|
|
273
280
|
},
|
|
274
281
|
refresh: (url) => searchApplied.current !== void 0 && url.search !== searchApplied.current
|
|
275
282
|
});
|
|
@@ -277,9 +284,9 @@ function AppRouter({ pathname, search, tree, notFound }) {
|
|
|
277
284
|
const show = () => {
|
|
278
285
|
if (loading.current > 0) return false;
|
|
279
286
|
setLatest((previous) => previous === shown ? {
|
|
287
|
+
...shown,
|
|
280
288
|
tree: shown.notFound,
|
|
281
|
-
notFound: void 0
|
|
282
|
-
order: shown.order
|
|
289
|
+
notFound: void 0
|
|
283
290
|
} : previous);
|
|
284
291
|
return true;
|
|
285
292
|
};
|
|
@@ -287,10 +294,13 @@ function AppRouter({ pathname, search, tree, notFound }) {
|
|
|
287
294
|
pathname,
|
|
288
295
|
children: /* @__PURE__ */ jsx(NavigationGeneration, {
|
|
289
296
|
value: generation,
|
|
290
|
-
children: /* @__PURE__ */ jsx(
|
|
291
|
-
value: current.
|
|
292
|
-
children:
|
|
293
|
-
|
|
297
|
+
children: /* @__PURE__ */ jsx(TreePathname, {
|
|
298
|
+
value: current.pathname,
|
|
299
|
+
children: /* @__PURE__ */ jsx(Recover, { children: /* @__PURE__ */ jsx(ShowNotFound, {
|
|
300
|
+
value: current.notFound === void 0 ? null : show,
|
|
301
|
+
children: current.tree
|
|
302
|
+
}) })
|
|
303
|
+
})
|
|
294
304
|
})
|
|
295
305
|
});
|
|
296
306
|
}
|
|
@@ -11,13 +11,14 @@ const connectAppRouter = () => {
|
|
|
11
11
|
setPayloadReader((stream) => createFromReadableStream(stream));
|
|
12
12
|
setServerCallback(async (id, args) => {
|
|
13
13
|
const sent = navigationsApplied();
|
|
14
|
+
const url = location.href;
|
|
14
15
|
const temporaryReferences = createTemporaryReferenceSet();
|
|
15
|
-
const payload = await createFromFetch(fetch(
|
|
16
|
+
const payload = await createFromFetch(fetch(url, {
|
|
16
17
|
method: "POST",
|
|
17
18
|
headers: { [ACTION_ID_HEADER]: id },
|
|
18
19
|
body: await encodeReply(args, { temporaryReferences })
|
|
19
20
|
}), { temporaryReferences });
|
|
20
|
-
return answerAction(payload, sent);
|
|
21
|
+
return answerAction(payload, sent, url);
|
|
21
22
|
});
|
|
22
23
|
};
|
|
23
24
|
//#endregion
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
//#region src/runtime/client-ready.d.ts
|
|
2
2
|
export type ClientReadyOptions = {
|
|
3
3
|
/**
|
|
4
|
-
* What to do with a row still on its way
|
|
5
|
-
*
|
|
6
|
-
* leaves it — a page streaming what it put under a boundary of its own
|
|
4
|
+
* What to do with a row still on its way: `'wait'` for it; `'leave'` one
|
|
5
|
+
* inside a `<Suspense>` to that boundary's fallback, as a transition
|
|
6
|
+
* leaves it — a page streaming what it put under a boundary of its own —
|
|
7
|
+
* and wait for the rest; or `'follow'` every one — settle without it, and
|
|
8
|
+
* walk it once it arrives, as an HTML render streaming it does.
|
|
7
9
|
*/
|
|
8
|
-
readonly
|
|
10
|
+
readonly rows: 'wait' | 'leave' | 'follow';
|
|
9
11
|
};
|
|
10
12
|
/**
|
|
11
13
|
* Settles once the client components a payload's trees name have loaded.
|
|
@@ -19,9 +21,11 @@ export type ClientReadyOptions = {
|
|
|
19
21
|
* holding elements), arrays and plain objects, each once, since a payload
|
|
20
22
|
* shares and nests them. A row still on its way (a lazy node: what a page
|
|
21
23
|
* sends once its data is in) is waited for and walked too, except as
|
|
22
|
-
* `
|
|
23
|
-
*
|
|
24
|
-
*
|
|
24
|
+
* `rows` says. A row followed is still walked once it arrives, the promise
|
|
25
|
+
* already settled, and a client component it names is read once it has
|
|
26
|
+
* loaded, as one waited for is. What fails — a row the server could not
|
|
27
|
+
* render, a module that would not load — ends the wait there, for the
|
|
28
|
+
* render to meet where an error boundary takes it.
|
|
25
29
|
*/
|
|
26
|
-
export declare const whenClientReady: (trees: readonly unknown[], {
|
|
30
|
+
export declare const whenClientReady: (trees: readonly unknown[], { rows }: ClientReadyOptions) => Promise<void>;
|
|
27
31
|
//#endregion
|
|
@@ -35,23 +35,39 @@ const whenSettled = (thenable) => new Promise((resolve) => {
|
|
|
35
35
|
* holding elements), arrays and plain objects, each once, since a payload
|
|
36
36
|
* shares and nests them. A row still on its way (a lazy node: what a page
|
|
37
37
|
* sends once its data is in) is waited for and walked too, except as
|
|
38
|
-
* `
|
|
39
|
-
*
|
|
40
|
-
*
|
|
38
|
+
* `rows` says. A row followed is still walked once it arrives, the promise
|
|
39
|
+
* already settled, and a client component it names is read once it has
|
|
40
|
+
* loaded, as one waited for is. What fails — a row the server could not
|
|
41
|
+
* render, a module that would not load — ends the wait there, for the
|
|
42
|
+
* render to meet where an error boundary takes it.
|
|
41
43
|
*/
|
|
42
|
-
const whenClientReady = async (trees, {
|
|
44
|
+
const whenClientReady = async (trees, { rows }) => {
|
|
43
45
|
const seen = /* @__PURE__ */ new WeakSet();
|
|
44
46
|
let waiting = [];
|
|
47
|
+
const settle = async () => {
|
|
48
|
+
while (waiting.length > 0) {
|
|
49
|
+
const round = waiting;
|
|
50
|
+
waiting = [];
|
|
51
|
+
await Promise.all(round.map(({ until }) => whenSettled(until)));
|
|
52
|
+
for (const { again } of round) again();
|
|
53
|
+
}
|
|
54
|
+
};
|
|
45
55
|
const read = (lazy, inside, isType) => {
|
|
46
56
|
let value;
|
|
47
57
|
try {
|
|
48
58
|
value = lazy._init(lazy._payload);
|
|
49
59
|
} catch (thrown) {
|
|
50
|
-
if (isThenable(thrown)
|
|
60
|
+
if (!isThenable(thrown)) return;
|
|
61
|
+
const again = () => {
|
|
62
|
+
read(lazy, inside, isType);
|
|
63
|
+
};
|
|
64
|
+
if (isType || rows === "wait" || rows === "leave" && !inside) waiting.push({
|
|
51
65
|
until: thrown,
|
|
52
|
-
again
|
|
53
|
-
|
|
54
|
-
|
|
66
|
+
again
|
|
67
|
+
});
|
|
68
|
+
else if (rows === "follow") whenSettled(thrown).then(() => {
|
|
69
|
+
again();
|
|
70
|
+
return settle();
|
|
55
71
|
});
|
|
56
72
|
return;
|
|
57
73
|
}
|
|
@@ -78,12 +94,7 @@ const whenClientReady = async (trees, { inSuspense }) => {
|
|
|
78
94
|
if (isPlain(value)) for (const prop of Object.values(value)) visit(prop, inside);
|
|
79
95
|
};
|
|
80
96
|
for (const tree of trees) visit(tree, false);
|
|
81
|
-
|
|
82
|
-
const round = waiting;
|
|
83
|
-
waiting = [];
|
|
84
|
-
await Promise.all(round.map(({ until }) => whenSettled(until)));
|
|
85
|
-
for (const { again } of round) again();
|
|
86
|
-
}
|
|
97
|
+
await settle();
|
|
87
98
|
};
|
|
88
99
|
//#endregion
|
|
89
100
|
export { whenClientReady };
|
|
@@ -13,7 +13,7 @@ const start = async () => {
|
|
|
13
13
|
const payload = await createFromReadableStream(rscStream);
|
|
14
14
|
setDocumentClient(payload.client);
|
|
15
15
|
await whenRevealed();
|
|
16
|
-
await whenClientReady([payload.tree, payload.notFound], {
|
|
16
|
+
await whenClientReady([payload.tree, payload.notFound], { rows: "wait" });
|
|
17
17
|
mount(document, /* @__PURE__ */ jsx(AppRouter, {
|
|
18
18
|
notFound: payload.notFound,
|
|
19
19
|
pathname: payload.pathname,
|
|
@@ -2,7 +2,7 @@ import { fileRequest, routeRequestOf } from "./request.mjs";
|
|
|
2
2
|
import { answer, inPhase, signingNonce, withRequest, writingFile } from "./request-scope.mjs";
|
|
3
3
|
import { methodNotAllowed, routeAnswerFor, runRoute } from "./route.mjs";
|
|
4
4
|
import { NOT_FOUND_SEGMENT } from "./pathname.mjs";
|
|
5
|
-
import { ACTION_ID_HEADER, NONCE_HEADER, NOT_FOUND_DIGEST, NOT_FOUND_HEADER, SHELL_HEADER } from "./payload.mjs";
|
|
5
|
+
import { ACTION_ID_HEADER, CLIENT_DEPS_HEADER, NONCE_HEADER, NOT_FOUND_DIGEST, NOT_FOUND_HEADER, SHELL_HEADER, clientDepsHeader } from "./payload.mjs";
|
|
6
6
|
import { isPayloadPath, pagePathFor } from "./payload-path.mjs";
|
|
7
7
|
import { isRedirect } from "./redirect.mjs";
|
|
8
8
|
import { noticeCancel } from "./cancel.mjs";
|
|
@@ -349,9 +349,12 @@ const respond = async (request) => {
|
|
|
349
349
|
}
|
|
350
350
|
});
|
|
351
351
|
if (import.meta.env.K8ORDO_MODE === "static") {
|
|
352
|
+
const [forHtml, forNamed] = rendered.stream.tee();
|
|
353
|
+
await drain(forNamed);
|
|
354
|
+
const named = [...rendered.named.values()];
|
|
352
355
|
let body;
|
|
353
356
|
try {
|
|
354
|
-
const html = await enter(() => ssr.renderHtml(
|
|
357
|
+
const html = await enter(() => ssr.renderHtml(forHtml, signingNonce(), showsPage, action.formState !== void 0, named));
|
|
355
358
|
body = await new Response(html).arrayBuffer();
|
|
356
359
|
} catch (error) {
|
|
357
360
|
return renderFailed(rendered.failures[0] ?? error);
|
|
@@ -363,12 +366,13 @@ const respond = async (request) => {
|
|
|
363
366
|
headers: {
|
|
364
367
|
"content-type": HTML_TYPE,
|
|
365
368
|
[NONCE_HEADER]: signingNonce(),
|
|
369
|
+
[CLIENT_DEPS_HEADER]: clientDepsHeader(rendered.named.values()),
|
|
366
370
|
...saidByPage,
|
|
367
371
|
...shellSaid
|
|
368
372
|
}
|
|
369
373
|
});
|
|
370
374
|
}
|
|
371
|
-
const html = await enter(() => ssr.renderHtml(rendered.stream, signingNonce(), showsPage));
|
|
375
|
+
const html = await enter(() => ssr.renderHtml(rendered.stream, signingNonce(), showsPage, action.formState !== void 0, []));
|
|
372
376
|
return new Response(html, {
|
|
373
377
|
status,
|
|
374
378
|
headers: { "content-type": HTML_TYPE }
|
|
@@ -397,6 +401,7 @@ const matchNotFound = (pathname, forShell) => {
|
|
|
397
401
|
};
|
|
398
402
|
const renderPayload = (payload, enter, temporaryReferences) => {
|
|
399
403
|
const failures = [];
|
|
404
|
+
const named = /* @__PURE__ */ new Map();
|
|
400
405
|
let said = false;
|
|
401
406
|
const { promise: saidNotFound, resolve } = Promise.withResolvers();
|
|
402
407
|
const controller = new AbortController();
|
|
@@ -413,11 +418,19 @@ const renderPayload = (payload, enter, temporaryReferences) => {
|
|
|
413
418
|
failures.push(error);
|
|
414
419
|
console.error("k8ordo: rendering %s failed", payload.pathname, error);
|
|
415
420
|
}
|
|
416
|
-
})
|
|
421
|
+
}, import.meta.env.K8ORDO_MODE === "static" ? { onClientReference: ({ id, name, deps }) => {
|
|
422
|
+
named.set(`${id}#${name}`, {
|
|
423
|
+
id,
|
|
424
|
+
name,
|
|
425
|
+
js: deps.js,
|
|
426
|
+
css: deps.css
|
|
427
|
+
});
|
|
428
|
+
} } : void 0));
|
|
417
429
|
return {
|
|
418
430
|
stream: noticeCancel(flight, (reason) => {
|
|
419
431
|
controller.abort(reason);
|
|
420
432
|
}),
|
|
433
|
+
named,
|
|
421
434
|
failures,
|
|
422
435
|
notFound: () => said,
|
|
423
436
|
saidNotFound,
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ClientDeps } from "./payload.mjs";
|
|
1
2
|
//#region src/runtime/entry.ssr.d.ts
|
|
2
3
|
/**
|
|
3
4
|
* The script every page's HTML loads, and so the client every payload is
|
|
@@ -12,5 +13,5 @@ export declare const clientEntry: string;
|
|
|
12
13
|
* one round trip. It is also what lets a prerendered `404.html` come alive —
|
|
13
14
|
* there is no payload file at a URL the application does not have.
|
|
14
15
|
*/
|
|
15
|
-
export declare function renderHtml(rscStream: ReadableStream<Uint8Array>, nonce: string, showsPage: boolean): Promise<ReadableStream>;
|
|
16
|
+
export declare function renderHtml(rscStream: ReadableStream<Uint8Array>, nonce: string, showsPage: boolean, carriesFormState: boolean, named: readonly ClientDeps[]): Promise<ReadableStream>;
|
|
16
17
|
//#endregion
|
|
@@ -1,10 +1,15 @@
|
|
|
1
|
+
import { SERVER_STYLESHEETS, STYLESHEET_ORDER_HREF } from "./payload.mjs";
|
|
2
|
+
import { whenClientReady } from "./client-ready.mjs";
|
|
1
3
|
import { AppRouter } from "./app-router.mjs";
|
|
2
4
|
import { noticeCancel } from "./cancel.mjs";
|
|
3
5
|
import { PageShownContext } from "./page-shown.mjs";
|
|
4
|
-
import {
|
|
6
|
+
import { use } from "react";
|
|
7
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
5
8
|
import { createFromReadableStream, getClientEntryUrl } from "@vitejs/plugin-rsc/ssr";
|
|
9
|
+
import { preinit, preloadModule } from "react-dom";
|
|
6
10
|
import { renderToReadableStream } from "react-dom/server.edge";
|
|
7
11
|
import { injectRSCPayload } from "rsc-html-stream/server";
|
|
12
|
+
import assetsManifest from "virtual:vite-rsc/assets-manifest";
|
|
8
13
|
//#region src/runtime/entry.ssr.tsx
|
|
9
14
|
/**
|
|
10
15
|
* The script every page's HTML loads, and so the client every payload is
|
|
@@ -12,6 +17,31 @@ import { injectRSCPayload } from "rsc-html-stream/server";
|
|
|
12
17
|
* the RSC plugin names the client entry to this environment only.
|
|
13
18
|
*/
|
|
14
19
|
const clientEntry = getClientEntryUrl();
|
|
20
|
+
const entryScripts = new Set(assetsManifest.clientEntryDeps?.js);
|
|
21
|
+
/**
|
|
22
|
+
* Asks React to link what each client component needs as the RSC plugin
|
|
23
|
+
* asks when the HTML render reads one — its scripts preloaded as modules,
|
|
24
|
+
* the client entry's own at the default priority and the rest low, its
|
|
25
|
+
* stylesheets under the plugin's precedence — so a file is written the same
|
|
26
|
+
* whichever of the two asked first: React writes it as first asked for.
|
|
27
|
+
*/
|
|
28
|
+
const linkClientDeps = (named) => {
|
|
29
|
+
for (const { js, css } of named) {
|
|
30
|
+
for (const href of js) {
|
|
31
|
+
const options = {
|
|
32
|
+
as: "script",
|
|
33
|
+
crossOrigin: "",
|
|
34
|
+
...entryScripts.has(href) ? {} : { fetchPriority: "low" }
|
|
35
|
+
};
|
|
36
|
+
preloadModule(href, options);
|
|
37
|
+
}
|
|
38
|
+
for (const href of css) preinit(href, {
|
|
39
|
+
as: "style",
|
|
40
|
+
crossOrigin: "anonymous",
|
|
41
|
+
precedence: "vite-rsc/client-reference"
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
};
|
|
15
45
|
/**
|
|
16
46
|
* The payload turned into HTML, with the payload itself written into that
|
|
17
47
|
* HTML. Hydration then reads what this render read, rather than asking the
|
|
@@ -19,23 +49,47 @@ const clientEntry = getClientEntryUrl();
|
|
|
19
49
|
* one round trip. It is also what lets a prerendered `404.html` come alive —
|
|
20
50
|
* there is no payload file at a URL the application does not have.
|
|
21
51
|
*/
|
|
22
|
-
async function renderHtml(rscStream, nonce, showsPage) {
|
|
23
|
-
const [
|
|
24
|
-
|
|
52
|
+
async function renderHtml(rscStream, nonce, showsPage, carriesFormState, named) {
|
|
53
|
+
const [forRender, forHydration] = rscStream.tee();
|
|
54
|
+
let forHtml = forRender;
|
|
55
|
+
let formState;
|
|
56
|
+
if (carriesFormState) {
|
|
57
|
+
const [forState, rest] = forRender.tee();
|
|
58
|
+
forHtml = rest;
|
|
59
|
+
({formState} = await createFromReadableStream(forState, { nonce }));
|
|
60
|
+
}
|
|
61
|
+
let payload;
|
|
62
|
+
let ready;
|
|
63
|
+
function Root() {
|
|
64
|
+
linkClientDeps(named);
|
|
65
|
+
payload ??= createFromReadableStream(forHtml, { nonce });
|
|
66
|
+
const { notFound, pathname, search, tree } = use(payload);
|
|
67
|
+
ready ??= whenClientReady([tree, notFound], { rows: import.meta.env.K8ORDO_MODE === "static" ? "wait" : "follow" });
|
|
68
|
+
use(ready);
|
|
69
|
+
return /* @__PURE__ */ jsx(AppRouter, {
|
|
70
|
+
notFound,
|
|
71
|
+
pathname,
|
|
72
|
+
search,
|
|
73
|
+
tree
|
|
74
|
+
});
|
|
75
|
+
}
|
|
25
76
|
const shown = Promise.withResolvers();
|
|
26
77
|
let readerLeft = false;
|
|
27
|
-
const
|
|
78
|
+
const styleNonce = import.meta.env.K8ORDO_MODE === "static" ? void 0 : nonce;
|
|
79
|
+
const htmlStream = await renderToReadableStream(/* @__PURE__ */ jsxs(PageShownContext, {
|
|
28
80
|
value: () => shown.resolve(void 0),
|
|
29
|
-
children: /* @__PURE__ */ jsx(
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
})
|
|
81
|
+
children: [/* @__PURE__ */ jsx("style", {
|
|
82
|
+
href: STYLESHEET_ORDER_HREF,
|
|
83
|
+
precedence: SERVER_STYLESHEETS,
|
|
84
|
+
nonce: styleNonce
|
|
85
|
+
}), /* @__PURE__ */ jsx(Root, {})]
|
|
35
86
|
}), {
|
|
36
87
|
bootstrapModules: [clientEntry],
|
|
37
|
-
nonce
|
|
38
|
-
|
|
88
|
+
nonce: styleNonce === void 0 ? nonce : {
|
|
89
|
+
script: nonce,
|
|
90
|
+
style: styleNonce
|
|
91
|
+
},
|
|
92
|
+
formState,
|
|
39
93
|
progressiveChunkSize: Number.POSITIVE_INFINITY,
|
|
40
94
|
onError: (error) => {
|
|
41
95
|
if (!readerLeft) console.error(error);
|
|
@@ -67,6 +67,18 @@ export declare const NOT_FOUND_HEADER = "x-k8ordo-not-found";
|
|
|
67
67
|
* everyone reads the same one.
|
|
68
68
|
*/
|
|
69
69
|
export declare const NONCE_HEADER = "x-k8ordo-nonce";
|
|
70
|
+
/**
|
|
71
|
+
* The `href` of the one `<style>` the HTML render writes itself, empty, under
|
|
72
|
+
* the precedence the RSC plugin links a Server Component's stylesheets with.
|
|
73
|
+
* React orders the precedences in `<head>` as each is first asked for, and
|
|
74
|
+
* puts a stylesheet loaded later after the last of its own; asked for first,
|
|
75
|
+
* this keeps the Server Components' stylesheets before the client
|
|
76
|
+
* components'. Under mode: 'static' it carries no nonce, and the build names
|
|
77
|
+
* it by hash where the policy decides a style element.
|
|
78
|
+
*/
|
|
79
|
+
export declare const STYLESHEET_ORDER_HREF = "k8ordo-stylesheet-order";
|
|
80
|
+
/** The precedence the RSC plugin links a Server Component's stylesheet with. */
|
|
81
|
+
export declare const SERVER_STYLESHEETS = "vite-rsc/importer-resources";
|
|
70
82
|
/**
|
|
71
83
|
* Said by the handler under mode: 'static' alone, on a shell's HTML and
|
|
72
84
|
* payload: the pattern whose fallback.tsx rendered it. The table walks in its
|
|
@@ -74,4 +86,29 @@ export declare const NONCE_HEADER = "x-k8ordo-nonce";
|
|
|
74
86
|
* one the build asked for, and the build compares instead of guessing.
|
|
75
87
|
*/
|
|
76
88
|
export declare const SHELL_HEADER = "x-k8ordo-shell";
|
|
89
|
+
/**
|
|
90
|
+
* A client component a payload names — the id the build's manifest keys its
|
|
91
|
+
* module with, and its export's name: two exports are two components — and
|
|
92
|
+
* the stylesheets and scripts the manifest says it needs.
|
|
93
|
+
*/
|
|
94
|
+
export type ClientDeps = {
|
|
95
|
+
readonly id: string;
|
|
96
|
+
readonly name: string;
|
|
97
|
+
readonly js: readonly string[];
|
|
98
|
+
readonly css: readonly string[];
|
|
99
|
+
};
|
|
100
|
+
/**
|
|
101
|
+
* Said by the handler under mode: 'static' alone, on a page's HTML: every
|
|
102
|
+
* client component the payload it rendered names (`clientDepsHeader`) — what
|
|
103
|
+
* the HTML render was handed to link — which the build checks the HTML
|
|
104
|
+
* links.
|
|
105
|
+
*/
|
|
106
|
+
export declare const CLIENT_DEPS_HEADER = "x-k8ordo-client-deps";
|
|
107
|
+
/**
|
|
108
|
+
* The value of `CLIENT_DEPS_HEADER`. Encoded, because a header's value is
|
|
109
|
+
* bytes, and a chunk is named after a module, whose name need not be ASCII.
|
|
110
|
+
*/
|
|
111
|
+
export declare const clientDepsHeader: (named: Iterable<ClientDeps>) => string;
|
|
112
|
+
/** What `clientDepsHeader` said. */
|
|
113
|
+
export declare const readClientDeps: (value: string) => ClientDeps[];
|
|
77
114
|
//#endregion
|
package/dist/runtime/payload.mjs
CHANGED
|
@@ -20,11 +20,37 @@ const NOT_FOUND_HEADER = "x-k8ordo-not-found";
|
|
|
20
20
|
*/
|
|
21
21
|
const NONCE_HEADER = "x-k8ordo-nonce";
|
|
22
22
|
/**
|
|
23
|
+
* The `href` of the one `<style>` the HTML render writes itself, empty, under
|
|
24
|
+
* the precedence the RSC plugin links a Server Component's stylesheets with.
|
|
25
|
+
* React orders the precedences in `<head>` as each is first asked for, and
|
|
26
|
+
* puts a stylesheet loaded later after the last of its own; asked for first,
|
|
27
|
+
* this keeps the Server Components' stylesheets before the client
|
|
28
|
+
* components'. Under mode: 'static' it carries no nonce, and the build names
|
|
29
|
+
* it by hash where the policy decides a style element.
|
|
30
|
+
*/
|
|
31
|
+
const STYLESHEET_ORDER_HREF = "k8ordo-stylesheet-order";
|
|
32
|
+
/** The precedence the RSC plugin links a Server Component's stylesheet with. */
|
|
33
|
+
const SERVER_STYLESHEETS = "vite-rsc/importer-resources";
|
|
34
|
+
/**
|
|
23
35
|
* Said by the handler under mode: 'static' alone, on a shell's HTML and
|
|
24
36
|
* payload: the pattern whose fallback.tsx rendered it. The table walks in its
|
|
25
37
|
* own order, so a shell pathname can be answered by another pattern than the
|
|
26
38
|
* one the build asked for, and the build compares instead of guessing.
|
|
27
39
|
*/
|
|
28
40
|
const SHELL_HEADER = "x-k8ordo-shell";
|
|
41
|
+
/**
|
|
42
|
+
* Said by the handler under mode: 'static' alone, on a page's HTML: every
|
|
43
|
+
* client component the payload it rendered names (`clientDepsHeader`) — what
|
|
44
|
+
* the HTML render was handed to link — which the build checks the HTML
|
|
45
|
+
* links.
|
|
46
|
+
*/
|
|
47
|
+
const CLIENT_DEPS_HEADER = "x-k8ordo-client-deps";
|
|
48
|
+
/**
|
|
49
|
+
* The value of `CLIENT_DEPS_HEADER`. Encoded, because a header's value is
|
|
50
|
+
* bytes, and a chunk is named after a module, whose name need not be ASCII.
|
|
51
|
+
*/
|
|
52
|
+
const clientDepsHeader = (named) => encodeURIComponent(JSON.stringify([...named]));
|
|
53
|
+
/** What `clientDepsHeader` said. */
|
|
54
|
+
const readClientDeps = (value) => JSON.parse(decodeURIComponent(value));
|
|
29
55
|
//#endregion
|
|
30
|
-
export { ACTION_ID_HEADER, NONCE_HEADER, NOT_FOUND_DIGEST, NOT_FOUND_HEADER, SHELL_HEADER };
|
|
56
|
+
export { ACTION_ID_HEADER, CLIENT_DEPS_HEADER, NONCE_HEADER, NOT_FOUND_DIGEST, NOT_FOUND_HEADER, SERVER_STYLESHEETS, SHELL_HEADER, STYLESHEET_ORDER_HREF, clientDepsHeader, readClientDeps };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { createContext } from "react";
|
|
3
|
+
//#region src/runtime/tree-pathname.ts
|
|
4
|
+
/**
|
|
5
|
+
* `@k8ordo/router`'s context for the pathname each tree was applied for,
|
|
6
|
+
* which `<Router>` provides and `@k8ordo/state` reads to keep a page state's
|
|
7
|
+
* values with the tree on screen. `AppRouter` provides it the same way.
|
|
8
|
+
*/
|
|
9
|
+
const KEY = Symbol.for("@k8ordo/router/tree-pathname");
|
|
10
|
+
const TreePathname = globalThis[KEY] ??= createContext(null);
|
|
11
|
+
//#endregion
|
|
12
|
+
export { TreePathname };
|