@k8ordo/framework 1.4.4 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/README.md +1 -1
  2. package/dist/generated.d.mts +23 -2
  3. package/dist/runtime/app-router.d.mts +10 -10
  4. package/dist/runtime/app-router.mjs +119 -83
  5. package/dist/runtime/browser-payloads.mjs +2 -1
  6. package/dist/runtime/client-files.d.mts +11 -0
  7. package/dist/runtime/client-files.mjs +42 -0
  8. package/dist/runtime/client-ready.d.mts +3 -2
  9. package/dist/runtime/document-root.mjs +1 -1
  10. package/dist/runtime/entry.browser.d.mts +1 -1
  11. package/dist/runtime/entry.browser.mjs +8 -12
  12. package/dist/runtime/entry.rsc.mjs +148 -102
  13. package/dist/runtime/entry.ssr.d.mts +21 -1
  14. package/dist/runtime/entry.ssr.mjs +10 -13
  15. package/dist/runtime/head-scripts.d.mts +18 -0
  16. package/dist/runtime/head-scripts.mjs +159 -0
  17. package/dist/runtime/hydrating-pathname.d.mts +16 -0
  18. package/dist/runtime/hydrating-pathname.mjs +26 -0
  19. package/dist/runtime/mount.d.mts +16 -11
  20. package/dist/runtime/mount.mjs +29 -18
  21. package/dist/runtime/payload.d.mts +22 -23
  22. package/dist/runtime/payload.mjs +11 -16
  23. package/dist/runtime/prefetch.d.mts +60 -25
  24. package/dist/runtime/prefetch.mjs +166 -38
  25. package/dist/runtime/recover.d.mts +2 -0
  26. package/dist/runtime/recover.mjs +11 -7
  27. package/dist/runtime/redirect.d.mts +20 -14
  28. package/dist/runtime/redirect.mjs +42 -15
  29. package/dist/runtime/request-scope.d.mts +1 -1
  30. package/dist/runtime/request-scope.mjs +11 -23
  31. package/dist/runtime/server.d.mts +6 -0
  32. package/dist/runtime/server.mjs +3 -0
  33. package/dist/runtime/stylesheets.d.mts +13 -0
  34. package/dist/runtime/stylesheets.mjs +15 -0
  35. package/dist/serve.d.mts +1 -0
  36. package/dist/serve.mjs +5 -11
  37. package/dist/static-file-BAjwBMMl.mjs +1274 -0
  38. package/dist/{static-host-C6D_11Gp.mjs → static-host-B-1fAfAw.mjs} +49 -74
  39. package/dist/vercel.d.mts +0 -1
  40. package/dist/vercel.mjs +20 -20
  41. package/dist/vite.mjs +228 -1518
  42. package/docs/GUIDE.md +3 -2
  43. package/docs/llms.txt +8 -4
  44. package/docs/references/csp.md +11 -9
  45. package/docs/references/deploy.md +17 -20
  46. package/docs/references/errors.md +0 -34
  47. package/docs/references/params.md +5 -5
  48. package/docs/references/routing.md +94 -31
  49. package/package.json +6 -6
  50. package/dist/runtime/redirect-file.d.mts +0 -20
  51. package/dist/runtime/redirect-file.mjs +0 -42
  52. package/dist/runtime/tree-pathname.d.mts +0 -2
  53. package/dist/runtime/tree-pathname.mjs +0 -11
  54. package/dist/server.d.mts +0 -110
  55. package/dist/server.mjs +0 -97
package/README.md CHANGED
@@ -35,7 +35,7 @@ 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.4.4 | yes | routing, re-exported by the framework |
38
+ | `@k8ordo/router` | ^2.0.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
41
  | `vite` | ≥8.1.0 | yes | the build (`framework()` is a Vite plugin) |
@@ -1,2 +1,23 @@
1
- import { ErrorComponent, ParamsOf, ParamsSchemaFor, ParsedParams, ParsedParamsMap, Register, defineRoutes } from "@k8ordo/router";
2
- export { type ErrorComponent, type ParamsOf, type ParamsSchemaFor, type ParsedParams, type ParsedParamsMap, type Register, defineRoutes };
1
+ import { ErrorComponent, ParamsOf, ParamsOf as ParamsOf$1, ParamsSchemaFor, ParsedParams, ParsedParamsMap, Register, defineRoutes } from "@k8ordo/router";
2
+ /**
3
+ * What a `guard.ts` receives: the request, and the params of the pattern its
4
+ * directory puts it under, as the strings the pathname carried — a guard runs
5
+ * above the page and every layout, where no schema has typed them.
6
+ */
7
+ type GuardContext<P extends string = string> = {
8
+ readonly request: Request;
9
+ readonly params: ParamsOf$1<P>;
10
+ };
11
+ /**
12
+ * What a `guard.ts` default-exports. A `Response` ends the request there — a
13
+ * redirect, a `401` — and returning nothing lets it through to what answers
14
+ * below. There is no `next()` to run the page and rewrite its answer: a page
15
+ * streams, and its headers leave before its body is written.
16
+ */
17
+ type Guard<P extends string = string> = (context: GuardContext<P>) => Response | void | Promise<Response | void>;
18
+ /** What a `redirect.ts` route file default-exports. */
19
+ type RedirectTarget = string | {
20
+ readonly to: string;
21
+ readonly permanent?: boolean;
22
+ };
23
+ export { type ErrorComponent, type Guard, type ParamsOf, type ParamsSchemaFor, type ParsedParams, type ParsedParamsMap, type RedirectTarget, type Register, defineRoutes };
@@ -1,8 +1,5 @@
1
1
  import { Payload } from "./payload.mjs";
2
2
  import { ReactNode } from "react";
3
- export declare const setDocumentClient: (client: string) => void;
4
- /** Taken before a Server Action is sent, and handed to `answerAction`. */
5
- export declare const navigationsApplied: () => number;
6
3
  /**
7
4
  * What a Server Action's answer does to the document, once it is read.
8
5
  * `sent` is `navigationsApplied()` as the action was sent, and `url` where
@@ -20,11 +17,14 @@ export declare const setPayloadReader: (read: (stream: ReadableStream<Uint8Array
20
17
  * A pathname the application does not have is still the server's answer: it
21
18
  * renders `not-found.tsx` and this renders whatever came back.
22
19
  */
23
- export declare function AppRouter({ pathname, search, tree, notFound }: {
24
- pathname: string;
25
- /** The search the page was rendered with, when it reads the search. */
26
- search?: string | undefined;
27
- tree: ReactNode;
28
- /** A shell's not-found, when the tree is a shell. */
29
- notFound?: ReactNode | undefined;
20
+ export declare function AppRouter({ arrived, reread }: {
21
+ /** The payload the document's HTML was rendered from. */
22
+ arrived: Payload;
23
+ /**
24
+ * The same payload read again, when the HTML was rendered for another
25
+ * pathname than the browser's: hydration keeps what the server wrote, and
26
+ * this copy — the same page, every element new — renders it again at the
27
+ * URL once all of it is hydrated.
28
+ */
29
+ reread?: Promise<Payload> | undefined;
30
30
  }): ReactNode;
@@ -1,13 +1,14 @@
1
1
  "use client";
2
2
  import { payloadPathFor } from "./payload-path.mjs";
3
+ import { preloadClientFiles, readClientFiles } from "./client-files.mjs";
3
4
  import { whenClientReady } from "./client-ready.mjs";
4
5
  import { ShowNotFound } from "./fallback-boundary.mjs";
6
+ import { doneHydrating, whenHydrated } from "./hydrating-pathname.mjs";
5
7
  import { isPayload } from "./is-payload.mjs";
6
- import { createPrefetchCache, prefetchTargetOf } from "./prefetch.mjs";
7
- import { Recover, markNavigated, reloadInstead } from "./recover.mjs";
8
- import { TreePathname } from "./tree-pathname.mjs";
9
- import { NavigationGeneration, PathnameProvider, normalizePathname, useInterceptedNavigation, withoutBase } from "@k8ordo/router";
10
- import { startTransition, useDeferredValue, useEffect, useRef, useState } from "react";
8
+ import { createPageCache, prefetchInViewUnderTouch, prefetchOnIntent } from "./prefetch.mjs";
9
+ import { Recover, markNavigated, navigationsApplied, reloadInstead } from "./recover.mjs";
10
+ import { NavigationGeneration, PathnameProvider, TreePathname, normalizePathname, useInterceptedNavigation, withoutBase } from "@k8ordo/router";
11
+ import { createContext, startTransition, useDeferredValue, useEffect, useLayoutEffect, useRef, useState } from "react";
11
12
  import { jsx } from "react/jsx-runtime";
12
13
  // src/runtime/app-router.tsx
13
14
  /**
@@ -19,23 +20,13 @@ import { jsx } from "react/jsx-runtime";
19
20
  */
20
21
  let mounted = null;
21
22
  /**
22
- * The client this document runs, as named by the payload its HTML was
23
- * rendered from — the same render that told the HTML which script to load.
24
- * A fact about the document, not about a component, which is why it is not
25
- * a prop.
23
+ * A payload rendered for another client than the document runs comes from a
24
+ * deploy the document predates, and may name client components its script
25
+ * has no entry for. That fails only once the tree renders, where the page's
26
+ * `error.tsx` catches it before `Recover` can — so it is decided before
27
+ * anything renders, and a document load brings the script that can.
26
28
  */
27
- let documentClient;
28
- const setDocumentClient = (client) => {
29
- documentClient = client;
30
- };
31
- /**
32
- * A payload rendered for another client comes from a deploy this document
33
- * predates, and may name client components its script has no entry for.
34
- * That fails only once the tree renders, where the page's `error.tsx` catches
35
- * it before `Recover` can — so it is decided before anything renders, and a
36
- * document load brings the script that can.
37
- */
38
- const canRender = (payload) => payload.client === documentClient;
29
+ const canRender = (payload, client) => payload.client === client;
39
30
  /**
40
31
  * How a payload is waited for before its tree is applied: until the client
41
32
  * components it renders have loaded — one still loading suspends where it
@@ -46,13 +37,11 @@ const canRender = (payload) => payload.client === documentClient;
46
37
  */
47
38
  const WHEN_APPLIED = { rows: "leave" };
48
39
  /**
49
- * How many trees navigations have applied — the router's, and `vite dev`'s
50
- * reload of the page on screen. A Server Action's answer is not counted:
51
- * answers to actions called in a row apply one after another.
40
+ * Whether a page's payload is a file — the same until the next deploy,
41
+ * however often it is asked for. Under `vite dev` a static application's
42
+ * pages are rendered per request too.
52
43
  */
53
- let navigations = 0;
54
- /** Taken before a Server Action is sent, and handed to `answerAction`. */
55
- const navigationsApplied = () => navigations;
44
+ const payloadsAreFiles = () => import.meta.env.K8ORDO_MODE === "static" && !import.meta.env.DEV;
56
45
  /**
57
46
  * What a Server Action's answer does to the document, once it is read.
58
47
  * `sent` is `navigationsApplied()` as the action was sent, and `url` where
@@ -64,9 +53,10 @@ const answerAction = async (payload, sent, url) => {
64
53
  await navigation.navigate(payload.redirect).finished;
65
54
  return;
66
55
  }
67
- if (!canRender(payload)) return reloadInstead();
56
+ if (!canRender(payload, mounted?.client)) return reloadInstead();
68
57
  await whenClientReady([payload.tree, payload.notFound], WHEN_APPLIED);
69
- if (navigations === sent) mounted?.apply(payload, normalizePathname(new URL(url).pathname));
58
+ await whenHydrated();
59
+ if (navigationsApplied() === sent) mounted?.apply(payload, normalizePathname(new URL(url).pathname));
70
60
  return payload.returnValue;
71
61
  };
72
62
  let readPayload = () => Promise.reject(/* @__PURE__ */ new Error("k8ordo: payloads are read in the browser only"));
@@ -88,16 +78,21 @@ const streams = /* @__PURE__ */ new WeakMap();
88
78
  * A page's payload, or `null` when what came back is not one — a file the
89
79
  * host serves, or a URL nothing answers. It resolves once the client
90
80
  * components the page renders have loaded, so a prefetch has those in hand
91
- * too.
81
+ * too; what a static build listed of their files is linked as the request
82
+ * leaves.
92
83
  */
93
- const fetchPage = async (payloadPath, signal) => {
84
+ const fetchPage = async (payloadPath, signal, priority = "auto") => {
94
85
  signal.throwIfAborted();
86
+ preloadClientFiles(payloadPath);
95
87
  const request = new AbortController();
96
88
  const cancel = () => {
97
89
  request.abort(signal.reason);
98
90
  };
99
91
  signal.addEventListener("abort", cancel, { once: true });
100
- const response = await fetch(payloadPath, { signal: request.signal });
92
+ const response = await fetch(payloadPath, {
93
+ signal: request.signal,
94
+ priority
95
+ });
101
96
  if (!isPayload(response)) return null;
102
97
  const payload = await readPayload(response.body);
103
98
  await whenClientReady([payload.tree, payload.notFound], WHEN_APPLIED);
@@ -114,15 +109,24 @@ const fetchPage = async (payloadPath, signal) => {
114
109
  /** How many payloads this document has applied. It has one router. */
115
110
  let applies = 0;
116
111
  /**
117
- * Takes over the request of a payload whose tree is being applied, and
118
- * returns what applying it puts in state.
112
+ * Read by no component: it changes so that React finishes hydrating. React
113
+ * hydrates a Suspense boundary at its own pace, when the browser is idle,
114
+ * and says nowhere when it is done; a context that changes above a boundary
115
+ * not yet hydrated makes React hydrate it before the transition that changed
116
+ * the context commits, so that commit is where all of the HTML is hydrated.
119
117
  */
120
- const hold = (streaming, payload, pathname) => {
118
+ const Hydrating = createContext(false);
119
+ /**
120
+ * Takes over the request of a payload whose tree is being applied, records
121
+ * the search it was rendered with, and returns what applying it puts in state.
122
+ */
123
+ const hold = (streaming, searchApplied, payload, pathname) => {
121
124
  applies += 1;
125
+ searchApplied.current = payload.search;
122
126
  const stream = streams.get(payload);
123
127
  if (stream !== void 0) {
124
128
  stream.keep();
125
- streaming.push({
129
+ if (!payloadsAreFiles()) streaming.current.push({
126
130
  trees: payload.notFound === void 0 ? [payload.tree] : [payload.tree, payload.notFound],
127
131
  cancel: stream.cancel,
128
132
  order: applies
@@ -136,23 +140,17 @@ const hold = (streaming, payload, pathname) => {
136
140
  };
137
141
  };
138
142
  /**
139
- * The router's prefetch cache, made the first time it is asked for. It is
140
- * only ever asked for in an event or an effect; made as `useRef`'s initial
141
- * value it would be built, and thrown away, on every render.
143
+ * The router's page cache, made the first time it is asked for. It is only
144
+ * ever asked for in an event or an effect; made as `useRef`'s initial value
145
+ * it would be built, and thrown away, on every render.
142
146
  */
143
- const cacheIn = (ref) => ref.current ??= createPrefetchCache((payloadPath, signal) => fetchPage(payloadPath, signal));
147
+ const cacheIn = (ref) => ref.current ??= createPageCache(fetchPage, { files: payloadsAreFiles() });
144
148
  /**
145
149
  * Where a page's payload is asked for: its path, with the URL's search. A page
146
150
  * that reads the search is rendered for it; for any other page the server
147
151
  * ignores it — and a static host, which has no such page, ignores it anyway.
148
152
  */
149
153
  const payloadUrlFor = (url) => `${payloadPathFor(url.pathname)}${url.search}`;
150
- /** What starts a prefetch: a pointer onto a link, focus on one, a press. */
151
- const INTENTS = [
152
- "pointerover",
153
- "focusin",
154
- "pointerdown"
155
- ];
156
154
  /**
157
155
  * The client half under the framework: the tree comes from the server, so
158
156
  * there is no route table in the browser at all — only navigation. The
@@ -163,18 +161,55 @@ const INTENTS = [
163
161
  * A pathname the application does not have is still the server's answer: it
164
162
  * renders `not-found.tsx` and this renders whatever came back.
165
163
  */
166
- function AppRouter({ pathname, search, tree, notFound }) {
164
+ function AppRouter({ arrived, reread }) {
167
165
  const [latest, setLatest] = useState(() => ({
168
- tree,
169
- notFound,
166
+ tree: arrived.tree,
167
+ notFound: arrived.notFound,
170
168
  order: 0,
171
169
  pathname: typeof location === "undefined" ? null : normalizePathname(location.pathname)
172
170
  }));
173
171
  const current = useDeferredValue(latest);
172
+ const [hydrating, setHydrating] = useState(reread !== void 0);
174
173
  const prefetched = useRef(null);
175
- const searchApplied = useRef(search);
174
+ const searchApplied = useRef(arrived.search);
176
175
  const streaming = useRef([]);
177
176
  const loading = useRef(0);
177
+ useEffect(() => {
178
+ if (!hydrating) return;
179
+ startTransition(() => {
180
+ setHydrating(false);
181
+ });
182
+ }, [hydrating]);
183
+ useLayoutEffect(() => {
184
+ if (hydrating || reread === void 0) return void 0;
185
+ doneHydrating();
186
+ const unmounted = new AbortController();
187
+ (async () => {
188
+ const again = await reread;
189
+ await whenClientReady([again.tree, again.notFound], { rows: "wait" });
190
+ if (unmounted.signal.aborted) return;
191
+ startTransition(() => {
192
+ setLatest((previous) => {
193
+ if (previous.order !== 0) return previous;
194
+ return previous.tree === arrived.tree ? {
195
+ ...previous,
196
+ tree: again.tree,
197
+ notFound: again.notFound
198
+ } : {
199
+ ...previous,
200
+ tree: again.notFound
201
+ };
202
+ });
203
+ });
204
+ })();
205
+ return () => {
206
+ unmounted.abort();
207
+ };
208
+ }, [
209
+ hydrating,
210
+ reread,
211
+ arrived.tree
212
+ ]);
178
213
  useEffect(() => {
179
214
  streaming.current = streaming.current.filter((entry) => {
180
215
  if (entry.order > latest.order) return true;
@@ -185,9 +220,9 @@ function AppRouter({ pathname, search, tree, notFound }) {
185
220
  }, [latest, current]);
186
221
  useEffect(() => {
187
222
  mounted = {
223
+ client: arrived.client,
188
224
  apply: (payload, postedTo) => {
189
- searchApplied.current = payload.search;
190
- const next = hold(streaming.current, payload, postedTo);
225
+ const next = hold(streaming, searchApplied, payload, postedTo);
191
226
  startTransition(() => {
192
227
  setLatest(next);
193
228
  });
@@ -199,7 +234,7 @@ function AppRouter({ pathname, search, tree, notFound }) {
199
234
  return () => {
200
235
  mounted = null;
201
236
  };
202
- }, []);
237
+ }, [arrived.client]);
203
238
  useEffect(() => {
204
239
  if (import.meta.hot === void 0) return void 0;
205
240
  let inFlight;
@@ -208,6 +243,7 @@ function AppRouter({ pathname, search, tree, notFound }) {
208
243
  const controller = new AbortController();
209
244
  inFlight = controller;
210
245
  cacheIn(prefetched).clear();
246
+ await whenHydrated();
211
247
  const url = location.href;
212
248
  let payload;
213
249
  try {
@@ -217,11 +253,9 @@ function AppRouter({ pathname, search, tree, notFound }) {
217
253
  throw error;
218
254
  }
219
255
  if (payload === null || location.href !== url) return;
220
- if (!canRender(payload)) return reloadInstead();
256
+ if (!canRender(payload, arrived.client)) return reloadInstead();
221
257
  markNavigated();
222
- navigations += 1;
223
- const next = hold(streaming.current, payload, normalizePathname(new URL(url).pathname));
224
- searchApplied.current = payload.search;
258
+ const next = hold(streaming, searchApplied, payload, normalizePathname(new URL(url).pathname));
225
259
  startTransition(() => {
226
260
  setLatest(next);
227
261
  });
@@ -234,18 +268,18 @@ function AppRouter({ pathname, search, tree, notFound }) {
234
268
  inFlight?.abort();
235
269
  import.meta.hot?.off("rsc:update", listener);
236
270
  };
237
- }, []);
271
+ }, [arrived.client]);
238
272
  useEffect(() => {
239
- const onIntent = (event) => {
240
- const url = prefetchTargetOf(event.target);
241
- if (url !== null) cacheIn(prefetched).prefetch(payloadUrlFor(url));
242
- };
243
- for (const type of INTENTS) document.addEventListener(type, onIntent, {
244
- capture: true,
245
- passive: true
246
- });
273
+ if (!payloadsAreFiles()) return;
274
+ cacheIn(prefetched).keep(payloadUrlFor(new URL(location.href)), arrived);
275
+ readClientFiles();
276
+ }, [arrived]);
277
+ useEffect(() => {
278
+ const prefetch = (priority) => (url) => cacheIn(prefetched).prefetch(payloadUrlFor(url), priority);
279
+ const stops = payloadsAreFiles() ? [prefetchInViewUnderTouch(prefetch("low"))] : [];
280
+ stops.push(prefetchOnIntent(prefetch("auto")));
247
281
  return () => {
248
- for (const type of INTENTS) document.removeEventListener(type, onIntent, { capture: true });
282
+ for (const stop of stops) stop();
249
283
  };
250
284
  }, []);
251
285
  const { generation } = useInterceptedNavigation({
@@ -256,14 +290,15 @@ function AppRouter({ pathname, search, tree, notFound }) {
256
290
  const payloadPath = payloadUrlFor(url);
257
291
  let payload;
258
292
  try {
259
- payload = await (cacheIn(prefetched).take(payloadPath, signal) ?? fetchPage(payloadPath, signal));
293
+ payload = await cacheIn(prefetched).load(payloadPath, signal);
260
294
  } catch (error) {
261
295
  if (signal.aborted) throw error;
262
296
  return await reloadInstead();
263
297
  }
264
298
  signal.throwIfAborted();
265
299
  if (payload === null) return await reloadInstead();
266
- if (!canRender(payload)) return await reloadInstead();
300
+ if (!canRender(payload, arrived.client)) return await reloadInstead();
301
+ await whenHydrated();
267
302
  return {
268
303
  payload,
269
304
  pathname: normalizePathname(url.pathname)
@@ -274,9 +309,7 @@ function AppRouter({ pathname, search, tree, notFound }) {
274
309
  },
275
310
  apply: (loaded) => {
276
311
  markNavigated();
277
- navigations += 1;
278
- searchApplied.current = loaded.payload.search;
279
- setLatest(hold(streaming.current, loaded.payload, loaded.pathname));
312
+ setLatest(hold(streaming, searchApplied, loaded.payload, loaded.pathname));
280
313
  },
281
314
  refresh: (url) => searchApplied.current !== void 0 && url.search !== searchApplied.current
282
315
  });
@@ -290,18 +323,21 @@ function AppRouter({ pathname, search, tree, notFound }) {
290
323
  } : previous);
291
324
  return true;
292
325
  };
293
- return /* @__PURE__ */ jsx(PathnameProvider, {
294
- pathname,
295
- children: /* @__PURE__ */ jsx(NavigationGeneration, {
296
- value: generation,
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
- }) })
326
+ return /* @__PURE__ */ jsx(Hydrating, {
327
+ value: hydrating,
328
+ children: /* @__PURE__ */ jsx(PathnameProvider, {
329
+ pathname: arrived.pathname,
330
+ children: /* @__PURE__ */ jsx(NavigationGeneration, {
331
+ value: generation,
332
+ children: /* @__PURE__ */ jsx(TreePathname, {
333
+ value: current.pathname,
334
+ children: /* @__PURE__ */ jsx(Recover, { children: /* @__PURE__ */ jsx(ShowNotFound, {
335
+ value: current.notFound === void 0 ? null : show,
336
+ children: current.tree
337
+ }) })
338
+ })
303
339
  })
304
340
  })
305
341
  });
306
342
  }
307
- export { AppRouter, answerAction, navigationsApplied, setDocumentClient, setPayloadReader };
343
+ export { AppRouter, answerAction, setPayloadReader };
@@ -1,5 +1,6 @@
1
1
  import { ACTION_ID_HEADER } from "./payload.mjs";
2
- import { answerAction, navigationsApplied, setPayloadReader } from "./app-router.mjs";
2
+ import { navigationsApplied } from "./recover.mjs";
3
+ import { answerAction, setPayloadReader } from "./app-router.mjs";
3
4
  import { createFromFetch, createFromReadableStream, createTemporaryReferenceSet, encodeReply, setServerCallback } from "@vitejs/plugin-rsc/browser";
4
5
  // src/runtime/browser-payloads.ts
5
6
  /**
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Reads the static build's list of the files each page's client components
3
+ * need. Without one — `vite dev`, a host that lost it — a payload names its
4
+ * files as it is read, as it always does.
5
+ */
6
+ export declare const readClientFiles: () => Promise<void>;
7
+ /**
8
+ * Links what the page whose payload is at `payloadPath` needs: its scripts
9
+ * as module preloads, its stylesheets as preloads.
10
+ */
11
+ export declare const preloadClientFiles: (payloadPath: string) => void;
@@ -0,0 +1,42 @@
1
+ import { CLIENT_FILES_PATH } from "./payload.mjs";
2
+ import { withBase } from "@k8ordo/router";
3
+ // src/runtime/client-files.ts
4
+ /** Each page's files, by its payload's path; empty until the list is read. */
5
+ let listed = /* @__PURE__ */ new Map();
6
+ /**
7
+ * Reads the static build's list of the files each page's client components
8
+ * need. Without one — `vite dev`, a host that lost it — a payload names its
9
+ * files as it is read, as it always does.
10
+ */
11
+ const readClientFiles = async () => {
12
+ let read;
13
+ try {
14
+ const response = await fetch(withBase(CLIENT_FILES_PATH), { priority: "low" });
15
+ if (!response.ok) return;
16
+ read = await response.json();
17
+ } catch {
18
+ return;
19
+ }
20
+ listed = new Map(Object.entries(read.pages).map(([page, indexes]) => [page, indexes.flatMap((index) => read.files[index] ?? [])]));
21
+ };
22
+ /**
23
+ * Links what the page whose payload is at `payloadPath` needs: its scripts
24
+ * as module preloads, its stylesheets as preloads.
25
+ */
26
+ const preloadClientFiles = (payloadPath) => {
27
+ const files = listed.get(new URL(payloadPath, location.href).pathname);
28
+ if (files === void 0) return;
29
+ const present = new Set(Array.from(document.querySelectorAll("link[href]"), (link) => link.href));
30
+ for (const href of files) {
31
+ if (present.has(new URL(href, location.href).href)) continue;
32
+ const link = document.createElement("link");
33
+ if (new URL(href, location.href).pathname.endsWith(".css")) {
34
+ link.rel = "preload";
35
+ link.as = "style";
36
+ } else link.rel = "modulepreload";
37
+ link.crossOrigin = "";
38
+ link.href = href;
39
+ document.head.append(link);
40
+ }
41
+ };
42
+ export { preloadClientFiles, readClientFiles };
@@ -1,4 +1,4 @@
1
- export type ClientReadyOptions = {
1
+ type ClientReadyOptions = {
2
2
  /**
3
3
  * What to do with a row still on its way: `'wait'` for it; `'leave'` one
4
4
  * inside a `<Suspense>` to that boundary's fallback, as a transition
@@ -26,4 +26,5 @@ export type ClientReadyOptions = {
26
26
  * render, a module that would not load — ends the wait there, for the
27
27
  * render to meet where an error boundary takes it.
28
28
  */
29
- export declare const whenClientReady: (trees: readonly unknown[], { rows }: ClientReadyOptions) => Promise<void>;
29
+ export declare const whenClientReady: (trees: readonly unknown[], { rows }: ClientReadyOptions) => Promise<void>;
30
+ export {};
@@ -1,4 +1,4 @@
1
- import { SERVER_STYLESHEETS, STYLESHEET_ORDER_HREF } from "./payload.mjs";
1
+ import { SERVER_STYLESHEETS, STYLESHEET_ORDER_HREF } from "./stylesheets.mjs";
2
2
  import { Fragment, jsx, jsxs } from "react/jsx-runtime";
3
3
  // src/runtime/document-root.tsx
4
4
  /**
@@ -1 +1 @@
1
- export {}
1
+ import "virtual:k8ordo/locales";
@@ -1,26 +1,22 @@
1
1
  import { whenClientReady } from "./client-ready.mjs";
2
- import { AppRouter, setDocumentClient } from "./app-router.mjs";
3
2
  import { connectAppRouter } from "./browser-payloads.mjs";
4
- import { DocumentRoot } from "./document-root.mjs";
5
- import { mount } from "./mount.mjs";
3
+ import { mount, renderedHere } from "./mount.mjs";
6
4
  import { reportCaught } from "./page-boundary.mjs";
7
5
  import { whenRevealed } from "./revealed.mjs";
8
- import { jsx } from "react/jsx-runtime";
9
6
  import { createFromReadableStream } from "@vitejs/plugin-rsc/browser";
10
7
  import { rscStream } from "rsc-html-stream/client";
8
+ import "virtual:k8ordo/locales";
11
9
  // src/runtime/entry.browser.tsx
12
10
  connectAppRouter();
13
11
  const start = async () => {
14
- const payload = await createFromReadableStream(rscStream);
15
- setDocumentClient(payload.client);
12
+ const [stream, copy] = rscStream.tee();
13
+ const payload = await createFromReadableStream(stream);
14
+ let reread;
15
+ if (renderedHere(payload.pathname)) copy.cancel();
16
+ else reread = createFromReadableStream(copy);
16
17
  await whenRevealed();
17
18
  await whenClientReady([payload.tree, payload.notFound], { rows: "wait" });
18
- mount(document, /* @__PURE__ */ jsx(DocumentRoot, { children: /* @__PURE__ */ jsx(AppRouter, {
19
- notFound: payload.notFound,
20
- pathname: payload.pathname,
21
- search: payload.search,
22
- tree: payload.tree
23
- }) }), payload.pathname, {
19
+ mount(document, payload, reread, {
24
20
  formState: payload.formState,
25
21
  onCaughtError: reportCaught
26
22
  });