@rangojs/router 0.0.0-experimental.5 → 0.0.0-experimental.50

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 (301) hide show
  1. package/AGENTS.md +9 -0
  2. package/README.md +884 -4
  3. package/dist/bin/rango.js +1606 -0
  4. package/dist/vite/index.js +4567 -769
  5. package/package.json +77 -58
  6. package/skills/breadcrumbs/SKILL.md +250 -0
  7. package/skills/cache-guide/SKILL.md +262 -0
  8. package/skills/caching/SKILL.md +85 -23
  9. package/skills/composability/SKILL.md +172 -0
  10. package/skills/debug-manifest/SKILL.md +12 -8
  11. package/skills/document-cache/SKILL.md +18 -16
  12. package/skills/fonts/SKILL.md +167 -0
  13. package/skills/hooks/SKILL.md +334 -72
  14. package/skills/host-router/SKILL.md +218 -0
  15. package/skills/intercept/SKILL.md +131 -8
  16. package/skills/layout/SKILL.md +100 -3
  17. package/skills/links/SKILL.md +89 -30
  18. package/skills/loader/SKILL.md +388 -38
  19. package/skills/middleware/SKILL.md +171 -34
  20. package/skills/mime-routes/SKILL.md +128 -0
  21. package/skills/parallel/SKILL.md +204 -1
  22. package/skills/prerender/SKILL.md +643 -0
  23. package/skills/rango/SKILL.md +85 -16
  24. package/skills/response-routes/SKILL.md +411 -0
  25. package/skills/route/SKILL.md +226 -14
  26. package/skills/router-setup/SKILL.md +123 -30
  27. package/skills/tailwind/SKILL.md +129 -0
  28. package/skills/theme/SKILL.md +9 -8
  29. package/skills/typesafety/SKILL.md +318 -89
  30. package/skills/use-cache/SKILL.md +324 -0
  31. package/src/__internal.ts +102 -4
  32. package/src/bin/rango.ts +321 -0
  33. package/src/browser/action-coordinator.ts +97 -0
  34. package/src/browser/action-response-classifier.ts +99 -0
  35. package/src/browser/event-controller.ts +92 -64
  36. package/src/browser/history-state.ts +80 -0
  37. package/src/browser/intercept-utils.ts +52 -0
  38. package/src/browser/link-interceptor.ts +24 -4
  39. package/src/browser/logging.ts +55 -0
  40. package/src/browser/merge-segment-loaders.ts +20 -12
  41. package/src/browser/navigation-bridge.ts +282 -557
  42. package/src/browser/navigation-client.ts +157 -71
  43. package/src/browser/navigation-store.ts +33 -50
  44. package/src/browser/navigation-transaction.ts +297 -0
  45. package/src/browser/network-error-handler.ts +61 -0
  46. package/src/browser/partial-update.ts +303 -310
  47. package/src/browser/prefetch/cache.ts +206 -0
  48. package/src/browser/prefetch/fetch.ts +144 -0
  49. package/src/browser/prefetch/observer.ts +65 -0
  50. package/src/browser/prefetch/policy.ts +48 -0
  51. package/src/browser/prefetch/queue.ts +128 -0
  52. package/src/browser/rango-state.ts +112 -0
  53. package/src/browser/react/Link.tsx +193 -73
  54. package/src/browser/react/NavigationProvider.tsx +160 -13
  55. package/src/browser/react/context.ts +6 -0
  56. package/src/browser/react/filter-segment-order.ts +11 -0
  57. package/src/browser/react/index.ts +12 -12
  58. package/src/browser/react/location-state-shared.ts +95 -53
  59. package/src/browser/react/location-state.ts +60 -15
  60. package/src/browser/react/mount-context.ts +24 -1
  61. package/src/browser/react/nonce-context.ts +23 -0
  62. package/src/browser/react/shallow-equal.ts +27 -0
  63. package/src/browser/react/use-action.ts +29 -51
  64. package/src/browser/react/use-client-cache.ts +5 -3
  65. package/src/browser/react/use-handle.ts +32 -79
  66. package/src/browser/react/use-href.tsx +2 -2
  67. package/src/browser/react/use-link-status.ts +6 -5
  68. package/src/browser/react/use-navigation.ts +22 -63
  69. package/src/browser/react/use-params.ts +65 -0
  70. package/src/browser/react/use-pathname.ts +47 -0
  71. package/src/browser/react/use-router.ts +63 -0
  72. package/src/browser/react/use-search-params.ts +56 -0
  73. package/src/browser/react/use-segments.ts +80 -97
  74. package/src/browser/response-adapter.ts +73 -0
  75. package/src/browser/rsc-router.tsx +188 -55
  76. package/src/browser/scroll-restoration.ts +117 -44
  77. package/src/browser/segment-reconciler.ts +221 -0
  78. package/src/browser/segment-structure-assert.ts +16 -0
  79. package/src/browser/server-action-bridge.ts +504 -599
  80. package/src/browser/shallow.ts +6 -1
  81. package/src/browser/types.ts +118 -47
  82. package/src/browser/validate-redirect-origin.ts +29 -0
  83. package/src/build/generate-manifest.ts +235 -24
  84. package/src/build/generate-route-types.ts +36 -0
  85. package/src/build/index.ts +13 -0
  86. package/src/build/route-trie.ts +265 -0
  87. package/src/build/route-types/ast-helpers.ts +25 -0
  88. package/src/build/route-types/ast-route-extraction.ts +98 -0
  89. package/src/build/route-types/codegen.ts +102 -0
  90. package/src/build/route-types/include-resolution.ts +411 -0
  91. package/src/build/route-types/param-extraction.ts +48 -0
  92. package/src/build/route-types/per-module-writer.ts +128 -0
  93. package/src/build/route-types/router-processing.ts +479 -0
  94. package/src/build/route-types/scan-filter.ts +78 -0
  95. package/src/build/runtime-discovery.ts +231 -0
  96. package/src/cache/background-task.ts +34 -0
  97. package/src/cache/cache-key-utils.ts +44 -0
  98. package/src/cache/cache-policy.ts +125 -0
  99. package/src/cache/cache-runtime.ts +342 -0
  100. package/src/cache/cache-scope.ts +167 -309
  101. package/src/cache/cf/cf-cache-store.ts +571 -17
  102. package/src/cache/cf/index.ts +13 -3
  103. package/src/cache/document-cache.ts +116 -77
  104. package/src/cache/handle-capture.ts +81 -0
  105. package/src/cache/handle-snapshot.ts +41 -0
  106. package/src/cache/index.ts +1 -15
  107. package/src/cache/memory-segment-store.ts +191 -13
  108. package/src/cache/profile-registry.ts +73 -0
  109. package/src/cache/read-through-swr.ts +134 -0
  110. package/src/cache/segment-codec.ts +256 -0
  111. package/src/cache/taint.ts +98 -0
  112. package/src/cache/types.ts +72 -122
  113. package/src/client.rsc.tsx +3 -1
  114. package/src/client.tsx +106 -126
  115. package/src/component-utils.ts +4 -4
  116. package/src/components/DefaultDocument.tsx +5 -1
  117. package/src/context-var.ts +86 -0
  118. package/src/debug.ts +19 -9
  119. package/src/errors.ts +108 -2
  120. package/src/handle.ts +15 -29
  121. package/src/handles/MetaTags.tsx +73 -20
  122. package/src/handles/breadcrumbs.ts +66 -0
  123. package/src/handles/index.ts +1 -0
  124. package/src/handles/meta.ts +30 -13
  125. package/src/host/cookie-handler.ts +165 -0
  126. package/src/host/errors.ts +97 -0
  127. package/src/host/index.ts +53 -0
  128. package/src/host/pattern-matcher.ts +214 -0
  129. package/src/host/router.ts +352 -0
  130. package/src/host/testing.ts +79 -0
  131. package/src/host/types.ts +146 -0
  132. package/src/host/utils.ts +25 -0
  133. package/src/href-client.ts +119 -29
  134. package/src/index.rsc.ts +153 -19
  135. package/src/index.ts +211 -30
  136. package/src/internal-debug.ts +11 -0
  137. package/src/loader.rsc.ts +26 -147
  138. package/src/loader.ts +27 -10
  139. package/src/network-error-thrower.tsx +3 -1
  140. package/src/outlet-provider.tsx +45 -0
  141. package/src/prerender/param-hash.ts +37 -0
  142. package/src/prerender/store.ts +185 -0
  143. package/src/prerender.ts +463 -0
  144. package/src/reverse.ts +330 -0
  145. package/src/root-error-boundary.tsx +41 -29
  146. package/src/route-content-wrapper.tsx +7 -4
  147. package/src/route-definition/dsl-helpers.ts +959 -0
  148. package/src/route-definition/helper-factories.ts +200 -0
  149. package/src/route-definition/helpers-types.ts +430 -0
  150. package/src/route-definition/index.ts +52 -0
  151. package/src/route-definition/redirect.ts +93 -0
  152. package/src/route-definition.ts +1 -1428
  153. package/src/route-map-builder.ts +217 -123
  154. package/src/route-name.ts +53 -0
  155. package/src/route-types.ts +59 -8
  156. package/src/router/content-negotiation.ts +116 -0
  157. package/src/router/debug-manifest.ts +72 -0
  158. package/src/router/error-handling.ts +9 -9
  159. package/src/router/find-match.ts +160 -0
  160. package/src/router/handler-context.ts +374 -81
  161. package/src/router/intercept-resolution.ts +397 -0
  162. package/src/router/lazy-includes.ts +237 -0
  163. package/src/router/loader-resolution.ts +215 -122
  164. package/src/router/logging.ts +251 -0
  165. package/src/router/manifest.ts +154 -35
  166. package/src/router/match-api.ts +620 -0
  167. package/src/router/match-context.ts +5 -3
  168. package/src/router/match-handlers.ts +440 -0
  169. package/src/router/match-middleware/background-revalidation.ts +108 -93
  170. package/src/router/match-middleware/cache-lookup.ts +440 -10
  171. package/src/router/match-middleware/cache-store.ts +98 -26
  172. package/src/router/match-middleware/intercept-resolution.ts +57 -17
  173. package/src/router/match-middleware/segment-resolution.ts +27 -6
  174. package/src/router/match-pipelines.ts +10 -45
  175. package/src/router/match-result.ts +55 -33
  176. package/src/router/metrics.ts +240 -15
  177. package/src/router/middleware-cookies.ts +55 -0
  178. package/src/router/middleware-types.ts +222 -0
  179. package/src/router/middleware.ts +327 -369
  180. package/src/router/pattern-matching.ts +211 -43
  181. package/src/router/prerender-match.ts +402 -0
  182. package/src/router/preview-match.ts +170 -0
  183. package/src/router/revalidation.ts +137 -38
  184. package/src/router/router-context.ts +41 -21
  185. package/src/router/router-interfaces.ts +452 -0
  186. package/src/router/router-options.ts +592 -0
  187. package/src/router/router-registry.ts +24 -0
  188. package/src/router/segment-resolution/fresh.ts +677 -0
  189. package/src/router/segment-resolution/helpers.ts +263 -0
  190. package/src/router/segment-resolution/loader-cache.ts +199 -0
  191. package/src/router/segment-resolution/revalidation.ts +1296 -0
  192. package/src/router/segment-resolution/static-store.ts +67 -0
  193. package/src/router/segment-resolution.ts +21 -0
  194. package/src/router/segment-wrappers.ts +291 -0
  195. package/src/router/telemetry-otel.ts +299 -0
  196. package/src/router/telemetry.ts +300 -0
  197. package/src/router/timeout.ts +148 -0
  198. package/src/router/trie-matching.ts +239 -0
  199. package/src/router/types.ts +77 -3
  200. package/src/router.ts +665 -4182
  201. package/src/rsc/handler-context.ts +45 -0
  202. package/src/rsc/handler.ts +764 -754
  203. package/src/rsc/helpers.ts +140 -6
  204. package/src/rsc/index.ts +0 -20
  205. package/src/rsc/loader-fetch.ts +209 -0
  206. package/src/rsc/manifest-init.ts +86 -0
  207. package/src/rsc/nonce.ts +14 -0
  208. package/src/rsc/origin-guard.ts +141 -0
  209. package/src/rsc/progressive-enhancement.ts +379 -0
  210. package/src/rsc/response-error.ts +37 -0
  211. package/src/rsc/response-route-handler.ts +347 -0
  212. package/src/rsc/rsc-rendering.ts +237 -0
  213. package/src/rsc/runtime-warnings.ts +42 -0
  214. package/src/rsc/server-action.ts +348 -0
  215. package/src/rsc/ssr-setup.ts +128 -0
  216. package/src/rsc/types.ts +38 -11
  217. package/src/search-params.ts +230 -0
  218. package/src/segment-system.tsx +172 -21
  219. package/src/server/context.ts +266 -58
  220. package/src/server/cookie-store.ts +190 -0
  221. package/src/server/fetchable-loader-store.ts +37 -0
  222. package/src/server/handle-store.ts +94 -15
  223. package/src/server/loader-registry.ts +15 -56
  224. package/src/server/request-context.ts +439 -73
  225. package/src/server.ts +35 -128
  226. package/src/ssr/index.tsx +101 -31
  227. package/src/static-handler.ts +114 -0
  228. package/src/theme/ThemeProvider.tsx +21 -15
  229. package/src/theme/ThemeScript.tsx +5 -5
  230. package/src/theme/constants.ts +5 -2
  231. package/src/theme/index.ts +4 -14
  232. package/src/theme/theme-context.ts +4 -30
  233. package/src/theme/theme-script.ts +21 -18
  234. package/src/types/boundaries.ts +158 -0
  235. package/src/types/cache-types.ts +198 -0
  236. package/src/types/error-types.ts +192 -0
  237. package/src/types/global-namespace.ts +100 -0
  238. package/src/types/handler-context.ts +773 -0
  239. package/src/types/index.ts +88 -0
  240. package/src/types/loader-types.ts +183 -0
  241. package/src/types/route-config.ts +170 -0
  242. package/src/types/route-entry.ts +109 -0
  243. package/src/types/segments.ts +150 -0
  244. package/src/types.ts +1 -1623
  245. package/src/urls/include-helper.ts +197 -0
  246. package/src/urls/index.ts +53 -0
  247. package/src/urls/path-helper-types.ts +339 -0
  248. package/src/urls/path-helper.ts +329 -0
  249. package/src/urls/pattern-types.ts +95 -0
  250. package/src/urls/response-types.ts +106 -0
  251. package/src/urls/type-extraction.ts +372 -0
  252. package/src/urls/urls-function.ts +98 -0
  253. package/src/urls.ts +1 -802
  254. package/src/use-loader.tsx +85 -77
  255. package/src/vite/discovery/bundle-postprocess.ts +184 -0
  256. package/src/vite/discovery/discover-routers.ts +344 -0
  257. package/src/vite/discovery/prerender-collection.ts +385 -0
  258. package/src/vite/discovery/route-types-writer.ts +258 -0
  259. package/src/vite/discovery/self-gen-tracking.ts +47 -0
  260. package/src/vite/discovery/state.ts +108 -0
  261. package/src/vite/discovery/virtual-module-codegen.ts +203 -0
  262. package/src/vite/index.ts +11 -782
  263. package/src/vite/plugin-types.ts +48 -0
  264. package/src/vite/plugins/cjs-to-esm.ts +93 -0
  265. package/src/vite/plugins/client-ref-dedup.ts +115 -0
  266. package/src/vite/plugins/client-ref-hashing.ts +105 -0
  267. package/src/vite/{expose-action-id.ts → plugins/expose-action-id.ts} +72 -53
  268. package/src/vite/plugins/expose-id-utils.ts +287 -0
  269. package/src/vite/plugins/expose-ids/export-analysis.ts +296 -0
  270. package/src/vite/plugins/expose-ids/handler-transform.ts +179 -0
  271. package/src/vite/plugins/expose-ids/loader-transform.ts +74 -0
  272. package/src/vite/plugins/expose-ids/router-transform.ts +110 -0
  273. package/src/vite/plugins/expose-ids/types.ts +45 -0
  274. package/src/vite/plugins/expose-internal-ids.ts +569 -0
  275. package/src/vite/plugins/refresh-cmd.ts +65 -0
  276. package/src/vite/plugins/use-cache-transform.ts +323 -0
  277. package/src/vite/plugins/version-injector.ts +83 -0
  278. package/src/vite/plugins/version-plugin.ts +266 -0
  279. package/src/vite/{virtual-entries.ts → plugins/virtual-entries.ts} +27 -16
  280. package/src/vite/plugins/virtual-stub-plugin.ts +29 -0
  281. package/src/vite/rango.ts +445 -0
  282. package/src/vite/router-discovery.ts +777 -0
  283. package/src/vite/utils/ast-handler-extract.ts +517 -0
  284. package/src/vite/utils/banner.ts +36 -0
  285. package/src/vite/utils/bundle-analysis.ts +137 -0
  286. package/src/vite/utils/manifest-utils.ts +70 -0
  287. package/src/vite/{package-resolution.ts → utils/package-resolution.ts} +25 -29
  288. package/src/vite/utils/prerender-utils.ts +189 -0
  289. package/src/vite/utils/shared-utils.ts +169 -0
  290. package/CLAUDE.md +0 -43
  291. package/src/browser/lru-cache.ts +0 -69
  292. package/src/browser/request-controller.ts +0 -164
  293. package/src/cache/memory-store.ts +0 -253
  294. package/src/href-context.ts +0 -33
  295. package/src/href.ts +0 -255
  296. package/src/server/route-manifest-cache.ts +0 -173
  297. package/src/vite/expose-handle-id.ts +0 -209
  298. package/src/vite/expose-loader-id.ts +0 -426
  299. package/src/vite/expose-location-state-id.ts +0 -177
  300. package/src/warmup/connection-warmup.tsx +0 -94
  301. /package/src/vite/{version.d.ts → plugins/version.d.ts} +0 -0
@@ -8,8 +8,27 @@
8
8
  * - Supports hash link scrolling
9
9
  */
10
10
 
11
+ import { debugLog } from "./logging.js";
12
+
13
+ /**
14
+ * Defers a callback to the next animation frame.
15
+ * Falls back to setTimeout(0) in environments without requestAnimationFrame.
16
+ */
17
+ const deferToNextPaint: (fn: () => void) => void =
18
+ typeof requestAnimationFrame === "function"
19
+ ? requestAnimationFrame
20
+ : (fn) => setTimeout(fn, 0);
21
+
11
22
  const SCROLL_STORAGE_KEY = "rsc-router-scroll-positions";
12
23
 
24
+ /**
25
+ * Maximum number of scroll position entries to retain.
26
+ * When exceeded, the oldest entries (by insertion order) are evicted.
27
+ * 200 entries is well within sessionStorage limits while covering
28
+ * realistic back/forward navigation depth.
29
+ */
30
+ const MAX_SCROLL_ENTRIES = 200;
31
+
13
32
  /**
14
33
  * Interval for polling scroll restoration during streaming (ms).
15
34
  * If content is still loading and we can't scroll to saved position,
@@ -29,6 +48,13 @@ const SCROLL_POLL_TIMEOUT_MS = 5000;
29
48
  */
30
49
  let savedScrollPositions: Record<string, number> = {};
31
50
 
51
+ /**
52
+ * Tracks insertion order of scroll position keys for LRU eviction.
53
+ * Most recent entries are at the end of the array.
54
+ * When a key is updated, it is moved to the end.
55
+ */
56
+ let scrollKeyOrder: string[] = [];
57
+
32
58
  /**
33
59
  * Whether scroll restoration has been initialized
34
60
  */
@@ -37,9 +63,12 @@ let initialized = false;
37
63
  /**
38
64
  * Custom getKey function for determining scroll restoration key
39
65
  */
40
- type GetScrollKeyFunction = (
41
- location: { pathname: string; search: string; hash: string; key: string }
42
- ) => string;
66
+ type GetScrollKeyFunction = (location: {
67
+ pathname: string;
68
+ search: string;
69
+ hash: string;
70
+ key: string;
71
+ }) => string;
43
72
 
44
73
  let customGetKey: GetScrollKeyFunction | null = null;
45
74
 
@@ -99,9 +128,13 @@ export function initScrollRestoration(options?: {
99
128
  const stored = sessionStorage.getItem(SCROLL_STORAGE_KEY);
100
129
  if (stored) {
101
130
  savedScrollPositions = JSON.parse(stored);
131
+ // Rebuild key order from loaded positions.
132
+ // Exact original order is lost across page loads, but this is
133
+ // acceptable -- the important invariant is bounded size.
134
+ scrollKeyOrder = Object.keys(savedScrollPositions);
102
135
  }
103
136
  } catch (e) {
104
- // Ignore parse errors
137
+ // Ignore parse errors, start with empty state
105
138
  }
106
139
 
107
140
  // Ensure current history entry has a key
@@ -117,31 +150,82 @@ export function initScrollRestoration(options?: {
117
150
 
118
151
  window.addEventListener("pagehide", handlePageHide);
119
152
 
120
- console.log("[Scroll] Initialized, loaded positions:", Object.keys(savedScrollPositions).length);
153
+ debugLog(
154
+ "[Scroll] Initialized, loaded positions:",
155
+ Object.keys(savedScrollPositions).length,
156
+ );
121
157
 
122
158
  return () => {
159
+ cancelScrollRestorationPolling();
123
160
  window.removeEventListener("pagehide", handlePageHide);
124
161
  window.history.scrollRestoration = "auto";
125
162
  initialized = false;
163
+ savedScrollPositions = {};
164
+ scrollKeyOrder = [];
126
165
  };
127
166
  }
128
167
 
129
168
  /**
130
- * Save the current scroll position for the current history entry
169
+ * Save the current scroll position for the current history entry.
170
+ * Maintains bounded size by evicting oldest entries when the limit is exceeded.
131
171
  */
132
172
  export function saveCurrentScrollPosition(): void {
133
173
  const key = getScrollKey();
174
+
175
+ // If this key already exists, remove it from its current position
176
+ // in the order array so it can be re-appended at the end (most recent).
177
+ const existingIndex = scrollKeyOrder.indexOf(key);
178
+ if (existingIndex !== -1) {
179
+ scrollKeyOrder.splice(existingIndex, 1);
180
+ }
181
+
134
182
  savedScrollPositions[key] = window.scrollY;
183
+ scrollKeyOrder.push(key);
184
+
185
+ // Evict oldest entries if we exceed the limit
186
+ while (scrollKeyOrder.length > MAX_SCROLL_ENTRIES) {
187
+ const oldestKey = scrollKeyOrder.shift()!;
188
+ delete savedScrollPositions[oldestKey];
189
+ }
135
190
  }
136
191
 
137
192
  /**
138
- * Persist scroll positions to sessionStorage
193
+ * Persist scroll positions to sessionStorage.
194
+ * If the write fails due to quota exceeded, progressively evict the oldest
195
+ * entries and retry until it succeeds or the store is empty.
139
196
  */
140
197
  function persistToSessionStorage(): void {
141
198
  try {
142
- sessionStorage.setItem(SCROLL_STORAGE_KEY, JSON.stringify(savedScrollPositions));
199
+ sessionStorage.setItem(
200
+ SCROLL_STORAGE_KEY,
201
+ JSON.stringify(savedScrollPositions),
202
+ );
143
203
  } catch (e) {
144
- console.warn("[Scroll] Failed to persist to sessionStorage:", e);
204
+ // Likely QuotaExceededError. Evict oldest entries and retry.
205
+ const evictCount = Math.max(1, Math.floor(scrollKeyOrder.length / 4));
206
+ for (let i = 0; i < evictCount && scrollKeyOrder.length > 0; i++) {
207
+ const oldestKey = scrollKeyOrder.shift()!;
208
+ delete savedScrollPositions[oldestKey];
209
+ }
210
+
211
+ try {
212
+ sessionStorage.setItem(
213
+ SCROLL_STORAGE_KEY,
214
+ JSON.stringify(savedScrollPositions),
215
+ );
216
+ } catch (retryErr) {
217
+ // Storage still full after eviction. Clear our key entirely so we
218
+ // don't block other sessionStorage consumers.
219
+ console.warn(
220
+ "[Scroll] Failed to persist to sessionStorage after eviction, clearing scroll data:",
221
+ retryErr,
222
+ );
223
+ try {
224
+ sessionStorage.removeItem(SCROLL_STORAGE_KEY);
225
+ } catch {
226
+ // Nothing more we can do
227
+ }
228
+ }
145
229
  }
146
230
  }
147
231
 
@@ -189,50 +273,35 @@ export function restoreScrollPosition(options?: {
189
273
  return false;
190
274
  }
191
275
 
192
- // Check if page is tall enough to scroll to saved position
193
- const maxScrollY = document.documentElement.scrollHeight - window.innerHeight;
194
- const canScrollToPosition = savedY <= maxScrollY;
195
-
196
- if (canScrollToPosition) {
197
- window.scrollTo(0, savedY);
198
- console.log("[Scroll] Restored position:", savedY, "for key:", key);
199
- return true;
200
- }
201
-
202
- // Scroll as far as we can for now
203
- window.scrollTo(0, maxScrollY);
204
- console.log("[Scroll] Partial restore to:", maxScrollY, "target:", savedY);
205
-
206
- // Poll while streaming until we can scroll to target position
276
+ // If streaming, poll until streaming ends then scroll to saved position
207
277
  if (options?.retryIfStreaming && options?.isStreaming?.()) {
208
278
  const startTime = Date.now();
209
279
 
210
280
  pendingPollInterval = setInterval(() => {
211
- // Stop if we've exceeded the timeout
212
281
  if (Date.now() - startTime > SCROLL_POLL_TIMEOUT_MS) {
213
- console.log("[Scroll] Polling timeout, giving up");
282
+ debugLog("[Scroll] Polling timeout, giving up");
214
283
  cancelScrollRestorationPolling();
215
284
  return;
216
285
  }
217
286
 
218
- // Stop if streaming ended
219
287
  if (!options.isStreaming?.()) {
220
- console.log("[Scroll] Streaming ended, stopping poll");
221
- cancelScrollRestorationPolling();
222
- return;
223
- }
224
-
225
- // Check if we can now scroll to the target position
226
- const currentMaxScrollY = document.documentElement.scrollHeight - window.innerHeight;
227
- if (savedY <= currentMaxScrollY) {
228
288
  window.scrollTo(0, savedY);
229
- console.log("[Scroll] Poll restored position:", savedY);
289
+ debugLog("[Scroll] Restored after streaming:", savedY);
230
290
  cancelScrollRestorationPolling();
231
291
  }
232
292
  }, SCROLL_POLL_INTERVAL_MS);
293
+
294
+ return true;
233
295
  }
234
296
 
235
- return false;
297
+ // Not streaming — scroll after React commits and browser paints.
298
+ // startTransition defers the DOM commit, so scrolling synchronously
299
+ // would be overwritten when React replaces the content.
300
+ deferToNextPaint(() => {
301
+ window.scrollTo(0, savedY);
302
+ debugLog("[Scroll] Restored position:", savedY, "for key:", key);
303
+ });
304
+ return true;
236
305
  }
237
306
 
238
307
  /**
@@ -249,7 +318,7 @@ export function scrollToHash(): boolean {
249
318
  const element = document.getElementById(id);
250
319
  if (element) {
251
320
  element.scrollIntoView();
252
- console.log("[Scroll] Scrolled to hash element:", id);
321
+ debugLog("[Scroll] Scrolled to hash element:", id);
253
322
  return true;
254
323
  }
255
324
  } catch (e) {
@@ -306,13 +375,17 @@ export function handleNavigationEnd(options: {
306
375
  // Fall through to hash or top if no saved position
307
376
  }
308
377
 
309
- // Try hash scrolling first
310
- if (scrollToHash()) {
311
- return;
312
- }
378
+ // Defer hash and scroll-to-top to after React paints the new content,
379
+ // so the user doesn't see the current page jump before the new route appears.
380
+ deferToNextPaint(() => {
381
+ // Try hash scrolling first
382
+ if (scrollToHash()) {
383
+ return;
384
+ }
313
385
 
314
- // Default: scroll to top
315
- scrollToTop();
386
+ // Default: scroll to top
387
+ scrollToTop();
388
+ });
316
389
  }
317
390
 
318
391
  /**
@@ -0,0 +1,221 @@
1
+ import type { ResolvedSegment } from "./types.js";
2
+ import {
3
+ mergeSegmentLoaders,
4
+ needsLoaderMerge,
5
+ insertMissingDiffSegments,
6
+ } from "./merge-segment-loaders.js";
7
+ import { assertSegmentStructure } from "./segment-structure-assert.js";
8
+ import { splitInterceptSegments } from "./intercept-utils.js";
9
+
10
+ /**
11
+ * Determines the merging behavior for segment reconciliation.
12
+ *
13
+ * - 'action': From server-action-bridge's own merge. Always merges loaders,
14
+ * always preserves cached loading (even undefined), never clears cached
15
+ * segment loading.
16
+ * - 'navigation': From partial-update during normal navigation. Does NOT merge
17
+ * loaders, preserves cached loading only when defined, clears truthy loading
18
+ * on cached segments not in server diff.
19
+ * - 'stale-revalidation': From partial-update during stale revalidation or
20
+ * action-triggered refetch. Merges loaders, always preserves cached loading
21
+ * (same as action), clears truthy loading on cached segments not in server diff.
22
+ */
23
+ export type ReconcileActor = "navigation" | "action" | "stale-revalidation";
24
+
25
+ export interface ReconcileInput {
26
+ actor: ReconcileActor;
27
+ /** All segment IDs the server expects the client to have (matched array) */
28
+ matched: string[];
29
+ /** Segment IDs that changed (diff array) */
30
+ diff: string[];
31
+ /** Segments returned from server (raw array, keyed internally by ID) */
32
+ serverSegments: ResolvedSegment[];
33
+ /** Cached segments from current page (raw array, keyed internally by ID) */
34
+ cachedSegments: ResolvedSegment[];
35
+ /** When true, diff segments not in matched are inserted after their parent
36
+ * layout. Used during navigation when consolidation fetch returns loader
37
+ * segments that aren't in the matched array. */
38
+ insertMissingDiff?: boolean;
39
+ }
40
+
41
+ export interface ReconcileResult {
42
+ /** All merged segments in matched order (for caching and committing) */
43
+ segments: ResolvedSegment[];
44
+ /** Main segments excluding intercepts (for rendering) */
45
+ mainSegments: ResolvedSegment[];
46
+ /** Intercept segments only (passed via render options) */
47
+ interceptSegments: ResolvedSegment[];
48
+ }
49
+
50
+ /**
51
+ * Single source of truth for merging server segments with cached segments.
52
+ *
53
+ * Replaces the duplicated merge loops in server-action-bridge.ts and
54
+ * partial-update.ts. The actor parameter controls the subtle behavioral
55
+ * differences between action and navigation merging:
56
+ *
57
+ * Loading preservation:
58
+ * - action/stale-revalidation: Always preserves cached loading value when it
59
+ * differs from server (even when cached is undefined). This prevents tree
60
+ * structure changes that would remount components and destroy useActionState
61
+ * during action revalidation or action-triggered refetch.
62
+ * - navigation: Preserves cached loading only when the cached value is defined
63
+ * (not undefined). When cached is undefined, lets server value through
64
+ * because we're building a new tree.
65
+ *
66
+ * Loader merging:
67
+ * - action/stale-revalidation: Merges partial loader data when server returns
68
+ * fewer loaders than cached (revalidation only updated some loaders).
69
+ * - navigation: Does not merge (full navigation fetches complete data).
70
+ *
71
+ * Cached segment handling (segments in matched but not in server response):
72
+ * - action: Returns cached segment as-is (preserve tree structure).
73
+ * - navigation/stale-revalidation: Clears truthy loading to undefined
74
+ * (prevents showing stale skeletons), but preserves loading=false
75
+ * (suppressed boundary is structural).
76
+ */
77
+ export function reconcileSegments(input: ReconcileInput): ReconcileResult {
78
+ const { actor, matched, diff, insertMissingDiff } = input;
79
+ const shouldMergeLoaders = actor !== "navigation";
80
+ const context = actor === "action" ? "action-bridge" : "partial-update";
81
+
82
+ // Build lookup maps from arrays
83
+ const serverSegments = new Map<string, ResolvedSegment>();
84
+ input.serverSegments.forEach((s) => serverSegments.set(s.id, s));
85
+ const cachedSegments = new Map<string, ResolvedSegment>();
86
+ input.cachedSegments.forEach((s) => cachedSegments.set(s.id, s));
87
+
88
+ const segments = matched
89
+ .map((segId: string) => {
90
+ const fromServer = serverSegments.get(segId);
91
+ const fromCache = cachedSegments.get(segId);
92
+
93
+ if (fromServer) {
94
+ // Merge partial loader data when server returns fewer loaders than cached
95
+ if (shouldMergeLoaders && needsLoaderMerge(fromServer, fromCache)) {
96
+ return mergeSegmentLoaders(fromServer, fromCache);
97
+ }
98
+
99
+ // Preserve cached structural properties to maintain consistent React tree.
100
+ // Changing these between renders alters the element nesting
101
+ // (with/without RouteContentWrapper, MountContextProvider, etc.),
102
+ // causing React to remount components and destroy useActionState.
103
+ if (fromCache) {
104
+ let merged = fromServer;
105
+
106
+ // When server returns component: null for a layout segment, it means
107
+ // "this segment doesn't need re-rendering" - preserve the cached component
108
+ // to maintain the outlet chain and prevent React tree changes
109
+ if (
110
+ fromServer.component === null &&
111
+ fromServer.type === "layout" &&
112
+ fromCache.component != null
113
+ ) {
114
+ merged = { ...merged, component: fromCache.component };
115
+ }
116
+
117
+ // Loading preservation is actor-aware:
118
+ // - action/stale-revalidation: always preserve cached value to prevent
119
+ // tree remount (even when cached is undefined, to avoid adding a
120
+ // Suspense boundary that wasn't there before)
121
+ // - navigation: only when cached is defined (building a new tree)
122
+ if (actor !== "navigation") {
123
+ if (fromServer.loading !== fromCache.loading) {
124
+ merged = { ...merged, loading: fromCache.loading };
125
+ }
126
+ } else {
127
+ if (
128
+ fromCache.loading !== undefined &&
129
+ fromServer.loading !== fromCache.loading
130
+ ) {
131
+ merged = { ...merged, loading: fromCache.loading };
132
+ }
133
+ }
134
+
135
+ // mountPath: SSR segments may lack mountPath while revalidated segments
136
+ // include it. The conditional MountContextProvider wrapper changes tree depth.
137
+ if (fromServer.mountPath !== fromCache.mountPath) {
138
+ merged = { ...merged, mountPath: fromCache.mountPath };
139
+ }
140
+
141
+ // Dev-mode assertion: warn if the merged result still differs from cache
142
+ // in tree-structural properties. This catches bugs where the merge code
143
+ // above fails to preserve a value it should have.
144
+ assertSegmentStructure(fromCache, merged, context);
145
+
146
+ return merged;
147
+ }
148
+ return fromServer;
149
+ }
150
+
151
+ // Fall back to cached segment (server expects client to already have it)
152
+ if (!fromCache) {
153
+ if (actor === "action") {
154
+ console.error(`[Browser] MISSING SEGMENT: ${segId} not in cache!`);
155
+ } else {
156
+ console.warn(`[Browser] Missing segment: ${segId}`);
157
+ }
158
+ return fromCache;
159
+ }
160
+
161
+ // For non-action actors: cached segments the server decided not to re-render.
162
+ // - Preserve loading=false (suppressed boundary) to maintain tree structure
163
+ // - Preserve parallel segment loading so renderSegments can reconstruct
164
+ // parallel-owned loader markers from the cached slot metadata
165
+ // - Clear other truthy loading values to prevent suspense on cached content
166
+ if (actor !== "action") {
167
+ if (fromCache.type === "parallel" && fromCache.loading !== undefined) {
168
+ return fromCache;
169
+ }
170
+ if (fromCache.loading !== undefined && fromCache.loading !== false) {
171
+ return { ...fromCache, loading: undefined };
172
+ }
173
+ }
174
+
175
+ return fromCache;
176
+ })
177
+ .filter(Boolean) as ResolvedSegment[];
178
+
179
+ // Insert diff segments not in matched (e.g., loader segments from consolidation fetch).
180
+ // Only needed during navigation - action bridge doesn't use this.
181
+ if (insertMissingDiff) {
182
+ const matchedIdSet = new Set(matched);
183
+ insertMissingDiffSegments(segments, diff, matchedIdSet, serverSegments);
184
+ }
185
+
186
+ const { main, intercept } = splitInterceptSegments(segments);
187
+
188
+ return {
189
+ segments,
190
+ mainSegments: main,
191
+ interceptSegments: intercept,
192
+ };
193
+ }
194
+
195
+ /**
196
+ * Reconcile error segments with cached segments.
197
+ *
198
+ * For error responses, the server returns the error boundary segment.
199
+ * This function overlays error segments onto the full cached tree,
200
+ * preserving sibling layouts that aren't in the error parent chain.
201
+ */
202
+ export function reconcileErrorSegments(
203
+ cachedSegments: ResolvedSegment[],
204
+ errorSegments: ResolvedSegment[],
205
+ ): ReconcileResult {
206
+ const errorMap = new Map<string, ResolvedSegment>();
207
+ errorSegments.forEach((s) => errorMap.set(s.id, s));
208
+
209
+ const segments = cachedSegments.map((cached) => {
210
+ const fromServer = errorMap.get(cached.id);
211
+ return fromServer || cached;
212
+ });
213
+
214
+ const { main, intercept } = splitInterceptSegments(segments);
215
+
216
+ return {
217
+ segments,
218
+ mainSegments: main,
219
+ interceptSegments: intercept,
220
+ };
221
+ }
@@ -57,6 +57,22 @@ export function assertSegmentStructure(
57
57
  `The merge code should preserve the cached loading value.`,
58
58
  );
59
59
  }
60
+
61
+ // Check mountPath consistency. MountContextProvider is conditionally added
62
+ // in renderSegments() when mountPath is truthy, changing tree depth.
63
+ const cachedHasMount = !!cached.mountPath;
64
+ const incomingHasMount = !!incoming.mountPath;
65
+ if (cachedHasMount !== incomingHasMount) {
66
+ console.warn(
67
+ `[RSC Router] MountContextProvider mismatch detected in ${context} ` +
68
+ `for segment "${cached.id}": mountPath changed from ` +
69
+ `${cachedHasMount ? `"${cached.mountPath}"` : "undefined"} to ` +
70
+ `${incomingHasMount ? `"${incoming.mountPath}"` : "undefined"}. ` +
71
+ `This will cause React to remount the component, destroying ` +
72
+ `useActionState and other client state. ` +
73
+ `The merge code should preserve the cached mountPath value.`,
74
+ );
75
+ }
60
76
  }
61
77
 
62
78
  function describeLoading(loading: unknown): string {