@rangojs/router 0.0.0-experimental.259 → 0.0.0-experimental.26

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 (225) hide show
  1. package/README.md +294 -28
  2. package/dist/bin/rango.js +355 -47
  3. package/dist/vite/index.js +1658 -1239
  4. package/package.json +3 -3
  5. package/skills/cache-guide/SKILL.md +9 -5
  6. package/skills/caching/SKILL.md +4 -4
  7. package/skills/document-cache/SKILL.md +2 -2
  8. package/skills/hooks/SKILL.md +40 -29
  9. package/skills/host-router/SKILL.md +218 -0
  10. package/skills/intercept/SKILL.md +79 -0
  11. package/skills/layout/SKILL.md +62 -2
  12. package/skills/loader/SKILL.md +229 -15
  13. package/skills/middleware/SKILL.md +109 -30
  14. package/skills/parallel/SKILL.md +57 -2
  15. package/skills/prerender/SKILL.md +189 -19
  16. package/skills/rango/SKILL.md +1 -2
  17. package/skills/response-routes/SKILL.md +3 -3
  18. package/skills/route/SKILL.md +44 -3
  19. package/skills/router-setup/SKILL.md +80 -3
  20. package/skills/theme/SKILL.md +5 -4
  21. package/skills/typesafety/SKILL.md +59 -16
  22. package/skills/use-cache/SKILL.md +16 -2
  23. package/src/__internal.ts +1 -1
  24. package/src/bin/rango.ts +56 -19
  25. package/src/browser/action-coordinator.ts +97 -0
  26. package/src/browser/event-controller.ts +29 -48
  27. package/src/browser/history-state.ts +80 -0
  28. package/src/browser/intercept-utils.ts +1 -1
  29. package/src/browser/link-interceptor.ts +19 -3
  30. package/src/browser/merge-segment-loaders.ts +9 -2
  31. package/src/browser/navigation-bridge.ts +66 -443
  32. package/src/browser/navigation-client.ts +34 -62
  33. package/src/browser/navigation-store.ts +4 -33
  34. package/src/browser/navigation-transaction.ts +295 -0
  35. package/src/browser/partial-update.ts +103 -151
  36. package/src/browser/prefetch/cache.ts +67 -0
  37. package/src/browser/prefetch/fetch.ts +137 -0
  38. package/src/browser/prefetch/observer.ts +65 -0
  39. package/src/browser/prefetch/policy.ts +42 -0
  40. package/src/browser/prefetch/queue.ts +88 -0
  41. package/src/browser/rango-state.ts +112 -0
  42. package/src/browser/react/Link.tsx +154 -44
  43. package/src/browser/react/NavigationProvider.tsx +32 -0
  44. package/src/browser/react/context.ts +6 -0
  45. package/src/browser/react/filter-segment-order.ts +11 -0
  46. package/src/browser/react/index.ts +2 -6
  47. package/src/browser/react/location-state-shared.ts +29 -11
  48. package/src/browser/react/location-state.ts +6 -4
  49. package/src/browser/react/nonce-context.ts +23 -0
  50. package/src/browser/react/shallow-equal.ts +27 -0
  51. package/src/browser/react/use-action.ts +23 -45
  52. package/src/browser/react/use-client-cache.ts +5 -3
  53. package/src/browser/react/use-handle.ts +21 -64
  54. package/src/browser/react/use-navigation.ts +7 -32
  55. package/src/browser/react/use-params.ts +5 -34
  56. package/src/browser/react/use-pathname.ts +2 -3
  57. package/src/browser/react/use-router.ts +3 -6
  58. package/src/browser/react/use-search-params.ts +2 -1
  59. package/src/browser/react/use-segments.ts +75 -114
  60. package/src/browser/response-adapter.ts +73 -0
  61. package/src/browser/rsc-router.tsx +46 -22
  62. package/src/browser/scroll-restoration.ts +10 -7
  63. package/src/browser/server-action-bridge.ts +458 -405
  64. package/src/browser/types.ts +21 -35
  65. package/src/browser/validate-redirect-origin.ts +29 -0
  66. package/src/build/generate-manifest.ts +38 -13
  67. package/src/build/generate-route-types.ts +4 -0
  68. package/src/build/index.ts +1 -0
  69. package/src/build/route-trie.ts +19 -3
  70. package/src/build/route-types/codegen.ts +13 -4
  71. package/src/build/route-types/include-resolution.ts +13 -0
  72. package/src/build/route-types/per-module-writer.ts +15 -3
  73. package/src/build/route-types/router-processing.ts +170 -18
  74. package/src/build/runtime-discovery.ts +13 -1
  75. package/src/cache/background-task.ts +34 -0
  76. package/src/cache/cache-key-utils.ts +44 -0
  77. package/src/cache/cache-policy.ts +125 -0
  78. package/src/cache/cache-runtime.ts +136 -123
  79. package/src/cache/cache-scope.ts +76 -83
  80. package/src/cache/cf/cf-cache-store.ts +12 -7
  81. package/src/cache/document-cache.ts +93 -69
  82. package/src/cache/handle-capture.ts +81 -0
  83. package/src/cache/index.ts +0 -15
  84. package/src/cache/memory-segment-store.ts +43 -69
  85. package/src/cache/profile-registry.ts +43 -8
  86. package/src/cache/read-through-swr.ts +134 -0
  87. package/src/cache/segment-codec.ts +140 -117
  88. package/src/cache/taint.ts +30 -3
  89. package/src/cache/types.ts +1 -115
  90. package/src/client.rsc.tsx +0 -1
  91. package/src/client.tsx +53 -76
  92. package/src/errors.ts +6 -1
  93. package/src/handle.ts +1 -1
  94. package/src/handles/MetaTags.tsx +5 -2
  95. package/src/host/cookie-handler.ts +8 -3
  96. package/src/host/index.ts +0 -3
  97. package/src/host/router.ts +14 -1
  98. package/src/href-client.ts +3 -1
  99. package/src/index.rsc.ts +53 -10
  100. package/src/index.ts +73 -43
  101. package/src/loader.rsc.ts +12 -4
  102. package/src/loader.ts +8 -0
  103. package/src/prerender/store.ts +60 -18
  104. package/src/prerender.ts +76 -18
  105. package/src/reverse.ts +11 -7
  106. package/src/root-error-boundary.tsx +30 -26
  107. package/src/route-definition/dsl-helpers.ts +9 -6
  108. package/src/route-definition/index.ts +0 -3
  109. package/src/route-definition/redirect.ts +15 -3
  110. package/src/route-map-builder.ts +38 -2
  111. package/src/route-name.ts +53 -0
  112. package/src/route-types.ts +7 -0
  113. package/src/router/content-negotiation.ts +1 -1
  114. package/src/router/debug-manifest.ts +16 -3
  115. package/src/router/handler-context.ts +96 -17
  116. package/src/router/intercept-resolution.ts +6 -4
  117. package/src/router/lazy-includes.ts +4 -0
  118. package/src/router/loader-resolution.ts +6 -11
  119. package/src/router/logging.ts +100 -3
  120. package/src/router/manifest.ts +32 -3
  121. package/src/router/match-api.ts +62 -54
  122. package/src/router/match-context.ts +3 -0
  123. package/src/router/match-handlers.ts +185 -11
  124. package/src/router/match-middleware/background-revalidation.ts +65 -85
  125. package/src/router/match-middleware/cache-lookup.ts +78 -10
  126. package/src/router/match-middleware/cache-store.ts +2 -0
  127. package/src/router/match-pipelines.ts +8 -43
  128. package/src/router/match-result.ts +0 -9
  129. package/src/router/metrics.ts +233 -13
  130. package/src/router/middleware-types.ts +34 -39
  131. package/src/router/middleware.ts +290 -130
  132. package/src/router/pattern-matching.ts +61 -10
  133. package/src/router/prerender-match.ts +36 -6
  134. package/src/router/preview-match.ts +7 -1
  135. package/src/router/revalidation.ts +61 -2
  136. package/src/router/router-context.ts +15 -0
  137. package/src/router/router-interfaces.ts +158 -40
  138. package/src/router/router-options.ts +223 -1
  139. package/src/router/router-registry.ts +5 -2
  140. package/src/router/segment-resolution/fresh.ts +165 -242
  141. package/src/router/segment-resolution/helpers.ts +263 -0
  142. package/src/router/segment-resolution/loader-cache.ts +102 -98
  143. package/src/router/segment-resolution/revalidation.ts +394 -272
  144. package/src/router/segment-resolution/static-store.ts +2 -2
  145. package/src/router/segment-resolution.ts +1 -3
  146. package/src/router/segment-wrappers.ts +3 -0
  147. package/src/router/telemetry-otel.ts +299 -0
  148. package/src/router/telemetry.ts +300 -0
  149. package/src/router/timeout.ts +148 -0
  150. package/src/router/trie-matching.ts +20 -2
  151. package/src/router/types.ts +7 -1
  152. package/src/router.ts +203 -18
  153. package/src/rsc/handler-context.ts +13 -2
  154. package/src/rsc/handler.ts +489 -438
  155. package/src/rsc/helpers.ts +125 -5
  156. package/src/rsc/index.ts +0 -20
  157. package/src/rsc/loader-fetch.ts +84 -42
  158. package/src/rsc/manifest-init.ts +3 -2
  159. package/src/rsc/origin-guard.ts +141 -0
  160. package/src/rsc/progressive-enhancement.ts +245 -19
  161. package/src/rsc/response-route-handler.ts +347 -0
  162. package/src/rsc/rsc-rendering.ts +47 -43
  163. package/src/rsc/runtime-warnings.ts +42 -0
  164. package/src/rsc/server-action.ts +166 -66
  165. package/src/rsc/ssr-setup.ts +128 -0
  166. package/src/rsc/types.ts +20 -2
  167. package/src/search-params.ts +38 -23
  168. package/src/server/context.ts +61 -7
  169. package/src/server/cookie-store.ts +190 -0
  170. package/src/server/fetchable-loader-store.ts +11 -6
  171. package/src/server/handle-store.ts +84 -12
  172. package/src/server/loader-registry.ts +11 -46
  173. package/src/server/request-context.ts +275 -49
  174. package/src/server.ts +6 -0
  175. package/src/ssr/index.tsx +67 -28
  176. package/src/static-handler.ts +7 -0
  177. package/src/theme/ThemeProvider.tsx +6 -1
  178. package/src/theme/index.ts +4 -18
  179. package/src/theme/theme-context.ts +1 -28
  180. package/src/theme/theme-script.ts +2 -1
  181. package/src/types/cache-types.ts +6 -1
  182. package/src/types/error-types.ts +3 -0
  183. package/src/types/global-namespace.ts +22 -0
  184. package/src/types/handler-context.ts +103 -16
  185. package/src/types/index.ts +1 -1
  186. package/src/types/loader-types.ts +9 -6
  187. package/src/types/route-config.ts +17 -26
  188. package/src/types/route-entry.ts +28 -0
  189. package/src/types/segments.ts +0 -5
  190. package/src/urls/include-helper.ts +49 -8
  191. package/src/urls/index.ts +1 -0
  192. package/src/urls/path-helper-types.ts +30 -12
  193. package/src/urls/path-helper.ts +17 -2
  194. package/src/urls/pattern-types.ts +21 -1
  195. package/src/urls/response-types.ts +29 -7
  196. package/src/urls/type-extraction.ts +23 -15
  197. package/src/use-loader.tsx +27 -9
  198. package/src/vite/discovery/bundle-postprocess.ts +32 -52
  199. package/src/vite/discovery/discover-routers.ts +52 -26
  200. package/src/vite/discovery/prerender-collection.ts +58 -41
  201. package/src/vite/discovery/route-types-writer.ts +7 -7
  202. package/src/vite/discovery/state.ts +7 -7
  203. package/src/vite/discovery/virtual-module-codegen.ts +5 -2
  204. package/src/vite/index.ts +10 -51
  205. package/src/vite/plugins/client-ref-dedup.ts +115 -0
  206. package/src/vite/plugins/client-ref-hashing.ts +3 -3
  207. package/src/vite/plugins/expose-internal-ids.ts +4 -3
  208. package/src/vite/plugins/refresh-cmd.ts +65 -0
  209. package/src/vite/plugins/use-cache-transform.ts +91 -3
  210. package/src/vite/plugins/version-plugin.ts +188 -18
  211. package/src/vite/rango.ts +61 -36
  212. package/src/vite/router-discovery.ts +173 -100
  213. package/src/vite/utils/prerender-utils.ts +81 -0
  214. package/src/vite/utils/shared-utils.ts +19 -9
  215. package/skills/testing/SKILL.md +0 -226
  216. package/src/browser/lru-cache.ts +0 -61
  217. package/src/browser/react/prefetch.ts +0 -27
  218. package/src/browser/request-controller.ts +0 -164
  219. package/src/cache/memory-store.ts +0 -253
  220. package/src/href-context.ts +0 -33
  221. package/src/route-definition/route-function.ts +0 -119
  222. package/src/router.gen.ts +0 -6
  223. package/src/static-handler.gen.ts +0 -5
  224. package/src/urls.gen.ts +0 -8
  225. /package/{CLAUDE.md → AGENTS.md} +0 -0
@@ -4,12 +4,11 @@ import type {
4
4
  RscPayload,
5
5
  } from "./types.js";
6
6
  import { createPartialUpdater } from "./partial-update.js";
7
- import { createNavigationTransaction } from "./navigation-bridge.js";
7
+ import { createNavigationTransaction } from "./navigation-transaction.js";
8
8
  import {
9
9
  reconcileSegments,
10
10
  reconcileErrorSegments,
11
11
  } from "./segment-reconciler.js";
12
- import { classifyActionResponse } from "./action-response-classifier.js";
13
12
  import { startTransition } from "react";
14
13
  import type { EventController } from "./event-controller.js";
15
14
  import {
@@ -22,6 +21,14 @@ import {
22
21
  isBrowserDebugEnabled,
23
22
  startBrowserTransaction,
24
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";
25
32
 
26
33
  // Polyfill Symbol.dispose/asyncDispose for Safari and older browsers
27
34
  if (typeof Symbol.dispose === "undefined") {
@@ -92,27 +99,31 @@ export function createServerActionBridge(
92
99
  interceptSourceUrl?: string | null;
93
100
  }): Promise<void> {
94
101
  const src = opts?.interceptSourceUrl ?? null;
95
- using navTx = createNavigationTransaction(
102
+ const navTx = createNavigationTransaction(
96
103
  store,
97
104
  eventController,
98
105
  window.location.href,
99
106
  { replace: true, skipLoadingState: true },
100
107
  );
101
- await fetchPartialUpdate(
102
- window.location.href,
103
- opts?.segments ?? [],
104
- false,
105
- navTx.handle.signal,
106
- navTx.with({
107
- url: window.location.href,
108
- storeOnly: true,
109
- ...(src ? { intercept: true, interceptSourceUrl: src } : {}),
110
- }),
111
- {
112
- isAction: true,
113
- ...(src ? { interceptSourceUrl: src } : {}),
114
- },
115
- );
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
+ }
116
127
  }
117
128
 
118
129
  /**
@@ -130,406 +141,495 @@ export function createServerActionBridge(
130
141
  log("action start", { id, argsCount: args.length });
131
142
 
132
143
  // Start action in event controller - handles lifecycle tracking
133
- using handle = eventController.startAction(id, args);
134
-
135
- const segmentState = store.getSegmentState();
144
+ const handle = eventController.startAction(id, args);
145
+ try {
146
+ const segmentState = store.getSegmentState();
147
+
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();
151
+
152
+ // Create temporary references for serialization
153
+ const temporaryReferences = deps.createTemporaryReferenceSet();
154
+
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;
159
+
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
+ }
136
171
 
137
- // Mark cache as stale immediately when action starts
138
- // This ensures SWR pattern kicks in if user navigates away during action
139
- store.markCacheAsStaleAndBroadcast();
172
+ // Encode arguments
173
+ const encodedBody = await deps.encodeReply(args, { temporaryReferences });
140
174
 
141
- // Create temporary references for serialization
142
- const temporaryReferences = deps.createTemporaryReferenceSet();
175
+ log("sending action request", {
176
+ url: url.href,
177
+ bodyType: typeof encodedBody,
178
+ isFormData: encodedBody instanceof FormData,
179
+ segmentCount: segmentState.currentSegmentIds.length,
180
+ });
143
181
 
144
- // Capture URL pathname at action start to detect navigation during action
145
- // Must use window.location (not store.path) because intercepts change URL
146
- // without changing store.path (e.g., /kanban -> /kanban/card/1)
147
- const actionStartPathname = window.location.pathname;
182
+ // Track when the stream completes
183
+ let resolveStreamComplete: () => void;
184
+ const streamComplete = new Promise<void>((resolve) => {
185
+ resolveStreamComplete = resolve;
186
+ });
148
187
 
149
- // Build action request URL with current segments
150
- const url = new URL(window.location.href);
151
- url.searchParams.set("_rsc_action", id);
152
- url.searchParams.set(
153
- "_rsc_segments",
154
- segmentState.currentSegmentIds.join(","),
155
- );
156
- // Add version param for version mismatch detection
157
- if (version) {
158
- url.searchParams.set("_rsc_v", version);
159
- }
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
+ }
160
234
 
161
- // Encode arguments
162
- const encodedBody = await deps.encodeReply(args, { temporaryReferences });
163
-
164
- log("sending action request", {
165
- url: url.href,
166
- bodyType: typeof encodedBody,
167
- isFormData: encodedBody instanceof FormData,
168
- segmentCount: segmentState.currentSegmentIds.length,
169
- });
170
-
171
- // Track when the stream completes
172
- let resolveStreamComplete: () => void;
173
- const streamComplete = new Promise<void>((resolve) => {
174
- resolveStreamComplete = resolve;
175
- });
176
-
177
- // Get intercept source URL if in intercept context
178
- const interceptSourceUrl = store.getInterceptSourceUrl();
179
-
180
- // Track streaming token - will be set when response arrives
181
- let streamingToken: { end(): void } | null = null;
182
-
183
- // Send action request with stream tracking
184
- const responsePromise = fetch(url, {
185
- method: "POST",
186
- headers: {
187
- "rsc-action": id,
188
- "X-RSC-Router-Client-Path": segmentState.currentUrl,
189
- ...(tx && { "X-RSC-Router-Request-Id": tx.requestId }),
190
- // Send intercept source URL so server can maintain intercept context
191
- ...(interceptSourceUrl && {
192
- "X-RSC-Router-Intercept-Source": interceptSourceUrl,
193
- }),
194
- },
195
- body: encodedBody,
196
- }).then(async (response) => {
197
- // Check for version mismatch - server wants us to reload
198
- const reloadUrl = response.headers.get("X-RSC-Reload");
199
- if (reloadUrl) {
200
- // Validate origin to prevent open redirect via crafted headers
201
- try {
202
- const target = new URL(reloadUrl, window.location.origin);
203
- if (target.origin !== window.location.origin) {
204
- throw new Error(
205
- `X-RSC-Reload blocked: origin mismatch (${target.origin})`,
206
- );
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;
207
250
  }
208
- } catch (e) {
209
- console.error("[rango]", e);
210
- return response;
251
+ return new Promise<Response>(() => {});
211
252
  }
212
- log("version mismatch on action, reloading", { reloadUrl });
213
- window.location.href = reloadUrl;
214
- // Return a never-resolving promise to prevent further processing
215
- return new Promise<Response>(() => {});
253
+ if (redirect === "blocked") {
254
+ resolveStreamComplete();
255
+ return emptyResponse();
256
+ }
257
+
258
+ // Start streaming immediately when response arrives
259
+ if (!handle.signal.aborted) {
260
+ streamingToken = handle.startStreaming();
261
+ }
262
+
263
+ return teeWithCompletion(response, () => {
264
+ log("stream complete");
265
+ streamingToken?.end();
266
+ resolveStreamComplete();
267
+ });
268
+ });
269
+
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
+ }
292
+
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;
216
304
  }
217
305
 
218
- // Simple redirect from action (no state, no RSC payload).
219
- // Short-circuits before createFromFetch — no Flight deserialization needed.
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,
311
+ });
312
+
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
+ }
320
+
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.
220
326
  // Check handle.signal.aborted to avoid redirecting from a stale action
221
327
  // when the user has already navigated away.
222
- const simpleRedirectUrl = response.headers.get("X-RSC-Redirect");
223
- if (simpleRedirectUrl && !handle.signal.aborted) {
224
- if (tx) {
225
- browserDebugLog(tx, "action simple redirect", {
226
- url: simpleRedirectUrl,
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,
227
336
  });
337
+ handle.complete(returnValue?.data);
338
+ return returnValue?.data;
228
339
  }
229
- handle.complete(undefined);
340
+ const redirectState = metadata.locationState;
341
+ log("action redirect", { url: redirectUrl });
342
+ handle.complete(returnValue?.data);
230
343
  if (onNavigate) {
231
- await onNavigate(simpleRedirectUrl, {
344
+ await onNavigate(redirectUrl, {
345
+ state: redirectState,
232
346
  replace: true,
233
347
  _skipCache: true,
234
348
  });
235
349
  } else {
236
- window.location.href = simpleRedirectUrl;
350
+ window.location.href = redirectUrl;
237
351
  }
238
- return new Promise<Response>(() => {});
352
+ return returnValue?.data;
239
353
  }
240
354
 
241
- // Start streaming immediately when response arrives
242
- if (!handle.signal.aborted) {
243
- streamingToken = handle.startStreaming();
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;
244
362
  }
245
363
 
246
- if (!response.body) {
247
- // No body means stream is already complete
248
- streamingToken?.end();
249
- resolveStreamComplete();
250
- return response;
364
+ const { matched, diff, segments, isPartial, isError } = metadata || {};
365
+
366
+ // Log action result
367
+ if (returnValue && !returnValue.ok) {
368
+ console.error(`[Browser] Action failed:`, returnValue.data);
251
369
  }
252
370
 
253
- // Tee the stream: one for RSC runtime, one for tracking completion
254
- const [rscStream, trackingStream] = response.body.tee();
371
+ // Handle error responses with error boundary UI
372
+ if (isError && isPartial && segments && diff) {
373
+ log("processing error boundary response");
255
374
 
256
- // Consume the tracking stream to detect when it closes
257
- (async () => {
258
- const reader = trackingStream.getReader();
259
- try {
260
- while (true) {
261
- const { done } = await reader.read();
262
- if (done) break;
263
- }
264
- } finally {
265
- reader.releaseLock();
266
- log("stream complete");
267
- streamingToken?.end();
268
- resolveStreamComplete();
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);
269
379
  }
270
- })().catch((error) => {
271
- console.error("[STREAMING] Error reading tracking stream:", error);
272
- streamingToken?.end();
273
- });
274
380
 
275
- // Return response with the RSC stream
276
- return new Response(rscStream, {
277
- headers: response.headers,
278
- status: response.status,
279
- statusText: response.statusText,
280
- });
281
- });
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();
282
384
 
283
- // Deserialize response (MUST use same temporaryReferences)
284
- let payload: RscPayload;
285
- try {
286
- payload = await deps.createFromFetch<RscPayload>(responsePromise, {
287
- temporaryReferences,
288
- });
289
- } catch (error) {
290
- // Clean up streaming token on error (may be null if fetch failed before .then() ran)
291
- // The token is assigned in .then() callback which runs before this catch block,
292
- // but TypeScript doesn't track cross-async assignments, so use type assertion
293
- (streamingToken as { end(): void } | null)?.end();
294
- // resolveStreamComplete is assigned in the Promise constructor so it's safe to call
295
- resolveStreamComplete!();
296
-
297
- // Convert network-level errors to NetworkError for proper handling
298
- const networkError = toNetworkError(error, {
299
- url: url.toString(),
300
- operation: "action",
301
- });
302
- if (networkError) {
303
- handle.fail(networkError);
304
- emitNetworkError(onUpdate, networkError, segmentState.currentUrl);
305
- throw networkError;
306
- }
307
- throw error;
308
- }
385
+ // Clear concurrent action tracking - no consolidation needed when showing error
386
+ handle.clearConsolidation();
387
+
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);
309
395
 
310
- log("action response received", {
311
- isPartial: payload.metadata?.isPartial,
312
- isError: payload.metadata?.isError,
313
- matchedCount: payload.metadata?.matched?.length ?? 0,
314
- diffCount: payload.metadata?.diff?.length ?? 0,
315
- });
316
-
317
- // Process response
318
- const { metadata, returnValue } = payload;
319
-
320
- // Handle action redirect: server converted the redirect to a Flight payload
321
- // so we can perform SPA navigation instead of a full page reload.
322
- // Check handle.signal.aborted to avoid redirecting from a stale action
323
- // when the user has already navigated away.
324
- if (metadata?.redirect && !handle.signal.aborted) {
325
- const { url: redirectUrl } = metadata.redirect;
326
- const redirectState = metadata.locationState;
327
- console.log(`[Browser] Action redirect to ${redirectUrl}`);
328
- handle.complete(returnValue?.data);
329
- if (onNavigate) {
330
- await onNavigate(redirectUrl, {
331
- state: redirectState,
332
- replace: true,
333
- _skipCache: true,
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,
334
403
  });
335
- } else {
336
- window.location.href = redirectUrl;
337
- }
338
- return returnValue?.data;
339
- }
340
404
 
341
- const { matched, diff, segments, isPartial, isError } = metadata || {};
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
+ }
342
424
 
343
- // Log action result
344
- if (returnValue && !returnValue.ok) {
345
- console.error(`[Browser] Action failed:`, returnValue.data);
346
- }
425
+ // Update UI with error boundary
426
+ startTransition(() => {
427
+ onUpdate({ root: errorTree, metadata: metadata! });
428
+ });
347
429
 
348
- // Handle error responses with error boundary UI
349
- if (isError && isPartial && segments && diff) {
350
- log("processing error boundary response");
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
+ );
351
435
 
352
- // Abort all other pending action requests - error takes precedence
353
- // This prevents other actions from completing and overwriting the error UI
354
- eventController.abortAllActions();
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
+ );
444
+
445
+ // Throw the error so the action promise rejects
446
+ if (returnValue && !returnValue.ok) {
447
+ throw returnValue.data;
448
+ }
355
449
 
356
- // Clear concurrent action tracking - no consolidation needed when showing error
357
- handle.clearConsolidation();
450
+ // No error in returnValue (shouldn't happen with isError: true)
451
+ handle.complete(undefined);
452
+ return undefined;
453
+ }
358
454
 
359
- // Get current page's cached segments
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
+ }
465
+
466
+ log("processing partial update", {
467
+ serverSegments: segments?.length ?? 0,
468
+ diff: diff?.join(", ") ?? "",
469
+ matched: matched?.join(", ") ?? "",
470
+ });
471
+
472
+ // Record revalidated segments for concurrent action tracking
473
+ if (diff) {
474
+ handle.recordRevalidatedSegments(diff);
475
+ }
476
+
477
+ // Get current page's cached segments for merging
360
478
  const currentKey = store.getHistoryKey();
361
479
  const cached = store.getCachedSegments(currentKey);
362
480
  const cachedSegments = cached?.segments || [];
363
481
 
364
- // Reconcile error segments with cached tree
365
- const errorResult = reconcileErrorSegments(cachedSegments, segments);
366
-
367
- // Render the full tree with error segment merged with parent layouts
368
- const errorTree = await renderSegments(errorResult.mainSegments, {
369
- isAction: true,
370
- interceptSegments:
371
- errorResult.interceptSegments.length > 0
372
- ? errorResult.interceptSegments
373
- : undefined,
374
- });
482
+ if (!matched) {
483
+ throw new Error("No matched segments in response");
484
+ }
375
485
 
376
- // Update UI with error boundary
377
- startTransition(() => {
378
- onUpdate({ root: errorTree, metadata: metadata! });
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,
379
493
  });
494
+ const fullSegments = reconciled.segments;
380
495
 
381
- // Update segment tracking to exclude error segment IDs
382
- const errorSegmentIds = new Set(diff);
383
- const segmentIdsAfterError = segmentState.currentSegmentIds.filter(
384
- (id) => !errorSegmentIds.has(id),
385
- );
386
-
387
- // Update store state
388
- store.setSegmentIds(segmentIdsAfterError);
389
- const currentHandleData = eventController.getHandleState().data;
390
- store.cacheSegmentsForHistory(
391
- currentKey,
392
- errorResult.segments,
393
- currentHandleData,
394
- );
496
+ const returnData = returnValue?.data;
395
497
 
396
- // Throw the error so the action promise rejects
397
498
  if (returnValue && !returnValue.ok) {
398
499
  handle.fail(returnValue.data);
399
500
  throw returnValue.data;
400
501
  }
401
502
 
402
- // No error in returnValue (shouldn't happen with isError: true)
403
- handle.complete(undefined);
404
- return undefined;
405
- }
406
-
407
- if (!isPartial) {
408
- // Full update not supported for actions
409
- throw new Error(
410
- `[Browser] Full update after action is not supported yet`,
411
- );
412
- }
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
+ });
413
517
 
414
- log("processing partial update", {
415
- serverSegments: segments?.length ?? 0,
416
- diff: diff?.join(", ") ?? "",
417
- matched: matched?.join(", ") ?? "",
418
- });
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;
540
+ }
419
541
 
420
- // Record revalidated segments for concurrent action tracking
421
- if (diff) {
422
- handle.recordRevalidatedSegments(diff);
423
- }
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;
548
+ }
424
549
 
425
- // Get current page's cached segments for merging
426
- const currentKey = store.getHistoryKey();
427
- const cached = store.getCachedSegments(currentKey);
428
- const cachedSegments = cached?.segments || [];
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
+ }
429
558
 
430
- if (!matched) {
431
- throw new Error("No matched segments in response");
432
- }
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
+ );
433
568
 
434
- // Reconcile server segments with cached segments (single source of truth)
435
- const reconciled = reconcileSegments({
436
- actor: "action",
437
- matched,
438
- diff: diff || [],
439
- serverSegments: segments || [],
440
- cachedSegments,
441
- });
442
- const fullSegments = reconciled.segments;
443
-
444
- const returnData = returnValue?.data;
445
-
446
- if (returnValue && !returnValue.ok) {
447
- handle.fail(returnValue.data);
448
- throw returnValue.data;
449
- }
569
+ // Clear consolidation tracking before fetch
570
+ handle.clearConsolidation();
450
571
 
451
- // Classify the post-reconciliation scenario
452
- const consolidationSegments = handle.getConsolidationSegments();
453
- const otherFetchingActions = [
454
- ...eventController.getInflightActions().values(),
455
- ].filter((a) => a.phase === "fetching" && a.id !== handle.id);
456
-
457
- const scenario = classifyActionResponse({
458
- actionStartPathname,
459
- currentPathname: window.location.pathname,
460
- actionStartLocationKey: locationKey,
461
- currentLocationKey: window.history.state?.key,
462
- reconciledSegmentCount: fullSegments.length,
463
- matchedCount: matched.length,
464
- consolidationSegments: consolidationSegments || null,
465
- otherFetchingActionCount: otherFetchingActions.length,
466
- currentInterceptSource: store.getInterceptSourceUrl(),
467
- });
468
-
469
- switch (scenario.type) {
470
- case "navigated-away": {
471
- log("user navigated away during action", {
472
- from: actionStartPathname,
473
- to: window.location.pathname,
474
- historyKeyChanged: scenario.historyKeyChanged,
475
- });
476
- // Clear concurrent action tracking - don't consolidate for old route's segments
477
- handle.clearConsolidation();
572
+ await refetchRoute({
573
+ segments: segmentsToSend,
574
+ interceptSourceUrl,
575
+ });
576
+ store.broadcastCacheInvalidation();
577
+ break;
578
+ }
478
579
 
479
- if (scenario.historyKeyChanged) {
480
- if (!scenario.onInterceptRoute) {
481
- store.markCacheAsStaleAndBroadcast();
482
- refetchRoute().catch((error) => {
483
- if (isBackgroundSuppressible(error)) return;
484
- console.error("[Browser] Background revalidation failed:", error);
485
- });
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
+ );
486
594
  }
487
595
  break;
488
596
  }
489
597
 
490
- // Same history key but different pathname - safe to refetch current route
491
- store.markCacheAsStaleAndBroadcast();
492
- await refetchRoute({
493
- interceptSourceUrl: store.getInterceptSourceUrl(),
494
- });
495
- break;
496
- }
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
+ });
497
607
 
498
- case "hmr-missing": {
499
- console.warn(
500
- `[Browser] Missing segments after action (HMR detected), refetching...`,
501
- );
502
- await refetchRoute({ interceptSourceUrl });
503
- store.broadcastCacheInvalidation();
504
- break;
505
- }
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
+ }
506
613
 
507
- case "consolidation-needed": {
508
- log("consolidation fetch needed", { segmentIds: scenario.segmentIds });
509
- // Calculate segments to send (exclude the ones we want fresh)
510
- const currentSegmentIds = store.getSegmentState().currentSegmentIds;
511
- const segmentsToSend = currentSegmentIds.filter(
512
- (sid) => !scenario.segmentIds.includes(sid),
513
- );
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;
620
+ }
514
621
 
515
- // Clear consolidation tracking before fetch
516
- handle.clearConsolidation();
622
+ startTransition(() => {
623
+ onUpdate({ root: newTree, metadata: metadata! });
624
+ });
517
625
 
518
- await refetchRoute({
519
- segments: segmentsToSend,
520
- interceptSourceUrl,
521
- });
522
- store.broadcastCacheInvalidation();
523
- break;
524
- }
626
+ // Apply server-set location state to history.state (non-redirect flow)
627
+ const actionLocationState = metadata?.locationState;
628
+ if (actionLocationState) {
629
+ mergeLocationState(actionLocationState);
630
+ }
525
631
 
526
- case "concurrent-skip": {
527
- log("skipping UI update, other actions fetching", {
528
- otherCount: scenario.otherFetchingCount,
529
- });
530
- // Only update store if history key hasn't changed (user didn't navigate away)
531
- const currentKeyNow = store.getHistoryKey();
532
- if (currentKeyNow === currentKey) {
632
+ // Update store state
533
633
  store.setSegmentIds(matched);
534
634
  const currentHandleData = eventController.getHandleState().data;
535
635
  store.cacheSegmentsForHistory(
@@ -537,53 +637,16 @@ export function createServerActionBridge(
537
637
  fullSegments,
538
638
  currentHandleData,
539
639
  );
540
- }
541
- break;
542
- }
543
-
544
- case "normal": {
545
- // Prepare new tree (await loader data resolution)
546
- const newTree = await renderSegments(reconciled.mainSegments, {
547
- isAction: true,
548
- interceptSegments:
549
- reconciled.interceptSegments.length > 0
550
- ? reconciled.interceptSegments
551
- : undefined,
552
- });
553
-
554
- // Re-check if user navigated away (could happen during async renderSegments)
555
- if (window.location.pathname !== actionStartPathname) {
556
- log("user navigated during render, skipping");
640
+ store.markCacheAsStaleAndBroadcast();
557
641
  break;
558
642
  }
559
-
560
- // Verify the store's current key still matches what we captured at action start
561
- // If they differ, user navigated away and we should NOT cache under the old key
562
- const currentKeyNow = store.getHistoryKey();
563
- if (currentKeyNow !== currentKey) {
564
- log("history key changed during action, skipping cache update");
565
- break;
566
- }
567
-
568
- startTransition(() => {
569
- onUpdate({ root: newTree, metadata: metadata! });
570
- });
571
-
572
- // Update store state
573
- store.setSegmentIds(matched);
574
- const currentHandleData = eventController.getHandleState().data;
575
- store.cacheSegmentsForHistory(
576
- currentKey,
577
- fullSegments,
578
- currentHandleData,
579
- );
580
- store.markCacheAsStaleAndBroadcast();
581
- break;
582
643
  }
583
- }
584
644
 
585
- handle.complete(returnData);
586
- return returnData;
645
+ handle.complete(returnData);
646
+ return returnData;
647
+ } finally {
648
+ handle[Symbol.dispose]();
649
+ }
587
650
  }
588
651
 
589
652
  return {
@@ -598,16 +661,6 @@ export function createServerActionBridge(
598
661
  deps.setServerCallback(handleServerAction);
599
662
  isRegistered = true;
600
663
  },
601
-
602
- /**
603
- * Unregister the server action callback
604
- */
605
- unregister(): void {
606
- if (!isRegistered) {
607
- return;
608
- }
609
- isRegistered = false;
610
- },
611
664
  };
612
665
  }
613
666