@rangojs/router 0.20.0 → 0.22.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 (331) hide show
  1. package/dist/bin/rango.js +39 -15
  2. package/dist/types/browser/event-controller.d.ts +49 -0
  3. package/dist/types/browser/history-state.d.ts +16 -4
  4. package/dist/types/browser/navigation-store.d.ts +9 -0
  5. package/dist/types/browser/navigation-transaction.d.ts +7 -0
  6. package/dist/types/browser/partial-update.d.ts +2 -1
  7. package/dist/types/browser/prefetch/default-strategy.d.ts +16 -20
  8. package/dist/types/browser/rango-state.d.ts +3 -1
  9. package/dist/types/browser/react/NavigationProvider.d.ts +14 -1
  10. package/dist/types/browser/react/context.d.ts +25 -0
  11. package/dist/types/browser/react/location-state-shared.d.ts +84 -10
  12. package/dist/types/browser/react/location-state.d.ts +7 -2
  13. package/dist/types/browser/rsc-router.d.ts +13 -2
  14. package/dist/types/browser/types.d.ts +10 -0
  15. package/dist/types/build/generate-manifest.d.ts +2 -0
  16. package/dist/types/cache/background-task.d.ts +22 -0
  17. package/dist/types/cache/cache-key-utils.d.ts +50 -5
  18. package/dist/types/cache/cache-policy.d.ts +17 -0
  19. package/dist/types/cache/cache-scope.d.ts +5 -6
  20. package/dist/types/cache/cf/cf-cache-store.d.ts +34 -7
  21. package/dist/types/cache/cf/cf-cache-types.d.ts +12 -4
  22. package/dist/types/cache/document-cache.d.ts +8 -5
  23. package/dist/types/cache/handle-snapshot.d.ts +59 -50
  24. package/dist/types/cache/index.d.ts +3 -1
  25. package/dist/types/cache/invalidation-order.d.ts +14 -0
  26. package/dist/types/cache/memory-segment-store.d.ts +33 -1
  27. package/dist/types/cache/shell-snapshot.d.ts +29 -4
  28. package/dist/types/cache/store-scope.d.ts +22 -0
  29. package/dist/types/cache/tag-invalidation.d.ts +7 -6
  30. package/dist/types/cache/types.d.ts +84 -24
  31. package/dist/types/cache/vercel/vercel-cache-store.d.ts +35 -17
  32. package/dist/types/client-urls/navigation.d.ts +7 -1
  33. package/dist/types/client-urls/optimistic-location.d.ts +10 -4
  34. package/dist/types/errors.d.ts +6 -0
  35. package/dist/types/handles/deferred-resolution.d.ts +18 -1
  36. package/dist/types/internal-suspense-audit.d.ts +1 -0
  37. package/dist/types/loader-store.d.ts +4 -8
  38. package/dist/types/outlet-provider.d.ts +2 -1
  39. package/dist/types/prerender/build-shell-capture.d.ts +8 -6
  40. package/dist/types/prerender/cloudflare.d.ts +49 -0
  41. package/dist/types/prerender/create-prerender-trigger.d.ts +121 -0
  42. package/dist/types/prerender/index.d.ts +12 -0
  43. package/dist/types/prerender/memory-prerender-store.d.ts +29 -0
  44. package/dist/types/prerender/on-demand.d.ts +272 -0
  45. package/dist/types/prerender/producer-guard.d.ts +33 -0
  46. package/dist/types/prerender/shell-manifest-key.d.ts +1 -18
  47. package/dist/types/prerender/warm-request.d.ts +65 -0
  48. package/dist/types/prerender/warm.d.ts +65 -0
  49. package/dist/types/prerender/writable-store.d.ts +170 -0
  50. package/dist/types/prerender.d.ts +27 -0
  51. package/dist/types/reverse.d.ts +1 -1
  52. package/dist/types/route-content-wrapper.d.ts +3 -1
  53. package/dist/types/router/error-handling.d.ts +2 -0
  54. package/dist/types/router/handler-context.d.ts +1 -1
  55. package/dist/types/router/match-middleware/cache-lookup.d.ts +15 -1
  56. package/dist/types/router/navigation-snapshot.d.ts +8 -1
  57. package/dist/types/router/pattern-matching.d.ts +2 -0
  58. package/dist/types/router/prefetch-default.d.ts +4 -4
  59. package/dist/types/router/prerender-match.d.ts +23 -1
  60. package/dist/types/router/route-trie-builder.d.ts +8 -1
  61. package/dist/types/router/router-context.d.ts +1 -0
  62. package/dist/types/router/router-interfaces.d.ts +32 -1
  63. package/dist/types/router/router-options.d.ts +39 -5
  64. package/dist/types/router/segment-resolution/fresh.d.ts +7 -0
  65. package/dist/types/router/segment-resolution/loader-cache.d.ts +23 -10
  66. package/dist/types/router/segment-resolution/revalidation.d.ts +5 -1
  67. package/dist/types/router/segment-resolution/streamed-handler-boundary.d.ts +43 -0
  68. package/dist/types/router/trie-matching.d.ts +2 -0
  69. package/dist/types/router-versions.d.ts +39 -0
  70. package/dist/types/rsc/handler-context.d.ts +5 -0
  71. package/dist/types/rsc/shell-build-manifest.d.ts +9 -6
  72. package/dist/types/rsc/shell-capture-constants.d.ts +20 -5
  73. package/dist/types/rsc/shell-capture.d.ts +14 -1
  74. package/dist/types/rsc/shell-serve.d.ts +2 -2
  75. package/dist/types/rsc/ssr-module-loader.d.ts +15 -0
  76. package/dist/types/rsc/types.d.ts +12 -6
  77. package/dist/types/segment-boundary-content.d.ts +17 -0
  78. package/dist/types/segment-loader-promise.d.ts +2 -6
  79. package/dist/types/server/build-version-table.d.ts +53 -0
  80. package/dist/types/server/context.d.ts +12 -4
  81. package/dist/types/server/handle-store.d.ts +89 -46
  82. package/dist/types/server/request-context.d.ts +84 -9
  83. package/dist/types/suspense-audit-react.d.ts +26 -0
  84. package/dist/types/suspense-audit.d.ts +157 -0
  85. package/dist/types/testing/action-form.d.ts +34 -0
  86. package/dist/types/testing/build-versions.d.ts +32 -0
  87. package/dist/types/testing/dom.entry.d.ts +1 -1
  88. package/dist/types/testing/flight.entry.d.ts +3 -1
  89. package/dist/types/testing/index.d.ts +6 -0
  90. package/dist/types/testing/internal/seed-vars.d.ts +7 -2
  91. package/dist/types/testing/internal/server-modules.d.ts +8 -0
  92. package/dist/types/testing/location-state-key.d.ts +5 -1
  93. package/dist/types/testing/render-route.d.ts +77 -6
  94. package/dist/types/testing/serve-shell-request.d.ts +55 -4
  95. package/dist/types/testing/shell-status.d.ts +14 -6
  96. package/dist/types/testing/vitest-stubs/plugin-rsc.d.ts +9 -6
  97. package/dist/types/testing/vitest-stubs/version.d.ts +1 -0
  98. package/dist/types/thenable-status.d.ts +6 -0
  99. package/dist/types/types/handler-context.d.ts +15 -5
  100. package/dist/types/types/route-entry.d.ts +5 -0
  101. package/dist/types/urls/path-helper.d.ts +18 -0
  102. package/dist/types/vite/discovery/build-versions.d.ts +393 -0
  103. package/dist/types/vite/discovery/installed-packages.d.ts +26 -0
  104. package/dist/types/vite/discovery/router-versions-phase.d.ts +52 -0
  105. package/dist/types/vite/discovery/shell-prerender-phase.d.ts +7 -17
  106. package/dist/types/vite/discovery/state.d.ts +25 -0
  107. package/dist/types/vite/encryption-key.d.ts +18 -0
  108. package/dist/types/vite/inject-client-debug.d.ts +6 -25
  109. package/dist/types/vite/plugin-types.d.ts +27 -0
  110. package/dist/types/vite/plugins/{version-injector.d.ts → entry-bootstrap-injector.d.ts} +4 -6
  111. package/dist/types/vite/plugins/expose-ids/export-analysis.d.ts +1 -0
  112. package/dist/types/vite/plugins/expose-ids/router-transform.d.ts +14 -0
  113. package/dist/types/vite/plugins/stable-rsc-output.d.ts +51 -0
  114. package/dist/types/vite/plugins/version-plugin.d.ts +9 -11
  115. package/dist/types/vite/plugins/virtual-entries.d.ts +18 -1
  116. package/dist/types/vite/router-discovery.d.ts +6 -0
  117. package/dist/types/vite/utils/bundle-analysis.d.ts +6 -2
  118. package/dist/types/vite/utils/client-chunks.d.ts +2 -0
  119. package/dist/types/vite/utils/compare-strings.d.ts +7 -0
  120. package/dist/types/vite/utils/package-resolution.d.ts +8 -0
  121. package/dist/types/vite/utils/prerender-utils.d.ts +2 -0
  122. package/dist/vite/index.js +1707 -931
  123. package/package.json +12 -3
  124. package/skills/cache-guide/SKILL.md +102 -37
  125. package/skills/caching/SKILL.md +56 -9
  126. package/skills/catalog.json +1 -1
  127. package/skills/client-urls/SKILL.md +16 -0
  128. package/skills/cloudflare/SKILL.md +133 -1
  129. package/skills/comparison/references/framework-comparison.md +17 -13
  130. package/skills/deployment-caching/SKILL.md +33 -12
  131. package/skills/document-cache/SKILL.md +16 -14
  132. package/skills/hooks/SKILL.md +25 -0
  133. package/skills/hooks/handle-and-actions.md +5 -0
  134. package/skills/hooks/navigation.md +38 -0
  135. package/skills/hooks/state.md +213 -11
  136. package/skills/hooks/urls.md +40 -1
  137. package/skills/host-router/SKILL.md +31 -0
  138. package/skills/intercept/SKILL.md +6 -0
  139. package/skills/loader/SKILL.md +25 -5
  140. package/skills/middleware/SKILL.md +2 -0
  141. package/skills/migrate-nextjs/SKILL.md +82 -9
  142. package/skills/ppr/SKILL.md +150 -48
  143. package/skills/prerender/SKILL.md +651 -10
  144. package/skills/rango/SKILL.md +9 -7
  145. package/skills/response-routes/SKILL.md +5 -5
  146. package/skills/route/SKILL.md +29 -0
  147. package/skills/router-setup/SKILL.md +15 -1
  148. package/skills/testing/SKILL.md +32 -27
  149. package/skills/testing/cache-prerender.md +18 -17
  150. package/skills/testing/client-components.md +193 -8
  151. package/skills/testing/handles.md +2 -0
  152. package/skills/testing/loader.md +1 -1
  153. package/skills/testing/middleware.md +1 -1
  154. package/skills/testing/render-handler.md +18 -18
  155. package/skills/testing/serve-shell-request.md +167 -22
  156. package/skills/testing/server-actions.md +17 -17
  157. package/skills/testing/server-tree.md +1 -1
  158. package/skills/typesafety/generated-files-and-cli.md +1 -1
  159. package/skills/use-cache/SKILL.md +25 -10
  160. package/skills/vercel/SKILL.md +5 -3
  161. package/skills/view-transitions/SKILL.md +9 -4
  162. package/src/bin/rango.ts +1 -1
  163. package/src/browser/event-controller.ts +119 -2
  164. package/src/browser/history-state.ts +39 -17
  165. package/src/browser/link-interceptor.ts +2 -2
  166. package/src/browser/navigation-bridge.ts +104 -41
  167. package/src/browser/navigation-store.ts +26 -0
  168. package/src/browser/navigation-transaction.ts +24 -12
  169. package/src/browser/network-error-handler.ts +2 -0
  170. package/src/browser/partial-update.ts +58 -14
  171. package/src/browser/prefetch/default-strategy.ts +22 -42
  172. package/src/browser/rango-state.ts +3 -1
  173. package/src/browser/react/Link.tsx +37 -43
  174. package/src/browser/react/NavigationProvider.tsx +85 -1
  175. package/src/browser/react/context.ts +30 -0
  176. package/src/browser/react/location-state-shared.ts +200 -34
  177. package/src/browser/react/location-state.ts +28 -77
  178. package/src/browser/react/use-action.ts +4 -1
  179. package/src/browser/react/use-handle.ts +3 -0
  180. package/src/browser/react/use-link-status.ts +4 -3
  181. package/src/browser/react/use-navigation.ts +4 -1
  182. package/src/browser/react/use-params.ts +4 -1
  183. package/src/browser/react/use-pathname.ts +4 -1
  184. package/src/browser/react/use-search-params.ts +5 -3
  185. package/src/browser/react/use-segments.ts +36 -54
  186. package/src/browser/rsc-router.tsx +93 -31
  187. package/src/browser/segment-reconciler.ts +3 -3
  188. package/src/browser/server-action-bridge.ts +43 -7
  189. package/src/browser/types.ts +11 -0
  190. package/src/build/generate-manifest.ts +17 -0
  191. package/src/build/merge-full-manifests.ts +5 -0
  192. package/src/build/route-trie.ts +1 -0
  193. package/src/cache/background-task.ts +61 -0
  194. package/src/cache/cache-key-utils.ts +91 -12
  195. package/src/cache/cache-policy.ts +11 -2
  196. package/src/cache/cache-runtime.ts +23 -23
  197. package/src/cache/cache-scope.ts +48 -36
  198. package/src/cache/cf/cf-cache-store.ts +91 -66
  199. package/src/cache/cf/cf-cache-types.ts +12 -4
  200. package/src/cache/document-cache.ts +35 -14
  201. package/src/cache/handle-snapshot.ts +118 -86
  202. package/src/cache/index.ts +5 -0
  203. package/src/cache/invalidation-order.ts +53 -0
  204. package/src/cache/memory-segment-store.ts +52 -1
  205. package/src/cache/shell-snapshot.ts +52 -5
  206. package/src/cache/store-scope.ts +36 -0
  207. package/src/cache/tag-invalidation.ts +7 -5
  208. package/src/cache/types.ts +90 -24
  209. package/src/cache/vercel/vercel-cache-store.ts +68 -26
  210. package/src/client-urls/client-root.tsx +1 -0
  211. package/src/client-urls/navigation.ts +10 -0
  212. package/src/client-urls/optimistic-location.ts +10 -4
  213. package/src/client.tsx +40 -3
  214. package/src/errors.ts +13 -6
  215. package/src/handles/deferred-resolution.ts +36 -0
  216. package/src/internal-suspense-audit.ts +12 -0
  217. package/src/loader-store.ts +4 -9
  218. package/src/outlet-provider.tsx +25 -1
  219. package/src/prerender/build-shell-capture.ts +11 -9
  220. package/src/prerender/cloudflare.ts +130 -0
  221. package/src/prerender/create-prerender-trigger.ts +780 -0
  222. package/src/prerender/index.ts +39 -0
  223. package/src/prerender/memory-prerender-store.ts +115 -0
  224. package/src/prerender/on-demand.ts +339 -0
  225. package/src/prerender/producer-guard.ts +66 -0
  226. package/src/prerender/shell-manifest-key.ts +12 -17
  227. package/src/prerender/warm-request.ts +154 -0
  228. package/src/prerender/warm.ts +193 -0
  229. package/src/prerender/writable-store.ts +278 -0
  230. package/src/prerender.ts +29 -0
  231. package/src/reverse.ts +1 -1
  232. package/src/route-content-wrapper.tsx +107 -13
  233. package/src/router/error-handling.ts +12 -12
  234. package/src/router/find-match.ts +1 -0
  235. package/src/router/handler-context.ts +2 -0
  236. package/src/router/intercept-resolution.ts +15 -8
  237. package/src/router/loader-resolution.ts +2 -10
  238. package/src/router/manifest.ts +2 -0
  239. package/src/router/match-middleware/background-revalidation.ts +2 -0
  240. package/src/router/match-middleware/cache-lookup.ts +239 -54
  241. package/src/router/navigation-snapshot.ts +25 -6
  242. package/src/router/pattern-matching.ts +14 -0
  243. package/src/router/prefetch-default.ts +4 -4
  244. package/src/router/prerender-match.ts +460 -352
  245. package/src/router/request-classification.ts +15 -12
  246. package/src/router/route-trie-builder.ts +9 -0
  247. package/src/router/router-context.ts +1 -0
  248. package/src/router/router-interfaces.ts +35 -1
  249. package/src/router/router-options.ts +45 -5
  250. package/src/router/segment-resolution/fresh.ts +20 -19
  251. package/src/router/segment-resolution/loader-cache.ts +110 -77
  252. package/src/router/segment-resolution/revalidation.ts +34 -24
  253. package/src/router/segment-resolution/streamed-handler-boundary.ts +133 -0
  254. package/src/router/trie-matching.ts +3 -0
  255. package/src/router-versions.ts +60 -0
  256. package/src/router.ts +107 -1
  257. package/src/rsc/full-payload.ts +6 -2
  258. package/src/rsc/handler-context.ts +5 -0
  259. package/src/rsc/handler.ts +64 -7
  260. package/src/rsc/progressive-enhancement.ts +5 -1
  261. package/src/rsc/response-cache-serve.ts +10 -4
  262. package/src/rsc/rsc-rendering.ts +65 -12
  263. package/src/rsc/shell-build-manifest.ts +17 -12
  264. package/src/rsc/shell-capture-constants.ts +39 -7
  265. package/src/rsc/shell-capture.ts +144 -45
  266. package/src/rsc/shell-serve.ts +12 -6
  267. package/src/rsc/ssr-module-loader.ts +26 -0
  268. package/src/rsc/types.ts +12 -6
  269. package/src/segment-boundary-content.ts +25 -0
  270. package/src/segment-loader-promise.ts +28 -38
  271. package/src/segment-system.tsx +117 -57
  272. package/src/server/build-version-table.ts +130 -0
  273. package/src/server/context.ts +40 -17
  274. package/src/server/cookie-store.ts +5 -0
  275. package/src/server/handle-store.ts +357 -127
  276. package/src/server/request-context.ts +149 -12
  277. package/src/ssr/ssr-root.tsx +32 -13
  278. package/src/suspense-audit-react.tsx +98 -0
  279. package/src/suspense-audit.ts +1030 -0
  280. package/src/testing/action-form.ts +66 -0
  281. package/src/testing/build-versions.ts +64 -0
  282. package/src/testing/dispatch.ts +6 -0
  283. package/src/testing/dom.entry.ts +1 -0
  284. package/src/testing/flight-runtime.d.ts +27 -0
  285. package/src/testing/flight.entry.ts +4 -0
  286. package/src/testing/index.ts +22 -0
  287. package/src/testing/internal/context.ts +2 -1
  288. package/src/testing/internal/seed-vars.ts +14 -2
  289. package/src/testing/internal/server-modules.ts +32 -0
  290. package/src/testing/location-state-key.ts +18 -8
  291. package/src/testing/render-handler.ts +2 -1
  292. package/src/testing/render-route.tsx +293 -46
  293. package/src/testing/serve-shell-request.ts +262 -37
  294. package/src/testing/shell-status.ts +17 -6
  295. package/src/testing/vitest-stubs/plugin-rsc.ts +37 -11
  296. package/src/testing/vitest-stubs/version.ts +4 -1
  297. package/src/thenable-status.ts +10 -0
  298. package/src/types/handler-context.ts +15 -5
  299. package/src/types/route-entry.ts +6 -0
  300. package/src/urls/path-helper.ts +69 -9
  301. package/src/use-loader.tsx +57 -30
  302. package/src/vite/discovery/build-versions.ts +1281 -0
  303. package/src/vite/discovery/bundle-postprocess.ts +4 -0
  304. package/src/vite/discovery/discover-routers.ts +15 -0
  305. package/src/vite/discovery/installed-packages.ts +159 -0
  306. package/src/vite/discovery/prerender-collection.ts +38 -1
  307. package/src/vite/discovery/router-versions-phase.ts +336 -0
  308. package/src/vite/discovery/shell-prerender-phase.ts +33 -43
  309. package/src/vite/discovery/state.ts +36 -1
  310. package/src/vite/discovery/virtual-module-codegen.ts +7 -2
  311. package/src/vite/encryption-key.ts +101 -9
  312. package/src/vite/inject-client-debug.ts +42 -20
  313. package/src/vite/plugin-types.ts +28 -0
  314. package/src/vite/plugins/{version-injector.ts → entry-bootstrap-injector.ts} +7 -22
  315. package/src/vite/plugins/expose-ids/export-analysis.ts +1 -1
  316. package/src/vite/plugins/expose-ids/router-transform.ts +36 -15
  317. package/src/vite/plugins/expose-internal-ids.ts +12 -3
  318. package/src/vite/plugins/stable-rsc-output.ts +147 -0
  319. package/src/vite/plugins/version-plugin.ts +19 -24
  320. package/src/vite/plugins/version.d.ts +17 -2
  321. package/src/vite/plugins/virtual-entries.ts +39 -6
  322. package/src/vite/rango.ts +28 -3
  323. package/src/vite/router-discovery.ts +96 -13
  324. package/src/vite/utils/bundle-analysis.ts +39 -7
  325. package/src/vite/utils/client-chunks.ts +5 -1
  326. package/src/vite/utils/compare-strings.ts +9 -0
  327. package/src/vite/utils/package-resolution.ts +9 -0
  328. package/src/vite/utils/prerender-utils.ts +4 -1
  329. package/src/vite/utils/shared-utils.ts +1 -1
  330. package/dist/types/segment-content-promise.d.ts +0 -13
  331. package/src/segment-content-promise.ts +0 -67
@@ -8,8 +8,8 @@
8
8
 
9
9
  import type { Plugin, ViteDevServer } from "vite";
10
10
  import { createServer as createViteServer } from "vite";
11
- import { resolve } from "node:path";
12
- import { readFileSync } from "node:fs";
11
+ import { dirname, join, resolve } from "node:path";
12
+ import { existsSync, readFileSync } from "node:fs";
13
13
  import { createRequire, register } from "node:module";
14
14
  import { pathToFileURL } from "node:url";
15
15
  import {
@@ -34,6 +34,7 @@ import {
34
34
  internalDebugNoCacheMiddleware,
35
35
  } from "./inject-client-debug.js";
36
36
  import { createVersionPlugin } from "./plugins/version-plugin.js";
37
+ import { uniformVersions } from "../router-versions.js";
37
38
  import { getVirtualEntrySSR, VIRTUAL_IDS } from "./plugins/virtual-entries.js";
38
39
  import { createVirtualStubPlugin } from "./plugins/virtual-stub-plugin.js";
39
40
  import {
@@ -64,6 +65,13 @@ import {
64
65
  refreshRecordedClientUrlProjections,
65
66
  } from "./discovery/client-urls-projection.js";
66
67
  import { runShellPrerenderPhase } from "./discovery/shell-prerender-phase.js";
68
+ import { runRouterVersionsPhase } from "./discovery/router-versions-phase.js";
69
+ import {
70
+ recordBundleFiles,
71
+ recordClientGraph,
72
+ recordServerGraph,
73
+ recordVersionModuleFiles,
74
+ } from "./discovery/build-versions.js";
67
75
  import { describeDiscoveryFailure } from "./discovery/discovery-errors.js";
68
76
  import { findTransitionWhenError } from "../transition-when-ref.js";
69
77
  import { transitionWhenHoistPlugin } from "./plugins/transition-when-hoist.js";
@@ -320,11 +328,44 @@ import type {
320
328
  BuildEnvResult,
321
329
  } from "./plugin-types.js";
322
330
 
331
+ const WRANGLER_CONFIG_NAMES: readonly string[] = [
332
+ "wrangler.json",
333
+ "wrangler.jsonc",
334
+ "wrangler.toml",
335
+ ];
336
+
337
+ /**
338
+ * getPlatformProxy() looks for the wrangler config from process.cwd() and
339
+ * resolves its default persist path against cwd, so building from another
340
+ * directory dropped the app's bindings (#1037). Search upward from the Vite
341
+ * root with wrangler's own file names and order. Persisted state goes under
342
+ * the Vite root, where @cloudflare/vite-plugin reads it in dev and preview
343
+ * (its getPersistenceRoot), also when the config sits above the root. No
344
+ * config found: no options, wrangler's own lookup.
345
+ */
346
+ function wranglerProxyOptions(
347
+ root: string,
348
+ ): Record<string, unknown> | undefined {
349
+ const viteRoot = resolve(root);
350
+ for (let dir = viteRoot; ; dir = dirname(dir)) {
351
+ for (const name of WRANGLER_CONFIG_NAMES) {
352
+ const configPath = join(dir, name);
353
+ if (existsSync(configPath)) {
354
+ return {
355
+ configPath,
356
+ persist: { path: join(viteRoot, ".wrangler", "state", "v3") },
357
+ };
358
+ }
359
+ }
360
+ if (dirname(dir) === dir) return undefined;
361
+ }
362
+ }
363
+
323
364
  /**
324
365
  * Resolve the buildEnv option into a concrete { env, dispose? } result.
325
366
  * Handles all four input shapes: false, "auto", factory, plain object.
326
367
  */
327
- async function resolveBuildEnv(
368
+ export async function resolveBuildEnv(
328
369
  option: BuildEnvOption | undefined,
329
370
  factoryCtx: BuildEnvFactoryContext,
330
371
  ): Promise<BuildEnvResult | null> {
@@ -348,7 +389,9 @@ async function resolveBuildEnv(
348
389
  )) as {
349
390
  getPlatformProxy: (opts?: any) => Promise<any>;
350
391
  };
351
- const proxy = await getPlatformProxy();
392
+ const proxy = await getPlatformProxy(
393
+ wranglerProxyOptions(factoryCtx.root),
394
+ );
352
395
  return {
353
396
  env: proxy.env as Record<string, unknown>,
354
397
  dispose: proxy.dispose,
@@ -410,6 +453,7 @@ function resetPrerenderCollection(s: DiscoveryState): void {
410
453
  s.staticManifestEntries = null;
411
454
  s.shellCandidates = null;
412
455
  s.prerenderPayloadValues = null;
456
+ s.buildData = [];
413
457
  }
414
458
 
415
459
  /**
@@ -947,6 +991,14 @@ export function createRouterDiscoveryPlugin(
947
991
  (globalThis as any).__rscRouterDiscoveryActive ?? false,
948
992
  );
949
993
  s.devServerOrigin = getDevServerOrigin();
994
+ // The temp Node realm below shares this globalThis and runs the dev
995
+ // /__rsc_shell capture, which must replay the prerender store as
996
+ // the worker's HIT does: a capture that renders the Prerender
997
+ // handler live stores a doc record the HIT never reads, and the
998
+ // HIT loses the loader pushes its prelude rendered (#1057). That
999
+ // realm never evaluates the virtual module that sets this on the
1000
+ // Node preset.
1001
+ globalThis.__PRERENDER_DEV_URL = s.devServerOrigin;
950
1002
 
951
1003
  // Create a temp Node.js server to run runtime discovery and generate
952
1004
  // named route types (static parser can't resolve factory calls).
@@ -1533,7 +1585,7 @@ export function createRouterDiscoveryPlugin(
1533
1585
  maxSnapshotBytes,
1534
1586
  captureTimeout,
1535
1587
  buildEnv: s.resolvedBuildEnv,
1536
- buildVersion: version,
1588
+ versions: uniformVersions(version),
1537
1589
  captureShellHTML: ssrModule.captureShellHTML,
1538
1590
  debug: !!debugDiscovery,
1539
1591
  });
@@ -2261,18 +2313,27 @@ export function createRouterDiscoveryPlugin(
2261
2313
  }
2262
2314
  },
2263
2315
 
2264
- // Post-build PPR shell capture (producer B, #699): runs after EVERY
2265
- // environment bundle is written — the shell prelude embeds built client
2266
- // asset URLs (bootstrap entry), which do not exist at buildStart. The
2267
- // kept temp server and the buildEnv were deferred AS A PAIR in
2268
- // buildStart's finally; this finally is the pair's success-path owner
2269
- // (buildEnd below owns the aborted-build path) — the phase itself is a
2270
- // pure producer and tears down only the globals it installs.
2316
+ // Post-build phases, after EVERY environment bundle is written.
2317
+ //
2318
+ // 1. Cache versions (discovery/router-versions-phase.ts): hashes the
2319
+ // server output as postprocessBundle left it and writes the per-router
2320
+ // table into the built version module. First, because phase 2 stamps
2321
+ // its entries with these versions and reads the same RSC out dir.
2322
+ // 2. PPR shell capture (producer B, #699): the shell prelude embeds built
2323
+ // client asset URLs (bootstrap entry), which do not exist at
2324
+ // buildStart. The kept temp server and the buildEnv were deferred AS A
2325
+ // PAIR in buildStart's finally; this finally is the pair's success-path
2326
+ // owner (buildEnd below owns the aborted-build path) — the phase itself
2327
+ // is a pure producer and tears down only the globals it installs.
2271
2328
  buildApp: {
2272
2329
  order: "post",
2273
2330
  async handler(builder) {
2274
2331
  try {
2275
- await runShellPrerenderPhase(s, builder as any);
2332
+ await runShellPrerenderPhase(
2333
+ s,
2334
+ builder as any,
2335
+ runRouterVersionsPhase(s, builder as any),
2336
+ );
2276
2337
  } finally {
2277
2338
  if (s.isBuildMode) {
2278
2339
  const tempServer = s.shellPhaseTempServer;
@@ -2387,6 +2448,22 @@ export function createRouterDiscoveryPlugin(
2387
2448
  // Record handler chunk metadata and RSC entry filename during RSC build.
2388
2449
  // Used by closeBundle for handler code eviction and prerender data injection.
2389
2450
  generateBundle(_options: any, bundle: any) {
2451
+ // Inputs of the per-router cache versions (discovery/build-versions.ts).
2452
+ // Each pass overwrites its slot: plugin-rsc runs the RSC and SSR builds
2453
+ // twice, and the real pass comes last.
2454
+ if (s.isBuildMode) {
2455
+ const envName = this.environment?.name;
2456
+ if (envName) {
2457
+ s.versionModuleFiles.set(envName, recordVersionModuleFiles(bundle));
2458
+ }
2459
+ if (envName === "rsc") {
2460
+ s.serverBuildGraph = recordServerGraph(this, bundle);
2461
+ } else if (envName === "client") {
2462
+ s.clientBuildGraph = recordClientGraph(this, bundle);
2463
+ } else if (envName === "ssr") {
2464
+ s.ssrBundle = recordBundleFiles(bundle);
2465
+ }
2466
+ }
2390
2467
  if (this.environment?.name !== "rsc") return;
2391
2468
  const genStart = debugBuild ? performance.now() : 0;
2392
2469
 
@@ -2428,6 +2505,12 @@ export function createRouterDiscoveryPlugin(
2428
2505
  s.resolvedPrerenderModules,
2429
2506
  "Prerender",
2430
2507
  false,
2508
+ // Mark onDemand exports (retained, not evicted) from the
2509
+ // discovery-derived $$id set — the same evaluated source that sets
2510
+ // the runtime od trie flag, so retention and serve can never
2511
+ // disagree. Populated by discoverRouters (buildStart), which runs
2512
+ // before this generateBundle pass.
2513
+ s.onDemandHandlerIds,
2431
2514
  );
2432
2515
  if (handlers.length > 0) {
2433
2516
  const existing = s.handlerChunkInfoMap.get(fileName);
@@ -29,20 +29,40 @@ export function findMatchingParenInBundle(
29
29
 
30
30
  /**
31
31
  * Scan a bundled chunk for handler exports and extract their names + $$id values.
32
- * Optionally detects passthrough flag. @internal Exported for testing only.
32
+ * Optionally detects the passthrough flag by parsing the call body, and marks
33
+ * onDemand from the authoritative discovery-derived handlerId set.
34
+ * @internal Exported for testing only.
33
35
  */
34
36
  export function extractHandlerExportsFromChunk(
35
37
  chunkCode: string,
36
38
  handlerModules: Map<string, string[]>,
37
39
  fnName: string,
38
40
  detectPassthrough: boolean,
39
- ): Array<{ name: string; handlerId: string; passthrough: boolean }> {
41
+ // handlerIds ($$id) of routes the evaluated source marks onDemand — the
42
+ // single source of truth shared with the runtime od trie flag. Keyed on the
43
+ // same $$id the chunk scan extracts, so any spelling of the option (literal,
44
+ // spread, imported const) retains the producer; a textual scan of the call
45
+ // body cannot see non-literal spellings and silently evicted them.
46
+ onDemandHandlerIds?: ReadonlyMap<string, string>,
47
+ ): Array<{
48
+ name: string;
49
+ handlerId: string;
50
+ passthrough: boolean;
51
+ onDemand: boolean;
52
+ }> {
40
53
  const handlers: Array<{
41
54
  name: string;
42
55
  handlerId: string;
43
56
  passthrough: boolean;
57
+ onDemand: boolean;
44
58
  }> = [];
45
59
 
60
+ // Only parse a call body (an O(callBody) paren walk per export) when the whole
61
+ // chunk actually contains the marker we're detecting — the common case (no
62
+ // passthrough opt-in anywhere in the chunk) skips the walk entirely.
63
+ const scanPassthrough =
64
+ detectPassthrough && chunkCode.includes("passthrough");
65
+
46
66
  for (const [, handlerNames] of handlerModules) {
47
67
  for (const name of handlerNames) {
48
68
  const eName = escapeRegExp(name);
@@ -53,7 +73,7 @@ export function extractHandlerExportsFromChunk(
53
73
  if (!match) continue;
54
74
 
55
75
  let isPassthrough = false;
56
- if (detectPassthrough) {
76
+ if (scanPassthrough) {
57
77
  const eFnName = escapeRegExp(fnName);
58
78
  const callStartRe = new RegExp(
59
79
  `(?:const|let|var)\\s+${eName}\\s*=\\s*${eFnName}\\s*(?:<[^>]*>)?\\s*\\(`,
@@ -68,7 +88,12 @@ export function extractHandlerExportsFromChunk(
68
88
  }
69
89
  }
70
90
  }
71
- handlers.push({ name, handlerId: match[1], passthrough: isPassthrough });
91
+ handlers.push({
92
+ name,
93
+ handlerId: match[1],
94
+ passthrough: isPassthrough,
95
+ onDemand: onDemandHandlerIds?.has(match[1]) ?? false,
96
+ });
72
97
  }
73
98
  }
74
99
 
@@ -82,7 +107,12 @@ export function extractHandlerExportsFromChunk(
82
107
  */
83
108
  export function evictHandlerCode(
84
109
  code: string,
85
- exports: Array<{ name: string; handlerId: string; passthrough?: boolean }>,
110
+ exports: Array<{
111
+ name: string;
112
+ handlerId: string;
113
+ passthrough?: boolean;
114
+ onDemand?: boolean;
115
+ }>,
86
116
  fnName: string,
87
117
  brand: string,
88
118
  ): { code: string; savedBytes: number } | null {
@@ -90,8 +120,10 @@ export function evictHandlerCode(
90
120
  let modified = code;
91
121
 
92
122
  const eFnName = escapeRegExp(fnName);
93
- for (const { name, handlerId, passthrough } of exports) {
94
- if (passthrough) continue;
123
+ for (const { name, handlerId, passthrough, onDemand } of exports) {
124
+ // Keep the producer body for passthrough (live fallback) and onDemand
125
+ // (router.prerender() must be able to invoke the retained producer).
126
+ if (passthrough || onDemand) continue;
95
127
 
96
128
  const eName = escapeRegExp(name);
97
129
  // Match const/let/var: Rolldown (Vite 8) emits top-level bindings in the
@@ -206,8 +206,12 @@ export function resolveClientChunks(
206
206
  return option;
207
207
  }
208
208
 
209
+ /** @vitejs/plugin-rsc's client-reference map; its group modules sit under it. */
210
+ export const CLIENT_REFERENCES_MODULE_ID =
211
+ "\0virtual:vite-rsc/client-references";
212
+
209
213
  /** Module-id prefix of the per-group virtual module @vitejs/plugin-rsc emits. */
210
- const CLIENT_GROUP_PREFIX = "\0virtual:vite-rsc/client-references/group/";
214
+ const CLIENT_GROUP_PREFIX = `${CLIENT_REFERENCES_MODULE_ID}/group/`;
211
215
 
212
216
  /**
213
217
  * Build-end group report under `DEBUG=rango:chunks`: one line per client
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Code-unit order, the same on every machine. Generated modules and the cache
3
+ * version inputs are sorted with it: `localeCompare` depends on the build
4
+ * machine's ICU data, and build output has to be byte-identical across machines
5
+ * (discovery/build-versions.ts hashes it).
6
+ */
7
+ export function compareStrings(a: string, b: string): number {
8
+ return a < b ? -1 : a > b ? 1 : 0;
9
+ }
@@ -72,11 +72,20 @@ export function getPackageAliases(): Record<string, string> {
72
72
  return aliases;
73
73
  }
74
74
 
75
+ /**
76
+ * @vitejs/plugin-rsc puts these vendor specifiers in `optimizeDeps.include`,
77
+ * and Vite resolves them from the app root, where pnpm's strict layout does
78
+ * not link the plugin unless the app lists it (#1050). Aliased to the copy
79
+ * the router resolves, which therefore also wins over an app's own
80
+ * @vitejs/plugin-rsc. Must hold every vendor entry the plugin includes:
81
+ * optimize-deps-vendor-resolution.test.ts checks the resolved config.
82
+ */
75
83
  export function getVendorAliases(): Record<string, string> {
76
84
  const specs = [
77
85
  "@vitejs/plugin-rsc/vendor/react-server-dom/client.browser",
78
86
  "@vitejs/plugin-rsc/vendor/react-server-dom/client.edge",
79
87
  "@vitejs/plugin-rsc/vendor/react-server-dom/server.edge",
88
+ "@vitejs/plugin-rsc/vendor/react-server-dom/static.edge",
80
89
  ];
81
90
  const aliases: Record<string, string> = {};
82
91
  for (const spec of specs) {
@@ -199,8 +199,11 @@ export function resolvePrerenderError(
199
199
  throw error;
200
200
  }
201
201
 
202
+ /** Build scratch space, relative to the project root. Never shipped. */
203
+ export const RANGO_BUILD_DIR = "node_modules/.rangojs-router-build";
204
+
202
205
  function getStagedAssetDir(projectRoot: string): string {
203
- return resolve(projectRoot, "node_modules/.rangojs-router-build/rsc-assets");
206
+ return resolve(projectRoot, RANGO_BUILD_DIR, "rsc-assets");
204
207
  }
205
208
 
206
209
  export function resetStagedBuildAssets(projectRoot: string): void {
@@ -15,7 +15,7 @@ import type { HeadScriptsOption } from "../plugin-types.js";
15
15
 
16
16
  // Cloudflare preset: @cloudflare/vite-plugin sets optimizeDeps.entries (string
17
17
  // or array) on the rsc environment. Single source for both the discovery plugin
18
- // and the version injector so they target the same entry.
18
+ // and the entry bootstrap injector so they target the same entry.
19
19
  export function resolveRscEntryFromConfig(
20
20
  config: ResolvedConfig,
21
21
  ): string | undefined {
@@ -1,13 +0,0 @@
1
- import type { ReactNode } from "react";
2
- /**
3
- * Return a stable Promise wrapping `component`, memoized on the component ref.
4
- *
5
- * A fresh `Promise.resolve(component)` each render would suspend for one
6
- * microtask and briefly commit the loading fallback inside Suspender — the
7
- * intercept / parallel-slot flicker this indirection prevents. Reusing the
8
- * same Promise ref keeps React's `use()` in "known fulfilled" state after
9
- * the first observation.
10
- *
11
- * @internal
12
- */
13
- export declare function getMemoizedContentPromise(component: ReactNode): Promise<ReactNode>;
@@ -1,67 +0,0 @@
1
- import type { ReactNode } from "react";
2
-
3
- /**
4
- * Stable Promise wrappers keyed on the component itself. Objects (React
5
- * elements, functions, lazy payloads) land in a WeakMap so entries GC when
6
- * the underlying component is released; primitives (string, number, boolean,
7
- * null) land in a Map so memoization still applies to text-/null-backed
8
- * segments like those in partial-update flows. Keeping this cache outside
9
- * the segment eliminates preservation fields on ResolvedSegment — it survives
10
- * reconciliation naturally because the component ref is what's stable.
11
- *
12
- * Browser-only. On the server each SSR render needs a fresh pending promise
13
- * so Suspense can emit the loading fallback HTML before content streams. A
14
- * shared already-resolved promise has `.status === "fulfilled"` attached by
15
- * React on its first observation — subsequent `use()` calls return
16
- * synchronously without suspending, so the Suspense fallback never makes it
17
- * into the initial HTML. Route-definition components share refs across
18
- * requests, so a global cache would leak tracked state between renders.
19
- */
20
- const IS_BROWSER = typeof window !== "undefined";
21
- const objectContentCache = IS_BROWSER
22
- ? new WeakMap<object, Promise<ReactNode>>()
23
- : null;
24
- const primitiveContentCache = IS_BROWSER
25
- ? new Map<unknown, Promise<ReactNode>>()
26
- : null;
27
-
28
- /**
29
- * Return a stable Promise wrapping `component`, memoized on the component ref.
30
- *
31
- * A fresh `Promise.resolve(component)` each render would suspend for one
32
- * microtask and briefly commit the loading fallback inside Suspender — the
33
- * intercept / parallel-slot flicker this indirection prevents. Reusing the
34
- * same Promise ref keeps React's `use()` in "known fulfilled" state after
35
- * the first observation.
36
- *
37
- * @internal
38
- */
39
- export function getMemoizedContentPromise(
40
- component: ReactNode,
41
- ): Promise<ReactNode> {
42
- if (component instanceof Promise) {
43
- return component as Promise<ReactNode>;
44
- }
45
-
46
- if (!objectContentCache || !primitiveContentCache) {
47
- return Promise.resolve(component);
48
- }
49
-
50
- if (component !== null && typeof component === "object") {
51
- const cached = objectContentCache.get(component);
52
- if (cached) {
53
- return cached;
54
- }
55
- const promise = Promise.resolve(component);
56
- objectContentCache.set(component, promise);
57
- return promise;
58
- }
59
-
60
- const cached = primitiveContentCache.get(component);
61
- if (cached) {
62
- return cached;
63
- }
64
- const promise = Promise.resolve(component);
65
- primitiveContentCache.set(component, promise);
66
- return promise;
67
- }