@rangojs/router 0.0.0-experimental.fb4fdc18 → 0.0.0-experimental.fce7fbd1

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 (214) hide show
  1. package/README.md +9 -9
  2. package/dist/bin/rango.js +147 -57
  3. package/dist/testing/vitest.js +48 -0
  4. package/dist/vite/index.js +914 -485
  5. package/package.json +55 -11
  6. package/skills/bundle-analysis/SKILL.md +159 -0
  7. package/skills/cache-guide/SKILL.md +220 -30
  8. package/skills/caching/SKILL.md +116 -8
  9. package/skills/composability/SKILL.md +27 -2
  10. package/skills/document-cache/SKILL.md +78 -55
  11. package/skills/handler-use/SKILL.md +3 -1
  12. package/skills/hooks/SKILL.md +214 -18
  13. package/skills/host-router/SKILL.md +45 -20
  14. package/skills/intercept/SKILL.md +26 -4
  15. package/skills/layout/SKILL.md +6 -7
  16. package/skills/links/SKILL.md +173 -17
  17. package/skills/loader/SKILL.md +149 -6
  18. package/skills/middleware/SKILL.md +13 -9
  19. package/skills/migrate-nextjs/SKILL.md +1 -1
  20. package/skills/mime-routes/SKILL.md +27 -0
  21. package/skills/observability/SKILL.md +137 -0
  22. package/skills/parallel/SKILL.md +5 -6
  23. package/skills/prerender/SKILL.md +14 -33
  24. package/skills/rango/SKILL.md +242 -26
  25. package/skills/react-compiler/SKILL.md +168 -0
  26. package/skills/response-routes/SKILL.md +58 -9
  27. package/skills/route/SKILL.md +13 -4
  28. package/skills/router-setup/SKILL.md +3 -3
  29. package/skills/server-actions/SKILL.md +53 -41
  30. package/skills/testing/SKILL.md +599 -0
  31. package/skills/typesafety/SKILL.md +310 -26
  32. package/skills/use-cache/SKILL.md +34 -5
  33. package/skills/view-transitions/SKILL.md +294 -0
  34. package/src/__augment-tests__/augment.ts +81 -0
  35. package/src/__augment-tests__/augmented.check.ts +117 -0
  36. package/src/browser/action-coordinator.ts +53 -36
  37. package/src/browser/event-controller.ts +42 -66
  38. package/src/browser/history-state.ts +21 -0
  39. package/src/browser/index.ts +3 -3
  40. package/src/browser/navigation-bridge.ts +6 -6
  41. package/src/browser/navigation-client.ts +12 -15
  42. package/src/browser/navigation-store.ts +7 -8
  43. package/src/browser/navigation-transaction.ts +10 -28
  44. package/src/browser/partial-update.ts +9 -19
  45. package/src/browser/react/NavigationProvider.tsx +29 -40
  46. package/src/browser/react/index.ts +3 -0
  47. package/src/browser/react/location-state-shared.ts +175 -4
  48. package/src/browser/react/location-state.ts +39 -13
  49. package/src/browser/react/use-handle.ts +17 -9
  50. package/src/browser/react/use-params.ts +3 -4
  51. package/src/browser/react/use-reverse.ts +106 -0
  52. package/src/browser/react/use-router.ts +14 -1
  53. package/src/browser/response-adapter.ts +25 -0
  54. package/src/browser/rsc-router.tsx +30 -16
  55. package/src/browser/scroll-restoration.ts +22 -14
  56. package/src/browser/segment-structure-assert.ts +2 -2
  57. package/src/browser/server-action-bridge.ts +23 -30
  58. package/src/browser/types.ts +2 -0
  59. package/src/build/collect-fallback-refs.ts +107 -0
  60. package/src/build/generate-manifest.ts +60 -35
  61. package/src/build/generate-route-types.ts +2 -0
  62. package/src/build/index.ts +2 -0
  63. package/src/build/route-types/codegen.ts +4 -4
  64. package/src/build/route-types/include-resolution.ts +1 -1
  65. package/src/build/route-types/per-module-writer.ts +7 -4
  66. package/src/build/route-types/router-processing.ts +55 -14
  67. package/src/build/route-types/scan-filter.ts +1 -1
  68. package/src/build/route-types/source-scan.ts +118 -0
  69. package/src/build/runtime-discovery.ts +9 -20
  70. package/src/cache/cache-scope.ts +28 -42
  71. package/src/cache/cf/cf-cache-store.ts +49 -6
  72. package/src/client.rsc.tsx +3 -0
  73. package/src/client.tsx +10 -8
  74. package/src/context-var.ts +5 -5
  75. package/src/decode-loader-results.ts +36 -0
  76. package/src/errors.ts +30 -1
  77. package/src/handle.ts +26 -13
  78. package/src/host/index.ts +2 -2
  79. package/src/host/router.ts +129 -57
  80. package/src/host/types.ts +31 -2
  81. package/src/host/utils.ts +1 -1
  82. package/src/href-client.ts +140 -20
  83. package/src/index.rsc.ts +6 -4
  84. package/src/index.ts +13 -6
  85. package/src/loader-store.ts +500 -0
  86. package/src/loader.rsc.ts +2 -5
  87. package/src/loader.ts +3 -10
  88. package/src/missing-id-error.ts +68 -0
  89. package/src/prerender.ts +4 -4
  90. package/src/response-utils.ts +9 -0
  91. package/src/reverse.ts +65 -41
  92. package/src/route-content-wrapper.tsx +6 -28
  93. package/src/route-definition/dsl-helpers.ts +238 -263
  94. package/src/route-definition/helper-factories.ts +29 -139
  95. package/src/route-definition/helpers-types.ts +37 -14
  96. package/src/route-definition/use-item-types.ts +32 -0
  97. package/src/route-types.ts +19 -41
  98. package/src/router/basename.ts +14 -0
  99. package/src/router/content-negotiation.ts +15 -2
  100. package/src/router/error-handling.ts +1 -1
  101. package/src/router/handler-context.ts +4 -42
  102. package/src/router/intercept-resolution.ts +4 -18
  103. package/src/router/lazy-includes.ts +2 -2
  104. package/src/router/loader-resolution.ts +16 -2
  105. package/src/router/match-handlers.ts +62 -20
  106. package/src/router/match-middleware/cache-lookup.ts +44 -91
  107. package/src/router/match-middleware/cache-store.ts +3 -2
  108. package/src/router/match-result.ts +32 -30
  109. package/src/router/metrics.ts +1 -1
  110. package/src/router/middleware-types.ts +1 -1
  111. package/src/router/middleware.ts +46 -78
  112. package/src/router/prerender-match.ts +1 -1
  113. package/src/router/preview-match.ts +3 -1
  114. package/src/router/request-classification.ts +4 -28
  115. package/src/router/revalidation.ts +43 -1
  116. package/src/router/router-interfaces.ts +45 -28
  117. package/src/router/router-options.ts +40 -1
  118. package/src/router/router-registry.ts +2 -5
  119. package/src/router/segment-resolution/fresh.ts +19 -6
  120. package/src/router/segment-resolution/revalidation.ts +19 -6
  121. package/src/router/segment-resolution/view-transition-default.ts +36 -0
  122. package/src/router/substitute-pattern-params.ts +56 -0
  123. package/src/router/telemetry.ts +99 -0
  124. package/src/router/types.ts +8 -0
  125. package/src/router.ts +37 -21
  126. package/src/rsc/handler-context.ts +2 -2
  127. package/src/rsc/handler.ts +20 -65
  128. package/src/rsc/helpers.ts +22 -2
  129. package/src/rsc/index.ts +1 -1
  130. package/src/rsc/origin-guard.ts +28 -10
  131. package/src/rsc/response-route-handler.ts +32 -52
  132. package/src/rsc/rsc-rendering.ts +27 -53
  133. package/src/rsc/runtime-warnings.ts +9 -10
  134. package/src/rsc/server-action.ts +13 -37
  135. package/src/rsc/ssr-setup.ts +16 -0
  136. package/src/rsc/types.ts +2 -2
  137. package/src/search-params.ts +4 -4
  138. package/src/segment-system.tsx +121 -65
  139. package/src/serialize.ts +243 -0
  140. package/src/server/context.ts +118 -51
  141. package/src/server/cookie-store.ts +28 -4
  142. package/src/server/request-context.ts +10 -0
  143. package/src/static-handler.ts +1 -1
  144. package/src/testing/cache-status.ts +166 -0
  145. package/src/testing/collect-handle.ts +63 -0
  146. package/src/testing/dispatch.ts +440 -0
  147. package/src/testing/dom.entry.ts +22 -0
  148. package/src/testing/e2e/fixture.ts +154 -0
  149. package/src/testing/e2e/index.ts +149 -0
  150. package/src/testing/e2e/matchers.ts +51 -0
  151. package/src/testing/e2e/page-helpers.ts +272 -0
  152. package/src/testing/e2e/parity.ts +306 -0
  153. package/src/testing/e2e/server.ts +183 -0
  154. package/src/testing/flight-matchers.ts +104 -0
  155. package/src/testing/flight-runtime.d.ts +21 -0
  156. package/src/testing/flight.entry.ts +22 -0
  157. package/src/testing/flight.ts +182 -0
  158. package/src/testing/generated-routes.ts +223 -0
  159. package/src/testing/index.ts +105 -0
  160. package/src/testing/internal/context.ts +193 -0
  161. package/src/testing/render-route.tsx +536 -0
  162. package/src/testing/run-loader.ts +296 -0
  163. package/src/testing/run-middleware.ts +170 -0
  164. package/src/testing/vitest-stubs/cloudflare-email.ts +9 -0
  165. package/src/testing/vitest-stubs/cloudflare-workers.ts +21 -0
  166. package/src/testing/vitest-stubs/plugin-rsc.ts +16 -0
  167. package/src/testing/vitest-stubs/version.ts +5 -0
  168. package/src/testing/vitest.ts +183 -0
  169. package/src/types/global-namespace.ts +39 -26
  170. package/src/types/handler-context.ts +56 -11
  171. package/src/types/index.ts +1 -0
  172. package/src/types/segments.ts +18 -1
  173. package/src/urls/include-helper.ts +10 -53
  174. package/src/urls/index.ts +0 -3
  175. package/src/urls/path-helper-types.ts +11 -3
  176. package/src/urls/path-helper.ts +17 -52
  177. package/src/urls/pattern-types.ts +36 -19
  178. package/src/urls/response-types.ts +20 -19
  179. package/src/urls/type-extraction.ts +26 -116
  180. package/src/urls/urls-function.ts +1 -5
  181. package/src/use-loader.tsx +413 -42
  182. package/src/vite/debug.ts +1 -0
  183. package/src/vite/discovery/bundle-postprocess.ts +6 -6
  184. package/src/vite/discovery/discover-routers.ts +70 -48
  185. package/src/vite/discovery/discovery-errors.ts +194 -0
  186. package/src/vite/discovery/prerender-collection.ts +19 -25
  187. package/src/vite/discovery/route-types-writer.ts +40 -84
  188. package/src/vite/discovery/state.ts +33 -0
  189. package/src/vite/discovery/virtual-module-codegen.ts +13 -23
  190. package/src/vite/index.ts +2 -0
  191. package/src/vite/plugin-types.ts +67 -0
  192. package/src/vite/plugins/cjs-to-esm.ts +3 -7
  193. package/src/vite/plugins/client-ref-hashing.ts +12 -1
  194. package/src/vite/plugins/cloudflare-protocol-stub.ts +1 -1
  195. package/src/vite/plugins/expose-action-id.ts +2 -2
  196. package/src/vite/plugins/expose-id-utils.ts +12 -8
  197. package/src/vite/plugins/expose-ids/export-analysis.ts +100 -20
  198. package/src/vite/plugins/expose-ids/handler-transform.ts +8 -61
  199. package/src/vite/plugins/expose-ids/loader-transform.ts +3 -5
  200. package/src/vite/plugins/expose-internal-ids.ts +47 -67
  201. package/src/vite/plugins/performance-tracks.ts +12 -16
  202. package/src/vite/plugins/use-cache-transform.ts +13 -11
  203. package/src/vite/plugins/version-injector.ts +2 -12
  204. package/src/vite/plugins/version-plugin.ts +59 -2
  205. package/src/vite/plugins/virtual-entries.ts +2 -2
  206. package/src/vite/rango.ts +67 -15
  207. package/src/vite/router-discovery.ts +208 -63
  208. package/src/vite/utils/ast-handler-extract.ts +15 -15
  209. package/src/vite/utils/bundle-analysis.ts +4 -2
  210. package/src/vite/utils/client-chunks.ts +190 -0
  211. package/src/vite/utils/forward-user-plugins.ts +193 -0
  212. package/src/vite/utils/manifest-utils.ts +21 -5
  213. package/src/vite/utils/shared-utils.ts +107 -26
  214. package/src/browser/action-response-classifier.ts +0 -99
@@ -5,13 +5,15 @@
5
5
  * from discovered router manifests and static source parsing.
6
6
  */
7
7
 
8
- import { dirname, basename, join, resolve } from "node:path";
8
+ import { dirname, join, resolve } from "node:path";
9
9
  import { readFileSync, writeFileSync, existsSync, unlinkSync } from "node:fs";
10
10
  import {
11
11
  generateRouteTypesSource,
12
12
  writeCombinedRouteTypes,
13
13
  findRouterFiles,
14
14
  buildCombinedRouteMapForRouterFile,
15
+ genFileTsPath,
16
+ resolveSearchSchemas,
15
17
  } from "../../build/generate-route-types.js";
16
18
  import type { DiscoveryState } from "./state.js";
17
19
  import { markSelfGenWrite } from "./self-gen-tracking.js";
@@ -35,6 +37,22 @@ function filterUserNamedRoutes(
35
37
  return filtered;
36
38
  }
37
39
 
40
+ // Write a gen file only when content changed, marking the write as
41
+ // self-generated BEFORE writeFileSync so the watcher distinguishes it from a
42
+ // manual edit (the HMR self-gen-loop guard).
43
+ function writeGenFileIfChanged(
44
+ state: DiscoveryState,
45
+ outPath: string,
46
+ source: string,
47
+ opts?: { log?: boolean },
48
+ ): void {
49
+ const existing = existsSync(outPath) ? readFileSync(outPath, "utf-8") : null;
50
+ if (existing === source) return;
51
+ markSelfGenWrite(state, outPath, source);
52
+ writeFileSync(outPath, source);
53
+ if (opts?.log) console.log(`[rango] Generated route types -> ${outPath}`);
54
+ }
55
+
38
56
  /**
39
57
  * Write combined route types for all router files.
40
58
  * Only writes when content has changed to avoid triggering HMR loops.
@@ -48,45 +66,16 @@ export function writeCombinedRouteTypesWithTracking(
48
66
  findRouterFiles(state.projectRoot, state.scanFilter);
49
67
  state.cachedRouterFiles = routerFiles;
50
68
 
51
- // Snapshot pre-write content to detect which files actually change.
52
- const preContent = new Map<string, string>();
53
- for (const routerFilePath of routerFiles) {
54
- const routerDir = dirname(routerFilePath);
55
- const routerBasename = basename(routerFilePath).replace(
56
- /\.(tsx?|jsx?)$/,
57
- "",
58
- );
59
- const outPath = join(routerDir, `${routerBasename}.named-routes.gen.ts`);
60
- try {
61
- preContent.set(outPath, readFileSync(outPath, "utf-8"));
62
- } catch {
63
- // File doesn't exist yet — any write is a real change.
64
- }
65
- }
66
-
67
- writeCombinedRouteTypes(state.projectRoot, routerFiles, opts);
68
-
69
- // Mark only files that were actually written so the watcher can
70
- // distinguish self-triggered change events from manual edits.
71
- // Marking unchanged files creates stale entries that interfere with
72
- // multi-server setups (e.g. shared webServer + isolated HMR server).
73
- for (const routerFilePath of routerFiles) {
74
- const routerDir = dirname(routerFilePath);
75
- const routerBasename = basename(routerFilePath).replace(
76
- /\.(tsx?|jsx?)$/,
77
- "",
78
- );
79
- const outPath = join(routerDir, `${routerBasename}.named-routes.gen.ts`);
80
- if (!existsSync(outPath)) continue;
81
- try {
82
- const content = readFileSync(outPath, "utf-8");
83
- if (content !== preContent.get(outPath)) {
84
- markSelfGenWrite(state, outPath, content);
85
- }
86
- } catch {
87
- // Ignore transient fs errors while files are being rewritten.
88
- }
89
- }
69
+ // Mark each gen file as self-generated BEFORE it is written, via the onWrite
70
+ // callback fired at every writeFileSync site, so the watcher distinguishes
71
+ // self-triggered change events from manual edits. The callback fires only
72
+ // for files actually written, so unchanged files are never marked (stale
73
+ // entries interfere with multi-server setups such as a shared webServer plus
74
+ // an isolated HMR server).
75
+ writeCombinedRouteTypes(state.projectRoot, routerFiles, {
76
+ ...opts,
77
+ onWrite: (outPath, content) => markSelfGenWrite(state, outPath, content),
78
+ });
90
79
  }
91
80
 
92
81
  /**
@@ -104,7 +93,7 @@ export function writeRouteTypesFiles(state: DiscoveryState): void {
104
93
  if (existsSync(oldCombinedPath)) {
105
94
  unlinkSync(oldCombinedPath);
106
95
  console.log(
107
- `[rsc-router] Removed stale combined route types: ${oldCombinedPath}`,
96
+ `[rango] Removed stale combined route types: ${oldCombinedPath}`,
108
97
  );
109
98
  }
110
99
  } catch {}
@@ -122,40 +111,22 @@ export function writeRouteTypesFiles(state: DiscoveryState): void {
122
111
  // the wrong location, causing non-deterministic type resolution.
123
112
  if (sourceFile.includes("node_modules")) {
124
113
  throw new Error(
125
- `[rsc-router] Router "${id}" has sourceFile inside node_modules: ${sourceFile}\n` +
114
+ `[rango] Router "${id}" has sourceFile inside node_modules: ${sourceFile}\n` +
126
115
  `This means createRouter() stack trace parsing matched a Vite internal frame.\n` +
127
116
  `Set an explicit \`id\` on createRouter() or check the call site.`,
128
117
  );
129
118
  }
130
119
 
131
- const routerDir = dirname(sourceFile);
132
- const routerBasename = basename(sourceFile).replace(/\.(tsx?|jsx?)$/, "");
133
- const outPath = join(routerDir, `${routerBasename}.named-routes.gen.ts`);
120
+ const outPath = genFileTsPath(sourceFile);
134
121
 
135
122
  // Filter out auto-generated route names (e.g. "$path____debug_reverse-test")
136
123
  // to match the static parser's output and prevent HMR oscillation.
137
124
  const userRoutes = filterUserNamedRoutes(routeManifest);
138
- let effectiveSearchSchemas = routeSearchSchemas;
139
-
140
- // Runtime manifest may omit search schema metadata in some module-runner
141
- // flows. Fall back to static source parsing from the router file.
142
- if (
143
- (!effectiveSearchSchemas ||
144
- Object.keys(effectiveSearchSchemas).length === 0) &&
145
- sourceFile
146
- ) {
147
- const staticParsed = buildCombinedRouteMapForRouterFile(sourceFile);
148
- if (Object.keys(staticParsed.searchSchemas).length > 0) {
149
- const filtered: Record<string, Record<string, string>> = {};
150
- for (const name of Object.keys(userRoutes)) {
151
- const schema = staticParsed.searchSchemas[name];
152
- if (schema) filtered[name] = schema;
153
- }
154
- if (Object.keys(filtered).length > 0) {
155
- effectiveSearchSchemas = filtered;
156
- }
157
- }
158
- }
125
+ const effectiveSearchSchemas = resolveSearchSchemas(
126
+ Object.keys(userRoutes),
127
+ routeSearchSchemas,
128
+ sourceFile,
129
+ );
159
130
 
160
131
  const source = generateRouteTypesSource(
161
132
  userRoutes,
@@ -163,14 +134,7 @@ export function writeRouteTypesFiles(state: DiscoveryState): void {
163
134
  ? effectiveSearchSchemas
164
135
  : undefined,
165
136
  );
166
- const existing = existsSync(outPath)
167
- ? readFileSync(outPath, "utf-8")
168
- : null;
169
- if (existing !== source) {
170
- markSelfGenWrite(state, outPath, source);
171
- writeFileSync(outPath, source);
172
- console.log(`[rsc-router] Generated route types -> ${outPath}`);
173
- }
137
+ writeGenFileIfChanged(state, outPath, source, { log: true });
174
138
  }
175
139
  }
176
140
 
@@ -236,22 +200,14 @@ export function supplementGenFilesWithRuntimeRoutes(
236
200
  }
237
201
  }
238
202
 
239
- const routerDir = dirname(sourceFile);
240
- const routerBasename = basename(sourceFile).replace(/\.(tsx?|jsx?)$/, "");
241
- const outPath = join(routerDir, `${routerBasename}.named-routes.gen.ts`);
203
+ const outPath = genFileTsPath(sourceFile);
242
204
  const source = generateRouteTypesSource(
243
205
  mergedRoutes,
244
206
  Object.keys(mergedSearchSchemas).length > 0
245
207
  ? mergedSearchSchemas
246
208
  : undefined,
247
209
  );
248
- const existing = existsSync(outPath)
249
- ? readFileSync(outPath, "utf-8")
250
- : null;
251
- if (existing !== source) {
252
- markSelfGenWrite(state, outPath, source);
253
- writeFileSync(outPath, source);
254
- }
210
+ writeGenFileIfChanged(state, outPath, source);
255
211
  }
256
212
  // No manual manifest update needed: the virtual module imports the gen
257
213
  // file, so Vite's HMR automatically re-evaluates it with fresh data.
@@ -20,6 +20,13 @@ export interface PluginOptions {
20
20
  buildEnv?: import("../plugin-types.js").BuildEnvOption;
21
21
  /** Deployment preset (needed for buildEnv "auto" resolution). */
22
22
  preset?: "node" | "cloudflare";
23
+ /**
24
+ * Shared context the built-in clientChunks strategy reads. Discovery populates
25
+ * it (registered fallback hashes + single-router name) before the client build
26
+ * invokes the strategy. Present only when the built-in strategy is active
27
+ * (`clientChunks: true`/default); undefined for `false` or a custom function.
28
+ */
29
+ clientChunkCtx?: import("../utils/client-chunks.js").ClientChunkContext;
23
30
  }
24
31
 
25
32
  export interface PrecomputedEntry {
@@ -45,6 +52,21 @@ export interface DiscoveryState {
45
52
  projectRoot: string;
46
53
  isBuildMode: boolean;
47
54
  userResolveAlias: any;
55
+ /**
56
+ * Data-only slice of the user's resolved config (resolve.* incl. native
57
+ * tsconfigPaths, define, oxc) mirrored into the discovery temp server so it
58
+ * resolves and transforms modules the same way the real environment does.
59
+ * See `utils/forward-user-plugins.ts`.
60
+ */
61
+ userRunnerConfig:
62
+ | import("../utils/forward-user-plugins.js").ForwardedRunnerConfig
63
+ | undefined;
64
+ /**
65
+ * User resolution plugins (resolveId/load), stripped to their resolution
66
+ * surface, forwarded into the discovery temp server. Lets third-party
67
+ * resolvers such as vite-tsconfig-paths participate in discovery.
68
+ */
69
+ userResolvePlugins: import("vite").Plugin[];
48
70
  scanFilter: ScanFilter | undefined;
49
71
  cachedRouterFiles: string[] | undefined;
50
72
  opts: PluginOptions | undefined;
@@ -76,6 +98,14 @@ export interface DiscoveryState {
76
98
  resolvedBuildEnv?: Record<string, unknown>;
77
99
  /** Cleanup function for build-time env resources (e.g., miniflare). */
78
100
  buildEnvDispose?: (() => Promise<void> | void) | null;
101
+
102
+ /**
103
+ * Set when the most recent HMR re-discovery threw. Cleared on the next
104
+ * successful discovery. Surfaced via debug logs so we can detect "manifest
105
+ * frozen at last-good after error → user fix in non-route file → no
106
+ * rediscovery trigger" scenarios.
107
+ */
108
+ lastDiscoveryError?: { message: string; at: number } | null;
79
109
  }
80
110
 
81
111
  export function createDiscoveryState(
@@ -87,6 +117,8 @@ export function createDiscoveryState(
87
117
  projectRoot: "",
88
118
  isBuildMode: false,
89
119
  userResolveAlias: undefined,
120
+ userRunnerConfig: undefined,
121
+ userResolvePlugins: [],
90
122
  scanFilter: undefined,
91
123
  cachedRouterFiles: undefined,
92
124
  opts,
@@ -113,5 +145,6 @@ export function createDiscoveryState(
113
145
  devServer: null,
114
146
  selfWrittenGenFiles: new Map(),
115
147
  SELF_WRITE_WINDOW_MS: 5_000,
148
+ lastDiscoveryError: null,
116
149
  };
117
150
  }
@@ -58,7 +58,7 @@ export function generateRoutesManifestModule(state: DiscoveryState): string {
58
58
  }
59
59
 
60
60
  const lines = [
61
- `import { setCachedManifest, setPrecomputedEntries, setRouteTrie, setRouterManifest, registerRouterManifestLoader, clearAllRouterData } from "@rangojs/router/server";`,
61
+ `import { setCachedManifest, setRouterManifest, registerRouterManifestLoader, clearAllRouterData } from "@rangojs/router/server";`,
62
62
  ...genFileImports,
63
63
  // Clear stale per-router cached data (manifest, trie, precomputed entries)
64
64
  // before re-populating. In Cloudflare dev mode, program reloads re-evaluate
@@ -101,28 +101,18 @@ export function generateRoutesManifestModule(state: DiscoveryState): string {
101
101
  }
102
102
  }
103
103
 
104
- // In dev mode, skip trie and precomputed entries injection. These are
105
- // computed once during initial discovery and become stale after route
106
- // changes. A stale trie would incorrectly match removed routes. The
107
- // handler falls back to Phase 2 regex matching against the live
108
- // router.urlpatterns, which is always correct after a program reload.
109
- // In build mode, the trie is always fresh (built from the final route
110
- // tree) so it's safe to inject.
111
- if (state.isBuildMode) {
112
- if (
113
- state.mergedPrecomputedEntries &&
114
- state.mergedPrecomputedEntries.length > 0
115
- ) {
116
- lines.push(
117
- `setPrecomputedEntries(${jsonParseExpression(state.mergedPrecomputedEntries)});`,
118
- );
119
- }
120
- if (state.mergedRouteTrie) {
121
- lines.push(
122
- `setRouteTrie(${jsonParseExpression(state.mergedRouteTrie)});`,
123
- );
124
- }
125
- }
104
+ // Per-router trie and precomputedEntries are NOT inlined eagerly.
105
+ // They live in the per-router lazy chunks (generatePerRouterModule) and
106
+ // are loaded via ensureRouterManifest(routerId), which is awaited before
107
+ // every request in router.fetch() and before findMatch is reached.
108
+ // Inlining the merged versions here would duplicate the per-router data
109
+ // (the merged trie/precomputedEntries equal the per-router data for
110
+ // single-router apps; for multi-router, the merged trie is dead code
111
+ // because find-match.ts only consumes per-router tries).
112
+ //
113
+ // In dev mode, the handler also falls back to Phase 2 regex matching
114
+ // against live router.urlpatterns, which is always correct after a
115
+ // program reload.
126
116
 
127
117
  // Register lazy loaders for per-router manifest modules.
128
118
  // Each import() uses a static string literal so Rollup creates separate chunks.
package/src/vite/index.ts CHANGED
@@ -13,6 +13,8 @@ export type {
13
13
  RangoNodeOptions,
14
14
  RangoCloudflareOptions,
15
15
  RangoOptions,
16
+ ClientChunks,
17
+ ClientChunkMeta,
16
18
  BuildEnvOption,
17
19
  BuildEnvFactory,
18
20
  BuildEnvFactoryContext,
@@ -47,6 +47,64 @@ export type BuildEnvOption =
47
47
  | Record<string, unknown>
48
48
  | BuildEnvFactory;
49
49
 
50
+ // -- Client chunking --------------------------------------------------------
51
+
52
+ /**
53
+ * Metadata for one client ("use client") module, passed to a {@link ClientChunks}
54
+ * function. Mirrors the shape `@vitejs/plugin-rsc` passes to its own
55
+ * `clientChunks` option.
56
+ */
57
+ export interface ClientChunkMeta {
58
+ /** Absolute module id of the "use client" file. */
59
+ id: string;
60
+ /** Normalized (posix) module id — convenient for path-based matching. */
61
+ normalizedId: string;
62
+ /**
63
+ * The RSC/server chunk that statically imports this client reference. This is
64
+ * the key used for the default grouping when no override is supplied: a single
65
+ * router that statically imports every route yields ONE `serverChunk`, hence
66
+ * one client chunk for all routes.
67
+ */
68
+ serverChunk: string;
69
+ }
70
+
71
+ /**
72
+ * Controls how client ("use client") components are grouped into browser
73
+ * chunks, i.e. per-route / per-feature code splitting of the client bundle.
74
+ *
75
+ * Without splitting, a single router ships ONE client chunk containing every
76
+ * route's client components (and their CSS) — navigating to one route downloads
77
+ * every other route's client code. (Host sub-apps loaded via a dynamic `import()`
78
+ * are the exception: each forms its own chunk.) This option controls how that
79
+ * monolith is split.
80
+ *
81
+ * Behavior branches:
82
+ * - `true` / omitted (**default**, pre-1.0): Rango's built-in **directory
83
+ * strategy**. It splits app `"use client"` modules by **route id** — the segment
84
+ * after a route-root directory (`routes`, `app`, `pages`, `features`, `handlers`,
85
+ * …) — so `routes/dashboard/**` becomes `app-dashboard` at any nesting depth.
86
+ * Where it finds NO route structure (a flat `src/components/`, or host sub-apps
87
+ * already split by a dynamic `import()`), it inherits the default grouping
88
+ * unchanged — so the shared `src/components` chunk stays shared and host apps do
89
+ * not leak across each other. Shared runtime (React, the router, `node_modules`)
90
+ * is never split.
91
+ * - `false`: opt out — inherit `@vitejs/plugin-rsc`'s default grouping everywhere
92
+ * (one chunk per router / per host sub-app).
93
+ * - function: full override. Return a chunk group name, or `undefined` to fall
94
+ * back to the default grouping for that one module. Forwarded directly to
95
+ * `@vitejs/plugin-rsc`'s `clientChunks`.
96
+ *
97
+ * Every module maps to exactly one group, so there is no byte duplication: a
98
+ * component used by two routes lives in one group and is fetched whenever it
99
+ * renders. Put genuinely shared client components OUTSIDE route directories so
100
+ * they land in the shared group rather than one route's chunk.
101
+ *
102
+ * @default true
103
+ */
104
+ export type ClientChunks =
105
+ | boolean
106
+ | ((meta: ClientChunkMeta) => string | undefined);
107
+
50
108
  // -- Plugin options ---------------------------------------------------------
51
109
 
52
110
  /**
@@ -59,6 +117,15 @@ interface RangoBaseOptions {
59
117
  */
60
118
  banner?: boolean;
61
119
 
120
+ /**
121
+ * Group client ("use client") components into browser chunks for per-route /
122
+ * per-feature code splitting. On by default (pre-1.0); pass `false` to opt out.
123
+ * See {@link ClientChunks}.
124
+ *
125
+ * @default true
126
+ */
127
+ clientChunks?: ClientChunks;
128
+
62
129
  /**
63
130
  * Environment bindings available to Prerender and Static handlers at build
64
131
  * time via `ctx.env`. Applies to both production build and dev on-demand
@@ -12,13 +12,10 @@ export function createCjsToEsmPlugin(): Plugin {
12
12
  name: "@rangojs/router:cjs-to-esm",
13
13
  enforce: "pre",
14
14
  transform(code, id) {
15
- const cleanId = id.split("?")[0];
15
+ const cleanId = id.split("?")[0].replaceAll("\\", "/");
16
16
 
17
17
  // Transform the client.browser.js entry point to re-export from CJS
18
- if (
19
- cleanId.includes("vendor/react-server-dom/client.browser.js") ||
20
- cleanId.includes("vendor\\react-server-dom\\client.browser.js")
21
- ) {
18
+ if (cleanId.includes("vendor/react-server-dom/client.browser.js")) {
22
19
  const isProd = process.env.NODE_ENV === "production";
23
20
  const cjsFile = isProd
24
21
  ? "./cjs/react-server-dom-webpack-client.browser.production.js"
@@ -33,8 +30,7 @@ export function createCjsToEsmPlugin(): Plugin {
33
30
 
34
31
  // Transform the actual CJS files to ESM
35
32
  if (
36
- (cleanId.includes("vendor/react-server-dom/cjs/") ||
37
- cleanId.includes("vendor\\react-server-dom\\cjs\\")) &&
33
+ cleanId.includes("vendor/react-server-dom/cjs/") &&
38
34
  cleanId.includes("client.browser")
39
35
  ) {
40
36
  let transformed = code;
@@ -22,6 +22,17 @@ const FS_PREFIX = "/@fs/";
22
22
  * Returns the input unchanged if it doesn't match a known dev-mode pattern
23
23
  * (e.g., already a production hash).
24
24
  */
25
+ /**
26
+ * The production client-reference key hash: `sha256(relativeId).slice(0,12)`,
27
+ * matching @vitejs/plugin-rsc's `hashString`. Exported so the client-chunks
28
+ * strategy can hash a `clientChunks` callback's `meta.normalizedId` (already the
29
+ * project-root-relative id) and compare it against fallback hashes collected
30
+ * during discovery.
31
+ */
32
+ export function hashRefKey(relativeId: string): string {
33
+ return createHash("sha256").update(relativeId).digest("hex").slice(0, 12);
34
+ }
35
+
25
36
  export function computeProductionHash(
26
37
  projectRoot: string,
27
38
  refKey: string,
@@ -49,7 +60,7 @@ export function computeProductionHash(
49
60
  return refKey;
50
61
  }
51
62
 
52
- return createHash("sha256").update(toHash).digest("hex").slice(0, 12);
63
+ return hashRefKey(toHash);
53
64
  }
54
65
 
55
66
  // Regex to match registerClientReference() calls as emitted by @vitejs/plugin-rsc.
@@ -139,7 +139,7 @@ export function createCloudflareProtocolStubPlugin(): Plugin {
139
139
 
140
140
  let ast: AstNode;
141
141
  try {
142
- ast = this.parse(code) as unknown as AstNode;
142
+ ast = this.parse(code, { lang: "tsx" }) as unknown as AstNode;
143
143
  } catch {
144
144
  // Malformed source — let a downstream plugin surface the parse error.
145
145
  return null;
@@ -42,7 +42,7 @@ function getRscPluginApi(config: ResolvedConfig): RscPluginApi | undefined {
42
42
  );
43
43
  if (plugin) {
44
44
  console.warn(
45
- `[rsc-router:expose-action-id] RSC plugin found by API structure (name: "${plugin.name}"). ` +
45
+ `[rango:expose-action-id] RSC plugin found by API structure (name: "${plugin.name}"). ` +
46
46
  `Consider updating the name lookup if the plugin was renamed.`,
47
47
  );
48
48
  }
@@ -287,7 +287,7 @@ export function exposeActionId(): Plugin {
287
287
 
288
288
  if (!rscPluginApi) {
289
289
  throw new Error(
290
- "[rsc-router] Could not find @vitejs/plugin-rsc. " +
290
+ "[rango] Could not find @vitejs/plugin-rsc. " +
291
291
  "@rangojs/router requires the Vite RSC plugin, which is included automatically by rango().",
292
292
  );
293
293
  }
@@ -32,18 +32,22 @@ export function makeStubId(
32
32
  }
33
33
 
34
34
  /**
35
- * Generate an 8-char hex hash for an inline static handler call site.
36
- * Uses file path and line number (plus optional index for same-line collisions).
35
+ * Generate an 8-char hex hash for an inline handler call site.
36
+ *
37
+ * Keyed on the source-order INDEX of the call (the Nth inline `fnName(...)` in
38
+ * the file), NOT its line number. Line numbers shift between the prerender
39
+ * build context and the production build context (preceding transforms differ,
40
+ * e.g. plugin-react boilerplate), which would desync the prerender manifest key
41
+ * from the runtime handler id and break prerender/static freezing. The
42
+ * source-order index is invariant to line shifts; `fnName` keeps Static and
43
+ * Prerender inline ids from colliding at the same index.
37
44
  */
38
45
  export function hashInlineId(
39
46
  filePath: string,
40
- lineNumber: number,
41
- index?: number,
47
+ fnName: string,
48
+ index: number,
42
49
  ): string {
43
- const input =
44
- index !== undefined && index > 0
45
- ? `${filePath}:${lineNumber}:${index}`
46
- : `${filePath}:${lineNumber}`;
50
+ const input = `${filePath}:${fnName}:${index}`;
47
51
  return crypto.createHash("sha256").update(input).digest("hex").slice(0, 8);
48
52
  }
49
53