@rangojs/router 0.0.0-experimental.19 → 0.0.0-experimental.1fa245e2

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 (160) hide show
  1. package/{CLAUDE.md → AGENTS.md} +4 -0
  2. package/README.md +122 -30
  3. package/dist/bin/rango.js +245 -63
  4. package/dist/vite/index.js +859 -418
  5. package/package.json +3 -3
  6. package/skills/breadcrumbs/SKILL.md +250 -0
  7. package/skills/cache-guide/SKILL.md +32 -0
  8. package/skills/caching/SKILL.md +49 -8
  9. package/skills/document-cache/SKILL.md +2 -2
  10. package/skills/hooks/SKILL.md +33 -31
  11. package/skills/host-router/SKILL.md +218 -0
  12. package/skills/links/SKILL.md +3 -1
  13. package/skills/loader/SKILL.md +72 -22
  14. package/skills/middleware/SKILL.md +2 -0
  15. package/skills/parallel/SKILL.md +126 -0
  16. package/skills/prerender/SKILL.md +112 -70
  17. package/skills/rango/SKILL.md +0 -1
  18. package/skills/route/SKILL.md +34 -4
  19. package/skills/router-setup/SKILL.md +95 -5
  20. package/skills/typesafety/SKILL.md +35 -23
  21. package/src/__internal.ts +92 -0
  22. package/src/bin/rango.ts +18 -0
  23. package/src/browser/app-version.ts +14 -0
  24. package/src/browser/event-controller.ts +5 -0
  25. package/src/browser/link-interceptor.ts +4 -0
  26. package/src/browser/navigation-bridge.ts +114 -18
  27. package/src/browser/navigation-client.ts +126 -44
  28. package/src/browser/navigation-store.ts +43 -8
  29. package/src/browser/navigation-transaction.ts +11 -9
  30. package/src/browser/partial-update.ts +80 -15
  31. package/src/browser/prefetch/cache.ts +166 -27
  32. package/src/browser/prefetch/fetch.ts +52 -39
  33. package/src/browser/prefetch/policy.ts +6 -0
  34. package/src/browser/prefetch/queue.ts +92 -20
  35. package/src/browser/prefetch/resource-ready.ts +77 -0
  36. package/src/browser/react/Link.tsx +70 -14
  37. package/src/browser/react/NavigationProvider.tsx +40 -4
  38. package/src/browser/react/context.ts +7 -2
  39. package/src/browser/react/use-handle.ts +9 -58
  40. package/src/browser/react/use-router.ts +21 -8
  41. package/src/browser/rsc-router.tsx +143 -59
  42. package/src/browser/scroll-restoration.ts +41 -42
  43. package/src/browser/segment-reconciler.ts +6 -1
  44. package/src/browser/server-action-bridge.ts +454 -436
  45. package/src/browser/types.ts +60 -5
  46. package/src/build/generate-manifest.ts +6 -6
  47. package/src/build/generate-route-types.ts +5 -0
  48. package/src/build/route-trie.ts +19 -3
  49. package/src/build/route-types/include-resolution.ts +8 -1
  50. package/src/build/route-types/router-processing.ts +346 -87
  51. package/src/build/route-types/scan-filter.ts +8 -1
  52. package/src/cache/cache-runtime.ts +15 -11
  53. package/src/cache/cache-scope.ts +48 -7
  54. package/src/cache/cf/cf-cache-store.ts +453 -11
  55. package/src/cache/cf/index.ts +5 -1
  56. package/src/cache/document-cache.ts +17 -7
  57. package/src/cache/index.ts +1 -0
  58. package/src/cache/taint.ts +55 -0
  59. package/src/client.rsc.tsx +2 -1
  60. package/src/client.tsx +3 -102
  61. package/src/context-var.ts +72 -2
  62. package/src/debug.ts +2 -2
  63. package/src/handle.ts +40 -0
  64. package/src/handles/breadcrumbs.ts +66 -0
  65. package/src/handles/index.ts +1 -0
  66. package/src/host/index.ts +0 -3
  67. package/src/index.rsc.ts +8 -37
  68. package/src/index.ts +40 -66
  69. package/src/prerender/store.ts +57 -15
  70. package/src/prerender.ts +138 -77
  71. package/src/reverse.ts +22 -1
  72. package/src/route-definition/dsl-helpers.ts +73 -25
  73. package/src/route-definition/helpers-types.ts +10 -6
  74. package/src/route-definition/index.ts +3 -3
  75. package/src/route-definition/redirect.ts +11 -3
  76. package/src/route-definition/resolve-handler-use.ts +149 -0
  77. package/src/route-map-builder.ts +7 -1
  78. package/src/route-types.ts +11 -0
  79. package/src/router/content-negotiation.ts +100 -1
  80. package/src/router/find-match.ts +4 -2
  81. package/src/router/handler-context.ts +108 -25
  82. package/src/router/intercept-resolution.ts +11 -4
  83. package/src/router/lazy-includes.ts +4 -1
  84. package/src/router/loader-resolution.ts +123 -11
  85. package/src/router/logging.ts +5 -2
  86. package/src/router/manifest.ts +9 -3
  87. package/src/router/match-api.ts +125 -190
  88. package/src/router/match-middleware/background-revalidation.ts +30 -2
  89. package/src/router/match-middleware/cache-lookup.ts +88 -16
  90. package/src/router/match-middleware/cache-store.ts +53 -10
  91. package/src/router/match-middleware/intercept-resolution.ts +9 -7
  92. package/src/router/match-middleware/segment-resolution.ts +61 -5
  93. package/src/router/match-result.ts +22 -15
  94. package/src/router/metrics.ts +238 -13
  95. package/src/router/middleware-types.ts +53 -12
  96. package/src/router/middleware.ts +172 -85
  97. package/src/router/navigation-snapshot.ts +182 -0
  98. package/src/router/pattern-matching.ts +20 -5
  99. package/src/router/prerender-match.ts +114 -10
  100. package/src/router/preview-match.ts +30 -102
  101. package/src/router/request-classification.ts +310 -0
  102. package/src/router/revalidation.ts +27 -7
  103. package/src/router/route-snapshot.ts +245 -0
  104. package/src/router/router-context.ts +6 -1
  105. package/src/router/router-interfaces.ts +50 -5
  106. package/src/router/router-options.ts +50 -19
  107. package/src/router/segment-resolution/fresh.ts +200 -19
  108. package/src/router/segment-resolution/helpers.ts +30 -25
  109. package/src/router/segment-resolution/loader-cache.ts +1 -0
  110. package/src/router/segment-resolution/revalidation.ts +429 -301
  111. package/src/router/segment-wrappers.ts +2 -0
  112. package/src/router/trie-matching.ts +20 -2
  113. package/src/router/types.ts +1 -0
  114. package/src/router.ts +88 -15
  115. package/src/rsc/handler.ts +546 -359
  116. package/src/rsc/index.ts +0 -20
  117. package/src/rsc/manifest-init.ts +5 -1
  118. package/src/rsc/progressive-enhancement.ts +25 -8
  119. package/src/rsc/rsc-rendering.ts +35 -43
  120. package/src/rsc/server-action.ts +16 -10
  121. package/src/rsc/ssr-setup.ts +128 -0
  122. package/src/rsc/types.ts +10 -1
  123. package/src/search-params.ts +16 -13
  124. package/src/segment-system.tsx +140 -4
  125. package/src/server/context.ts +148 -16
  126. package/src/server/loader-registry.ts +9 -8
  127. package/src/server/request-context.ts +182 -34
  128. package/src/server.ts +6 -0
  129. package/src/ssr/index.tsx +4 -0
  130. package/src/static-handler.ts +18 -6
  131. package/src/theme/index.ts +4 -13
  132. package/src/types/cache-types.ts +4 -4
  133. package/src/types/handler-context.ts +149 -49
  134. package/src/types/loader-types.ts +36 -9
  135. package/src/types/route-config.ts +17 -8
  136. package/src/types/route-entry.ts +8 -1
  137. package/src/types/segments.ts +2 -5
  138. package/src/urls/path-helper-types.ts +9 -2
  139. package/src/urls/path-helper.ts +48 -13
  140. package/src/urls/pattern-types.ts +12 -0
  141. package/src/urls/response-types.ts +16 -6
  142. package/src/use-loader.tsx +73 -4
  143. package/src/vite/discovery/bundle-postprocess.ts +61 -89
  144. package/src/vite/discovery/discover-routers.ts +23 -5
  145. package/src/vite/discovery/prerender-collection.ts +48 -15
  146. package/src/vite/discovery/state.ts +17 -13
  147. package/src/vite/index.ts +8 -3
  148. package/src/vite/plugin-types.ts +51 -79
  149. package/src/vite/plugins/client-ref-dedup.ts +115 -0
  150. package/src/vite/plugins/expose-action-id.ts +1 -3
  151. package/src/vite/plugins/performance-tracks.ts +88 -0
  152. package/src/vite/plugins/refresh-cmd.ts +127 -0
  153. package/src/vite/plugins/version-plugin.ts +13 -1
  154. package/src/vite/rango.ts +174 -211
  155. package/src/vite/router-discovery.ts +169 -42
  156. package/src/vite/utils/banner.ts +3 -3
  157. package/src/vite/utils/prerender-utils.ts +78 -0
  158. package/src/vite/utils/shared-utils.ts +3 -2
  159. package/skills/testing/SKILL.md +0 -226
  160. package/src/route-definition/route-function.ts +0 -119
@@ -1,9 +1,70 @@
1
1
  "use client";
2
2
 
3
- import { useCallback, useContext, useEffect, useRef, useState } from "react";
3
+ import {
4
+ isValidElement,
5
+ useCallback,
6
+ useContext,
7
+ useEffect,
8
+ useMemo,
9
+ useRef,
10
+ useState,
11
+ type ReactNode,
12
+ } from "react";
4
13
  import { OutletContext, type OutletContextValue } from "./outlet-context.js";
5
14
  import type { LoaderDefinition, LoadOptions } from "./types.js";
6
15
 
16
+ /**
17
+ * Extract a specific loader's data from a content ReactNode.
18
+ *
19
+ * When a route registers loaders via loader(), the resolved data lives in
20
+ * the route's OutletProvider (rendered as <Outlet /> content). Parallel
21
+ * slots are siblings of <Outlet />, so they can't find it by walking
22
+ * the parent context chain. This helper traverses wrapper elements
23
+ * (MountContextProvider, ViewTransition, etc.) to reach the OutletProvider
24
+ * and extract the loader data directly.
25
+ */
26
+ const NOT_FOUND = Symbol("not-found");
27
+
28
+ function extractContentLoaderData(
29
+ node: ReactNode,
30
+ loaderId: string,
31
+ ): unknown | typeof NOT_FOUND {
32
+ if (!isValidElement(node)) return NOT_FOUND;
33
+ const props = node.props as Record<string, any> | undefined;
34
+ if (!props) return NOT_FOUND;
35
+
36
+ // Direct OutletProvider with loaderData
37
+ if (props.loaderData && loaderId in props.loaderData) {
38
+ return props.loaderData[loaderId];
39
+ }
40
+
41
+ // LoaderBoundary: loaderIds + loaderDataPromise (already resolved array).
42
+ // When the segment has loading(), loaderData is resolved inside
43
+ // LoaderBoundary via use(). If the promise was pre-awaited (forceAwait
44
+ // or isAction), the prop is a raw array we can index into.
45
+ if (
46
+ props.loaderIds &&
47
+ Array.isArray(props.loaderIds) &&
48
+ props.loaderDataPromise &&
49
+ !(props.loaderDataPromise instanceof Promise)
50
+ ) {
51
+ const idx = (props.loaderIds as string[]).indexOf(loaderId);
52
+ if (idx !== -1) {
53
+ const data = (props.loaderDataPromise as any[])[idx];
54
+ // loaderDataPromise entries may be { ok, data } result objects
55
+ if (data && typeof data === "object" && "ok" in data) {
56
+ return data.ok ? data.data : NOT_FOUND;
57
+ }
58
+ return data;
59
+ }
60
+ }
61
+
62
+ // Traverse into wrapper elements (MountContextProvider, ViewTransition,
63
+ // Suspense wrappers, etc.)
64
+ if (props.children) return extractContentLoaderData(props.children, loaderId);
65
+ return NOT_FOUND;
66
+ }
67
+
7
68
  /**
8
69
  * Payload returned by loader RSC requests
9
70
  */
@@ -71,19 +132,27 @@ function useLoaderInternal<T>(
71
132
  const context = useContext(OutletContext);
72
133
 
73
134
  // Get data from context (SSR/navigation)
74
- const getContextData = useCallback((): T | undefined => {
135
+ const contextData = useMemo((): T | undefined => {
75
136
  let current: OutletContextValue | null | undefined = context;
76
137
  while (current) {
77
138
  if (current.loaderData && loader.$$id in current.loaderData) {
78
139
  return current.loaderData[loader.$$id] as T;
79
140
  }
141
+ // Check content element — the route's OutletProvider is rendered as
142
+ // <Outlet /> content (a child), so its loaderData isn't in the parent
143
+ // chain. Parallel slots need to reach into it to find route-level loaders.
144
+ const contentData = extractContentLoaderData(
145
+ current.content,
146
+ loader.$$id,
147
+ );
148
+ if (contentData !== NOT_FOUND) {
149
+ return contentData as T;
150
+ }
80
151
  current = current.parent;
81
152
  }
82
153
  return undefined;
83
154
  }, [context, loader.$$id]);
84
155
 
85
- const contextData = getContextData();
86
-
87
156
  // Local state for fetched data (from load() calls)
88
157
  const [fetchedData, setFetchedData] = useState<T | undefined>(undefined);
89
158
  const [isLoading, setIsLoading] = useState(false);
@@ -6,9 +6,9 @@
6
6
  */
7
7
 
8
8
  import { resolve } from "node:path";
9
- import { createHash } from "node:crypto";
10
- import { readFileSync, writeFileSync, existsSync, mkdirSync } from "node:fs";
9
+ import { readFileSync, writeFileSync, existsSync } from "node:fs";
11
10
  import { evictHandlerCode } from "../utils/bundle-analysis.js";
11
+ import { copyStagedBuildAssets } from "../utils/prerender-utils.js";
12
12
  import type { DiscoveryState } from "./state.js";
13
13
 
14
14
  /**
@@ -17,11 +17,11 @@ import type { DiscoveryState } from "./state.js";
17
17
  */
18
18
  export function postprocessBundle(state: DiscoveryState): void {
19
19
  const hasPrerenderData =
20
- state.prerenderCollectedData &&
21
- Object.keys(state.prerenderCollectedData).length > 0;
20
+ state.prerenderManifestEntries &&
21
+ Object.keys(state.prerenderManifestEntries).length > 0;
22
22
  const hasStaticData =
23
- state.staticCollectedData &&
24
- Object.keys(state.staticCollectedData).length > 0;
23
+ state.staticManifestEntries &&
24
+ Object.keys(state.staticManifestEntries).length > 0;
25
25
  if (!hasPrerenderData && !hasStaticData) return;
26
26
 
27
27
  // Find RSC entry (recorded in generateBundle, fallback to dist/rsc/index.js)
@@ -31,25 +31,25 @@ export function postprocessBundle(state: DiscoveryState): void {
31
31
  state.rscEntryFileName ?? "index.js",
32
32
  );
33
33
 
34
- // 1. Evict handler code from __prerender-handlers and __static-handlers chunks.
35
- // handlerChunkInfo/staticHandlerChunkInfo are populated by generateBundle
34
+ // 1. Evict handler code from whichever chunks contain handler exports.
35
+ // handlerChunkInfoMap/staticHandlerChunkInfoMap are populated by generateBundle
36
36
  // after the production RSC build. In Vite 6 multi-environment builds, the
37
- // RSC build runs twice (analysis + production). Chunk info is only available
38
- // after the production pass, so we run eviction whenever it becomes available.
37
+ // RSC build runs twice (analysis + production). The maps are cleared at the
38
+ // start of each generateBundle pass so only production data is used here.
39
39
  const evictionTargets: Array<{
40
- info: typeof state.handlerChunkInfo;
40
+ infos: Iterable<import("./state.js").ChunkInfo>;
41
41
  fnName: string;
42
42
  brand: string;
43
43
  label: string;
44
44
  }> = [
45
45
  {
46
- info: state.handlerChunkInfo,
46
+ infos: state.handlerChunkInfoMap.values(),
47
47
  fnName: "Prerender",
48
48
  brand: "prerenderHandler",
49
49
  label: "handler code from RSC bundle",
50
50
  },
51
51
  {
52
- info: state.staticHandlerChunkInfo,
52
+ infos: state.staticHandlerChunkInfoMap.values(),
53
53
  fnName: "Static",
54
54
  brand: "staticHandler",
55
55
  label: "static handler code",
@@ -57,70 +57,58 @@ export function postprocessBundle(state: DiscoveryState): void {
57
57
  ];
58
58
 
59
59
  for (const target of evictionTargets) {
60
- if (!target.info) continue;
61
- const chunkPath = resolve(
62
- state.projectRoot,
63
- "dist/rsc",
64
- target.info.fileName,
65
- );
66
- try {
67
- const code = readFileSync(chunkPath, "utf-8");
68
- const result = evictHandlerCode(
69
- code,
70
- target.info.exports,
71
- target.fnName,
72
- target.brand,
73
- );
74
- if (result) {
75
- writeFileSync(chunkPath, result.code);
76
- const savedKB = (result.savedBytes / 1024).toFixed(1);
77
- console.log(
78
- `[rsc-router] Evicted ${target.label} (${savedKB} KB saved): ${target.info.fileName}`,
60
+ for (const info of target.infos) {
61
+ const chunkPath = resolve(state.projectRoot, "dist/rsc", info.fileName);
62
+ try {
63
+ const code = readFileSync(chunkPath, "utf-8");
64
+ const result = evictHandlerCode(
65
+ code,
66
+ info.exports,
67
+ target.fnName,
68
+ target.brand,
69
+ );
70
+ if (result) {
71
+ writeFileSync(chunkPath, result.code);
72
+ const savedKB = (result.savedBytes / 1024).toFixed(1);
73
+ console.log(
74
+ `[rsc-router] Evicted ${target.label} (${savedKB} KB saved): ${info.fileName}`,
75
+ );
76
+ }
77
+ } catch (replaceErr: any) {
78
+ console.warn(
79
+ `[rsc-router] Failed to evict ${target.label}: ${replaceErr.message}`,
79
80
  );
80
81
  }
81
- } catch (replaceErr: any) {
82
- console.warn(
83
- `[rsc-router] Failed to evict ${target.label}: ${replaceErr.message}`,
84
- );
85
82
  }
86
83
  }
87
- state.handlerChunkInfo = null;
88
- state.staticHandlerChunkInfo = null;
84
+ state.handlerChunkInfoMap.clear();
85
+ state.staticHandlerChunkInfoMap.clear();
89
86
 
90
87
  // 2. Write prerender data as separate importable asset modules
91
- // and inject a manifest import into the RSC entry.
88
+ // and inject a lazy manifest loader into the RSC entry.
92
89
  if (hasPrerenderData && existsSync(rscEntryPath)) {
93
90
  const rscCode = readFileSync(rscEntryPath, "utf-8");
94
- // Check for the specific injection marker, not just the variable name.
95
- // The runtime code (prerender store) also references __PRERENDER_MANIFEST,
96
- // so a broad string check would false-positive and skip injection.
91
+ // Check for the specific injection marker to avoid double-injection.
97
92
  if (!rscCode.includes("__prerender-manifest.js")) {
98
93
  try {
99
- const assetsDir = resolve(state.projectRoot, "dist/rsc/assets");
100
- mkdirSync(assetsDir, { recursive: true });
101
-
102
- const manifestEntries: string[] = [];
103
- let totalBytes = 0;
94
+ let totalBytes = copyStagedBuildAssets(
95
+ state.projectRoot,
96
+ Object.values(state.prerenderManifestEntries!),
97
+ );
104
98
 
105
- for (const [key, entry] of Object.entries(
106
- state.prerenderCollectedData!,
99
+ const manifestMap: Record<string, string> = {};
100
+ for (const [key, assetFileName] of Object.entries(
101
+ state.prerenderManifestEntries!,
107
102
  )) {
108
- const entryJson = JSON.stringify(entry);
109
- const contentHash = createHash("sha256")
110
- .update(entryJson)
111
- .digest("hex")
112
- .slice(0, 8);
113
- const assetFileName = `__pr-${contentHash}.js`;
114
- const assetPath = resolve(assetsDir, assetFileName);
115
- const assetCode = `export default ${entryJson};\n`;
116
- writeFileSync(assetPath, assetCode);
117
- totalBytes += Buffer.byteLength(assetCode);
118
- manifestEntries.push(
119
- `${JSON.stringify(key)}:()=>import("./assets/${assetFileName}")`,
120
- );
103
+ manifestMap[key] = `./assets/${assetFileName}`;
121
104
  }
122
105
 
123
- const manifestCode = `const m={${manifestEntries.join(",")}};export default m;\n`;
106
+ const manifestCode = [
107
+ `const m=JSON.parse('${JSON.stringify(manifestMap).replace(/'/g, "\\'")}');`,
108
+ `export function loadPrerenderAsset(s){return import(s)}`,
109
+ `export default m;`,
110
+ "",
111
+ ].join("\n");
124
112
  const manifestPath = resolve(
125
113
  state.projectRoot,
126
114
  "dist/rsc/__prerender-manifest.js",
@@ -128,12 +116,12 @@ export function postprocessBundle(state: DiscoveryState): void {
128
116
  writeFileSync(manifestPath, manifestCode);
129
117
  totalBytes += Buffer.byteLength(manifestCode);
130
118
 
131
- const injection = `import __pm from "./__prerender-manifest.js";\nglobalThis.__PRERENDER_MANIFEST = __pm;\n`;
119
+ const injection = `globalThis.__loadPrerenderManifestModule = () => import("./__prerender-manifest.js");\n`;
132
120
  writeFileSync(rscEntryPath, injection + rscCode);
133
121
 
134
122
  const totalKB = (totalBytes / 1024).toFixed(1);
135
123
  console.log(
136
- `[rsc-router] Wrote prerender assets (${totalKB} KB total, ${Object.keys(state.prerenderCollectedData!).length} entries)`,
124
+ `[rsc-router] Wrote prerender assets (${totalKB} KB total, ${Object.keys(state.prerenderManifestEntries!).length} entries)`,
137
125
  );
138
126
  } catch (err: any) {
139
127
  throw new Error(
@@ -147,33 +135,17 @@ export function postprocessBundle(state: DiscoveryState): void {
147
135
  // and inject a __STATIC_MANIFEST import into the RSC entry.
148
136
  if (hasStaticData && existsSync(rscEntryPath)) {
149
137
  const rscCode = readFileSync(rscEntryPath, "utf-8");
150
- if (!rscCode.includes("__STATIC_MANIFEST")) {
138
+ if (!rscCode.includes("__static-manifest.js")) {
151
139
  try {
152
- const assetsDir = resolve(state.projectRoot, "dist/rsc/assets");
153
- mkdirSync(assetsDir, { recursive: true });
154
-
155
140
  const manifestEntries: string[] = [];
156
- let totalBytes = 0;
141
+ let totalBytes = copyStagedBuildAssets(
142
+ state.projectRoot,
143
+ Object.values(state.staticManifestEntries!),
144
+ );
157
145
 
158
- for (const [handlerId, { encoded, handles }] of Object.entries(
159
- state.staticCollectedData!,
146
+ for (const [handlerId, assetFileName] of Object.entries(
147
+ state.staticManifestEntries!,
160
148
  )) {
161
- // Store both the Flight payload and handle data
162
- const hasHandles = Object.keys(handles).length > 0;
163
- const exportValue = hasHandles
164
- ? JSON.stringify({ encoded, handles })
165
- : JSON.stringify(encoded);
166
- // Hash the full payload that is written so distinct handle
167
- // snapshots produce distinct asset filenames.
168
- const contentHash = createHash("sha256")
169
- .update(exportValue)
170
- .digest("hex")
171
- .slice(0, 8);
172
- const assetFileName = `__st-${contentHash}.js`;
173
- const assetPath = resolve(assetsDir, assetFileName);
174
- const assetCode = `export default ${exportValue};\n`;
175
- writeFileSync(assetPath, assetCode);
176
- totalBytes += Buffer.byteLength(assetCode);
177
149
  manifestEntries.push(
178
150
  `${JSON.stringify(handlerId)}:()=>import("./assets/${assetFileName}")`,
179
151
  );
@@ -197,7 +169,7 @@ export function postprocessBundle(state: DiscoveryState): void {
197
169
 
198
170
  const totalKB = (totalBytes / 1024).toFixed(1);
199
171
  console.log(
200
- `[rsc-router] Wrote static assets (${totalKB} KB total, ${Object.keys(state.staticCollectedData!).length} entries)`,
172
+ `[rsc-router] Wrote static assets (${totalKB} KB total, ${Object.keys(state.staticManifestEntries!).length} entries)`,
201
173
  );
202
174
  } catch (err: any) {
203
175
  throw new Error(
@@ -6,7 +6,11 @@
6
6
  * router, and builds route tries for O(path_length) matching.
7
7
  */
8
8
 
9
- import { buildCombinedRouteMapForRouterFile } from "../../build/generate-route-types.js";
9
+ import {
10
+ buildCombinedRouteMapForRouterFile,
11
+ formatNestedRouterConflictError,
12
+ findNestedRouterConflict,
13
+ } from "../../build/generate-route-types.js";
10
14
  import {
11
15
  flattenLeafEntries,
12
16
  buildRouteToStaticPrefix,
@@ -44,9 +48,8 @@ export async function discoverRouters(
44
48
  // No RSC routers found directly. Check for host routers with lazy handlers
45
49
  // that need to be resolved to trigger sub-app createRouter() calls.
46
50
  try {
47
- const hostMod = await rscEnv.runner.import("@rangojs/router/host");
48
51
  const hostRegistry: Map<string, any> | undefined =
49
- hostMod.HostRouterRegistry;
52
+ serverMod.HostRouterRegistry;
50
53
 
51
54
  if (hostRegistry && hostRegistry.size > 0) {
52
55
  console.log(
@@ -85,7 +88,7 @@ export async function discoverRouters(
85
88
  }
86
89
  }
87
90
  } catch {
88
- // @rangojs/router/host not available or import failed, skip
91
+ // Host-router discovery is best-effort; skip if unavailable
89
92
  }
90
93
 
91
94
  // If still no routers after host router resolution, fail
@@ -100,6 +103,17 @@ export async function discoverRouters(
100
103
  const buildMod = await rscEnv.runner.import("@rangojs/router/build");
101
104
  const generateManifestFull = buildMod.generateManifestFull;
102
105
 
106
+ const nestedRouterConflict = findNestedRouterConflict(
107
+ [...registry.values()]
108
+ .map((router) => router.__sourceFile)
109
+ .filter(
110
+ (sourceFile): sourceFile is string => typeof sourceFile === "string",
111
+ ),
112
+ );
113
+ if (nestedRouterConflict) {
114
+ throw new Error(formatNestedRouterConflictError(nestedRouterConflict));
115
+ }
116
+
103
117
  // Build into local variables first. Only commit to state after the
104
118
  // full pass succeeds, so a failed re-discovery preserves the last
105
119
  // known-good state instead of leaving it partially wiped.
@@ -121,7 +135,11 @@ export async function discoverRouters(
121
135
  continue;
122
136
  }
123
137
 
124
- const manifest = generateManifestFull(router.urlpatterns, routerMountIndex);
138
+ const manifest = generateManifestFull(
139
+ router.urlpatterns,
140
+ routerMountIndex,
141
+ router.__basename ? { urlPrefix: router.__basename } : undefined,
142
+ );
125
143
  routerMountIndex++;
126
144
  allManifests.push({ id, manifest });
127
145
  const routeCount = Object.keys(manifest.routeManifest).length;
@@ -13,12 +13,14 @@ import {
13
13
  runWithConcurrency,
14
14
  groupByConcurrency,
15
15
  notifyOnError,
16
+ stageBuildAssetModule,
16
17
  } from "../utils/prerender-utils.js";
17
18
  import type { DiscoveryState } from "./state.js";
18
19
 
19
20
  /**
20
21
  * Expand prerender routes into concrete URLs and render them via the
21
- * RSC runner. Stores collected data in state.prerenderCollectedData.
22
+ * RSC runner. Stages asset modules and stores key-to-file entries in
23
+ * state.prerenderManifestEntries.
22
24
  */
23
25
  export async function expandPrerenderRoutes(
24
26
  state: DiscoveryState,
@@ -52,11 +54,12 @@ export async function expandPrerenderRoutes(
52
54
  for (const { manifest } of allManifests) {
53
55
  if (!manifest.prerenderRoutes) continue;
54
56
  const defs = manifest._prerenderDefs || {};
57
+ const passthroughSet = new Set(manifest.passthroughRoutes || []);
55
58
  for (const routeName of manifest.prerenderRoutes) {
56
59
  const pattern = manifest.routeManifest[routeName];
57
60
  if (!pattern) continue;
58
61
  const def = defs[routeName];
59
- const isPassthroughRoute = !!def?.options?.passthrough;
62
+ const isPassthroughRoute = passthroughSet.has(routeName);
60
63
  const hasDynamic = pattern.includes(":") || pattern.includes("*");
61
64
  if (!hasDynamic) {
62
65
  // Static route: use pattern directly (strip trailing slash for URL)
@@ -71,12 +74,21 @@ export async function expandPrerenderRoutes(
71
74
  if (def?.getParams) {
72
75
  try {
73
76
  const buildVars: Record<string, any> = {};
77
+ const buildEnv = state.resolvedBuildEnv;
74
78
  const getParamsCtx = {
75
79
  build: true as const,
80
+ dev: !state.isBuildMode,
76
81
  set: ((keyOrVar: any, value: any) => {
77
82
  contextSet(buildVars, keyOrVar, value);
78
83
  }) as any,
79
84
  reverse: getParamsReverse,
85
+ get env() {
86
+ if (buildEnv !== undefined) return buildEnv;
87
+ throw new Error(
88
+ "[rsc-router] ctx.env is not available during build-time getParams(). " +
89
+ "Configure buildEnv in your rango() plugin options to enable build-time env access.",
90
+ );
91
+ },
80
92
  };
81
93
  const paramsList = await def.getParams(getParamsCtx);
82
94
  const concurrency = def.options?.concurrency ?? 1;
@@ -150,7 +162,7 @@ export async function expandPrerenderRoutes(
150
162
 
151
163
  const { hashParams } = await rscEnv.runner.import("@rangojs/router/build");
152
164
 
153
- const collectedData: Record<string, any> = {};
165
+ const manifestEntries: Record<string, string> = {};
154
166
  let doneCount = 0;
155
167
  let skipCount = 0;
156
168
  const startTotal = performance.now();
@@ -173,6 +185,7 @@ export async function expandPrerenderRoutes(
173
185
  {},
174
186
  entry.buildVars,
175
187
  entry.isPassthroughRoute,
188
+ state.resolvedBuildEnv,
176
189
  );
177
190
  if (!result) continue;
178
191
 
@@ -187,18 +200,30 @@ export async function expandPrerenderRoutes(
187
200
  }
188
201
 
189
202
  const paramHash = hashParams(result.params || {});
190
- collectedData[`${result.routeName}/${paramHash}`] = {
203
+ const mainKey = `${result.routeName}/${paramHash}`;
204
+ const mainValue = JSON.stringify({
191
205
  segments: result.segments,
192
206
  handles: result.handles,
193
- };
207
+ });
208
+ manifestEntries[mainKey] = stageBuildAssetModule(
209
+ state.projectRoot,
210
+ "__pr",
211
+ mainValue,
212
+ );
194
213
  if (result.interceptSegments?.length) {
195
- collectedData[`${result.routeName}/${paramHash}/i`] = {
214
+ const interceptKey = `${result.routeName}/${paramHash}/i`;
215
+ const interceptValue = JSON.stringify({
196
216
  segments: [...result.segments, ...result.interceptSegments],
197
217
  handles: {
198
218
  ...result.handles,
199
219
  ...(result.interceptHandles || {}),
200
220
  },
201
- };
221
+ });
222
+ manifestEntries[interceptKey] = stageBuildAssetModule(
223
+ state.projectRoot,
224
+ "__pr",
225
+ interceptValue,
226
+ );
202
227
  }
203
228
  const elapsed = (performance.now() - startUrl).toFixed(0);
204
229
  console.log(
@@ -244,7 +269,7 @@ export async function expandPrerenderRoutes(
244
269
 
245
270
  const totalElapsed = (performance.now() - startTotal).toFixed(0);
246
271
  if (doneCount > 0) {
247
- state.prerenderCollectedData = collectedData;
272
+ state.prerenderManifestEntries = manifestEntries;
248
273
  }
249
274
  const parts = [`${doneCount} done`];
250
275
  if (skipCount > 0) parts.push(`${skipCount} skipped`);
@@ -256,7 +281,8 @@ export async function expandPrerenderRoutes(
256
281
  /**
257
282
  * Render Static handlers at build time. Each Static handler is called
258
283
  * with a synthetic BuildContext and its output is RSC-serialized.
259
- * Stores collected data in state.staticCollectedData.
284
+ * Stages asset modules and stores handlerId-to-file entries in
285
+ * state.staticManifestEntries.
260
286
  */
261
287
  export async function renderStaticHandlers(
262
288
  state: DiscoveryState,
@@ -270,10 +296,7 @@ export async function renderStaticHandlers(
270
296
  )
271
297
  return;
272
298
 
273
- const collected: Record<
274
- string,
275
- { encoded: string; handles: Record<string, unknown[]> }
276
- > = {};
299
+ const manifestEntries: Record<string, string> = {};
277
300
  let staticDone = 0;
278
301
  let staticSkip = 0;
279
302
  let totalStaticCount = 0;
@@ -314,9 +337,19 @@ export async function renderStaticHandlers(
314
337
  def.handler,
315
338
  def.$$id,
316
339
  (def as any).$$routePrefix,
340
+ state.resolvedBuildEnv,
341
+ !state.isBuildMode,
317
342
  );
318
343
  if (result) {
319
- collected[def.$$id] = result;
344
+ const hasHandles = Object.keys(result.handles).length > 0;
345
+ const exportValue = hasHandles
346
+ ? JSON.stringify(result)
347
+ : JSON.stringify(result.encoded);
348
+ manifestEntries[def.$$id] = stageBuildAssetModule(
349
+ state.projectRoot,
350
+ "__st",
351
+ exportValue,
352
+ );
320
353
  const elapsed = (performance.now() - startHandler).toFixed(0);
321
354
  console.log(
322
355
  `[rsc-router] OK ${name.padEnd(40)} (${elapsed}ms)`,
@@ -355,7 +388,7 @@ export async function renderStaticHandlers(
355
388
 
356
389
  const totalStaticElapsed = (performance.now() - startStatic).toFixed(0);
357
390
  if (staticDone > 0) {
358
- state.staticCollectedData = collected;
391
+ state.staticManifestEntries = manifestEntries;
359
392
  }
360
393
  const staticParts = [`${staticDone} done`];
361
394
  if (staticSkip > 0) staticParts.push(`${staticSkip} skipped`);
@@ -13,11 +13,13 @@ export const VIRTUAL_ROUTES_MANIFEST_ID = "virtual:rsc-router/routes-manifest";
13
13
  export interface PluginOptions {
14
14
  enableBuildPrerender?: boolean;
15
15
  staticRouteTypesGeneration?: boolean;
16
- include?: string[];
17
- exclude?: string[];
18
16
  // Mutable ref for deferred auto-discovery (node preset).
19
17
  // The auto-discover config() hook populates this before configResolved.
20
18
  routerPathRef?: { path?: string };
19
+ /** Build-time env option from rango() config. */
20
+ buildEnv?: import("../plugin-types.js").BuildEnvOption;
21
+ /** Deployment preset (needed for buildEnv "auto" resolution). */
22
+ preset?: "node" | "cloudflare";
21
23
  }
22
24
 
23
25
  export interface PrecomputedEntry {
@@ -56,13 +58,10 @@ export interface DiscoveryState {
56
58
  perRouterPrecomputedMap: Map<string, PrecomputedEntry[]>;
57
59
  perRouterManifestDataMap: Map<string, Record<string, string>>;
58
60
 
59
- prerenderCollectedData: Record<string, any> | null;
60
- staticCollectedData: Record<
61
- string,
62
- { encoded: string; handles: Record<string, unknown[]> }
63
- > | null;
64
- handlerChunkInfo: ChunkInfo | null;
65
- staticHandlerChunkInfo: ChunkInfo | null;
61
+ prerenderManifestEntries: Record<string, string> | null;
62
+ staticManifestEntries: Record<string, string> | null;
63
+ handlerChunkInfoMap: Map<string, ChunkInfo>;
64
+ staticHandlerChunkInfoMap: Map<string, ChunkInfo>;
66
65
  rscEntryFileName: string | null;
67
66
  resolvedPrerenderModules: Map<string, string[]> | undefined;
68
67
  resolvedStaticModules: Map<string, string[]> | undefined;
@@ -72,6 +71,11 @@ export interface DiscoveryState {
72
71
  devServer: any;
73
72
  selfWrittenGenFiles: Map<string, { at: number; hash: string }>;
74
73
  SELF_WRITE_WINDOW_MS: number;
74
+
75
+ /** Resolved build-time env bindings (set during buildStart/configureServer). */
76
+ resolvedBuildEnv?: Record<string, unknown>;
77
+ /** Cleanup function for build-time env resources (e.g., miniflare). */
78
+ buildEnvDispose?: (() => Promise<void> | void) | null;
75
79
  }
76
80
 
77
81
  export function createDiscoveryState(
@@ -96,10 +100,10 @@ export function createDiscoveryState(
96
100
  perRouterPrecomputedMap: new Map(),
97
101
  perRouterManifestDataMap: new Map(),
98
102
 
99
- prerenderCollectedData: null,
100
- staticCollectedData: null,
101
- handlerChunkInfo: null,
102
- staticHandlerChunkInfo: null,
103
+ prerenderManifestEntries: null,
104
+ staticManifestEntries: null,
105
+ handlerChunkInfoMap: new Map(),
106
+ staticHandlerChunkInfoMap: new Map(),
103
107
  rscEntryFileName: null,
104
108
  resolvedPrerenderModules: undefined,
105
109
  resolvedStaticModules: undefined,
package/src/vite/index.ts CHANGED
@@ -1,15 +1,20 @@
1
1
  /**
2
2
  * Public API for @rangojs/router/vite
3
3
  *
4
- * Only the rango() plugin factory and its option types are part of the
5
- * public API. All other utilities are internal implementation details
6
- * consumed via direct imports within the package.
4
+ * Exports: rango() plugin factory, poke() dev utility plugin,
5
+ * and related option types. All other utilities are internal implementation
6
+ * details consumed via direct imports within the package.
7
7
  */
8
8
 
9
9
  export { rango } from "./rango.js";
10
+ export { poke } from "./plugins/refresh-cmd.js";
10
11
 
11
12
  export type {
12
13
  RangoNodeOptions,
13
14
  RangoCloudflareOptions,
14
15
  RangoOptions,
16
+ BuildEnvOption,
17
+ BuildEnvFactory,
18
+ BuildEnvFactoryContext,
19
+ BuildEnvResult,
15
20
  } from "./plugin-types.js";