@rangojs/router 0.0.0-experimental.1b930379 → 0.0.0-experimental.1fa245e2
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.
- package/AGENTS.md +4 -0
- package/README.md +76 -18
- package/dist/bin/rango.js +138 -50
- package/dist/vite/index.js +558 -319
- package/package.json +16 -15
- package/skills/cache-guide/SKILL.md +32 -0
- package/skills/caching/SKILL.md +45 -4
- package/skills/links/SKILL.md +3 -1
- package/skills/loader/SKILL.md +53 -43
- package/skills/middleware/SKILL.md +2 -0
- package/skills/parallel/SKILL.md +126 -0
- package/skills/prerender/SKILL.md +110 -68
- package/skills/route/SKILL.md +31 -0
- package/skills/router-setup/SKILL.md +87 -2
- package/skills/typesafety/SKILL.md +10 -0
- package/src/__internal.ts +1 -1
- package/src/browser/app-version.ts +14 -0
- package/src/browser/event-controller.ts +5 -0
- package/src/browser/navigation-bridge.ts +19 -13
- package/src/browser/navigation-client.ts +115 -58
- package/src/browser/navigation-store.ts +43 -8
- package/src/browser/navigation-transaction.ts +11 -9
- package/src/browser/partial-update.ts +80 -15
- package/src/browser/prefetch/cache.ts +57 -5
- package/src/browser/prefetch/fetch.ts +38 -23
- package/src/browser/prefetch/queue.ts +92 -20
- package/src/browser/prefetch/resource-ready.ts +77 -0
- package/src/browser/react/Link.tsx +53 -9
- package/src/browser/react/NavigationProvider.tsx +40 -4
- package/src/browser/react/context.ts +7 -2
- package/src/browser/react/use-handle.ts +9 -58
- package/src/browser/react/use-router.ts +21 -8
- package/src/browser/rsc-router.tsx +134 -59
- package/src/browser/scroll-restoration.ts +41 -42
- package/src/browser/segment-reconciler.ts +6 -1
- package/src/browser/server-action-bridge.ts +8 -6
- package/src/browser/types.ts +36 -5
- package/src/build/generate-manifest.ts +6 -6
- package/src/build/generate-route-types.ts +3 -0
- package/src/build/route-types/include-resolution.ts +8 -1
- package/src/build/route-types/router-processing.ts +223 -74
- package/src/build/route-types/scan-filter.ts +8 -1
- package/src/cache/cache-runtime.ts +15 -11
- package/src/cache/cache-scope.ts +48 -7
- package/src/cache/cf/cf-cache-store.ts +453 -11
- package/src/cache/cf/index.ts +5 -1
- package/src/cache/document-cache.ts +17 -7
- package/src/cache/index.ts +1 -0
- package/src/cache/taint.ts +55 -0
- package/src/client.tsx +2 -56
- package/src/context-var.ts +72 -2
- package/src/debug.ts +2 -2
- package/src/handle.ts +40 -0
- package/src/index.rsc.ts +3 -1
- package/src/index.ts +8 -0
- package/src/prerender/store.ts +5 -4
- package/src/prerender.ts +138 -77
- package/src/reverse.ts +22 -1
- package/src/route-definition/dsl-helpers.ts +73 -25
- package/src/route-definition/helpers-types.ts +10 -6
- package/src/route-definition/index.ts +3 -0
- package/src/route-definition/redirect.ts +11 -3
- package/src/route-definition/resolve-handler-use.ts +149 -0
- package/src/route-map-builder.ts +7 -1
- package/src/route-types.ts +11 -0
- package/src/router/content-negotiation.ts +100 -1
- package/src/router/find-match.ts +4 -2
- package/src/router/handler-context.ts +79 -23
- package/src/router/intercept-resolution.ts +11 -4
- package/src/router/lazy-includes.ts +4 -1
- package/src/router/loader-resolution.ts +122 -10
- package/src/router/logging.ts +5 -2
- package/src/router/manifest.ts +9 -3
- package/src/router/match-api.ts +124 -189
- package/src/router/match-middleware/background-revalidation.ts +30 -2
- package/src/router/match-middleware/cache-lookup.ts +88 -16
- package/src/router/match-middleware/cache-store.ts +53 -10
- package/src/router/match-middleware/intercept-resolution.ts +9 -7
- package/src/router/match-middleware/segment-resolution.ts +61 -5
- package/src/router/match-result.ts +22 -6
- package/src/router/metrics.ts +6 -1
- package/src/router/middleware-types.ts +6 -8
- package/src/router/middleware.ts +4 -6
- package/src/router/navigation-snapshot.ts +182 -0
- package/src/router/prerender-match.ts +110 -10
- package/src/router/preview-match.ts +30 -102
- package/src/router/request-classification.ts +310 -0
- package/src/router/route-snapshot.ts +245 -0
- package/src/router/router-context.ts +6 -1
- package/src/router/router-interfaces.ts +36 -4
- package/src/router/router-options.ts +37 -11
- package/src/router/segment-resolution/fresh.ts +183 -20
- package/src/router/segment-resolution/helpers.ts +29 -24
- package/src/router/segment-resolution/loader-cache.ts +1 -0
- package/src/router/segment-resolution/revalidation.ts +412 -297
- package/src/router/segment-wrappers.ts +2 -0
- package/src/router/types.ts +1 -0
- package/src/router.ts +59 -6
- package/src/rsc/handler.ts +460 -368
- package/src/rsc/manifest-init.ts +5 -1
- package/src/rsc/progressive-enhancement.ts +4 -0
- package/src/rsc/rsc-rendering.ts +5 -0
- package/src/rsc/server-action.ts +2 -0
- package/src/rsc/ssr-setup.ts +2 -2
- package/src/rsc/types.ts +8 -1
- package/src/segment-system.tsx +140 -4
- package/src/server/context.ts +140 -14
- package/src/server/loader-registry.ts +9 -8
- package/src/server/request-context.ts +144 -18
- package/src/ssr/index.tsx +4 -0
- package/src/static-handler.ts +18 -6
- package/src/types/cache-types.ts +4 -4
- package/src/types/handler-context.ts +137 -33
- package/src/types/loader-types.ts +36 -9
- package/src/types/route-entry.ts +8 -1
- package/src/types/segments.ts +2 -0
- package/src/urls/path-helper-types.ts +9 -2
- package/src/urls/path-helper.ts +48 -13
- package/src/urls/pattern-types.ts +12 -0
- package/src/urls/response-types.ts +16 -6
- package/src/use-loader.tsx +73 -4
- package/src/vite/discovery/bundle-postprocess.ts +30 -33
- package/src/vite/discovery/discover-routers.ts +5 -1
- package/src/vite/discovery/prerender-collection.ts +14 -1
- package/src/vite/discovery/state.ts +13 -6
- package/src/vite/index.ts +4 -0
- package/src/vite/plugin-types.ts +51 -79
- package/src/vite/plugins/expose-action-id.ts +1 -3
- package/src/vite/plugins/performance-tracks.ts +88 -0
- package/src/vite/plugins/refresh-cmd.ts +88 -26
- package/src/vite/plugins/version-plugin.ts +13 -1
- package/src/vite/rango.ts +163 -211
- package/src/vite/router-discovery.ts +153 -42
- package/src/vite/utils/banner.ts +3 -3
- package/src/vite/utils/prerender-utils.ts +18 -0
- package/src/vite/utils/shared-utils.ts +3 -2
|
@@ -23,6 +23,7 @@ import type { EventController } from "./event-controller.js";
|
|
|
23
23
|
import type { ResolvedThemeConfig, Theme } from "../theme/types.js";
|
|
24
24
|
import { initRangoState } from "./rango-state.js";
|
|
25
25
|
import { initPrefetchCache } from "./prefetch/cache.js";
|
|
26
|
+
import { setAppVersion } from "./app-version.js";
|
|
26
27
|
import {
|
|
27
28
|
isInterceptSegment,
|
|
28
29
|
splitInterceptSegments,
|
|
@@ -139,7 +140,6 @@ export async function initBrowserApp(
|
|
|
139
140
|
initialTheme,
|
|
140
141
|
} = options;
|
|
141
142
|
|
|
142
|
-
// Load initial payload from SSR-injected __FLIGHT_DATA__
|
|
143
143
|
const initialPayload =
|
|
144
144
|
await deps.createFromReadableStream<RscPayload>(rscStream);
|
|
145
145
|
|
|
@@ -164,6 +164,12 @@ export async function initBrowserApp(
|
|
|
164
164
|
...(storeOptions?.cacheSize && { cacheSize: storeOptions.cacheSize }),
|
|
165
165
|
});
|
|
166
166
|
|
|
167
|
+
// Seed router identity from the initial SSR payload so the first
|
|
168
|
+
// cross-app SPA navigation can detect the app switch.
|
|
169
|
+
if (initialPayload.metadata?.routerId) {
|
|
170
|
+
store.setRouterId?.(initialPayload.metadata.routerId);
|
|
171
|
+
}
|
|
172
|
+
|
|
167
173
|
// Create event controller for reactive state management
|
|
168
174
|
const eventController = createEventController({
|
|
169
175
|
initialLocation: new URL(window.location.href),
|
|
@@ -205,6 +211,7 @@ export async function initBrowserApp(
|
|
|
205
211
|
// Initialize the localStorage state key for cache invalidation.
|
|
206
212
|
// Uses the build version so a new deploy automatically busts all cached prefetches.
|
|
207
213
|
initRangoState(version ?? "0");
|
|
214
|
+
setAppVersion(version);
|
|
208
215
|
|
|
209
216
|
// Initialize the in-memory prefetch cache TTL from server config.
|
|
210
217
|
// A value of 0 disables the cache; undefined falls back to the module default.
|
|
@@ -231,7 +238,6 @@ export async function initBrowserApp(
|
|
|
231
238
|
deps,
|
|
232
239
|
onUpdate: (update) => store.emitUpdate(update),
|
|
233
240
|
renderSegments,
|
|
234
|
-
version,
|
|
235
241
|
onNavigate: (url, options) => {
|
|
236
242
|
if (!navigateFn) {
|
|
237
243
|
window.location.href = url;
|
|
@@ -249,7 +255,7 @@ export async function initBrowserApp(
|
|
|
249
255
|
client,
|
|
250
256
|
onUpdate: (update) => store.emitUpdate(update),
|
|
251
257
|
renderSegments,
|
|
252
|
-
version,
|
|
258
|
+
version: version,
|
|
253
259
|
});
|
|
254
260
|
|
|
255
261
|
// Connect action redirect → navigation bridge (now that both are initialized)
|
|
@@ -263,71 +269,139 @@ export async function initBrowserApp(
|
|
|
263
269
|
// Build initial tree with rootLayout
|
|
264
270
|
const initialTree = renderSegments(initialPayload.metadata!.segments);
|
|
265
271
|
|
|
266
|
-
// Setup HMR
|
|
272
|
+
// Setup HMR with debounce — burst saves (format-on-save, rapid edits)
|
|
273
|
+
// fire many rsc:update events in quick succession. Without debouncing,
|
|
274
|
+
// each event triggers a fetchPartial() which on slow routes can pile up
|
|
275
|
+
// and overwhelm the worker (cross-request promise issues, 500s).
|
|
267
276
|
if (import.meta.hot) {
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
277
|
+
let hmrTimer: ReturnType<typeof setTimeout> | null = null;
|
|
278
|
+
let hmrAbort: AbortController | null = null;
|
|
279
|
+
|
|
280
|
+
import.meta.hot.on("rsc:update", () => {
|
|
281
|
+
// Cancel any pending debounce timer
|
|
282
|
+
if (hmrTimer !== null) {
|
|
283
|
+
clearTimeout(hmrTimer);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// Abort any in-flight HMR fetch so it doesn't race with the next one
|
|
287
|
+
if (hmrAbort) {
|
|
288
|
+
hmrAbort.abort();
|
|
289
|
+
hmrAbort = null;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// Debounce: wait 200ms of quiet before fetching
|
|
293
|
+
hmrTimer = setTimeout(async () => {
|
|
294
|
+
hmrTimer = null;
|
|
295
|
+
|
|
296
|
+
// Don't interrupt an active user navigation — startNavigation()
|
|
297
|
+
// would abort it and refetch the old URL (window.location.href
|
|
298
|
+
// hasn't updated yet). The user's navigation will pick up the
|
|
299
|
+
// new server code when it completes. isNavigating covers the
|
|
300
|
+
// full lifecycle (fetching + streaming, before commit) without
|
|
301
|
+
// blocking on server actions.
|
|
302
|
+
if (eventController.getState().isNavigating) {
|
|
303
|
+
console.log("[RSCRouter] HMR: Skipping — navigation in progress");
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
console.log("[RSCRouter] HMR: Server update, refetching RSC");
|
|
308
|
+
|
|
309
|
+
const abort = new AbortController();
|
|
310
|
+
hmrAbort = abort;
|
|
311
|
+
|
|
312
|
+
const handle = eventController.startNavigation(window.location.href, {
|
|
313
|
+
replace: true,
|
|
285
314
|
});
|
|
315
|
+
const streamingToken = handle.startStreaming();
|
|
316
|
+
|
|
317
|
+
const interceptSourceUrl = store.getInterceptSourceUrl();
|
|
318
|
+
|
|
319
|
+
try {
|
|
320
|
+
const { payload, streamComplete } = await client.fetchPartial({
|
|
321
|
+
targetUrl: window.location.href,
|
|
322
|
+
segmentIds: [],
|
|
323
|
+
previousUrl: store.getSegmentState().currentUrl,
|
|
324
|
+
interceptSourceUrl: interceptSourceUrl || undefined,
|
|
325
|
+
routerId: store.getRouterId?.(),
|
|
326
|
+
hmr: true,
|
|
327
|
+
signal: abort.signal,
|
|
328
|
+
});
|
|
286
329
|
|
|
287
|
-
|
|
288
|
-
const segments = payload.metadata.segments || [];
|
|
289
|
-
const matched = payload.metadata.matched || [];
|
|
330
|
+
if (abort.signal.aborted) return;
|
|
290
331
|
|
|
291
|
-
//
|
|
292
|
-
//
|
|
293
|
-
//
|
|
294
|
-
|
|
332
|
+
// If the server returned a non-RSC response (404, 500 without
|
|
333
|
+
// error boundary), the payload won't have valid metadata.
|
|
334
|
+
// Reload to recover rather than leaving the page stale.
|
|
335
|
+
if (!payload.metadata) {
|
|
336
|
+
throw new Error("HMR refetch returned invalid payload");
|
|
337
|
+
}
|
|
295
338
|
|
|
296
|
-
//
|
|
297
|
-
|
|
298
|
-
|
|
339
|
+
// Update version BEFORE rebuilding state so that
|
|
340
|
+
// clearHistoryCache() runs first, then the fresh segment
|
|
341
|
+
// cache entry we create below survives.
|
|
342
|
+
const newVersion = payload.metadata.version;
|
|
343
|
+
if (newVersion && newVersion !== version) {
|
|
344
|
+
console.log(
|
|
345
|
+
"[RSCRouter] HMR: version changed",
|
|
346
|
+
version,
|
|
347
|
+
"→",
|
|
348
|
+
newVersion,
|
|
349
|
+
"clearing caches",
|
|
350
|
+
);
|
|
351
|
+
navigationBridge.updateVersion(newVersion);
|
|
299
352
|
}
|
|
300
353
|
|
|
301
|
-
|
|
302
|
-
|
|
354
|
+
if (payload.metadata?.isPartial) {
|
|
355
|
+
const segments = payload.metadata.segments || [];
|
|
356
|
+
const matched = payload.metadata.matched || [];
|
|
357
|
+
|
|
358
|
+
// Derive intercept state from the returned payload, not the
|
|
359
|
+
// pre-fetch store snapshot. If the HMR edit removed intercept
|
|
360
|
+
// behavior, the response won't contain intercept segments.
|
|
361
|
+
const responseIsIntercept = segments.some(isInterceptSegment);
|
|
362
|
+
|
|
363
|
+
// Sync store intercept state with what the server returned
|
|
364
|
+
if (!responseIsIntercept && interceptSourceUrl) {
|
|
365
|
+
store.setInterceptSourceUrl(null);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
store.setSegmentIds(matched);
|
|
369
|
+
store.setCurrentUrl(window.location.href);
|
|
370
|
+
|
|
371
|
+
const historyKey = generateHistoryKey(window.location.href, {
|
|
372
|
+
intercept: responseIsIntercept,
|
|
373
|
+
});
|
|
374
|
+
store.setHistoryKey(historyKey);
|
|
375
|
+
const currentHandleData = eventController.getHandleState().data;
|
|
376
|
+
store.cacheSegmentsForHistory(
|
|
377
|
+
historyKey,
|
|
378
|
+
segments,
|
|
379
|
+
currentHandleData,
|
|
380
|
+
);
|
|
381
|
+
|
|
382
|
+
const { main, intercept } = splitInterceptSegments(segments);
|
|
383
|
+
store.emitUpdate({
|
|
384
|
+
root: renderSegments(main, {
|
|
385
|
+
interceptSegments: intercept.length > 0 ? intercept : undefined,
|
|
386
|
+
}),
|
|
387
|
+
metadata: payload.metadata,
|
|
388
|
+
});
|
|
389
|
+
}
|
|
303
390
|
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
);
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
store.emitUpdate({
|
|
317
|
-
root: renderSegments(main, {
|
|
318
|
-
interceptSegments: intercept.length > 0 ? intercept : undefined,
|
|
319
|
-
}),
|
|
320
|
-
metadata: payload.metadata,
|
|
321
|
-
});
|
|
391
|
+
await streamComplete;
|
|
392
|
+
handle.complete(new URL(window.location.href));
|
|
393
|
+
console.log("[RSCRouter] HMR: RSC stream complete");
|
|
394
|
+
} catch (err) {
|
|
395
|
+
if (abort.signal.aborted) return;
|
|
396
|
+
console.warn("[RSCRouter] HMR: Refetch failed, reloading page", err);
|
|
397
|
+
window.location.reload();
|
|
398
|
+
return;
|
|
399
|
+
} finally {
|
|
400
|
+
if (hmrAbort === abort) hmrAbort = null;
|
|
401
|
+
streamingToken.end();
|
|
402
|
+
handle[Symbol.dispose]();
|
|
322
403
|
}
|
|
323
|
-
|
|
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
|
-
}
|
|
404
|
+
}, 200);
|
|
331
405
|
});
|
|
332
406
|
}
|
|
333
407
|
|
|
@@ -426,6 +500,7 @@ export function RSCRouter(_props: RSCRouterProps): React.ReactElement {
|
|
|
426
500
|
initialTheme={initialTheme}
|
|
427
501
|
warmupEnabled={warmupEnabled}
|
|
428
502
|
version={version}
|
|
503
|
+
basename={initialPayload.metadata?.basename}
|
|
429
504
|
/>
|
|
430
505
|
);
|
|
431
506
|
}
|
|
@@ -10,6 +10,15 @@
|
|
|
10
10
|
|
|
11
11
|
import { debugLog } from "./logging.js";
|
|
12
12
|
|
|
13
|
+
/**
|
|
14
|
+
* Defers a callback to the next animation frame.
|
|
15
|
+
* Falls back to setTimeout(0) in environments without requestAnimationFrame.
|
|
16
|
+
*/
|
|
17
|
+
const deferToNextPaint: (fn: () => void) => void =
|
|
18
|
+
typeof requestAnimationFrame === "function"
|
|
19
|
+
? requestAnimationFrame
|
|
20
|
+
: (fn) => setTimeout(fn, 0);
|
|
21
|
+
|
|
13
22
|
const SCROLL_STORAGE_KEY = "rsc-router-scroll-positions";
|
|
14
23
|
|
|
15
24
|
/**
|
|
@@ -264,51 +273,35 @@ export function restoreScrollPosition(options?: {
|
|
|
264
273
|
return false;
|
|
265
274
|
}
|
|
266
275
|
|
|
267
|
-
//
|
|
268
|
-
const maxScrollY = document.documentElement.scrollHeight - window.innerHeight;
|
|
269
|
-
const canScrollToPosition = savedY <= maxScrollY;
|
|
270
|
-
|
|
271
|
-
if (canScrollToPosition) {
|
|
272
|
-
window.scrollTo(0, savedY);
|
|
273
|
-
debugLog("[Scroll] Restored position:", savedY, "for key:", key);
|
|
274
|
-
return true;
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
// Scroll as far as we can for now
|
|
278
|
-
window.scrollTo(0, maxScrollY);
|
|
279
|
-
debugLog("[Scroll] Partial restore to:", maxScrollY, "target:", savedY);
|
|
280
|
-
|
|
281
|
-
// Poll while streaming until we can scroll to target position
|
|
276
|
+
// If streaming, poll until streaming ends then scroll to saved position
|
|
282
277
|
if (options?.retryIfStreaming && options?.isStreaming?.()) {
|
|
283
278
|
const startTime = Date.now();
|
|
284
279
|
|
|
285
280
|
pendingPollInterval = setInterval(() => {
|
|
286
|
-
// Stop if we've exceeded the timeout
|
|
287
281
|
if (Date.now() - startTime > SCROLL_POLL_TIMEOUT_MS) {
|
|
288
282
|
debugLog("[Scroll] Polling timeout, giving up");
|
|
289
283
|
cancelScrollRestorationPolling();
|
|
290
284
|
return;
|
|
291
285
|
}
|
|
292
286
|
|
|
293
|
-
// Stop if streaming ended
|
|
294
287
|
if (!options.isStreaming?.()) {
|
|
295
|
-
debugLog("[Scroll] Streaming ended, stopping poll");
|
|
296
|
-
cancelScrollRestorationPolling();
|
|
297
|
-
return;
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
// Check if we can now scroll to the target position
|
|
301
|
-
const currentMaxScrollY =
|
|
302
|
-
document.documentElement.scrollHeight - window.innerHeight;
|
|
303
|
-
if (savedY <= currentMaxScrollY) {
|
|
304
288
|
window.scrollTo(0, savedY);
|
|
305
|
-
debugLog("[Scroll]
|
|
289
|
+
debugLog("[Scroll] Restored after streaming:", savedY);
|
|
306
290
|
cancelScrollRestorationPolling();
|
|
307
291
|
}
|
|
308
292
|
}, SCROLL_POLL_INTERVAL_MS);
|
|
293
|
+
|
|
294
|
+
return true;
|
|
309
295
|
}
|
|
310
296
|
|
|
311
|
-
|
|
297
|
+
// Not streaming — scroll after React commits and browser paints.
|
|
298
|
+
// startTransition defers the DOM commit, so scrolling synchronously
|
|
299
|
+
// would be overwritten when React replaces the content.
|
|
300
|
+
deferToNextPaint(() => {
|
|
301
|
+
window.scrollTo(0, savedY);
|
|
302
|
+
debugLog("[Scroll] Restored position:", savedY, "for key:", key);
|
|
303
|
+
});
|
|
304
|
+
return true;
|
|
312
305
|
}
|
|
313
306
|
|
|
314
307
|
/**
|
|
@@ -363,32 +356,38 @@ export function handleNavigationEnd(options: {
|
|
|
363
356
|
scroll?: boolean;
|
|
364
357
|
isStreaming?: () => boolean;
|
|
365
358
|
}): void {
|
|
366
|
-
if (!initialized) {
|
|
367
|
-
return;
|
|
368
|
-
}
|
|
369
|
-
|
|
370
359
|
const { restore = false, scroll = true, isStreaming } = options;
|
|
371
360
|
|
|
372
|
-
// Don't scroll if explicitly disabled
|
|
373
|
-
if (scroll === false) {
|
|
361
|
+
// Don't scroll if explicitly disabled or not in a browser
|
|
362
|
+
if (scroll === false || typeof window === "undefined") {
|
|
374
363
|
return;
|
|
375
364
|
}
|
|
376
365
|
|
|
377
|
-
//
|
|
378
|
-
|
|
366
|
+
// Save/restore requires initialization (sessionStorage, history state).
|
|
367
|
+
// But basic scroll-to-top and hash scrolling work without it — this
|
|
368
|
+
// matters during cross-app navigation where ScrollRestoration unmounts
|
|
369
|
+
// and remounts, creating a brief window where initialized is false.
|
|
370
|
+
if (restore && initialized) {
|
|
379
371
|
if (restoreScrollPosition({ retryIfStreaming: true, isStreaming })) {
|
|
380
372
|
return;
|
|
381
373
|
}
|
|
382
374
|
// Fall through to hash or top if no saved position
|
|
383
375
|
}
|
|
384
376
|
|
|
385
|
-
//
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
377
|
+
// Defer hash and scroll-to-top to after React paints the new content,
|
|
378
|
+
// so the user doesn't see the current page jump before the new route appears.
|
|
379
|
+
deferToNextPaint(() => {
|
|
380
|
+
// Re-check: the deferred callback may fire after environment teardown
|
|
381
|
+
if (typeof window === "undefined") return;
|
|
382
|
+
|
|
383
|
+
// Try hash scrolling first
|
|
384
|
+
if (scrollToHash()) {
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
389
387
|
|
|
390
|
-
|
|
391
|
-
|
|
388
|
+
// Default: scroll to top
|
|
389
|
+
scrollToTop();
|
|
390
|
+
});
|
|
392
391
|
}
|
|
393
392
|
|
|
394
393
|
/**
|
|
@@ -160,8 +160,13 @@ export function reconcileSegments(input: ReconcileInput): ReconcileResult {
|
|
|
160
160
|
|
|
161
161
|
// For non-action actors: cached segments the server decided not to re-render.
|
|
162
162
|
// - Preserve loading=false (suppressed boundary) to maintain tree structure
|
|
163
|
-
// -
|
|
163
|
+
// - Preserve parallel segment loading so renderSegments can reconstruct
|
|
164
|
+
// parallel-owned loader markers from the cached slot metadata
|
|
165
|
+
// - Clear other truthy loading values to prevent suspense on cached content
|
|
164
166
|
if (actor !== "action") {
|
|
167
|
+
if (fromCache.type === "parallel" && fromCache.loading !== undefined) {
|
|
168
|
+
return fromCache;
|
|
169
|
+
}
|
|
165
170
|
if (fromCache.loading !== undefined && fromCache.loading !== false) {
|
|
166
171
|
return { ...fromCache, loading: undefined };
|
|
167
172
|
}
|
|
@@ -29,6 +29,7 @@ import {
|
|
|
29
29
|
} from "./response-adapter.js";
|
|
30
30
|
import { mergeLocationState } from "./history-state.js";
|
|
31
31
|
import { classifyActionOutcome } from "./action-coordinator.js";
|
|
32
|
+
import { getAppVersion } from "./app-version.js";
|
|
32
33
|
|
|
33
34
|
// Polyfill Symbol.dispose/asyncDispose for Safari and older browsers
|
|
34
35
|
if (typeof Symbol.dispose === "undefined") {
|
|
@@ -43,8 +44,6 @@ if (typeof Symbol.asyncDispose === "undefined") {
|
|
|
43
44
|
*/
|
|
44
45
|
export interface ServerActionBridgeConfigWithController extends ServerActionBridgeConfig {
|
|
45
46
|
eventController: EventController;
|
|
46
|
-
/** RSC version from initial payload metadata */
|
|
47
|
-
version?: string;
|
|
48
47
|
/** Callback to trigger SPA navigation (for action redirects) */
|
|
49
48
|
onNavigate?: (
|
|
50
49
|
url: string,
|
|
@@ -75,7 +74,6 @@ export function createServerActionBridge(
|
|
|
75
74
|
deps,
|
|
76
75
|
onUpdate,
|
|
77
76
|
renderSegments,
|
|
78
|
-
version,
|
|
79
77
|
onNavigate,
|
|
80
78
|
} = config;
|
|
81
79
|
|
|
@@ -86,7 +84,7 @@ export function createServerActionBridge(
|
|
|
86
84
|
client,
|
|
87
85
|
onUpdate,
|
|
88
86
|
renderSegments,
|
|
89
|
-
|
|
87
|
+
getVersion: getAppVersion,
|
|
90
88
|
});
|
|
91
89
|
|
|
92
90
|
/**
|
|
@@ -165,9 +163,15 @@ export function createServerActionBridge(
|
|
|
165
163
|
segmentState.currentSegmentIds.join(","),
|
|
166
164
|
);
|
|
167
165
|
// Add version param for version mismatch detection
|
|
166
|
+
const version = getAppVersion();
|
|
168
167
|
if (version) {
|
|
169
168
|
url.searchParams.set("_rsc_v", version);
|
|
170
169
|
}
|
|
170
|
+
// Add router ID for app switch detection
|
|
171
|
+
const rid = store.getRouterId?.();
|
|
172
|
+
if (rid) {
|
|
173
|
+
url.searchParams.set("_rsc_rid", rid);
|
|
174
|
+
}
|
|
171
175
|
|
|
172
176
|
// Encode arguments
|
|
173
177
|
const encodedBody = await deps.encodeReply(args, { temporaryReferences });
|
|
@@ -206,7 +210,6 @@ export function createServerActionBridge(
|
|
|
206
210
|
"rsc-action": id,
|
|
207
211
|
"X-RSC-Router-Client-Path": segmentState.currentUrl,
|
|
208
212
|
...(tx && { "X-RSC-Router-Request-Id": tx.requestId }),
|
|
209
|
-
// Send intercept source URL so server can maintain intercept context
|
|
210
213
|
...(interceptSourceUrl && {
|
|
211
214
|
"X-RSC-Router-Intercept-Source": interceptSourceUrl,
|
|
212
215
|
}),
|
|
@@ -309,7 +312,6 @@ export function createServerActionBridge(
|
|
|
309
312
|
matchedCount: payload.metadata?.matched?.length ?? 0,
|
|
310
313
|
diffCount: payload.metadata?.diff?.length ?? 0,
|
|
311
314
|
});
|
|
312
|
-
|
|
313
315
|
// Guard: if the action was aborted while streaming (e.g., user navigated
|
|
314
316
|
// away or abortAllActions fired), bail out before any reconcile/render/cache
|
|
315
317
|
// writes to avoid overwriting the current UI with stale action results.
|
package/src/browser/types.ts
CHANGED
|
@@ -32,6 +32,9 @@ export type HandleData = Record<string, Record<string, unknown[]>>;
|
|
|
32
32
|
export interface RscMetadata {
|
|
33
33
|
pathname: string;
|
|
34
34
|
segments: ResolvedSegment[];
|
|
35
|
+
/** Router instance ID. When this changes between navigations, the client
|
|
36
|
+
* forces a full tree replacement (app switch via host router). */
|
|
37
|
+
routerId?: string;
|
|
35
38
|
isPartial?: boolean;
|
|
36
39
|
isError?: boolean;
|
|
37
40
|
matched?: string[];
|
|
@@ -70,6 +73,8 @@ export interface RscMetadata {
|
|
|
70
73
|
* Included when theme is enabled in router config.
|
|
71
74
|
*/
|
|
72
75
|
initialTheme?: Theme;
|
|
76
|
+
/** URL prefix for all routes (from createRouter({ basename })). */
|
|
77
|
+
basename?: string;
|
|
73
78
|
/** Whether connection warmup is enabled */
|
|
74
79
|
warmupEnabled?: boolean;
|
|
75
80
|
/** Server-side redirect with optional state (for partial requests) */
|
|
@@ -215,6 +220,15 @@ export interface SegmentState {
|
|
|
215
220
|
export interface NavigationUpdate {
|
|
216
221
|
root: ReactNode | Promise<ReactNode>;
|
|
217
222
|
metadata: RscMetadata;
|
|
223
|
+
/** Scroll behavior to apply after React commits this update */
|
|
224
|
+
scroll?: {
|
|
225
|
+
/** For back/forward: restore saved position */
|
|
226
|
+
restore?: boolean;
|
|
227
|
+
/** Set to false to disable scrolling entirely */
|
|
228
|
+
enabled?: boolean;
|
|
229
|
+
/** Function to check if streaming is in progress */
|
|
230
|
+
isStreaming?: () => boolean;
|
|
231
|
+
};
|
|
218
232
|
}
|
|
219
233
|
|
|
220
234
|
/**
|
|
@@ -332,7 +346,13 @@ export type ReadonlyURLSearchParams = Omit<
|
|
|
332
346
|
export interface RscBrowserDependencies {
|
|
333
347
|
createFromFetch: <T>(
|
|
334
348
|
response: Promise<Response>,
|
|
335
|
-
options?: {
|
|
349
|
+
options?: {
|
|
350
|
+
temporaryReferences?: any;
|
|
351
|
+
findSourceMapURL?: (
|
|
352
|
+
filename: string,
|
|
353
|
+
environmentName: string,
|
|
354
|
+
) => string | null;
|
|
355
|
+
},
|
|
336
356
|
) => Promise<T>;
|
|
337
357
|
createFromReadableStream: <T>(stream: ReadableStream) => Promise<T>;
|
|
338
358
|
encodeReply: (
|
|
@@ -394,10 +414,13 @@ export interface NavigationStore {
|
|
|
394
414
|
segments: ResolvedSegment[],
|
|
395
415
|
handleData?: HandleData,
|
|
396
416
|
): void;
|
|
397
|
-
getCachedSegments(
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
417
|
+
getCachedSegments(historyKey: string):
|
|
418
|
+
| {
|
|
419
|
+
segments: ResolvedSegment[];
|
|
420
|
+
stale: boolean;
|
|
421
|
+
handleData?: HandleData;
|
|
422
|
+
routerId?: string;
|
|
423
|
+
}
|
|
401
424
|
| undefined;
|
|
402
425
|
hasHistoryCache(historyKey: string): boolean;
|
|
403
426
|
updateCacheHandleData(historyKey: string, handleData: HandleData): void;
|
|
@@ -413,6 +436,10 @@ export interface NavigationStore {
|
|
|
413
436
|
getInterceptSourceUrl(): string | null;
|
|
414
437
|
setInterceptSourceUrl(url: string | null): void;
|
|
415
438
|
|
|
439
|
+
// Router identity tracking (for cross-app navigation detection)
|
|
440
|
+
getRouterId?(): string | undefined;
|
|
441
|
+
setRouterId?(id: string): void;
|
|
442
|
+
|
|
416
443
|
// UI update notifications
|
|
417
444
|
onUpdate(callback: UpdateSubscriber): () => void;
|
|
418
445
|
emitUpdate(update: NavigationUpdate): void;
|
|
@@ -443,6 +470,8 @@ export interface FetchPartialOptions {
|
|
|
443
470
|
interceptSourceUrl?: string;
|
|
444
471
|
/** RSC version for cache invalidation detection */
|
|
445
472
|
version?: string;
|
|
473
|
+
/** Current router ID — server detects app switch and returns full response */
|
|
474
|
+
routerId?: string;
|
|
446
475
|
/** If true, this is an HMR refetch - server should invalidate manifest cache */
|
|
447
476
|
hmr?: boolean;
|
|
448
477
|
}
|
|
@@ -511,6 +540,8 @@ export interface NavigationBridge {
|
|
|
511
540
|
refresh(): Promise<void>;
|
|
512
541
|
handlePopstate(): Promise<void>;
|
|
513
542
|
registerLinkInterception(): () => void;
|
|
543
|
+
/** Update the RSC version (e.g. after HMR). Clears prefetch cache. */
|
|
544
|
+
updateVersion(newVersion: string): void;
|
|
514
545
|
}
|
|
515
546
|
|
|
516
547
|
/**
|
|
@@ -45,7 +45,7 @@ export interface GeneratedManifest {
|
|
|
45
45
|
routeTrailingSlash?: Record<string, string>;
|
|
46
46
|
/** Route names using Prerender (for dev-mode Node.js delegation) */
|
|
47
47
|
prerenderRoutes?: string[];
|
|
48
|
-
/** Route names with
|
|
48
|
+
/** Route names wrapped with Passthrough() (live handler for runtime fallback) */
|
|
49
49
|
passthroughRoutes?: string[];
|
|
50
50
|
/** Route name → response type for non-RSC routes */
|
|
51
51
|
responseTypeRoutes?: Record<string, string>;
|
|
@@ -150,10 +150,7 @@ function buildPrefixTreeNode(
|
|
|
150
150
|
if (prerenderDefs && entry.prerenderDef) {
|
|
151
151
|
prerenderDefs[name] = entry.prerenderDef;
|
|
152
152
|
}
|
|
153
|
-
if (
|
|
154
|
-
passthroughRoutes &&
|
|
155
|
-
entry.prerenderDef?.options?.passthrough === true
|
|
156
|
-
) {
|
|
153
|
+
if (passthroughRoutes && entry.isPassthrough === true) {
|
|
157
154
|
passthroughRoutes.push(name);
|
|
158
155
|
}
|
|
159
156
|
}
|
|
@@ -285,6 +282,7 @@ export function generateManifest<TEnv>(
|
|
|
285
282
|
export function generateManifestFull<TEnv>(
|
|
286
283
|
urlpatterns: UrlPatterns<TEnv, any>,
|
|
287
284
|
mountIndex: number = 0,
|
|
285
|
+
options?: { urlPrefix?: string },
|
|
288
286
|
): FullManifest {
|
|
289
287
|
const routeManifest: Record<string, string> = {};
|
|
290
288
|
const routeAncestry: Record<string, string[]> = {};
|
|
@@ -310,6 +308,8 @@ export function generateManifestFull<TEnv>(
|
|
|
310
308
|
counters: {},
|
|
311
309
|
mountIndex,
|
|
312
310
|
trackedIncludes, // Enable include tracking
|
|
311
|
+
// basename sets the initial URL prefix for all path() registrations
|
|
312
|
+
...(options?.urlPrefix ? { urlPrefix: options.urlPrefix } : {}),
|
|
313
313
|
},
|
|
314
314
|
() => {
|
|
315
315
|
const helpers = createRouteHelpers();
|
|
@@ -347,7 +347,7 @@ export function generateManifestFull<TEnv>(
|
|
|
347
347
|
if (entry.prerenderDef) {
|
|
348
348
|
prerenderDefs[name] = entry.prerenderDef;
|
|
349
349
|
}
|
|
350
|
-
if (entry.
|
|
350
|
+
if (entry.isPassthrough === true) {
|
|
351
351
|
passthroughRoutes.push(name);
|
|
352
352
|
}
|
|
353
353
|
}
|
|
@@ -25,6 +25,9 @@ export {
|
|
|
25
25
|
} from "./route-types/include-resolution.js";
|
|
26
26
|
export {
|
|
27
27
|
extractUrlsVariableFromRouter,
|
|
28
|
+
extractUrlsFromRouter,
|
|
29
|
+
extractBasenameFromRouter,
|
|
30
|
+
type UrlsExtractionResult,
|
|
28
31
|
buildCombinedRouteMapForRouterFile,
|
|
29
32
|
detectUnresolvableIncludes,
|
|
30
33
|
detectUnresolvableIncludesForUrlsFile,
|
|
@@ -357,12 +357,17 @@ function buildRouteMapFromBlock(
|
|
|
357
357
|
/**
|
|
358
358
|
* Build route map and search schemas together.
|
|
359
359
|
* Internal helper used by the include resolution path.
|
|
360
|
+
*
|
|
361
|
+
* @param inlineBlock - Optional pre-extracted code block (e.g. from an inline
|
|
362
|
+
* builder function). When provided, variableName is ignored and the block
|
|
363
|
+
* is parsed directly for path()/include() calls.
|
|
360
364
|
*/
|
|
361
365
|
export function buildCombinedRouteMapWithSearch(
|
|
362
366
|
filePath: string,
|
|
363
367
|
variableName?: string,
|
|
364
368
|
visited?: Set<string>,
|
|
365
369
|
diagnosticsOut?: UnresolvableInclude[],
|
|
370
|
+
inlineBlock?: string,
|
|
366
371
|
): {
|
|
367
372
|
routes: Record<string, string>;
|
|
368
373
|
searchSchemas: Record<string, Record<string, string>>;
|
|
@@ -384,7 +389,9 @@ export function buildCombinedRouteMapWithSearch(
|
|
|
384
389
|
}
|
|
385
390
|
|
|
386
391
|
let block: string;
|
|
387
|
-
if (
|
|
392
|
+
if (inlineBlock) {
|
|
393
|
+
block = inlineBlock;
|
|
394
|
+
} else if (variableName) {
|
|
388
395
|
const extracted = extractUrlsBlockForVariable(source, variableName);
|
|
389
396
|
if (!extracted) return { routes: {}, searchSchemas: {} };
|
|
390
397
|
block = extracted;
|