@rangojs/router 0.0.0-experimental.8 → 0.0.0-experimental.8a4d0430

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 (300) hide show
  1. package/AGENTS.md +5 -0
  2. package/README.md +884 -4
  3. package/dist/bin/rango.js +1601 -0
  4. package/dist/vite/index.js +4474 -867
  5. package/package.json +60 -51
  6. package/skills/breadcrumbs/SKILL.md +250 -0
  7. package/skills/cache-guide/SKILL.md +262 -0
  8. package/skills/caching/SKILL.md +50 -21
  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 +78 -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 +87 -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 +285 -553
  42. package/src/browser/navigation-client.ts +124 -71
  43. package/src/browser/navigation-store.ts +33 -50
  44. package/src/browser/navigation-transaction.ts +295 -0
  45. package/src/browser/network-error-handler.ts +61 -0
  46. package/src/browser/partial-update.ts +258 -308
  47. package/src/browser/prefetch/cache.ts +146 -0
  48. package/src/browser/prefetch/fetch.ts +135 -0
  49. package/src/browser/prefetch/observer.ts +65 -0
  50. package/src/browser/prefetch/policy.ts +42 -0
  51. package/src/browser/prefetch/queue.ts +88 -0
  52. package/src/browser/rango-state.ts +112 -0
  53. package/src/browser/react/Link.tsx +185 -73
  54. package/src/browser/react/NavigationProvider.tsx +51 -11
  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 +6 -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 +107 -26
  76. package/src/browser/scroll-restoration.ts +92 -16
  77. package/src/browser/segment-reconciler.ts +216 -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 +109 -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 +469 -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 +338 -0
  100. package/src/cache/cache-scope.ts +120 -303
  101. package/src/cache/cf/cf-cache-store.ts +119 -7
  102. package/src/cache/cf/index.ts +8 -2
  103. package/src/cache/document-cache.ts +101 -72
  104. package/src/cache/handle-capture.ts +81 -0
  105. package/src/cache/handle-snapshot.ts +41 -0
  106. package/src/cache/index.ts +0 -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 +17 -7
  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 +21 -15
  126. package/src/host/errors.ts +8 -8
  127. package/src/host/index.ts +4 -7
  128. package/src/host/pattern-matcher.ts +27 -27
  129. package/src/host/router.ts +61 -39
  130. package/src/host/testing.ts +8 -8
  131. package/src/host/types.ts +15 -7
  132. package/src/host/utils.ts +1 -1
  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 -157
  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 +934 -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 +211 -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 +158 -0
  160. package/src/router/handler-context.ts +374 -81
  161. package/src/router/intercept-resolution.ts +395 -0
  162. package/src/router/lazy-includes.ts +234 -0
  163. package/src/router/loader-resolution.ts +215 -122
  164. package/src/router/logging.ts +248 -0
  165. package/src/router/manifest.ts +148 -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 +80 -93
  170. package/src/router/match-middleware/cache-lookup.ts +382 -9
  171. package/src/router/match-middleware/cache-store.ts +51 -22
  172. package/src/router/match-middleware/intercept-resolution.ts +55 -17
  173. package/src/router/match-middleware/segment-resolution.ts +24 -6
  174. package/src/router/match-pipelines.ts +10 -45
  175. package/src/router/match-result.ts +34 -28
  176. package/src/router/metrics.ts +235 -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 +324 -367
  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 +36 -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 +570 -0
  189. package/src/router/segment-resolution/helpers.ts +263 -0
  190. package/src/router/segment-resolution/loader-cache.ts +198 -0
  191. package/src/router/segment-resolution/revalidation.ts +1241 -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 +289 -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 +692 -4257
  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 +235 -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 +25 -13
  219. package/src/server/context.ts +182 -51
  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 +430 -70
  225. package/src/server.ts +35 -130
  226. package/src/ssr/index.tsx +100 -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 +687 -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 +102 -0
  243. package/src/types/segments.ts +148 -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 +110 -0
  261. package/src/vite/discovery/virtual-module-codegen.ts +203 -0
  262. package/src/vite/index.ts +11 -1133
  263. package/src/vite/plugin-types.ts +131 -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 -51
  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 +254 -0
  279. package/src/vite/{virtual-entries.ts → plugins/virtual-entries.ts} +23 -14
  280. package/src/vite/plugins/virtual-stub-plugin.ts +29 -0
  281. package/src/vite/rango.ts +510 -0
  282. package/src/vite/router-discovery.ts +785 -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/vite/{version.d.ts → plugins/version.d.ts} +0 -0
@@ -2,20 +2,33 @@ import type {
2
2
  ServerActionBridge,
3
3
  ServerActionBridgeConfig,
4
4
  RscPayload,
5
- ResolvedSegment,
6
- NavigationStore,
7
5
  } from "./types.js";
8
6
  import { createPartialUpdater } from "./partial-update.js";
9
- import { createNavigationTransaction } from "./navigation-bridge.js";
7
+ import { createNavigationTransaction } from "./navigation-transaction.js";
10
8
  import {
11
- mergeSegmentLoaders,
12
- needsLoaderMerge,
13
- } from "./merge-segment-loaders.js";
14
- import { assertSegmentStructure } from "./segment-structure-assert.js";
15
- import { startTransition, createElement } from "react";
16
- import type { EventController, ActionHandle } from "./event-controller.js";
17
- import { NetworkError, isNetworkError } from "../errors.js";
18
- import { NetworkErrorThrower } from "../network-error-thrower.js";
9
+ reconcileSegments,
10
+ reconcileErrorSegments,
11
+ } from "./segment-reconciler.js";
12
+ import { startTransition } from "react";
13
+ import type { EventController } from "./event-controller.js";
14
+ import {
15
+ toNetworkError,
16
+ emitNetworkError,
17
+ isBackgroundSuppressible,
18
+ } from "./network-error-handler.js";
19
+ import {
20
+ browserDebugLog,
21
+ isBrowserDebugEnabled,
22
+ startBrowserTransaction,
23
+ } from "./logging.js";
24
+ import { validateRedirectOrigin } from "./validate-redirect-origin.js";
25
+ import {
26
+ extractRscHeaderUrl,
27
+ emptyResponse,
28
+ teeWithCompletion,
29
+ } from "./response-adapter.js";
30
+ import { mergeLocationState } from "./history-state.js";
31
+ import { classifyActionOutcome } from "./action-coordinator.js";
19
32
 
20
33
  // Polyfill Symbol.dispose/asyncDispose for Safari and older browsers
21
34
  if (typeof Symbol.dispose === "undefined") {
@@ -25,26 +38,18 @@ if (typeof Symbol.asyncDispose === "undefined") {
25
38
  (Symbol as any).asyncDispose = Symbol("Symbol.asyncDispose");
26
39
  }
27
40
 
28
- /**
29
- * Normalize action ID - returns the ID as-is
30
- *
31
- * Server actions have IDs like "hash#actionName" or "src/actions.ts#actionName".
32
- * The full ID is used for tracking in the event controller. When subscribing
33
- * via useAction, both exact matching (full ID) and suffix matching (action name
34
- * only) are supported by the event controller.
35
- */
36
- function normalizeActionId(actionId: string): string {
37
- return actionId;
38
- }
39
-
40
41
  /**
41
42
  * Extended configuration for server action bridge with event controller
42
43
  */
43
- export interface ServerActionBridgeConfigWithController
44
- extends ServerActionBridgeConfig {
44
+ export interface ServerActionBridgeConfigWithController extends ServerActionBridgeConfig {
45
45
  eventController: EventController;
46
46
  /** RSC version from initial payload metadata */
47
47
  version?: string;
48
+ /** Callback to trigger SPA navigation (for action redirects) */
49
+ onNavigate?: (
50
+ url: string,
51
+ options?: { state?: unknown; replace?: boolean; _skipCache?: boolean },
52
+ ) => Promise<void>;
48
53
  }
49
54
 
50
55
  /**
@@ -61,10 +66,18 @@ export interface ServerActionBridgeConfigWithController
61
66
  * @returns ServerActionBridge instance
62
67
  */
63
68
  export function createServerActionBridge(
64
- config: ServerActionBridgeConfigWithController
69
+ config: ServerActionBridgeConfigWithController,
65
70
  ): ServerActionBridge {
66
- const { store, client, eventController, deps, onUpdate, renderSegments, version } =
67
- config;
71
+ const {
72
+ store,
73
+ client,
74
+ eventController,
75
+ deps,
76
+ onUpdate,
77
+ renderSegments,
78
+ version,
79
+ onNavigate,
80
+ } = config;
68
81
 
69
82
  let isRegistered = false;
70
83
 
@@ -76,280 +89,385 @@ export function createServerActionBridge(
76
89
  version,
77
90
  });
78
91
 
92
+ /**
93
+ * Refetch current route via a navigation transaction.
94
+ * Encapsulates the repeated pattern of creating a navTx + fetchPartialUpdate
95
+ * used by navigated-away, hmr-missing, and consolidation-needed scenarios.
96
+ */
97
+ async function refetchRoute(opts?: {
98
+ segments?: string[];
99
+ interceptSourceUrl?: string | null;
100
+ }): Promise<void> {
101
+ const src = opts?.interceptSourceUrl ?? null;
102
+ const navTx = createNavigationTransaction(
103
+ store,
104
+ eventController,
105
+ window.location.href,
106
+ { replace: true, skipLoadingState: true },
107
+ );
108
+ try {
109
+ await fetchPartialUpdate(
110
+ window.location.href,
111
+ opts?.segments ?? [],
112
+ false,
113
+ navTx.handle.signal,
114
+ navTx.with({
115
+ url: window.location.href,
116
+ storeOnly: true,
117
+ ...(src ? { intercept: true, interceptSourceUrl: src } : {}),
118
+ }),
119
+ {
120
+ type: "action" as const,
121
+ ...(src ? { interceptSourceUrl: src } : {}),
122
+ },
123
+ );
124
+ } finally {
125
+ navTx[Symbol.dispose]();
126
+ }
127
+ }
128
+
79
129
  /**
80
130
  * Server action callback handler
81
131
  */
82
132
  async function handleServerAction(id: string, args: any[]): Promise<unknown> {
83
- // Normalize action ID to just the function name for store tracking
133
+ const tx = isBrowserDebugEnabled()
134
+ ? startBrowserTransaction("action")
135
+ : null;
136
+ const log = (msg: string, details?: Record<string, unknown>) => {
137
+ if (tx) browserDebugLog(tx, msg, details);
138
+ };
139
+
84
140
  const locationKey = window.history.state?.key;
85
- const actionId = normalizeActionId(id);
86
- console.log("ID", { id, actionId, args });
141
+ log("action start", { id, argsCount: args.length });
87
142
 
88
143
  // Start action in event controller - handles lifecycle tracking
89
- using handle = eventController.startAction(actionId, args);
90
-
91
- const segmentState = store.getSegmentState();
92
- console.log(`[Browser] Args:`, args);
144
+ const handle = eventController.startAction(id, args);
145
+ try {
146
+ const segmentState = store.getSegmentState();
93
147
 
94
- // Mark cache as stale immediately when action starts
95
- // This ensures SWR pattern kicks in if user navigates away during action
96
- store.markCacheAsStaleAndBroadcast();
148
+ // Mark cache as stale immediately when action starts
149
+ // This ensures SWR pattern kicks in if user navigates away during action
150
+ store.markCacheAsStaleAndBroadcast();
97
151
 
98
- // Create temporary references for serialization
99
- const temporaryReferences = deps.createTemporaryReferenceSet();
152
+ // Create temporary references for serialization
153
+ const temporaryReferences = deps.createTemporaryReferenceSet();
100
154
 
101
- // Capture URL pathname at action start to detect navigation during action
102
- // Must use window.location (not store.path) because intercepts change URL
103
- // without changing store.path (e.g., /kanban -> /kanban/card/1)
104
- const actionStartPathname = window.location.pathname;
155
+ // Capture URL pathname at action start to detect navigation during action
156
+ // Must use window.location (not store.path) because intercepts change URL
157
+ // without changing store.path (e.g., /kanban -> /kanban/card/1)
158
+ const actionStartPathname = window.location.pathname;
105
159
 
106
- // Build action request URL with current segments
107
- const url = new URL(window.location.href);
108
- url.searchParams.set("_rsc_action", id);
109
- url.searchParams.set(
110
- "_rsc_segments",
111
- segmentState.currentSegmentIds.join(",")
112
- );
113
- // Add version param for version mismatch detection
114
- if (version) {
115
- url.searchParams.set("_rsc_v", version);
116
- }
160
+ // Build action request URL with current segments
161
+ const url = new URL(window.location.href);
162
+ url.searchParams.set("_rsc_action", id);
163
+ url.searchParams.set(
164
+ "_rsc_segments",
165
+ segmentState.currentSegmentIds.join(","),
166
+ );
167
+ // Add version param for version mismatch detection
168
+ if (version) {
169
+ url.searchParams.set("_rsc_v", version);
170
+ }
117
171
 
118
- // Encode arguments
119
- const encodedBody = await deps.encodeReply(args, { temporaryReferences });
172
+ // Encode arguments
173
+ const encodedBody = await deps.encodeReply(args, { temporaryReferences });
120
174
 
121
- console.log(
122
- `[Browser] Encoded body type:`,
123
- typeof encodedBody,
124
- encodedBody instanceof FormData
125
- );
126
- console.log(`[Browser] Sending action request to: ${url.href}`);
127
- console.log(
128
- `[Browser] Current segments: ${segmentState.currentSegmentIds.join(", ")}`
129
- );
175
+ log("sending action request", {
176
+ url: url.href,
177
+ bodyType: typeof encodedBody,
178
+ isFormData: encodedBody instanceof FormData,
179
+ segmentCount: segmentState.currentSegmentIds.length,
180
+ });
130
181
 
131
- // Track when the stream completes
132
- let resolveStreamComplete: () => void;
133
- const streamComplete = new Promise<void>((resolve) => {
134
- resolveStreamComplete = resolve;
135
- });
136
-
137
- // Get intercept source URL if in intercept context
138
- const interceptSourceUrl = store.getInterceptSourceUrl();
139
-
140
- // Track streaming token - will be set when response arrives
141
- let streamingToken: { end(): void } | null = null;
142
-
143
- // Send action request with stream tracking
144
- const responsePromise = fetch(url, {
145
- method: "POST",
146
- headers: {
147
- "rsc-action": id,
148
- "X-RSC-Router-Client-Path": segmentState.currentUrl,
149
- // Send intercept source URL so server can maintain intercept context
150
- ...(interceptSourceUrl && {
151
- "X-RSC-Router-Intercept-Source": interceptSourceUrl,
152
- }),
153
- },
154
- body: encodedBody,
155
- }).then(async (response) => {
156
- // Check for version mismatch - server wants us to reload
157
- const reloadUrl = response.headers.get("X-RSC-Reload");
158
- if (reloadUrl) {
159
- console.log(`[Browser] Version mismatch on action - reloading: ${reloadUrl}`);
160
- window.location.href = reloadUrl;
161
- // Return a never-resolving promise to prevent further processing
162
- return new Promise<Response>(() => {});
163
- }
182
+ // Track when the stream completes
183
+ let resolveStreamComplete: () => void;
184
+ const streamComplete = new Promise<void>((resolve) => {
185
+ resolveStreamComplete = resolve;
186
+ });
164
187
 
165
- // Start streaming immediately when response arrives
166
- if (!handle.signal.aborted) {
167
- streamingToken = handle.startStreaming();
168
- }
188
+ // Get intercept source URL if in intercept context
189
+ const interceptSourceUrl = store.getInterceptSourceUrl();
190
+
191
+ // Track streaming token - will be set when response arrives
192
+ let streamingToken: { end(): void } | null = null;
193
+
194
+ // Use a dedicated abort controller for the fetch so we can cancel network
195
+ // I/O without disrupting the Flight stream once the response has arrived.
196
+ // Aborting a response mid-stream causes React's Flight decoder to throw
197
+ // asynchronous unhandled errors (BodyStreamBuffer was aborted).
198
+ const fetchAbort = new AbortController();
199
+ const onHandleAbort = () => fetchAbort.abort();
200
+ handle.signal.addEventListener("abort", onHandleAbort, { once: true });
201
+
202
+ // Send action request with stream tracking
203
+ const responsePromise = fetch(url, {
204
+ method: "POST",
205
+ headers: {
206
+ "rsc-action": id,
207
+ "X-RSC-Router-Client-Path": segmentState.currentUrl,
208
+ ...(tx && { "X-RSC-Router-Request-Id": tx.requestId }),
209
+ // Send intercept source URL so server can maintain intercept context
210
+ ...(interceptSourceUrl && {
211
+ "X-RSC-Router-Intercept-Source": interceptSourceUrl,
212
+ }),
213
+ },
214
+ body: encodedBody,
215
+ signal: fetchAbort.signal,
216
+ }).then(async (response) => {
217
+ // Response arrived — disconnect fetch abort from handle abort so
218
+ // abortAllActions() doesn't disrupt the in-progress Flight stream.
219
+ handle.signal.removeEventListener("abort", onHandleAbort);
220
+
221
+ // Check for version mismatch - server wants us to reload
222
+ const reload = extractRscHeaderUrl(response, "X-RSC-Reload");
223
+ if (reload === "blocked") {
224
+ resolveStreamComplete();
225
+ return emptyResponse();
226
+ }
227
+ if (reload) {
228
+ log("version mismatch on action, reloading", {
229
+ reloadUrl: reload.url,
230
+ });
231
+ window.location.href = reload.url;
232
+ return new Promise<Response>(() => {});
233
+ }
169
234
 
170
- if (!response.body) {
171
- // No body means stream is already complete
172
- streamingToken?.end();
173
- resolveStreamComplete();
174
- return response;
175
- }
235
+ // Simple redirect from action (no state, no RSC payload).
236
+ // Short-circuits before createFromFetch no Flight deserialization needed.
237
+ // Check handle.signal.aborted to avoid redirecting from a stale action
238
+ // when the user has already navigated away.
239
+ const redirect = extractRscHeaderUrl(response, "X-RSC-Redirect");
240
+ if (redirect && redirect !== "blocked" && !handle.signal.aborted) {
241
+ log("action simple redirect", { url: redirect.url });
242
+ handle.complete(undefined);
243
+ if (onNavigate) {
244
+ await onNavigate(redirect.url, {
245
+ replace: true,
246
+ _skipCache: true,
247
+ });
248
+ } else {
249
+ window.location.href = redirect.url;
250
+ }
251
+ return new Promise<Response>(() => {});
252
+ }
253
+ if (redirect === "blocked") {
254
+ resolveStreamComplete();
255
+ return emptyResponse();
256
+ }
176
257
 
177
- // Tee the stream: one for RSC runtime, one for tracking completion
178
- const [rscStream, trackingStream] = response.body.tee();
258
+ // Start streaming immediately when response arrives
259
+ if (!handle.signal.aborted) {
260
+ streamingToken = handle.startStreaming();
261
+ }
179
262
 
180
- // Consume the tracking stream to detect when it closes
181
- (async () => {
182
- const reader = trackingStream.getReader();
183
- try {
184
- while (true) {
185
- const { done } = await reader.read();
186
- if (done) break;
187
- }
188
- } finally {
189
- reader.releaseLock();
190
- console.log("[STREAMING] RSC stream complete");
263
+ return teeWithCompletion(response, () => {
264
+ log("stream complete");
191
265
  streamingToken?.end();
192
266
  resolveStreamComplete();
193
- }
194
- })().catch((error) => {
195
- console.error("[STREAMING] Error reading tracking stream:", error);
196
- streamingToken?.end();
267
+ });
197
268
  });
198
269
 
199
- // Return response with the RSC stream
200
- return new Response(rscStream, {
201
- headers: response.headers,
202
- status: response.status,
203
- statusText: response.statusText,
204
- });
205
- });
270
+ // Deserialize response (MUST use same temporaryReferences)
271
+ let payload: RscPayload;
272
+ try {
273
+ payload = await deps.createFromFetch<RscPayload>(responsePromise, {
274
+ temporaryReferences,
275
+ });
276
+ } catch (error) {
277
+ // Clean up streaming token on error (may be null if fetch failed before .then() ran)
278
+ // The token is assigned in .then() callback which runs before this catch block,
279
+ // but TypeScript doesn't track cross-async assignments, so use type assertion
280
+ (streamingToken as { end(): void } | null)?.end();
281
+ // resolveStreamComplete is assigned in the Promise constructor so it's safe to call
282
+ resolveStreamComplete!();
283
+
284
+ // Silently swallow abort errors — the action was intentionally cancelled
285
+ // (e.g., user navigated away or abortAllActions was called).
286
+ // Return undefined instead of throwing to avoid surfacing as a page error.
287
+ // Check both DOMException AbortError and stream-level abort messages
288
+ // (BodyStreamBuffer was aborted) that propagate from the aborted fetch.
289
+ if (handle.signal.aborted) {
290
+ return undefined;
291
+ }
206
292
 
207
- // Deserialize response (MUST use same temporaryReferences)
208
- let payload: RscPayload;
209
- try {
210
- payload = await deps.createFromFetch<RscPayload>(responsePromise, {
211
- temporaryReferences,
293
+ // Convert network-level errors to NetworkError for proper handling
294
+ const networkError = toNetworkError(error, {
295
+ url: url.toString(),
296
+ operation: "action",
297
+ });
298
+ if (networkError) {
299
+ handle.fail(networkError);
300
+ emitNetworkError(onUpdate, networkError, segmentState.currentUrl);
301
+ throw networkError;
302
+ }
303
+ throw error;
304
+ }
305
+
306
+ log("action response received", {
307
+ isPartial: payload.metadata?.isPartial,
308
+ isError: payload.metadata?.isError,
309
+ matchedCount: payload.metadata?.matched?.length ?? 0,
310
+ diffCount: payload.metadata?.diff?.length ?? 0,
212
311
  });
213
- } catch (error) {
214
- // Clean up streaming token on error (may be null if fetch failed before .then() ran)
215
- // The token is assigned in .then() callback which runs before this catch block,
216
- // but TypeScript doesn't track cross-async assignments, so use type assertion
217
- (streamingToken as { end(): void } | null)?.end();
218
- // resolveStreamComplete is assigned in the Promise constructor so it's safe to call
219
- resolveStreamComplete!();
220
-
221
- // Convert network-level errors to NetworkError for proper handling
222
- if (isNetworkError(error)) {
223
- const networkError = new NetworkError(
224
- "Unable to connect to server. Please check your connection.",
225
- {
226
- cause: error,
227
- url: url.toString(),
228
- operation: "action",
229
- }
230
- );
231
312
 
232
- // Mark action as failed
233
- handle.fail(networkError);
313
+ // Guard: if the action was aborted while streaming (e.g., user navigated
314
+ // away or abortAllActions fired), bail out before any reconcile/render/cache
315
+ // writes to avoid overwriting the current UI with stale action results.
316
+ if (handle.signal.aborted) {
317
+ log("action aborted after response, skipping reconciliation");
318
+ return undefined;
319
+ }
234
320
 
235
- // Emit the network error so the root error boundary can catch it
236
- // NetworkErrorThrower throws during render to trigger the error boundary
237
- startTransition(() => {
238
- onUpdate({
239
- root: createElement(NetworkErrorThrower, { error: networkError }),
240
- metadata: {
241
- pathname: segmentState.currentUrl,
242
- segments: [],
243
- isError: true,
244
- },
321
+ // Process response
322
+ const { metadata, returnValue } = payload;
323
+
324
+ // Handle action redirect: server converted the redirect to a Flight payload
325
+ // so we can perform SPA navigation instead of a full page reload.
326
+ // Check handle.signal.aborted to avoid redirecting from a stale action
327
+ // when the user has already navigated away.
328
+ if (metadata?.redirect && !handle.signal.aborted) {
329
+ const redirectUrl = validateRedirectOrigin(
330
+ metadata.redirect.url,
331
+ window.location.origin,
332
+ );
333
+ if (!redirectUrl) {
334
+ log("blocked action redirect payload", {
335
+ url: metadata.redirect.url,
245
336
  });
246
- });
247
-
248
- throw networkError;
337
+ handle.complete(returnValue?.data);
338
+ return returnValue?.data;
339
+ }
340
+ const redirectState = metadata.locationState;
341
+ log("action redirect", { url: redirectUrl });
342
+ handle.complete(returnValue?.data);
343
+ if (onNavigate) {
344
+ await onNavigate(redirectUrl, {
345
+ state: redirectState,
346
+ replace: true,
347
+ _skipCache: true,
348
+ });
349
+ } else {
350
+ window.location.href = redirectUrl;
351
+ }
352
+ return returnValue?.data;
249
353
  }
250
- throw error;
251
- }
252
354
 
253
- console.log(`[Browser] Action response received:`, payload.metadata);
355
+ // Bail out if the action was aborted after deserialization (e.g. user
356
+ // navigated away or abortAllActions was called while the Flight stream
357
+ // was being consumed). Without this check the code below would mutate
358
+ // the store / UI for a stale action.
359
+ if (handle.signal.aborted) {
360
+ log("action aborted after deserialization, skipping mutations");
361
+ return returnValue?.data;
362
+ }
254
363
 
255
- // Process response
256
- const { metadata, returnValue } = payload;
257
- const { matched, diff, segments, isPartial, isError } = metadata || {};
364
+ const { matched, diff, segments, isPartial, isError } = metadata || {};
258
365
 
259
- // Log action result
260
- if (returnValue) {
261
- console.log(`[Browser] Action result:`, returnValue);
262
- if (!returnValue.ok) {
366
+ // Log action result
367
+ if (returnValue && !returnValue.ok) {
263
368
  console.error(`[Browser] Action failed:`, returnValue.data);
264
369
  }
265
- }
266
370
 
267
- // Handle error responses with error boundary UI
268
- if (isError && isPartial && segments && diff) {
269
- console.log(`[Browser] Processing error boundary response`);
371
+ // Handle error responses with error boundary UI
372
+ if (isError && isPartial && segments && diff) {
373
+ log("processing error boundary response");
270
374
 
271
- // Abort all other pending action requests - error takes precedence
272
- // This prevents other actions from completing and overwriting the error UI
273
- eventController.abortAllActions();
375
+ // Fail current handle BEFORE aborting all actions so the event controller
376
+ // records the error state (abortAllActions clears inflight entries)
377
+ if (returnValue && !returnValue.ok) {
378
+ handle.fail(returnValue.data);
379
+ }
274
380
 
275
- // Clear concurrent action tracking - no consolidation needed when showing error
276
- handle.clearConsolidation();
381
+ // Abort all other pending action requests - error takes precedence
382
+ // This prevents other actions from completing and overwriting the error UI
383
+ eventController.abortAllActions();
277
384
 
278
- // Get current page's cached segments
279
- const currentKey = store.getHistoryKey();
280
- const cached = store.getCachedSegments(currentKey);
281
- const cachedSegments = cached?.segments || [];
385
+ // Clear concurrent action tracking - no consolidation needed when showing error
386
+ handle.clearConsolidation();
282
387
 
283
- // Create lookup for error segment from server
284
- const errorSegmentMap = new Map<string, ResolvedSegment>();
285
- segments.forEach((s: ResolvedSegment) => errorSegmentMap.set(s.id, s));
286
-
287
- // For error responses, use ALL cached segments but replace the errored one
288
- // This preserves sibling layouts that aren't in the parent chain
289
- const fullSegments = cachedSegments.map((cached) => {
290
- // Replace the error segment with the one from server
291
- const fromServer = errorSegmentMap.get(cached.id);
292
- if (fromServer) return fromServer;
293
- return cached;
294
- });
388
+ // Get current page's cached segments
389
+ const currentKey = store.getHistoryKey();
390
+ const cached = store.getCachedSegments(currentKey);
391
+ const cachedSegments = cached?.segments || [];
392
+
393
+ // Reconcile error segments with cached tree
394
+ const errorResult = reconcileErrorSegments(cachedSegments, segments);
395
+
396
+ // Render the full tree with error segment merged with parent layouts
397
+ const errorTree = await renderSegments(errorResult.mainSegments, {
398
+ isAction: true,
399
+ interceptSegments:
400
+ errorResult.interceptSegments.length > 0
401
+ ? errorResult.interceptSegments
402
+ : undefined,
403
+ });
295
404
 
296
- // INTERCEPT HANDLING: Separate intercept segments for explicit injection
297
- const isInterceptSegment = (s: ResolvedSegment) =>
298
- s.namespace?.startsWith("intercept:") ||
299
- (s.type === "parallel" && s.id.includes(".@"));
300
-
301
- const interceptSegments = fullSegments.filter(isInterceptSegment);
302
- const mainSegments = fullSegments.filter((s) => !isInterceptSegment(s));
303
-
304
- // Render the full tree with error segment merged with parent layouts
305
- const errorRenderOptions = {
306
- isAction: true,
307
- interceptSegments:
308
- interceptSegments.length > 0 ? interceptSegments : undefined,
309
- };
310
- const errorTree = await renderSegments(mainSegments, errorRenderOptions);
311
-
312
- // Update UI with error boundary
313
- startTransition(() => {
314
- onUpdate({ root: errorTree, metadata: metadata! });
315
- });
405
+ // Re-check route stability after async renderSegments user may have
406
+ // navigated away while the error tree was being prepared.
407
+ if (window.location.pathname !== actionStartPathname) {
408
+ log("user navigated during error render, skipping");
409
+ if (returnValue && !returnValue.ok) {
410
+ throw returnValue.data;
411
+ }
412
+ handle.complete(undefined);
413
+ return undefined;
414
+ }
415
+ const currentKeyNow = store.getHistoryKey();
416
+ if (currentKeyNow !== currentKey) {
417
+ log("history key changed during error render, skipping cache update");
418
+ if (returnValue && !returnValue.ok) {
419
+ throw returnValue.data;
420
+ }
421
+ handle.complete(undefined);
422
+ return undefined;
423
+ }
316
424
 
317
- console.log(`[Browser] Error boundary UI rendered`);
425
+ // Update UI with error boundary
426
+ startTransition(() => {
427
+ onUpdate({ root: errorTree, metadata: metadata! });
428
+ });
318
429
 
319
- // Update segment tracking to exclude error segment IDs
320
- const errorSegmentIds = new Set(diff);
321
- const segmentIdsAfterError = segmentState.currentSegmentIds.filter(
322
- (id) => !errorSegmentIds.has(id)
323
- );
430
+ // Update segment tracking to exclude error segment IDs
431
+ const errorSegmentIds = new Set(diff);
432
+ const segmentIdsAfterError = segmentState.currentSegmentIds.filter(
433
+ (id) => !errorSegmentIds.has(id),
434
+ );
324
435
 
325
- // Update store state
326
- store.setSegmentIds(segmentIdsAfterError);
327
- const currentHandleData = eventController.getHandleState().data;
328
- store.cacheSegmentsForHistory(currentKey, fullSegments, currentHandleData);
436
+ // Update store state
437
+ store.setSegmentIds(segmentIdsAfterError);
438
+ const currentHandleData = eventController.getHandleState().data;
439
+ store.cacheSegmentsForHistory(
440
+ currentKey,
441
+ errorResult.segments,
442
+ currentHandleData,
443
+ );
329
444
 
330
- console.log(
331
- `[Browser] Segment IDs updated (excluding error segments):`,
332
- segmentIdsAfterError
333
- );
445
+ // Throw the error so the action promise rejects
446
+ if (returnValue && !returnValue.ok) {
447
+ throw returnValue.data;
448
+ }
334
449
 
335
- // Throw the error so the action promise rejects
336
- if (returnValue && !returnValue.ok) {
337
- handle.fail(returnValue.data);
338
- throw returnValue.data;
450
+ // No error in returnValue (shouldn't happen with isError: true)
451
+ handle.complete(undefined);
452
+ return undefined;
339
453
  }
340
454
 
341
- // No error in returnValue (shouldn't happen with isError: true)
342
- handle.complete(undefined);
343
- return undefined;
344
- }
455
+ if (!isPartial) {
456
+ // Protocol invariant: action revalidation responses MUST be partial.
457
+ // The server always sends isPartial: true for successful revalidation
458
+ // and isPartial: true + isError: true for error boundary responses.
459
+ // A non-partial payload here indicates a server-side bug.
460
+ throw new Error(
461
+ `[Browser] Action response missing isPartial — the server must ` +
462
+ `always send partial payloads for action revalidation.`,
463
+ );
464
+ }
345
465
 
346
- if (isPartial) {
347
- console.log(`[Browser] Processing partial update`);
348
- console.log(
349
- `[Browser] Server sent ${segments?.length || 0} segments in diff:`,
350
- diff
351
- );
352
- console.log(`[Browser] Server expects client to have:`, matched);
466
+ log("processing partial update", {
467
+ serverSegments: segments?.length ?? 0,
468
+ diff: diff?.join(", ") ?? "",
469
+ matched: matched?.join(", ") ?? "",
470
+ });
353
471
 
354
472
  // Record revalidated segments for concurrent action tracking
355
473
  if (diff) {
@@ -360,375 +478,174 @@ export function createServerActionBridge(
360
478
  const currentKey = store.getHistoryKey();
361
479
  const cached = store.getCachedSegments(currentKey);
362
480
  const cachedSegments = cached?.segments || [];
363
- const currentSegmentMap = new Map<string, ResolvedSegment>();
364
- cachedSegments.forEach((s) => currentSegmentMap.set(s.id, s));
365
-
366
- console.log(
367
- `[Browser] Client cache has ${currentSegmentMap.size} entries:`,
368
- Array.from(currentSegmentMap.keys())
369
- );
370
-
371
- // Create lookup for new segments from server
372
- const newSegmentMap = new Map<string, ResolvedSegment>();
373
- (segments || []).forEach((s: ResolvedSegment) =>
374
- newSegmentMap.set(s.id, s)
375
- );
376
481
 
377
482
  if (!matched) {
378
- console.log(`[Browser] Matched segments: ${matched}`);
379
483
  throw new Error("No matched segments in response");
380
484
  }
381
485
 
382
- // Rebuild from matched: merge server segments with cached, or use cached as fallback
383
- const fullSegments = matched
384
- .map((segId: string) => {
385
- const fromServer = newSegmentMap.get(segId);
386
- const fromCache = currentSegmentMap.get(segId);
387
-
388
- if (fromServer) {
389
- // Server returned this segment - check if we need to merge partial loaders
390
- if (needsLoaderMerge(fromServer, fromCache)) {
391
- return mergeSegmentLoaders(fromServer, fromCache);
392
- }
393
- // When server returns component: null for a layout segment, it means
394
- // "this segment doesn't need re-rendering" - preserve the cached component
395
- // to maintain the outlet chain and prevent React tree changes
396
- const cached = currentSegmentMap.get(segId); // Re-fetch to avoid type narrowing issues
397
- if (
398
- fromServer.component === null &&
399
- fromServer.type === "layout" &&
400
- cached?.component != null
401
- ) {
402
- console.log(
403
- `[Browser] Preserving cached component for layout ${segId} (server returned null)`
404
- );
405
- return { ...fromServer, component: cached.component };
406
- }
407
- // Dev-mode assertion: warn if tree structure would change
408
- if (cached) {
409
- assertSegmentStructure(cached, fromServer, "action-bridge");
410
- }
411
- // Preserve cached loading value to maintain consistent tree structure.
412
- // SSR may set loading=false for skipSSR routes, but actions set
413
- // loading=<skeleton> (isSSR=false). Changing loading between renders
414
- // alters the React tree (with/without RouteContentWrapper), causing
415
- // remounts that destroy useActionState.
416
- if (
417
- cached &&
418
- cached.loading !== undefined &&
419
- fromServer.loading !== cached.loading
420
- ) {
421
- return { ...fromServer, loading: cached.loading };
422
- }
423
- return fromServer;
424
- }
425
-
426
- // Fall back to current page's cached segments
427
- if (!fromCache) {
428
- console.error(`[Browser] MISSING SEGMENT: ${segId} not in cache!`);
429
- }
430
- return fromCache;
431
- })
432
- .filter(Boolean) as ResolvedSegment[];
433
-
434
- console.log(
435
- `[Browser] Rebuilt ${fullSegments.length} segments from matched array`
436
- );
486
+ // Reconcile server segments with cached segments (single source of truth)
487
+ const reconciled = reconcileSegments({
488
+ actor: "action",
489
+ matched,
490
+ diff: diff || [],
491
+ serverSegments: segments || [],
492
+ cachedSegments,
493
+ });
494
+ const fullSegments = reconciled.segments;
437
495
 
438
496
  const returnData = returnValue?.data;
439
497
 
440
- console.log(
441
- `[Browser] Action complete - UI updated (after action state committed)`
442
- );
443
-
444
498
  if (returnValue && !returnValue.ok) {
445
499
  handle.fail(returnValue.data);
446
500
  throw returnValue.data;
447
501
  }
448
502
 
449
- // Check if user navigated away during the action
450
- const currentPathname = window.location.pathname;
451
- const currentLocationKey = window.history.state?.key;
452
- const userNavigatedAway =
453
- currentPathname !== actionStartPathname ||
454
- currentLocationKey !== locationKey;
455
-
456
- if (userNavigatedAway) {
457
- console.log(
458
- `[Browser] User navigated away during action (${actionStartPathname} -> ${currentPathname})`
459
- );
460
- // Clear concurrent action tracking - don't consolidate for old route's segments
461
- handle.clearConsolidation();
462
-
463
- // Check if the history key changed (different cache entry)
464
- // This happens when navigating between intercept and non-intercept routes
465
- if (currentLocationKey !== locationKey) {
466
- console.log(
467
- `[Browser] History key changed (${locationKey} -> ${currentLocationKey}), triggering background revalidation`
468
- );
503
+ // Classify the post-reconciliation scenario
504
+ const scenario = classifyActionOutcome({
505
+ handleId: handle.id,
506
+ inflightActions: eventController.getInflightActions(),
507
+ hadAnyConcurrentActions: eventController.hadAnyConcurrentActions(),
508
+ revalidatedSegments: handle.getRevalidatedSegments(),
509
+ actionStartPathname,
510
+ currentPathname: window.location.pathname,
511
+ actionStartLocationKey: locationKey,
512
+ currentLocationKey: window.history.state?.key,
513
+ reconciledSegmentCount: fullSegments.length,
514
+ matchedCount: matched.length,
515
+ currentInterceptSource: store.getInterceptSourceUrl(),
516
+ });
469
517
 
470
- // The action completed on the server, but the user navigated to a different route.
471
- // The navigation fetch may have gotten stale data (before action committed).
472
- // Trigger a background revalidation of the CURRENT route to get fresh data.
473
- // User navigated to a different history entry.
474
- // Check if we should do background revalidation:
475
- // - YES if user is on a non-intercept route (safe to revalidate)
476
- // - NO if user is on an intercept route (would lose background segments)
477
- const currentInterceptSource = store.getInterceptSourceUrl();
478
- if (currentInterceptSource) {
479
- // User is on an intercept route - skip revalidation to preserve background
480
- console.log(
481
- `[Browser] Skipping background revalidation - user on intercept route`
482
- );
483
- } else {
484
- // User is on a non-intercept route - safe to revalidate
485
- console.log(
486
- `[Browser] History key changed, triggering background revalidation`
487
- );
488
- store.markCacheAsStaleAndBroadcast();
489
- using navTx = createNavigationTransaction(
490
- store,
491
- eventController,
492
- window.location.href,
493
- { replace: true, skipLoadingState: true }
494
- );
495
- fetchPartialUpdate(
496
- window.location.href,
497
- [],
498
- false,
499
- navTx.handle.signal,
500
- navTx.with({
501
- url: window.location.href,
502
- storeOnly: true,
503
- }),
504
- {
505
- isAction: true,
506
- }
507
- ).then(() => {
508
- console.log(`[Browser] Background revalidation complete`);
509
- });
518
+ switch (scenario.type) {
519
+ case "navigated-away": {
520
+ log("user navigated away during action", {
521
+ from: actionStartPathname,
522
+ to: window.location.pathname,
523
+ historyKeyChanged: scenario.historyKeyChanged,
524
+ });
525
+ // Clear concurrent action tracking - don't consolidate for old route's segments
526
+ handle.clearConsolidation();
527
+
528
+ if (scenario.historyKeyChanged) {
529
+ if (!scenario.onInterceptRoute) {
530
+ store.markCacheAsStaleAndBroadcast();
531
+ refetchRoute().catch((error) => {
532
+ if (isBackgroundSuppressible(error)) return;
533
+ console.error(
534
+ "[Browser] Background revalidation failed:",
535
+ error,
536
+ );
537
+ });
538
+ }
539
+ break;
510
540
  }
511
541
 
512
- handle.complete(returnData);
513
- return returnData;
542
+ // Same history key but different pathname - safe to refetch current route
543
+ store.markCacheAsStaleAndBroadcast();
544
+ await refetchRoute({
545
+ interceptSourceUrl: store.getInterceptSourceUrl(),
546
+ });
547
+ break;
514
548
  }
515
549
 
516
- // Same history key but different pathname (e.g., same-page navigation)
517
- // Safe to refetch current route
518
- console.log(`[Browser] Same history key, refetching current route`);
519
- store.markCacheAsStaleAndBroadcast();
520
- using navTx = createNavigationTransaction(
521
- store,
522
- eventController,
523
- window.location.href,
524
- { replace: true, skipLoadingState: true }
525
- );
526
- // Preserve intercept context
527
- const currentInterceptSource = store.getInterceptSourceUrl();
528
- await fetchPartialUpdate(
529
- window.location.href,
530
- [], // Empty array = refetch all segments for current route
531
- false,
532
- navTx.handle.signal,
533
- navTx.with({
534
- url: window.location.href,
535
- storeOnly: true,
536
- intercept: !!currentInterceptSource,
537
- interceptSourceUrl: currentInterceptSource ?? undefined,
538
- }),
539
- {
540
- isAction: true,
541
- interceptSourceUrl: currentInterceptSource ?? undefined,
542
- }
543
- );
544
- console.log(`[Browser] Refetch after navigation complete`);
545
- handle.complete(returnData);
546
- return returnData;
547
- }
548
-
549
- // HMR resilience check - only runs if user DIDN'T navigate away
550
- if (fullSegments.length < matched.length) {
551
- console.warn(
552
- `[Browser] Missing segments after action (HMR detected), refetching...`
553
- );
554
-
555
- using navTx = createNavigationTransaction(
556
- store,
557
- eventController,
558
- window.location.href,
559
- { replace: true, skipLoadingState: true }
560
- );
561
- await fetchPartialUpdate(
562
- window.location.href,
563
- [],
564
- false,
565
- navTx.handle.signal,
566
- navTx.with({
567
- url: window.location.href,
568
- storeOnly: true,
569
- intercept: !!interceptSourceUrl,
570
- interceptSourceUrl: interceptSourceUrl ?? undefined,
571
- }),
572
- {
573
- isAction: true,
574
- interceptSourceUrl: interceptSourceUrl ?? undefined,
575
- }
576
- );
577
- console.log(
578
- `[Browser] Refetch complete (HMR), now returning action result`
579
- );
550
+ case "hmr-missing": {
551
+ console.warn(
552
+ `[Browser] Missing segments after action (HMR detected), refetching...`,
553
+ );
554
+ await refetchRoute({ interceptSourceUrl });
555
+ store.broadcastCacheInvalidation();
556
+ break;
557
+ }
580
558
 
581
- // Broadcast to other tabs
582
- store.broadcastCacheInvalidation();
583
- handle.complete(returnData);
584
- return returnData;
585
- }
559
+ case "consolidation-needed": {
560
+ log("consolidation fetch needed", {
561
+ segmentIds: scenario.segmentIds,
562
+ });
563
+ // Calculate segments to send (exclude the ones we want fresh)
564
+ const currentSegmentIds = store.getSegmentState().currentSegmentIds;
565
+ const segmentsToSend = currentSegmentIds.filter(
566
+ (sid) => !scenario.segmentIds.includes(sid),
567
+ );
586
568
 
587
- // Check if we need a consolidation fetch due to concurrent actions
588
- const consolidationSegments = handle.getConsolidationSegments();
569
+ // Clear consolidation tracking before fetch
570
+ handle.clearConsolidation();
589
571
 
590
- if (consolidationSegments && consolidationSegments.length > 0) {
591
- // This is the last concurrent action - do consolidation fetch
592
- console.log(
593
- `[Browser] Concurrent actions detected - consolidation fetch needed for:`,
594
- consolidationSegments
595
- );
596
- // Calculate segments to send (exclude the ones we want fresh)
597
- const currentSegmentIds = store.getSegmentState().currentSegmentIds;
598
- const segmentsToSend = currentSegmentIds.filter(
599
- (id) => !consolidationSegments.includes(id)
600
- );
572
+ await refetchRoute({
573
+ segments: segmentsToSend,
574
+ interceptSourceUrl,
575
+ });
576
+ store.broadcastCacheInvalidation();
577
+ break;
578
+ }
601
579
 
602
- console.log(
603
- `[Browser] Sending segments (excluding revalidated):`,
604
- segmentsToSend
605
- );
580
+ case "concurrent-skip": {
581
+ log("skipping UI update, other actions fetching", {
582
+ otherCount: scenario.otherFetchingCount,
583
+ });
584
+ // Only update store if history key hasn't changed (user didn't navigate away)
585
+ const currentKeyNow = store.getHistoryKey();
586
+ if (currentKeyNow === currentKey) {
587
+ store.setSegmentIds(matched);
588
+ const currentHandleData = eventController.getHandleState().data;
589
+ store.cacheSegmentsForHistory(
590
+ currentKey,
591
+ fullSegments,
592
+ currentHandleData,
593
+ );
594
+ }
595
+ break;
596
+ }
606
597
 
607
- // Clear consolidation tracking before fetch
608
- handle.clearConsolidation();
598
+ case "normal": {
599
+ // Prepare new tree (await loader data resolution)
600
+ const newTree = await renderSegments(reconciled.mainSegments, {
601
+ isAction: true,
602
+ interceptSegments:
603
+ reconciled.interceptSegments.length > 0
604
+ ? reconciled.interceptSegments
605
+ : undefined,
606
+ });
609
607
 
610
- using navTx = createNavigationTransaction(
611
- store,
612
- eventController,
613
- window.location.href,
614
- { replace: true, skipLoadingState: true }
615
- );
608
+ // Re-check if user navigated away (could happen during async renderSegments)
609
+ if (window.location.pathname !== actionStartPathname) {
610
+ log("user navigated during render, skipping");
611
+ break;
612
+ }
616
613
 
617
- console.warn("Fetch partial", id);
618
- await fetchPartialUpdate(
619
- window.location.href,
620
- segmentsToSend,
621
- false,
622
- navTx.handle.signal,
623
- navTx.with({
624
- url: window.location.href,
625
- storeOnly: true,
626
- intercept: !!interceptSourceUrl,
627
- interceptSourceUrl: interceptSourceUrl ?? undefined,
628
- }),
629
- {
630
- isAction: true,
631
- interceptSourceUrl: interceptSourceUrl ?? undefined,
614
+ // Verify the store's current key still matches what we captured at action start
615
+ // If they differ, user navigated away and we should NOT cache under the old key
616
+ const currentKeyNow = store.getHistoryKey();
617
+ if (currentKeyNow !== currentKey) {
618
+ log("history key changed during action, skipping cache update");
619
+ break;
632
620
  }
633
- );
634
621
 
635
- console.log(`[Browser] Consolidation fetch complete`);
636
- // Broadcast to other tabs
637
- store.broadcastCacheInvalidation();
638
- console.log(
639
- `[Browser] Consolidate/Reconcile - Returning to React:`,
640
- returnData
641
- );
622
+ startTransition(() => {
623
+ onUpdate({ root: newTree, metadata: metadata! });
624
+ });
642
625
 
643
- handle.complete(returnData);
644
- return returnData;
645
- }
626
+ // Apply server-set location state to history.state (non-redirect flow)
627
+ const actionLocationState = metadata?.locationState;
628
+ if (actionLocationState) {
629
+ mergeLocationState(actionLocationState);
630
+ }
646
631
 
647
- // Check if there are OTHER actions still fetching (waiting for server response)
648
- // Exclude the current action since we already have our response
649
- // We don't need to wait for streaming to complete - just for the response to arrive
650
- const otherFetchingActions = [...eventController.getInflightActions().values()].filter(
651
- (a) => a.phase === "fetching" && a.id !== handle.id
652
- );
653
- if (otherFetchingActions.length > 0) {
654
- console.log(
655
- `[Browser] Skipping UI update - ${otherFetchingActions.length} other action(s) still fetching`
656
- );
657
- console.log(
658
- `[Browser] Multi actions - Returning to React (no cache clear):`,
659
- returnData
660
- );
661
- // Only update store if history key hasn't changed (user didn't navigate away)
662
- const currentKeyNow = store.getHistoryKey();
663
- if (currentKeyNow === currentKey) {
632
+ // Update store state
664
633
  store.setSegmentIds(matched);
665
634
  const currentHandleData = eventController.getHandleState().data;
666
- store.cacheSegmentsForHistory(currentKey, fullSegments, currentHandleData);
667
- } else {
668
- console.log(
669
- `[Browser] History key changed during multi-action (${currentKey} -> ${currentKeyNow}), skipping cache update`
635
+ store.cacheSegmentsForHistory(
636
+ currentKey,
637
+ fullSegments,
638
+ currentHandleData,
670
639
  );
640
+ store.markCacheAsStaleAndBroadcast();
641
+ break;
671
642
  }
672
- handle.complete(returnData);
673
- return returnData;
674
- }
675
-
676
- // No concurrent actions - normal flow with single action
677
- // INTERCEPT HANDLING: Separate intercept segments for explicit injection
678
- const isInterceptSegment = (s: ResolvedSegment) =>
679
- s.namespace?.startsWith("intercept:") ||
680
- (s.type === "parallel" && s.id.includes(".@"));
681
-
682
- const interceptSegments = fullSegments.filter(isInterceptSegment);
683
- const mainSegments = fullSegments.filter((s) => !isInterceptSegment(s));
684
-
685
- // Prepare new tree (await loader data resolution)
686
- const renderOptions = {
687
- isAction: true,
688
- interceptSegments:
689
- interceptSegments.length > 0 ? interceptSegments : undefined,
690
- };
691
- const newTree = await renderSegments(mainSegments, renderOptions);
692
-
693
- // Re-check if user navigated away (could happen during async wait)
694
- const currentPathnameNow = window.location.pathname;
695
- if (currentPathnameNow !== actionStartPathname) {
696
- console.log(
697
- `[Browser] User navigated during UI update scheduling, skipping`
698
- );
699
- handle.complete(returnData);
700
- return returnData;
701
- }
702
-
703
- // Verify the store's current key still matches what we captured at action start
704
- // If they differ, user navigated away and we should NOT cache under the old key
705
- const currentKeyNow = store.getHistoryKey();
706
- if (currentKeyNow !== currentKey) {
707
- console.log(
708
- `[Browser] History key changed during action (${currentKey} -> ${currentKeyNow}), skipping cache update`
709
- );
710
- handle.complete(returnData);
711
- return returnData;
712
643
  }
713
644
 
714
- startTransition(() => {
715
- onUpdate({ root: newTree, metadata: metadata! });
716
- });
717
-
718
- // Update store state
719
- store.setSegmentIds(matched);
720
- const currentHandleData = eventController.getHandleState().data;
721
- store.cacheSegmentsForHistory(currentKey, fullSegments, currentHandleData);
722
- store.markCacheAsStaleAndBroadcast();
723
-
724
- console.log(`[Browser] Normal - Returning to React:`, returnData);
725
645
  handle.complete(returnData);
726
646
  return returnData;
727
- } else {
728
- // Full update not supported for actions
729
- throw new Error(
730
- `[Browser] Full update after action is not supported yet`
731
- );
647
+ } finally {
648
+ handle[Symbol.dispose]();
732
649
  }
733
650
  }
734
651
 
@@ -743,18 +660,6 @@ export function createServerActionBridge(
743
660
  }
744
661
  deps.setServerCallback(handleServerAction);
745
662
  isRegistered = true;
746
- console.log("[Browser] Server action callback registered");
747
- },
748
-
749
- /**
750
- * Unregister the server action callback
751
- */
752
- unregister(): void {
753
- if (!isRegistered) {
754
- return;
755
- }
756
- isRegistered = false;
757
- console.log("[Browser] Server action bridge unregistered");
758
663
  },
759
664
  };
760
665
  }