@ivogt/rsc-router 0.0.0-experimental.1

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