@rangojs/router 0.0.0-experimental.cb54cbba → 0.0.0-experimental.debug-cache-2383ca26

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 (65) hide show
  1. package/AGENTS.md +4 -0
  2. package/dist/bin/rango.js +8 -3
  3. package/dist/vite/index.js +139 -200
  4. package/package.json +15 -14
  5. package/skills/caching/SKILL.md +37 -4
  6. package/skills/parallel/SKILL.md +126 -0
  7. package/src/browser/event-controller.ts +5 -0
  8. package/src/browser/navigation-bridge.ts +1 -3
  9. package/src/browser/navigation-client.ts +60 -27
  10. package/src/browser/navigation-transaction.ts +11 -9
  11. package/src/browser/partial-update.ts +50 -9
  12. package/src/browser/prefetch/cache.ts +57 -5
  13. package/src/browser/prefetch/fetch.ts +30 -21
  14. package/src/browser/prefetch/queue.ts +53 -13
  15. package/src/browser/react/Link.tsx +9 -1
  16. package/src/browser/react/NavigationProvider.tsx +27 -0
  17. package/src/browser/rsc-router.tsx +109 -57
  18. package/src/browser/scroll-restoration.ts +31 -34
  19. package/src/browser/segment-reconciler.ts +6 -1
  20. package/src/browser/types.ts +9 -0
  21. package/src/build/route-types/router-processing.ts +12 -2
  22. package/src/cache/cache-runtime.ts +15 -11
  23. package/src/cache/cache-scope.ts +43 -3
  24. package/src/cache/cf/cf-cache-store.ts +453 -11
  25. package/src/cache/cf/index.ts +5 -1
  26. package/src/cache/document-cache.ts +17 -7
  27. package/src/cache/index.ts +1 -0
  28. package/src/debug.ts +2 -2
  29. package/src/route-definition/dsl-helpers.ts +32 -7
  30. package/src/route-definition/redirect.ts +2 -2
  31. package/src/route-map-builder.ts +7 -1
  32. package/src/router/find-match.ts +4 -2
  33. package/src/router/intercept-resolution.ts +2 -0
  34. package/src/router/lazy-includes.ts +4 -1
  35. package/src/router/logging.ts +5 -2
  36. package/src/router/manifest.ts +9 -3
  37. package/src/router/match-middleware/background-revalidation.ts +30 -2
  38. package/src/router/match-middleware/cache-lookup.ts +66 -9
  39. package/src/router/match-middleware/cache-store.ts +53 -10
  40. package/src/router/match-middleware/intercept-resolution.ts +9 -7
  41. package/src/router/match-middleware/segment-resolution.ts +8 -5
  42. package/src/router/match-result.ts +22 -6
  43. package/src/router/metrics.ts +6 -1
  44. package/src/router/middleware.ts +2 -1
  45. package/src/router/router-context.ts +6 -1
  46. package/src/router/segment-resolution/fresh.ts +122 -15
  47. package/src/router/segment-resolution/loader-cache.ts +1 -0
  48. package/src/router/segment-resolution/revalidation.ts +347 -290
  49. package/src/router/segment-wrappers.ts +2 -0
  50. package/src/router.ts +5 -1
  51. package/src/segment-system.tsx +140 -4
  52. package/src/server/context.ts +90 -13
  53. package/src/server/request-context.ts +10 -4
  54. package/src/ssr/index.tsx +1 -0
  55. package/src/types/handler-context.ts +103 -17
  56. package/src/types/route-entry.ts +7 -0
  57. package/src/types/segments.ts +2 -0
  58. package/src/urls/path-helper.ts +1 -1
  59. package/src/vite/discovery/state.ts +0 -2
  60. package/src/vite/plugin-types.ts +0 -83
  61. package/src/vite/plugins/expose-action-id.ts +1 -3
  62. package/src/vite/plugins/version-plugin.ts +13 -1
  63. package/src/vite/rango.ts +144 -209
  64. package/src/vite/router-discovery.ts +0 -8
  65. package/src/vite/utils/banner.ts +3 -3
@@ -1,16 +1,20 @@
1
1
  /**
2
2
  * Prefetch Cache
3
3
  *
4
- * In-memory cache storing prefetch Response objects for instant cache hits
4
+ * In-memory cache storing prefetched Response objects for instant cache hits
5
5
  * on subsequent navigation. Cache key is source-dependent (includes the
6
6
  * current page URL) because the server's diff-based response depends on
7
7
  * where the user navigates from.
8
8
  *
9
+ * Also tracks in-flight prefetch promises. Each promise resolves to the
10
+ * navigation branch of a tee'd Response, allowing navigation to adopt a
11
+ * still-downloading prefetch without reparsing or buffering the body.
12
+ *
9
13
  * Replaces the previous browser HTTP cache approach which was unreliable
10
14
  * due to response draining race conditions and browser inconsistencies.
11
15
  */
12
16
 
13
- import { cancelAllPrefetches } from "./queue.js";
17
+ import { abortAllPrefetches } from "./queue.js";
14
18
  import { invalidateRangoState } from "../rango-state.js";
15
19
 
16
20
  // Default TTL: 5 minutes. Overridden by initPrefetchCache() with
@@ -44,6 +48,13 @@ interface PrefetchCacheEntry {
44
48
  const cache = new Map<string, PrefetchCacheEntry>();
45
49
  const inflight = new Set<string>();
46
50
 
51
+ /**
52
+ * In-flight promise map. When a prefetch fetch is in progress, its
53
+ * Promise<Response | null> is stored here so navigation can await
54
+ * it instead of starting a duplicate request.
55
+ */
56
+ const inflightPromises = new Map<string, Promise<Response | null>>();
57
+
47
58
  // Generation counter incremented on each clearPrefetchCache(). Fetches that
48
59
  // started before a clear carry a stale generation and must not store their
49
60
  // response (the data may be stale due to a server action invalidation).
@@ -78,6 +89,9 @@ export function hasPrefetch(key: string): boolean {
78
89
  * Consume a cached prefetch response. Returns null if not found or expired.
79
90
  * One-time consumption: the entry is deleted after retrieval.
80
91
  * Returns null when caching is disabled (TTL <= 0).
92
+ *
93
+ * Does NOT check in-flight prefetches — use consumeInflightPrefetch()
94
+ * for that (returns a Promise instead of a Response).
81
95
  */
82
96
  export function consumePrefetch(key: string): Response | null {
83
97
  if (cacheTTL <= 0) return null;
@@ -91,10 +105,33 @@ export function consumePrefetch(key: string): Response | null {
91
105
  return entry.response;
92
106
  }
93
107
 
108
+ /**
109
+ * Consume an in-flight prefetch promise. Returns null if no prefetch is
110
+ * in-flight for this key. The returned Promise resolves to the buffered
111
+ * Response (or null if the fetch failed/was aborted).
112
+ *
113
+ * One-time consumption: the promise entry is removed so a second call
114
+ * returns null. The `inflight` set entry is intentionally kept so that
115
+ * hasPrefetch() continues to return true while the underlying fetch is
116
+ * still downloading — this prevents prefetchDirect() or other callers
117
+ * from starting a duplicate request during the handoff window. The
118
+ * inflight flag is cleaned up naturally by clearPrefetchInflight() in
119
+ * the fetch's .finally().
120
+ */
121
+ export function consumeInflightPrefetch(
122
+ key: string,
123
+ ): Promise<Response | null> | null {
124
+ const promise = inflightPromises.get(key);
125
+ if (!promise) return null;
126
+ // Remove the promise (one-time consumption) but keep the inflight flag.
127
+ inflightPromises.delete(key);
128
+ return promise;
129
+ }
130
+
94
131
  /**
95
132
  * Store a prefetch response in the in-memory cache.
96
- * The response body must be fully buffered (e.g. via arrayBuffer()) before
97
- * storing, so the cached Response is self-contained and network-independent.
133
+ * The response should be a clone() of the original so the caller can
134
+ * still consume the body. The clone's body streams independently.
98
135
  *
99
136
  * Skips storage if the generation has changed since the fetch started
100
137
  * (a server action invalidated the cache mid-flight).
@@ -136,19 +173,34 @@ export function markPrefetchInflight(key: string): void {
136
173
  inflight.add(key);
137
174
  }
138
175
 
176
+ /**
177
+ * Store the in-flight Promise for a prefetch so navigation can reuse it.
178
+ */
179
+ export function setInflightPromise(
180
+ key: string,
181
+ promise: Promise<Response | null>,
182
+ ): void {
183
+ inflightPromises.set(key, promise);
184
+ }
185
+
139
186
  export function clearPrefetchInflight(key: string): void {
140
187
  inflight.delete(key);
188
+ inflightPromises.delete(key);
141
189
  }
142
190
 
143
191
  /**
144
192
  * Invalidate all prefetch state. Called when server actions mutate data.
145
193
  * Clears the in-memory cache, cancels in-flight prefetches, and rotates
146
194
  * the Rango state key so CDN-cached responses are also invalidated.
195
+ *
196
+ * Uses abortAllPrefetches (hard cancel) because in-flight responses
197
+ * may contain stale data after a mutation.
147
198
  */
148
199
  export function clearPrefetchCache(): void {
149
200
  generation++;
150
201
  inflight.clear();
202
+ inflightPromises.clear();
151
203
  cache.clear();
152
- cancelAllPrefetches();
204
+ abortAllPrefetches();
153
205
  invalidateRangoState();
154
206
  }
@@ -6,12 +6,16 @@
6
6
  * real navigation so the server returns a proper diff. The Response is fully
7
7
  * buffered and stored in an in-memory cache for instant consumption on
8
8
  * subsequent navigation.
9
+ *
10
+ * In-flight promises are tracked in the cache so that navigation can reuse
11
+ * a prefetch that is still downloading instead of starting a duplicate request.
9
12
  */
10
13
 
11
14
  import {
12
15
  buildPrefetchKey,
13
16
  hasPrefetch,
14
17
  markPrefetchInflight,
18
+ setInflightPromise,
15
19
  storePrefetch,
16
20
  clearPrefetchInflight,
17
21
  currentGeneration,
@@ -51,19 +55,20 @@ function buildPrefetchUrl(
51
55
  }
52
56
 
53
57
  /**
54
- * Core prefetch fetch logic. Fetches the response, fully buffers the body,
55
- * and stores it in the in-memory cache. Returns a Promise and accepts an
56
- * optional AbortSignal for cancellation by the prefetch queue.
58
+ * Core prefetch fetch logic. Fetches the response, tees the body, and stores
59
+ * one branch in the in-memory cache. The returned Promise resolves to the
60
+ * sibling navigation branch (or null on failure) so navigation can safely
61
+ * reuse an in-flight prefetch via consumeInflightPrefetch().
57
62
  */
58
63
  function executePrefetchFetch(
59
64
  key: string,
60
65
  fetchUrl: string,
61
66
  signal?: AbortSignal,
62
- ): Promise<void> {
67
+ ): Promise<Response | null> {
63
68
  const gen = currentGeneration();
64
69
  markPrefetchInflight(key);
65
70
 
66
- return fetch(fetchUrl, {
71
+ const promise: Promise<Response | null> = fetch(fetchUrl, {
67
72
  priority: "low" as RequestPriority,
68
73
  signal,
69
74
  headers: {
@@ -72,26 +77,27 @@ function executePrefetchFetch(
72
77
  "X-Rango-Prefetch": "1",
73
78
  },
74
79
  })
75
- .then(async (response) => {
76
- if (!response.ok) return;
77
- // Fully buffer the response body so the cached Response is
78
- // self-contained and doesn't depend on the network connection.
79
- // This eliminates the race condition where the user clicks before
80
- // the response body has been fully downloaded.
81
- const buffer = await response.arrayBuffer();
82
- const cachedResponse = new Response(buffer, {
80
+ .then((response) => {
81
+ if (!response.ok) return null;
82
+ // Don't buffer with arrayBuffer() that blocks until the entire
83
+ // body downloads, defeating streaming for slow loaders.
84
+ // Tee the body: one branch for navigation, one for cache storage.
85
+ const [navStream, cacheStream] = response.body!.tee();
86
+ const responseInit = {
83
87
  headers: response.headers,
84
88
  status: response.status,
85
89
  statusText: response.statusText,
86
- });
87
- storePrefetch(key, cachedResponse, gen);
88
- })
89
- .catch(() => {
90
- // Silently ignore prefetch failures (including abort)
90
+ };
91
+ storePrefetch(key, new Response(cacheStream, responseInit), gen);
92
+ return new Response(navStream, responseInit);
91
93
  })
94
+ .catch(() => null)
92
95
  .finally(() => {
93
96
  clearPrefetchInflight(key);
94
97
  });
98
+
99
+ setInflightPromise(key, promise);
100
+ return promise;
95
101
  }
96
102
 
97
103
  /**
@@ -128,8 +134,11 @@ export function prefetchQueued(
128
134
  const key = buildPrefetchKey(window.location.href, targetUrl);
129
135
  if (hasPrefetch(key)) return key;
130
136
  const fetchUrlStr = targetUrl.toString();
131
- enqueuePrefetch(key, (signal) =>
132
- executePrefetchFetch(key, fetchUrlStr, signal),
133
- );
137
+ enqueuePrefetch(key, (signal) => {
138
+ // Re-check at execution time: a hover-triggered prefetchDirect may
139
+ // have started or completed this key while the item sat in the queue.
140
+ if (hasPrefetch(key)) return Promise.resolve();
141
+ return executePrefetchFetch(key, fetchUrlStr, signal).then(() => {});
142
+ });
134
143
  return key;
135
144
  }
@@ -5,12 +5,22 @@
5
5
  * Hover prefetches bypass this queue — they fire directly for immediate response
6
6
  * to user intent.
7
7
  *
8
- * All queued/executing prefetches share a single AbortController so they can
9
- * be cancelled in bulk when a navigation starts.
8
+ * Draining is deferred to the next animation frame so prefetch network activity
9
+ * never blocks paint. This applies to both the initial batch and subsequent
10
+ * batches — every drain cycle yields to the browser first.
11
+ *
12
+ * When a navigation starts, queued prefetches are cancelled but executing ones
13
+ * are left running. Navigation can reuse their in-flight responses via the
14
+ * prefetch cache's inflight promise map, avoiding duplicate requests.
10
15
  */
11
16
 
12
17
  const MAX_CONCURRENT = 2;
13
18
 
19
+ const deferToNextPaint: (fn: () => void) => void =
20
+ typeof requestAnimationFrame === "function"
21
+ ? requestAnimationFrame
22
+ : (fn) => setTimeout(fn, 0);
23
+
14
24
  let active = 0;
15
25
  const queue: Array<{
16
26
  key: string;
@@ -19,6 +29,7 @@ const queue: Array<{
19
29
  const queued = new Set<string>();
20
30
  const executing = new Set<string>();
21
31
  let abortController: AbortController | null = null;
32
+ let drainScheduled = false;
22
33
 
23
34
  function startExecution(
24
35
  key: string,
@@ -34,6 +45,21 @@ function startExecution(
34
45
  if (executing.delete(key)) {
35
46
  active--;
36
47
  }
48
+ scheduleDrain();
49
+ });
50
+ }
51
+
52
+ /**
53
+ * Schedule a drain on the next animation frame.
54
+ * Coalesces multiple drain requests into a single rAF callback so
55
+ * batch completion doesn't schedule redundant frames.
56
+ */
57
+ function scheduleDrain(): void {
58
+ if (drainScheduled) return;
59
+ if (active >= MAX_CONCURRENT || queue.length === 0) return;
60
+ drainScheduled = true;
61
+ deferToNextPaint(() => {
62
+ drainScheduled = false;
37
63
  drain();
38
64
  });
39
65
  }
@@ -48,8 +74,8 @@ function drain(): void {
48
74
 
49
75
  /**
50
76
  * Enqueue a prefetch for concurrency-limited execution.
51
- * If below the concurrency limit, executes immediately.
52
- * Otherwise queues for later execution.
77
+ * Execution is always deferred to the next animation frame to avoid
78
+ * blocking paint, even when below the concurrency limit.
53
79
  * Deduplicates by key — items already queued or executing are skipped.
54
80
  *
55
81
  * The executor receives an AbortSignal that is aborted when
@@ -61,20 +87,33 @@ export function enqueuePrefetch(
61
87
  ): void {
62
88
  if (queued.has(key) || executing.has(key)) return;
63
89
 
64
- if (active < MAX_CONCURRENT) {
65
- startExecution(key, execute);
66
- } else {
67
- queued.add(key);
68
- queue.push({ key, execute });
69
- }
90
+ queued.add(key);
91
+ queue.push({ key, execute });
92
+ scheduleDrain();
70
93
  }
71
94
 
72
95
  /**
73
- * Cancel all in-flight and queued prefetches.
74
- * Called when a navigation starts speculative prefetches should not
75
- * compete with navigation fetches for connection slots.
96
+ * Cancel queued prefetches. Executing prefetches are left running so
97
+ * navigation can reuse their in-flight responses (checked via
98
+ * consumeInflightPrefetch in the prefetch cache). With MAX_CONCURRENT=2
99
+ * and priority: "low", in-flight prefetches don't meaningfully compete
100
+ * with navigation fetches under HTTP/2 multiplexing.
101
+ *
102
+ * Called when a navigation starts via the NavigationProvider's
103
+ * event controller subscription.
76
104
  */
77
105
  export function cancelAllPrefetches(): void {
106
+ queue.length = 0;
107
+ queued.clear();
108
+ drainScheduled = false;
109
+ }
110
+
111
+ /**
112
+ * Hard-cancel everything including in-flight prefetches.
113
+ * Used by clearPrefetchCache (server action invalidation) where
114
+ * in-flight responses would be stale.
115
+ */
116
+ export function abortAllPrefetches(): void {
78
117
  abortController?.abort();
79
118
  abortController = null;
80
119
 
@@ -85,4 +124,5 @@ export function cancelAllPrefetches(): void {
85
124
  // so active settles at 0 without underflow.
86
125
  executing.clear();
87
126
  active = 0;
127
+ drainScheduled = false;
88
128
  }
@@ -279,7 +279,15 @@ export const Link: ForwardRefExoticComponent<
279
279
  );
280
280
 
281
281
  const handleMouseEnter = useCallback(() => {
282
- if (resolvedStrategy === "hover" && !isExternal && ctx?.store) {
282
+ if (
283
+ (resolvedStrategy === "hover" || resolvedStrategy === "viewport") &&
284
+ !isExternal &&
285
+ ctx?.store
286
+ ) {
287
+ // For "hover", this is the primary prefetch trigger.
288
+ // For "viewport", this upgrades/prioritizes a potentially queued
289
+ // prefetch — prefetchDirect bypasses the queue, and hasPrefetch
290
+ // deduplicates if the viewport prefetch already completed.
283
291
  const segmentState = ctx.store.getSegmentState();
284
292
  prefetchDirect(to, segmentState.currentSegmentIds, ctx.version);
285
293
  }
@@ -3,8 +3,10 @@
3
3
  import React, {
4
4
  useState,
5
5
  useEffect,
6
+ useLayoutEffect,
6
7
  useCallback,
7
8
  useMemo,
9
+ useRef,
8
10
  use,
9
11
  type ReactNode,
10
12
  } from "react";
@@ -25,6 +27,7 @@ import { ThemeProvider } from "../../theme/ThemeProvider.js";
25
27
  import { NonceContext } from "./nonce-context.js";
26
28
  import type { ResolvedThemeConfig, Theme } from "../../theme/types.js";
27
29
  import { cancelAllPrefetches } from "../prefetch/queue.js";
30
+ import { handleNavigationEnd } from "../scroll-restoration.js";
28
31
 
29
32
  /**
30
33
  * Process handles from an async generator, updating the event controller
@@ -301,9 +304,33 @@ export function NavigationProvider({
301
304
  return unsub;
302
305
  }, [eventController]);
303
306
 
307
+ // Pending scroll action to apply after React commits
308
+ const pendingScrollRef = useRef<NavigationUpdate["scroll"]>(undefined);
309
+
310
+ // Apply scroll after React commits the new content to the DOM
311
+ useLayoutEffect(() => {
312
+ const scrollAction = pendingScrollRef.current;
313
+ if (!scrollAction) return;
314
+ pendingScrollRef.current = undefined;
315
+
316
+ if (scrollAction.enabled === false) return;
317
+
318
+ handleNavigationEnd({
319
+ restore: scrollAction.restore,
320
+ scroll: scrollAction.enabled,
321
+ isStreaming: scrollAction.isStreaming,
322
+ });
323
+ });
324
+
304
325
  // Subscribe to UI updates (for re-rendering the tree)
305
326
  useEffect(() => {
306
327
  const unsubscribe = store.onUpdate((update) => {
328
+ // Capture scroll intent — it will be applied in useLayoutEffect
329
+ // after React commits this state update to the DOM.
330
+ // Always assign (even undefined) to clear stale scroll from prior navigations,
331
+ // so server actions or error updates don't accidentally replay old scroll.
332
+ pendingScrollRef.current = update.scroll;
333
+
307
334
  setPayload({
308
335
  root: update.root,
309
336
  metadata: update.metadata,
@@ -263,71 +263,123 @@ export async function initBrowserApp(
263
263
  // Build initial tree with rootLayout
264
264
  const initialTree = renderSegments(initialPayload.metadata!.segments);
265
265
 
266
- // Setup HMR
266
+ // Setup HMR with debounce — burst saves (format-on-save, rapid edits)
267
+ // fire many rsc:update events in quick succession. Without debouncing,
268
+ // each event triggers a fetchPartial() which on slow routes can pile up
269
+ // and overwhelm the worker (cross-request promise issues, 500s).
267
270
  if (import.meta.hot) {
268
- import.meta.hot.on("rsc:update", async () => {
269
- console.log("[RSCRouter] HMR: Server update, refetching RSC");
270
-
271
- const handle = eventController.startNavigation(window.location.href, {
272
- replace: true,
273
- });
274
- const streamingToken = handle.startStreaming();
275
-
276
- const interceptSourceUrl = store.getInterceptSourceUrl();
277
-
278
- try {
279
- const { payload, streamComplete } = await client.fetchPartial({
280
- targetUrl: window.location.href,
281
- segmentIds: [],
282
- previousUrl: store.getSegmentState().currentUrl,
283
- interceptSourceUrl: interceptSourceUrl || undefined,
284
- hmr: true,
285
- });
271
+ let hmrTimer: ReturnType<typeof setTimeout> | null = null;
272
+ let hmrAbort: AbortController | null = null;
286
273
 
287
- if (payload.metadata?.isPartial) {
288
- const segments = payload.metadata.segments || [];
289
- const matched = payload.metadata.matched || [];
274
+ import.meta.hot.on("rsc:update", () => {
275
+ // Cancel any pending debounce timer
276
+ if (hmrTimer !== null) {
277
+ clearTimeout(hmrTimer);
278
+ }
290
279
 
291
- // Derive intercept state from the returned payload, not the
292
- // pre-fetch store snapshot. If the HMR edit removed intercept
293
- // behavior, the response won't contain intercept segments.
294
- const responseIsIntercept = segments.some(isInterceptSegment);
280
+ // Abort any in-flight HMR fetch so it doesn't race with the next one
281
+ if (hmrAbort) {
282
+ hmrAbort.abort();
283
+ hmrAbort = null;
284
+ }
295
285
 
296
- // Sync store intercept state with what the server returned
297
- if (!responseIsIntercept && interceptSourceUrl) {
298
- store.setInterceptSourceUrl(null);
299
- }
286
+ // Debounce: wait 200ms of quiet before fetching
287
+ hmrTimer = setTimeout(async () => {
288
+ hmrTimer = null;
289
+
290
+ // Don't interrupt an active user navigation — startNavigation()
291
+ // would abort it and refetch the old URL (window.location.href
292
+ // hasn't updated yet). The user's navigation will pick up the
293
+ // new server code when it completes. isNavigating covers the
294
+ // full lifecycle (fetching + streaming, before commit) without
295
+ // blocking on server actions.
296
+ if (eventController.getState().isNavigating) {
297
+ console.log("[RSCRouter] HMR: Skipping — navigation in progress");
298
+ return;
299
+ }
300
300
 
301
- store.setSegmentIds(matched);
302
- store.setCurrentUrl(window.location.href);
301
+ console.log("[RSCRouter] HMR: Server update, refetching RSC");
303
302
 
304
- const historyKey = generateHistoryKey(window.location.href, {
305
- intercept: responseIsIntercept,
306
- });
307
- store.setHistoryKey(historyKey);
308
- const currentHandleData = eventController.getHandleState().data;
309
- store.cacheSegmentsForHistory(
310
- historyKey,
311
- segments,
312
- currentHandleData,
313
- );
314
-
315
- const { main, intercept } = splitInterceptSegments(segments);
316
- store.emitUpdate({
317
- root: renderSegments(main, {
318
- interceptSegments: intercept.length > 0 ? intercept : undefined,
319
- }),
320
- metadata: payload.metadata,
303
+ const abort = new AbortController();
304
+ hmrAbort = abort;
305
+
306
+ const handle = eventController.startNavigation(window.location.href, {
307
+ replace: true,
308
+ });
309
+ const streamingToken = handle.startStreaming();
310
+
311
+ const interceptSourceUrl = store.getInterceptSourceUrl();
312
+
313
+ try {
314
+ const { payload, streamComplete } = await client.fetchPartial({
315
+ targetUrl: window.location.href,
316
+ segmentIds: [],
317
+ previousUrl: store.getSegmentState().currentUrl,
318
+ interceptSourceUrl: interceptSourceUrl || undefined,
319
+ hmr: true,
320
+ signal: abort.signal,
321
321
  });
322
- }
323
322
 
324
- await streamComplete;
325
- handle.complete(new URL(window.location.href));
326
- console.log("[RSCRouter] HMR: RSC stream complete");
327
- } finally {
328
- streamingToken.end();
329
- handle[Symbol.dispose]();
330
- }
323
+ if (abort.signal.aborted) return;
324
+
325
+ // If the server returned a non-RSC response (404, 500 without
326
+ // error boundary), the payload won't have valid metadata.
327
+ // Reload to recover rather than leaving the page stale.
328
+ if (!payload.metadata) {
329
+ throw new Error("HMR refetch returned invalid payload");
330
+ }
331
+
332
+ if (payload.metadata?.isPartial) {
333
+ const segments = payload.metadata.segments || [];
334
+ const matched = payload.metadata.matched || [];
335
+
336
+ // Derive intercept state from the returned payload, not the
337
+ // pre-fetch store snapshot. If the HMR edit removed intercept
338
+ // behavior, the response won't contain intercept segments.
339
+ const responseIsIntercept = segments.some(isInterceptSegment);
340
+
341
+ // Sync store intercept state with what the server returned
342
+ if (!responseIsIntercept && interceptSourceUrl) {
343
+ store.setInterceptSourceUrl(null);
344
+ }
345
+
346
+ store.setSegmentIds(matched);
347
+ store.setCurrentUrl(window.location.href);
348
+
349
+ const historyKey = generateHistoryKey(window.location.href, {
350
+ intercept: responseIsIntercept,
351
+ });
352
+ store.setHistoryKey(historyKey);
353
+ const currentHandleData = eventController.getHandleState().data;
354
+ store.cacheSegmentsForHistory(
355
+ historyKey,
356
+ segments,
357
+ currentHandleData,
358
+ );
359
+
360
+ const { main, intercept } = splitInterceptSegments(segments);
361
+ store.emitUpdate({
362
+ root: renderSegments(main, {
363
+ interceptSegments: intercept.length > 0 ? intercept : undefined,
364
+ }),
365
+ metadata: payload.metadata,
366
+ });
367
+ }
368
+
369
+ await streamComplete;
370
+ handle.complete(new URL(window.location.href));
371
+ console.log("[RSCRouter] HMR: RSC stream complete");
372
+ } catch (err) {
373
+ if (abort.signal.aborted) return;
374
+ console.warn("[RSCRouter] HMR: Refetch failed, reloading page", err);
375
+ window.location.reload();
376
+ return;
377
+ } finally {
378
+ if (hmrAbort === abort) hmrAbort = null;
379
+ streamingToken.end();
380
+ handle[Symbol.dispose]();
381
+ }
382
+ }, 200);
331
383
  });
332
384
  }
333
385