@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
@@ -1,15 +1,20 @@
1
- import { ReactNode } from "react";
1
+ import { Payload } from "./payload.mjs";
2
2
  import { Root, hydrateRoot } from "react-dom/client";
3
3
  export type HydrateOptions = NonNullable<Parameters<typeof hydrateRoot>[2]>;
4
4
  /**
5
- * Brings the server's HTML to life where the browser is. Hydration holds the
6
- * first render to the HTML it finds, and that HTML was rendered for one
7
- * pathname: a component reading the URL as it renders (`@k8ordo/i18n`'s
8
- * messages read the locale segment) agrees with it only at that pathname. A
9
- * prerendered 404 is rendered once, for a pathname no visitor is at, and so
10
- * is a fallback.tsx's shell (`/ja/posts/!fallback`, served at `/ja/posts/3`);
11
- * both are rendered afresh instead — what the server wrote stays on screen
12
- * until the client's replaces it, rather than failing hydration and being
13
- * regenerated anyway, with the server's `<title>` left behind.
5
+ * Whether HTML rendered for `pathname` was rendered where the browser is:
6
+ * `location` below Vite's `base`, normalized as `usePathname` normalizes.
14
7
  */
15
- export declare const mount: (container: Document | Element, app: ReactNode, renderedFor: string, options?: HydrateOptions) => Root;
8
+ export declare const renderedHere: (pathname: string) => boolean;
9
+ /**
10
+ * Brings the server's HTML to life, keeping every node it wrote. Hydration
11
+ * holds the first render to the HTML it finds, and that HTML was rendered
12
+ * for one pathname: a component reading the URL as it renders
13
+ * (`@k8ordo/i18n`'s messages read the locale segment) agrees with it only at
14
+ * that pathname. A prerendered 404 is rendered once, for a pathname no
15
+ * visitor is at, and so is a fallback.tsx's shell (`/ja/posts/!fallback`,
16
+ * served at `/ja/posts/3`). Such HTML comes with `reread`, its payload read
17
+ * once more: it is hydrated as if the browser were at the payload's pathname,
18
+ * and `AppRouter` renders the copy where the browser is once it is.
19
+ */
20
+ export declare const mount: (container: Document | Element, payload: Payload, reread: Promise<Payload> | undefined, options?: HydrateOptions) => Root;
@@ -1,23 +1,34 @@
1
+ import { hydrateAs } from "./hydrating-pathname.mjs";
2
+ import { AppRouter } from "./app-router.mjs";
3
+ import { DocumentRoot } from "./document-root.mjs";
1
4
  import { normalizePathname, withoutBase } from "@k8ordo/router";
2
- import { createRoot, hydrateRoot } from "react-dom/client";
3
- // src/runtime/mount.ts
5
+ import { jsx } from "react/jsx-runtime";
6
+ import { hydrateRoot } from "react-dom/client";
7
+ // src/runtime/mount.tsx
4
8
  /**
5
- * Brings the server's HTML to life where the browser is. Hydration holds the
6
- * first render to the HTML it finds, and that HTML was rendered for one
7
- * pathname: a component reading the URL as it renders (`@k8ordo/i18n`'s
8
- * messages read the locale segment) agrees with it only at that pathname. A
9
- * prerendered 404 is rendered once, for a pathname no visitor is at, and so
10
- * is a fallback.tsx's shell (`/ja/posts/!fallback`, served at `/ja/posts/3`);
11
- * both are rendered afresh instead — what the server wrote stays on screen
12
- * until the client's replaces it, rather than failing hydration and being
13
- * regenerated anyway, with the server's `<title>` left behind.
9
+ * Whether HTML rendered for `pathname` was rendered where the browser is:
10
+ * `location` below Vite's `base`, normalized as `usePathname` normalizes.
14
11
  */
15
- const mount = (container, app, renderedFor, options) => {
12
+ const renderedHere = (pathname) => {
16
13
  const here = withoutBase(location.pathname);
17
- if (here !== null && normalizePathname(renderedFor) === normalizePathname(here)) return hydrateRoot(container, app, options);
18
- const { formState: _, ...rootOptions } = options ?? {};
19
- const root = createRoot(container, rootOptions);
20
- root.render(app);
21
- return root;
14
+ return here !== null && normalizePathname(pathname) === normalizePathname(here);
22
15
  };
23
- export { mount };
16
+ /**
17
+ * Brings the server's HTML to life, keeping every node it wrote. Hydration
18
+ * holds the first render to the HTML it finds, and that HTML was rendered
19
+ * for one pathname: a component reading the URL as it renders
20
+ * (`@k8ordo/i18n`'s messages read the locale segment) agrees with it only at
21
+ * that pathname. A prerendered 404 is rendered once, for a pathname no
22
+ * visitor is at, and so is a fallback.tsx's shell (`/ja/posts/!fallback`,
23
+ * served at `/ja/posts/3`). Such HTML comes with `reread`, its payload read
24
+ * once more: it is hydrated as if the browser were at the payload's pathname,
25
+ * and `AppRouter` renders the copy where the browser is once it is.
26
+ */
27
+ const mount = (container, payload, reread, options) => {
28
+ if (reread !== void 0) hydrateAs(payload.pathname);
29
+ return hydrateRoot(container, /* @__PURE__ */ jsx(DocumentRoot, { children: /* @__PURE__ */ jsx(AppRouter, {
30
+ arrived: payload,
31
+ reread
32
+ }) }), options);
33
+ };
34
+ export { mount, renderedHere };
@@ -66,18 +66,6 @@ export declare const NOT_FOUND_HEADER = "x-k8ordo-not-found";
66
66
  * everyone reads the same one.
67
67
  */
68
68
  export declare const NONCE_HEADER = "x-k8ordo-nonce";
69
- /**
70
- * The `href` of the one `<style>` the HTML render writes itself, empty, under
71
- * the precedence the RSC plugin links a Server Component's stylesheets with.
72
- * React orders the precedences in `<head>` as each is first asked for, and
73
- * puts a stylesheet loaded later after the last of its own; asked for first,
74
- * this keeps the Server Components' stylesheets before the client
75
- * components'. Under mode: 'static' it carries no nonce, and the build names
76
- * it by hash where the policy decides a style element.
77
- */
78
- export declare const STYLESHEET_ORDER_HREF = "k8ordo-stylesheet-order";
79
- /** The precedence the RSC plugin links a Server Component's stylesheet with. */
80
- export declare const SERVER_STYLESHEETS = "vite-rsc/importer-resources";
81
69
  /**
82
70
  * Said by the handler under mode: 'static' alone, on a shell's HTML and
83
71
  * payload: the pattern whose fallback.tsx rendered it. The table walks in its
@@ -85,22 +73,16 @@ export declare const SERVER_STYLESHEETS = "vite-rsc/importer-resources";
85
73
  * one the build asked for, and the build compares instead of guessing.
86
74
  */
87
75
  export declare const SHELL_HEADER = "x-k8ordo-shell";
88
- /**
89
- * A client component a payload names — the id the build's manifest keys its
90
- * module with, and its export's name: two exports are two components — and
91
- * the stylesheets and scripts the manifest says it needs.
92
- */
76
+ /** What a client module a payload names needs, as the build's manifest lists it. */
93
77
  export type ClientDeps = {
94
- readonly id: string;
95
- readonly name: string;
96
78
  readonly js: readonly string[];
97
79
  readonly css: readonly string[];
98
80
  };
99
81
  /**
100
82
  * Said by the handler under mode: 'static' alone, on a page's HTML: every
101
- * client component the payload it rendered names (`clientDepsHeader`) — what
102
- * the HTML render was handed to link — which the build checks the HTML
103
- * links.
83
+ * client module the payload it rendered names (`clientDepsHeader`) — what
84
+ * the HTML render was handed to link — which the build lists in
85
+ * `CLIENT_FILES_PATH`.
104
86
  */
105
87
  export declare const CLIENT_DEPS_HEADER = "x-k8ordo-client-deps";
106
88
  /**
@@ -109,4 +91,21 @@ export declare const CLIENT_DEPS_HEADER = "x-k8ordo-client-deps";
109
91
  */
110
92
  export declare const clientDepsHeader: (named: Iterable<ClientDeps>) => string;
111
93
  /** What `clientDepsHeader` said. */
112
- export declare const readClientDeps: (value: string) => ClientDeps[];
94
+ export declare const readClientDeps: (value: string) => ClientDeps[];
95
+ /**
96
+ * Where a static build lists the files each page's client components need,
97
+ * at the root of the site under Vite's `base`. A navigation links them as it
98
+ * asks for the payload, instead of once the payload has named them — a
99
+ * round trip sooner.
100
+ */
101
+ export declare const CLIENT_FILES_PATH = "/client-files.json";
102
+ /**
103
+ * What `CLIENT_FILES_PATH` holds: every file once, and for each page — by
104
+ * its payload's path, under the base, as a browser's URL spells it — the
105
+ * indexes of its files. A file every document links is left out: the
106
+ * document the visitor arrived on loaded it already.
107
+ */
108
+ export type ClientFiles = {
109
+ readonly files: readonly string[];
110
+ readonly pages: Readonly<Record<string, readonly number[]>>;
111
+ };
@@ -20,18 +20,6 @@ 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
- /**
35
23
  * Said by the handler under mode: 'static' alone, on a shell's HTML and
36
24
  * payload: the pattern whose fallback.tsx rendered it. The table walks in its
37
25
  * own order, so a shell pathname can be answered by another pattern than the
@@ -40,9 +28,9 @@ const SERVER_STYLESHEETS = "vite-rsc/importer-resources";
40
28
  const SHELL_HEADER = "x-k8ordo-shell";
41
29
  /**
42
30
  * 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.
31
+ * client module the payload it rendered names (`clientDepsHeader`) — what
32
+ * the HTML render was handed to link — which the build lists in
33
+ * `CLIENT_FILES_PATH`.
46
34
  */
47
35
  const CLIENT_DEPS_HEADER = "x-k8ordo-client-deps";
48
36
  /**
@@ -52,4 +40,11 @@ const CLIENT_DEPS_HEADER = "x-k8ordo-client-deps";
52
40
  const clientDepsHeader = (named) => encodeURIComponent(JSON.stringify([...named]));
53
41
  /** What `clientDepsHeader` said. */
54
42
  const readClientDeps = (value) => JSON.parse(decodeURIComponent(value));
55
- export { ACTION_ID_HEADER, CLIENT_DEPS_HEADER, NONCE_HEADER, NOT_FOUND_DIGEST, NOT_FOUND_HEADER, SERVER_STYLESHEETS, SHELL_HEADER, STYLESHEET_ORDER_HREF, clientDepsHeader, readClientDeps };
43
+ /**
44
+ * Where a static build lists the files each page's client components need,
45
+ * at the root of the site under Vite's `base`. A navigation links them as it
46
+ * asks for the payload, instead of once the payload has named them — a
47
+ * round trip sooner.
48
+ */
49
+ const CLIENT_FILES_PATH = "/client-files.json";
50
+ export { ACTION_ID_HEADER, CLIENT_DEPS_HEADER, CLIENT_FILES_PATH, NONCE_HEADER, NOT_FOUND_DIGEST, NOT_FOUND_HEADER, SHELL_HEADER, clientDepsHeader, readClientDeps };
@@ -1,17 +1,17 @@
1
1
  /**
2
- * How long a prefetched page stays usable, from the moment it was asked for.
3
- * Long enough to cover a pointer resting on a link and then clicking it; short
4
- * enough that a page hovered and left alone is not shown, much later, as it was
5
- * then — under mode: 'server' a page is rendered per request, and its data
6
- * may have moved on.
2
+ * How long a page rendered per request stays usable once prefetched, from the
3
+ * moment it was asked for. Long enough to cover a pointer resting on a link
4
+ * and then clicking it; short enough that a page hovered and left alone is not
5
+ * shown, much later, as it was then — under mode: 'server' a page is rendered
6
+ * per request, and its data may have moved on.
7
7
  */
8
8
  export declare const PREFETCH_LIFETIME = 30000;
9
9
  /**
10
- * The attribute that stops prefetching, on the link or on any element around
11
- * it: `data-k8ordo-prefetch="false"`. The nearest element carrying it decides,
12
- * so a link can opt back in inside a region that opted out.
10
+ * How many pages a cache of files holds — the pages prefetched and the pages
11
+ * visited, the least recently used given up first. A bound on memory alone: a
12
+ * file is the same page however long it is held.
13
13
  */
14
- export declare const PREFETCH_ATTRIBUTE = "data-k8ordo-prefetch";
14
+ export declare const KEPT_PAGES = 50;
15
15
  /**
16
16
  * The URL a pointer, a focus or a press is about to follow, when following it
17
17
  * would fetch a page's payload — `null` otherwise. What is left out is what a
@@ -20,23 +20,58 @@ export declare const PREFETCH_ATTRIBUTE = "data-k8ordo-prefetch";
20
20
  * page already on screen, where only the search or the fragment would change.
21
21
  */
22
22
  export declare const prefetchTargetOf: (target: EventTarget | null) => URL | null;
23
- export type PrefetchCache<T> = {
24
- /** Starts loading `key` unless a usable load of it is already held. */
25
- readonly prefetch: (key: string) => void;
23
+ /**
24
+ * Prefetches the link a pointer, a focus or a press is about to follow, and
25
+ * returns what stops it.
26
+ */
27
+ export declare const prefetchOnIntent: (prefetch: (url: URL) => Promise<unknown>) => (() => void);
28
+ /**
29
+ * Prefetches the links on screen (`prefetchInView`) while the primary pointer
30
+ * is coarse, and returns what stops it.
31
+ */
32
+ export declare const prefetchInViewUnderTouch: (prefetch: (url: URL) => Promise<unknown>) => (() => void);
33
+ /**
34
+ * Prefetches the links on screen, one at a time, in the order they came into
35
+ * view, and returns what stops it. A link that leaves before its turn is
36
+ * passed over, so a fast scroll fetches little; one at a time, so a page
37
+ * visited while they load shares the connection with one prefetch at most.
38
+ */
39
+ export declare const prefetchInView: (prefetch: (url: URL) => Promise<unknown>) => (() => void);
40
+ /**
41
+ * Where a page's load is handed to navigations from. Two kinds, by what a
42
+ * page's payload is:
43
+ *
44
+ * - **renders** (`files: false`): a page rendered per request. A load is
45
+ * started ahead of a navigation and handed to at most one — the next
46
+ * navigation to the same page within `PREFETCH_LIFETIME` of the load
47
+ * starting. Once taken it is gone: coming back to a page later fetches it
48
+ * afresh, as it did before anything was prefetched.
49
+ * - **files** (`files: true`): a page written into a file, the same until
50
+ * the next deploy. Every load is held — a prefetch's, a navigation's own,
51
+ * the document's page — and handed to every navigation to that page, going
52
+ * back and forward included, up to `KEPT_PAGES`.
53
+ *
54
+ * A load forgotten while nobody waits for it is aborted, so a request nobody
55
+ * will read does not run on; a navigation that took one and was overtaken
56
+ * aborts it, unless another navigation still waits for it.
57
+ */
58
+ export type PageCache<T> = {
59
+ /**
60
+ * Starts loading `key` unless a usable load of it is held, and returns the
61
+ * held load.
62
+ */
63
+ readonly prefetch: (key: string, priority: RequestPriority) => Promise<T>;
26
64
  /**
27
- * Hands over a usable load of `key` and forgets it; `undefined` if none.
28
- * From then on the load is the taker's: `signal` aborting aborts it, as it
29
- * would a load the taker had started itself.
65
+ * What a navigation to `key` renders: a usable load held, or one started
66
+ * for it. While it is on its way, `signal` aborting aborts it, as it would
67
+ * a load the navigation had started itself.
30
68
  */
31
- readonly take: (key: string, signal: AbortSignal) => Promise<T> | undefined;
32
- /** Forgets everything held, aborting what is still loading. */
69
+ readonly load: (key: string, signal: AbortSignal) => Promise<T>;
70
+ /** Holds a page already in hand: the document's own, among files. */
71
+ readonly keep: (key: string, value: T) => void;
72
+ /** Forgets everything held, aborting what nobody waits for yet. */
33
73
  readonly clear: () => void;
34
74
  };
35
- /**
36
- * Loads started ahead of a navigation, each handed to at most one — the next
37
- * navigation to the same page within `PREFETCH_LIFETIME` of the load starting.
38
- * Once taken it is gone: coming back to a page later fetches it afresh, as it
39
- * did before anything was prefetched. A load forgotten before anyone took it
40
- * is aborted, so a request nobody will read does not run on.
41
- */
42
- export declare const createPrefetchCache: <T>(load: (key: string, signal: AbortSignal) => Promise<T>, now?: () => number) => PrefetchCache<T>;
75
+ export declare const createPageCache: <T>(load: (key: string, signal: AbortSignal, priority: RequestPriority) => Promise<T>, { files }: {
76
+ readonly files: boolean;
77
+ }, now?: () => number) => PageCache<T>;
@@ -1,19 +1,31 @@
1
1
  import { normalizePathname, withoutBase } from "@k8ordo/router";
2
2
  // src/runtime/prefetch.ts
3
3
  /**
4
- * How long a prefetched page stays usable, from the moment it was asked for.
5
- * Long enough to cover a pointer resting on a link and then clicking it; short
6
- * enough that a page hovered and left alone is not shown, much later, as it was
7
- * then — under mode: 'server' a page is rendered per request, and its data
8
- * may have moved on.
4
+ * How long a page rendered per request stays usable once prefetched, from the
5
+ * moment it was asked for. Long enough to cover a pointer resting on a link
6
+ * and then clicking it; short enough that a page hovered and left alone is not
7
+ * shown, much later, as it was then — under mode: 'server' a page is rendered
8
+ * per request, and its data may have moved on.
9
9
  */
10
10
  const PREFETCH_LIFETIME = 3e4;
11
11
  /**
12
+ * How many pages a cache of files holds — the pages prefetched and the pages
13
+ * visited, the least recently used given up first. A bound on memory alone: a
14
+ * file is the same page however long it is held.
15
+ */
16
+ const KEPT_PAGES = 50;
17
+ /**
12
18
  * The attribute that stops prefetching, on the link or on any element around
13
19
  * it: `data-k8ordo-prefetch="false"`. The nearest element carrying it decides,
14
20
  * so a link can opt back in inside a region that opted out.
15
21
  */
16
22
  const PREFETCH_ATTRIBUTE = "data-k8ordo-prefetch";
23
+ /** What starts a prefetch: a pointer onto a link, focus on one, a press. */
24
+ const INTENTS = [
25
+ "pointerover",
26
+ "focusin",
27
+ "pointerdown"
28
+ ];
17
29
  /**
18
30
  * The URL a pointer, a focus or a press is about to follow, when following it
19
31
  * would fetch a page's payload — `null` otherwise. What is left out is what a
@@ -27,7 +39,7 @@ const prefetchTargetOf = (target) => {
27
39
  if (!(link instanceof HTMLAnchorElement)) return null;
28
40
  if (link.hasAttribute("download")) return null;
29
41
  if (link.target !== "" && link.target !== "_self") return null;
30
- if (link.closest(`[data-k8ordo-prefetch]`)?.getAttribute("data-k8ordo-prefetch") === "false") return null;
42
+ if (link.closest(`[${PREFETCH_ATTRIBUTE}]`)?.getAttribute(PREFETCH_ATTRIBUTE) === "false") return null;
31
43
  const url = new URL(link.href);
32
44
  if (url.origin !== location.origin) return null;
33
45
  if (withoutBase(url.pathname) === null) return null;
@@ -35,53 +47,169 @@ const prefetchTargetOf = (target) => {
35
47
  return url;
36
48
  };
37
49
  /**
38
- * Loads started ahead of a navigation, each handed to at most one — the next
39
- * navigation to the same page within `PREFETCH_LIFETIME` of the load starting.
40
- * Once taken it is gone: coming back to a page later fetches it afresh, as it
41
- * did before anything was prefetched. A load forgotten before anyone took it
42
- * is aborted, so a request nobody will read does not run on.
50
+ * Prefetches the link a pointer, a focus or a press is about to follow, and
51
+ * returns what stops it.
52
+ */
53
+ const prefetchOnIntent = (prefetch) => {
54
+ const onIntent = (event) => {
55
+ const url = prefetchTargetOf(event.target);
56
+ if (url !== null) prefetch(url);
57
+ };
58
+ for (const type of INTENTS) document.addEventListener(type, onIntent, {
59
+ capture: true,
60
+ passive: true
61
+ });
62
+ return () => {
63
+ for (const type of INTENTS) document.removeEventListener(type, onIntent, { capture: true });
64
+ };
65
+ };
66
+ /**
67
+ * Prefetches the links on screen (`prefetchInView`) while the primary pointer
68
+ * is coarse, and returns what stops it.
43
69
  */
44
- const createPrefetchCache = (load, now = () => performance.now()) => {
70
+ const prefetchInViewUnderTouch = (prefetch) => {
71
+ const coarse = matchMedia("(pointer: coarse)");
72
+ let stop;
73
+ const follow = () => {
74
+ stop?.();
75
+ stop = coarse.matches ? prefetchInView(prefetch) : void 0;
76
+ };
77
+ follow();
78
+ coarse.addEventListener("change", follow);
79
+ return () => {
80
+ coarse.removeEventListener("change", follow);
81
+ stop?.();
82
+ };
83
+ };
84
+ /**
85
+ * Prefetches the links on screen, one at a time, in the order they came into
86
+ * view, and returns what stops it. A link that leaves before its turn is
87
+ * passed over, so a fast scroll fetches little; one at a time, so a page
88
+ * visited while they load shares the connection with one prefetch at most.
89
+ */
90
+ const prefetchInView = (prefetch) => {
91
+ const waiting = /* @__PURE__ */ new Set();
92
+ let draining = false;
93
+ let stopped = false;
94
+ const drain = async () => {
95
+ if (draining) return;
96
+ draining = true;
97
+ for (const link of waiting) {
98
+ waiting.delete(link);
99
+ const url = prefetchTargetOf(link);
100
+ if (url !== null) await prefetch(url).catch(() => void 0);
101
+ if (stopped) break;
102
+ }
103
+ draining = false;
104
+ };
105
+ const view = new IntersectionObserver((entries) => {
106
+ for (const { isIntersecting, target } of entries) if (isIntersecting) waiting.add(target);
107
+ else waiting.delete(target);
108
+ drain();
109
+ });
110
+ const linksIn = (node) => node instanceof Element ? [...node.matches("a[href]") ? [node] : [], ...node.querySelectorAll("a[href]")] : [];
111
+ const tree = new MutationObserver((records) => {
112
+ for (const { addedNodes, removedNodes } of records) {
113
+ for (const node of removedNodes) for (const link of linksIn(node)) {
114
+ view.unobserve(link);
115
+ waiting.delete(link);
116
+ }
117
+ for (const node of addedNodes) for (const link of linksIn(node)) view.observe(link);
118
+ }
119
+ });
120
+ tree.observe(document.documentElement, {
121
+ childList: true,
122
+ subtree: true
123
+ });
124
+ for (const link of document.querySelectorAll("a[href]")) view.observe(link);
125
+ return () => {
126
+ stopped = true;
127
+ tree.disconnect();
128
+ view.disconnect();
129
+ waiting.clear();
130
+ };
131
+ };
132
+ const createPageCache = (load, { files }, now = () => performance.now()) => {
45
133
  const entries = /* @__PURE__ */ new Map();
46
- const usable = (entry) => now() - entry.at < PREFETCH_LIFETIME;
134
+ const usable = (entry) => files || now() - entry.at < 3e4;
47
135
  const forget = (key, entry) => {
48
136
  entries.delete(key);
49
- entry.controller.abort();
137
+ if (!files || !entry.settled) entry.controller.abort();
138
+ };
139
+ const hold = (key, entry) => {
140
+ entries.delete(key);
141
+ entries.set(key, entry);
142
+ if (entries.size <= 50 || !files) return;
143
+ for (const [oldest, held] of entries) if (oldest !== key && (held.settled || held.waiting === 0)) {
144
+ forget(oldest, held);
145
+ return;
146
+ }
147
+ };
148
+ const start = (key, priority) => {
149
+ const controller = new AbortController();
150
+ const entry = {
151
+ value: load(key, controller.signal, priority),
152
+ at: now(),
153
+ controller,
154
+ settled: false,
155
+ waiting: 0
156
+ };
157
+ entry.value.then(() => {
158
+ entry.settled = true;
159
+ }, () => {
160
+ entry.settled = true;
161
+ if (entries.get(key) === entry) entries.delete(key);
162
+ });
163
+ return entry;
164
+ };
165
+ const held = (key) => {
166
+ const entry = entries.get(key);
167
+ if (entry === void 0 || usable(entry)) return entry;
168
+ forget(key, entry);
50
169
  };
51
170
  const sweep = () => {
52
171
  for (const [key, entry] of entries) if (!usable(entry)) forget(key, entry);
53
172
  };
54
173
  return {
55
- prefetch: (key) => {
174
+ prefetch: (key, priority) => {
56
175
  sweep();
57
- if (entries.has(key)) return;
58
- const controller = new AbortController();
59
- const entry = {
60
- value: load(key, controller.signal),
61
- at: now(),
62
- controller
63
- };
64
- entries.set(key, entry);
65
- entry.value.catch(() => {
66
- if (entries.get(key) === entry) entries.delete(key);
67
- });
176
+ const entry = held(key) ?? start(key, priority);
177
+ hold(key, entry);
178
+ return entry.value;
68
179
  },
69
- take: (key, signal) => {
70
- const entry = entries.get(key);
71
- if (entry === void 0) return void 0;
72
- if (!usable(entry)) {
73
- forget(key, entry);
74
- return;
180
+ load: (key, signal) => {
181
+ let entry = held(key);
182
+ if (entry === void 0) {
183
+ if (!files) return load(key, signal, "auto");
184
+ entry = start(key, "auto");
185
+ }
186
+ if (files) hold(key, entry);
187
+ else entries.delete(key);
188
+ if (!entry.settled) {
189
+ const taken = entry;
190
+ taken.waiting += 1;
191
+ signal.addEventListener("abort", () => {
192
+ taken.waiting -= 1;
193
+ if (taken.settled || taken.waiting > 0) return;
194
+ if (entries.get(key) === taken) entries.delete(key);
195
+ taken.controller.abort(signal.reason);
196
+ }, { once: true });
75
197
  }
76
- entries.delete(key);
77
- signal.addEventListener("abort", () => {
78
- entry.controller.abort(signal.reason);
79
- }, { once: true });
80
198
  return entry.value;
81
199
  },
200
+ keep: (key, value) => {
201
+ hold(key, {
202
+ value: Promise.resolve(value),
203
+ at: now(),
204
+ controller: new AbortController(),
205
+ settled: true,
206
+ waiting: 0
207
+ });
208
+ },
82
209
  clear: () => {
83
- for (const [key, entry] of entries) forget(key, entry);
210
+ for (const [key, entry] of entries) if (entry.waiting === 0) forget(key, entry);
211
+ else entries.delete(key);
84
212
  }
85
213
  };
86
214
  };
87
- export { PREFETCH_ATTRIBUTE, PREFETCH_LIFETIME, createPrefetchCache, prefetchTargetOf };
215
+ export { KEPT_PAGES, PREFETCH_LIFETIME, createPageCache, prefetchInView, prefetchInViewUnderTouch, prefetchOnIntent, prefetchTargetOf };
@@ -1,5 +1,7 @@
1
1
  import { Component, ReactNode } from "react";
2
2
  export declare const markNavigated: () => void;
3
+ /** Taken before a Server Action is sent, and handed to `answerAction`. */
4
+ export declare const navigationsApplied: () => number;
3
5
  /** Whether the tree on screen came from a client navigation, not the HTML. */
4
6
  export declare const isNavigated: () => boolean;
5
7
  /**
@@ -3,16 +3,20 @@ import { reloadDocument } from "./reload.mjs";
3
3
  import { Component } from "react";
4
4
  // src/runtime/recover.tsx
5
5
  /**
6
- * Whether a client navigation has put a tree on screen yet. Until one has,
7
- * the tree being rendered is the one the server sent as HTML. A fact about
6
+ * How many trees navigations have applied — the router's, and `vite dev`'s
7
+ * reload of the page on screen. Until one has, the tree being rendered is the
8
+ * one the server sent as HTML. A Server Action's answer is not counted:
9
+ * answers to actions called in a row apply one after another. A fact about
8
10
  * the document, not about a component, which is why it is not a prop.
9
11
  */
10
- let navigated = false;
12
+ let navigations = 0;
11
13
  const markNavigated = () => {
12
- navigated = true;
14
+ navigations += 1;
13
15
  };
16
+ /** Taken before a Server Action is sent, and handed to `answerAction`. */
17
+ const navigationsApplied = () => navigations;
14
18
  /** Whether the tree on screen came from a client navigation, not the HTML. */
15
- const isNavigated = () => navigated;
19
+ const isNavigated = () => navigations > 0;
16
20
  /**
17
21
  * The document is being replaced; a value returned now would render into a
18
22
  * page that is on its way out.
@@ -38,11 +42,11 @@ var Recover = class extends Component {
38
42
  return { failed: true };
39
43
  }
40
44
  componentDidCatch(error) {
41
- if (!navigated) throw error;
45
+ if (!isNavigated()) throw error;
42
46
  reloadDocument();
43
47
  }
44
48
  render() {
45
49
  return this.state.failed ? null : this.props.children;
46
50
  }
47
51
  };
48
- export { Recover, isNavigated, markNavigated, reloadInstead };
52
+ export { Recover, isNavigated, markNavigated, navigationsApplied, reloadInstead };
@@ -1,18 +1,6 @@
1
- /**
2
- * A redirect thrown from a Server Action. Branded with a registry symbol
3
- * rather than a class: the framework bundles this module once into the
4
- * entry an application imports `redirect()` from and once into the runtime
5
- * it copies beside it (where the handler catches it), and two copies of a
6
- * class are two classes. `Symbol.for` is the one identity both share.
7
- */
8
- declare const BRAND: unique symbol;
9
- /**
10
- * An `Error` for the linter's and the stack's sake, recognised by the brand
11
- * and never by `instanceof`.
12
- */
1
+ /** A redirect thrown from a Server Action. */
13
2
  export declare class Redirect extends Error {
14
3
  readonly to: string;
15
- readonly [BRAND] = true;
16
4
  constructor(to: string);
17
5
  }
18
6
  /**
@@ -23,5 +11,23 @@ export declare class Redirect extends Error {
23
11
  * payload that tells the browser to navigate there.
24
12
  */
25
13
  export declare const redirect: (to: string) => never;
26
- export declare const isRedirect: (value: unknown) => value is Redirect;
14
+ /** What a `redirect.ts` route file default-exports. */
15
+ export type RedirectTarget = string | {
16
+ readonly to: string;
17
+ readonly permanent?: boolean;
18
+ };
19
+ type ResolvedRedirect = {
20
+ readonly to: string;
21
+ readonly permanent: boolean;
22
+ };
23
+ /**
24
+ * Where a `redirect.ts` sends a pathname the table matched to its pattern,
25
+ * with the matched params filled into the target, so `/:locale/legacy` can
26
+ * send to `/:locale/new`; `null` for a pattern no redirect declares. Which
27
+ * pattern answers is the table's to say — the redirect holds its place there
28
+ * — so this only reads the params again: the router hands them decoded, and a
29
+ * target moves a segment as the URL spelled it. The pathname is normalized as
30
+ * the table normalizes it, so `/old/` is `/old` here too.
31
+ */
32
+ export declare const resolveRedirects: (declared: Readonly<Record<string, RedirectTarget>>) => ((pattern: string, pathname: string) => ResolvedRedirect | null);
27
33
  export {};