@rangojs/router 0.0.0-experimental.b9cb8739 → 0.0.0-experimental.bd6e11bc
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/README.md +196 -43
- package/dist/bin/rango.js +277 -99
- package/dist/testing/vitest.js +48 -0
- package/dist/vite/index.js +2779 -1064
- package/dist/vite/index.js.bak +5448 -0
- package/dist/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
- package/package.json +57 -11
- package/skills/breadcrumbs/SKILL.md +3 -1
- package/skills/bundle-analysis/SKILL.md +159 -0
- package/skills/cache-guide/SKILL.md +243 -21
- package/skills/caching/SKILL.md +155 -6
- package/skills/composability/SKILL.md +27 -2
- package/skills/document-cache/SKILL.md +78 -55
- package/skills/handler-use/SKILL.md +364 -0
- package/skills/hooks/SKILL.md +229 -20
- package/skills/host-router/SKILL.md +45 -20
- package/skills/i18n/SKILL.md +276 -0
- package/skills/intercept/SKILL.md +46 -4
- package/skills/layout/SKILL.md +28 -7
- package/skills/links/SKILL.md +249 -17
- package/skills/loader/SKILL.md +273 -53
- package/skills/middleware/SKILL.md +49 -12
- package/skills/migrate-nextjs/SKILL.md +562 -0
- package/skills/migrate-react-router/SKILL.md +769 -0
- package/skills/mime-routes/SKILL.md +27 -0
- package/skills/observability/SKILL.md +137 -0
- package/skills/parallel/SKILL.md +197 -6
- package/skills/prerender/SKILL.md +123 -100
- package/skills/rango/SKILL.md +242 -22
- package/skills/react-compiler/SKILL.md +168 -0
- package/skills/response-routes/SKILL.md +66 -9
- package/skills/route/SKILL.md +88 -4
- package/skills/router-setup/SKILL.md +90 -5
- package/skills/server-actions/SKILL.md +751 -0
- package/skills/streams-and-websockets/SKILL.md +283 -0
- package/skills/testing/SKILL.md +716 -0
- package/skills/typesafety/SKILL.md +329 -27
- package/skills/use-cache/SKILL.md +34 -5
- package/skills/view-transitions/SKILL.md +294 -0
- package/src/__augment-tests__/augment.ts +81 -0
- package/src/__augment-tests__/augmented.check.ts +117 -0
- package/src/__internal.ts +1 -1
- package/src/browser/action-coordinator.ts +53 -36
- package/src/browser/app-shell.ts +52 -0
- package/src/browser/app-version.ts +14 -0
- package/src/browser/event-controller.ts +91 -70
- package/src/browser/history-state.ts +21 -0
- package/src/browser/index.ts +3 -3
- package/src/browser/navigation-bridge.ts +102 -16
- package/src/browser/navigation-client.ts +164 -59
- package/src/browser/navigation-store.ts +75 -17
- package/src/browser/navigation-transaction.ts +21 -37
- package/src/browser/partial-update.ts +139 -38
- package/src/browser/prefetch/cache.ts +175 -15
- package/src/browser/prefetch/fetch.ts +180 -33
- package/src/browser/prefetch/queue.ts +123 -20
- package/src/browser/prefetch/resource-ready.ts +77 -0
- package/src/browser/rango-state.ts +53 -13
- package/src/browser/react/Link.tsx +81 -9
- package/src/browser/react/NavigationProvider.tsx +110 -33
- package/src/browser/react/context.ts +7 -2
- package/src/browser/react/filter-segment-order.ts +51 -7
- package/src/browser/react/index.ts +3 -0
- package/src/browser/react/location-state-shared.ts +175 -4
- package/src/browser/react/location-state.ts +39 -13
- package/src/browser/react/use-handle.ts +23 -64
- package/src/browser/react/use-navigation.ts +22 -2
- package/src/browser/react/use-params.ts +20 -8
- package/src/browser/react/use-reverse.ts +106 -0
- package/src/browser/react/use-router.ts +43 -10
- package/src/browser/react/use-segments.ts +11 -8
- package/src/browser/response-adapter.ts +25 -0
- package/src/browser/rsc-router.tsx +191 -74
- package/src/browser/scroll-restoration.ts +41 -14
- package/src/browser/segment-reconciler.ts +36 -9
- package/src/browser/segment-structure-assert.ts +2 -2
- package/src/browser/server-action-bridge.ts +31 -36
- package/src/browser/types.ts +57 -5
- package/src/build/collect-fallback-refs.ts +107 -0
- package/src/build/generate-manifest.ts +65 -40
- package/src/build/generate-route-types.ts +5 -0
- package/src/build/index.ts +2 -0
- package/src/build/route-trie.ts +52 -25
- package/src/build/route-types/codegen.ts +4 -4
- package/src/build/route-types/include-resolution.ts +9 -2
- package/src/build/route-types/per-module-writer.ts +7 -4
- package/src/build/route-types/router-processing.ts +278 -88
- package/src/build/route-types/scan-filter.ts +9 -2
- package/src/build/route-types/source-scan.ts +118 -0
- package/src/build/runtime-discovery.ts +9 -20
- package/src/cache/cache-runtime.ts +15 -11
- package/src/cache/cache-scope.ts +76 -49
- package/src/cache/cf/cf-cache-store.ts +501 -18
- 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.rsc.tsx +3 -0
- package/src/client.tsx +94 -238
- package/src/context-var.ts +72 -2
- package/src/debug.ts +2 -2
- package/src/decode-loader-results.ts +36 -0
- package/src/errors.ts +30 -1
- package/src/handle.ts +65 -12
- package/src/host/index.ts +2 -2
- package/src/host/router.ts +129 -57
- package/src/host/types.ts +31 -2
- package/src/host/utils.ts +1 -1
- package/src/href-client.ts +140 -20
- package/src/index.rsc.ts +12 -5
- package/src/index.ts +61 -11
- package/src/loader-store.ts +500 -0
- package/src/loader.rsc.ts +2 -5
- package/src/loader.ts +3 -10
- package/src/missing-id-error.ts +68 -0
- package/src/outlet-context.ts +1 -1
- package/src/prerender/store.ts +5 -4
- package/src/prerender.ts +141 -80
- package/src/response-utils.ts +37 -0
- package/src/reverse.ts +65 -15
- package/src/route-content-wrapper.tsx +6 -28
- package/src/route-definition/dsl-helpers.ts +435 -260
- package/src/route-definition/helper-factories.ts +29 -139
- package/src/route-definition/helpers-types.ts +110 -34
- package/src/route-definition/index.ts +3 -0
- package/src/route-definition/redirect.ts +11 -3
- package/src/route-definition/resolve-handler-use.ts +155 -0
- package/src/route-definition/use-item-types.ts +32 -0
- package/src/route-map-builder.ts +7 -1
- package/src/route-types.ts +37 -41
- package/src/router/basename.ts +14 -0
- package/src/router/content-negotiation.ts +113 -1
- package/src/router/error-handling.ts +1 -1
- package/src/router/find-match.ts +4 -2
- package/src/router/handler-context.ts +77 -38
- package/src/router/intercept-resolution.ts +15 -22
- package/src/router/lazy-includes.ts +12 -9
- package/src/router/loader-resolution.ts +174 -22
- package/src/router/logging.ts +5 -2
- package/src/router/manifest.ts +31 -16
- package/src/router/match-api.ts +128 -192
- package/src/router/match-handlers.ts +63 -20
- package/src/router/match-middleware/background-revalidation.ts +30 -2
- package/src/router/match-middleware/cache-lookup.ts +136 -106
- package/src/router/match-middleware/cache-store.ts +54 -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 +125 -10
- package/src/router/metrics.ts +7 -2
- package/src/router/middleware-types.ts +21 -34
- package/src/router/middleware.ts +103 -90
- package/src/router/navigation-snapshot.ts +182 -0
- package/src/router/pattern-matching.ts +101 -17
- package/src/router/prerender-match.ts +110 -10
- package/src/router/preview-match.ts +32 -102
- package/src/router/request-classification.ts +286 -0
- package/src/router/revalidation.ts +58 -2
- package/src/router/route-snapshot.ts +245 -0
- package/src/router/router-context.ts +6 -1
- package/src/router/router-interfaces.ts +77 -28
- package/src/router/router-options.ts +76 -11
- package/src/router/router-registry.ts +2 -5
- package/src/router/segment-resolution/fresh.ts +223 -24
- 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 +466 -285
- package/src/router/segment-resolution/view-transition-default.ts +36 -0
- package/src/router/segment-wrappers.ts +2 -0
- package/src/router/substitute-pattern-params.ts +56 -0
- package/src/router/telemetry.ts +99 -0
- package/src/router/trie-matching.ts +18 -13
- package/src/router/types.ts +9 -0
- package/src/router/url-params.ts +49 -0
- package/src/router.ts +91 -23
- package/src/rsc/handler-context.ts +2 -2
- package/src/rsc/handler.ts +440 -381
- package/src/rsc/helpers.ts +91 -43
- package/src/rsc/index.ts +1 -1
- package/src/rsc/loader-fetch.ts +23 -3
- package/src/rsc/manifest-init.ts +5 -1
- package/src/rsc/origin-guard.ts +28 -10
- package/src/rsc/progressive-enhancement.ts +18 -2
- package/src/rsc/response-route-handler.ts +46 -53
- package/src/rsc/rsc-rendering.ts +41 -48
- package/src/rsc/runtime-warnings.ts +9 -10
- package/src/rsc/server-action.ts +25 -37
- package/src/rsc/ssr-setup.ts +18 -2
- package/src/rsc/types.ts +17 -3
- package/src/search-params.ts +4 -4
- package/src/segment-content-promise.ts +67 -0
- package/src/segment-loader-promise.ts +122 -0
- package/src/segment-system.tsx +219 -67
- package/src/serialize.ts +243 -0
- package/src/server/context.ts +277 -61
- package/src/server/cookie-store.ts +28 -4
- package/src/server/handle-store.ts +19 -0
- package/src/server/loader-registry.ts +9 -8
- package/src/server/request-context.ts +204 -60
- package/src/ssr/index.tsx +9 -1
- package/src/static-handler.ts +19 -7
- package/src/testing/cache-status.ts +166 -0
- package/src/testing/collect-handle.ts +63 -0
- package/src/testing/dispatch.ts +440 -0
- package/src/testing/dom.entry.ts +22 -0
- package/src/testing/e2e/fixture.ts +154 -0
- package/src/testing/e2e/index.ts +149 -0
- package/src/testing/e2e/matchers.ts +51 -0
- package/src/testing/e2e/page-helpers.ts +272 -0
- package/src/testing/e2e/parity.ts +306 -0
- package/src/testing/e2e/server.ts +183 -0
- package/src/testing/flight-matchers.ts +104 -0
- package/src/testing/flight-runtime.d.ts +21 -0
- package/src/testing/flight.entry.ts +22 -0
- package/src/testing/flight.ts +182 -0
- package/src/testing/generated-routes.ts +223 -0
- package/src/testing/index.ts +106 -0
- package/src/testing/internal/context.ts +255 -0
- package/src/testing/render-route.tsx +565 -0
- package/src/testing/run-loader.ts +296 -0
- package/src/testing/run-middleware.ts +179 -0
- package/src/testing/vitest-stubs/cloudflare-email.ts +9 -0
- package/src/testing/vitest-stubs/cloudflare-workers.ts +21 -0
- package/src/testing/vitest-stubs/plugin-rsc.ts +16 -0
- package/src/testing/vitest-stubs/version.ts +5 -0
- package/src/testing/vitest.ts +183 -0
- package/src/types/cache-types.ts +4 -4
- package/src/types/global-namespace.ts +39 -26
- package/src/types/handler-context.ts +194 -72
- package/src/types/index.ts +1 -0
- package/src/types/loader-types.ts +41 -15
- package/src/types/request-scope.ts +126 -0
- package/src/types/route-entry.ts +19 -1
- package/src/types/segments.ts +37 -1
- package/src/urls/include-helper.ts +34 -67
- package/src/urls/index.ts +0 -3
- package/src/urls/path-helper-types.ts +50 -9
- package/src/urls/path-helper.ts +63 -63
- package/src/urls/pattern-types.ts +48 -19
- package/src/urls/response-types.ts +25 -22
- package/src/urls/type-extraction.ts +26 -116
- package/src/urls/urls-function.ts +1 -5
- package/src/use-loader.tsx +487 -44
- package/src/vite/debug.ts +185 -0
- package/src/vite/discovery/bundle-postprocess.ts +34 -37
- package/src/vite/discovery/discover-routers.ts +105 -51
- package/src/vite/discovery/discovery-errors.ts +194 -0
- package/src/vite/discovery/gate-state.ts +171 -0
- package/src/vite/discovery/prerender-collection.ts +188 -93
- package/src/vite/discovery/route-types-writer.ts +40 -84
- package/src/vite/discovery/self-gen-tracking.ts +27 -1
- package/src/vite/discovery/state.ts +46 -6
- package/src/vite/discovery/virtual-module-codegen.ts +13 -23
- package/src/vite/index.ts +6 -0
- package/src/vite/plugin-types.ts +111 -72
- package/src/vite/plugins/cjs-to-esm.ts +8 -7
- package/src/vite/plugins/client-ref-dedup.ts +16 -0
- package/src/vite/plugins/client-ref-hashing.ts +28 -5
- package/src/vite/plugins/cloudflare-protocol-loader-hook.d.mts +23 -0
- package/src/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
- package/src/vite/plugins/cloudflare-protocol-stub.ts +214 -0
- package/src/vite/plugins/expose-action-id.ts +55 -33
- package/src/vite/plugins/expose-id-utils.ts +24 -8
- package/src/vite/plugins/expose-ids/export-analysis.ts +100 -20
- package/src/vite/plugins/expose-ids/handler-transform.ts +12 -35
- package/src/vite/plugins/expose-ids/loader-transform.ts +3 -5
- package/src/vite/plugins/expose-ids/router-transform.ts +20 -3
- package/src/vite/plugins/expose-internal-ids.ts +544 -317
- package/src/vite/plugins/performance-tracks.ts +92 -0
- package/src/vite/plugins/refresh-cmd.ts +88 -26
- package/src/vite/plugins/use-cache-transform.ts +65 -50
- package/src/vite/plugins/version-injector.ts +39 -23
- package/src/vite/plugins/version-plugin.ts +72 -3
- package/src/vite/plugins/virtual-entries.ts +2 -2
- package/src/vite/rango.ts +265 -226
- package/src/vite/router-discovery.ts +920 -137
- package/src/vite/utils/ast-handler-extract.ts +15 -15
- package/src/vite/utils/banner.ts +4 -4
- package/src/vite/utils/bundle-analysis.ts +4 -2
- package/src/vite/utils/client-chunks.ts +190 -0
- package/src/vite/utils/forward-user-plugins.ts +193 -0
- package/src/vite/utils/manifest-utils.ts +21 -5
- package/src/vite/utils/package-resolution.ts +41 -1
- package/src/vite/utils/prerender-utils.ts +38 -5
- package/src/vite/utils/shared-utils.ts +109 -27
- package/src/browser/action-response-classifier.ts +0 -99
|
@@ -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
|
/**
|
|
@@ -285,9 +294,13 @@ export function restoreScrollPosition(options?: {
|
|
|
285
294
|
return true;
|
|
286
295
|
}
|
|
287
296
|
|
|
288
|
-
// Not streaming — scroll
|
|
289
|
-
|
|
290
|
-
|
|
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
|
+
});
|
|
291
304
|
return true;
|
|
292
305
|
}
|
|
293
306
|
|
|
@@ -319,6 +332,8 @@ export function scrollToHash(): boolean {
|
|
|
319
332
|
* Scroll to top of page
|
|
320
333
|
*/
|
|
321
334
|
export function scrollToTop(): void {
|
|
335
|
+
if (typeof window === "undefined") return;
|
|
336
|
+
if (typeof window.scrollTo !== "function") return;
|
|
322
337
|
window.scrollTo(0, 0);
|
|
323
338
|
}
|
|
324
339
|
|
|
@@ -343,31 +358,43 @@ export function handleNavigationEnd(options: {
|
|
|
343
358
|
scroll?: boolean;
|
|
344
359
|
isStreaming?: () => boolean;
|
|
345
360
|
}): void {
|
|
346
|
-
if (!initialized) {
|
|
347
|
-
return;
|
|
348
|
-
}
|
|
349
|
-
|
|
350
361
|
const { restore = false, scroll = true, isStreaming } = options;
|
|
351
362
|
|
|
352
|
-
// Don't scroll if explicitly disabled
|
|
353
|
-
if (scroll === false) {
|
|
363
|
+
// Don't scroll if explicitly disabled or not in a browser
|
|
364
|
+
if (scroll === false || typeof window === "undefined") {
|
|
354
365
|
return;
|
|
355
366
|
}
|
|
356
367
|
|
|
357
|
-
//
|
|
358
|
-
|
|
368
|
+
// Save/restore requires initialization (sessionStorage, history state).
|
|
369
|
+
// But basic scroll-to-top and hash scrolling work without it — this
|
|
370
|
+
// matters during cross-app navigation where ScrollRestoration unmounts
|
|
371
|
+
// and remounts, creating a brief window where initialized is false.
|
|
372
|
+
if (restore && initialized) {
|
|
359
373
|
if (restoreScrollPosition({ retryIfStreaming: true, isStreaming })) {
|
|
360
374
|
return;
|
|
361
375
|
}
|
|
362
376
|
// Fall through to hash or top if no saved position
|
|
363
377
|
}
|
|
364
378
|
|
|
365
|
-
//
|
|
379
|
+
// scrollToHash / scrollToTop run synchronously here.
|
|
380
|
+
// handleNavigationEnd is invoked from NavigationProvider's
|
|
381
|
+
// useLayoutEffect (post-commit, pre-paint), so a sync scrollTo is
|
|
382
|
+
// captured by the upcoming paint AND by startViewTransition's snapshot.
|
|
383
|
+
// Deferring via rAF here pushed the call past the snapshot capture,
|
|
384
|
+
// making forward navigations wrapped in a layout/route view transition
|
|
385
|
+
// skip scroll-to-top — the live DOM scrolled but the captured snapshot
|
|
386
|
+
// was at the previous scroll position, so the user-facing page stayed
|
|
387
|
+
// visually clamped at the source page's scrollY (often the new tree's
|
|
388
|
+
// max scroll for tall→short navs). Y=0 / a hash element are robust
|
|
389
|
+
// against unmeasured layout, so sync scroll is correct here even
|
|
390
|
+
// before the new tree's scrollHeight settles.
|
|
391
|
+
//
|
|
392
|
+
// (The restore branch above keeps deferToNextPaint because savedY
|
|
393
|
+
// depends on the new tree's max scroll; sync scrollTo against an
|
|
394
|
+
// unmeasured DOM would clamp savedY to whatever the old/zero max was.)
|
|
366
395
|
if (scrollToHash()) {
|
|
367
396
|
return;
|
|
368
397
|
}
|
|
369
|
-
|
|
370
|
-
// Default: scroll to top
|
|
371
398
|
scrollToTop();
|
|
372
399
|
}
|
|
373
400
|
|
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
} from "./merge-segment-loaders.js";
|
|
7
7
|
import { assertSegmentStructure } from "./segment-structure-assert.js";
|
|
8
8
|
import { splitInterceptSegments } from "./intercept-utils.js";
|
|
9
|
+
import { debugLog } from "./logging.js";
|
|
9
10
|
|
|
10
11
|
/**
|
|
11
12
|
* Determines the merging behavior for segment reconciliation.
|
|
@@ -85,14 +86,29 @@ export function reconcileSegments(input: ReconcileInput): ReconcileResult {
|
|
|
85
86
|
const cachedSegments = new Map<string, ResolvedSegment>();
|
|
86
87
|
input.cachedSegments.forEach((s) => cachedSegments.set(s.id, s));
|
|
87
88
|
|
|
89
|
+
const diffSet = new Set(diff);
|
|
90
|
+
debugLog(
|
|
91
|
+
`[reconcile] actor=${actor}, matched=${matched.length}, diff=${diff.length}`,
|
|
92
|
+
);
|
|
93
|
+
debugLog(
|
|
94
|
+
`[reconcile] server segments: ${[...serverSegments.keys()].join(", ")}`,
|
|
95
|
+
);
|
|
96
|
+
debugLog(
|
|
97
|
+
`[reconcile] cached segments: ${[...cachedSegments.keys()].join(", ")}`,
|
|
98
|
+
);
|
|
99
|
+
|
|
88
100
|
const segments = matched
|
|
89
101
|
.map((segId: string) => {
|
|
90
102
|
const fromServer = serverSegments.get(segId);
|
|
91
103
|
const fromCache = cachedSegments.get(segId);
|
|
92
104
|
|
|
93
105
|
if (fromServer) {
|
|
106
|
+
const inDiff = diffSet.has(segId);
|
|
94
107
|
// Merge partial loader data when server returns fewer loaders than cached
|
|
95
108
|
if (shouldMergeLoaders && needsLoaderMerge(fromServer, fromCache)) {
|
|
109
|
+
debugLog(
|
|
110
|
+
`[reconcile] ${segId}: MERGE loaders (server partial, ${inDiff ? "in diff" : "not in diff"})`,
|
|
111
|
+
);
|
|
96
112
|
return mergeSegmentLoaders(fromServer, fromCache);
|
|
97
113
|
}
|
|
98
114
|
|
|
@@ -143,8 +159,14 @@ export function reconcileSegments(input: ReconcileInput): ReconcileResult {
|
|
|
143
159
|
// above fails to preserve a value it should have.
|
|
144
160
|
assertSegmentStructure(fromCache, merged, context);
|
|
145
161
|
|
|
162
|
+
debugLog(
|
|
163
|
+
`[reconcile] ${segId}: SERVER+CACHE merge (${inDiff ? "in diff" : "not in diff"}, type=${fromServer.type}, component=${fromServer.component === null ? "null→cached" : "server"})`,
|
|
164
|
+
);
|
|
146
165
|
return merged;
|
|
147
166
|
}
|
|
167
|
+
debugLog(
|
|
168
|
+
`[reconcile] ${segId}: SERVER only (${inDiff ? "in diff" : "not in diff"}, type=${fromServer.type}, no cache entry)`,
|
|
169
|
+
);
|
|
148
170
|
return fromServer;
|
|
149
171
|
}
|
|
150
172
|
|
|
@@ -158,15 +180,20 @@ export function reconcileSegments(input: ReconcileInput): ReconcileResult {
|
|
|
158
180
|
return fromCache;
|
|
159
181
|
}
|
|
160
182
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
183
|
+
debugLog(
|
|
184
|
+
`[reconcile] ${segId}: CACHE only (not from server, type=${fromCache.type}, component=${fromCache.component != null ? "yes" : "null"})`,
|
|
185
|
+
);
|
|
186
|
+
|
|
187
|
+
// Return the cached segment as-is, regardless of actor. We used to clear
|
|
188
|
+
// truthy `loading` here to prevent a stale Suspense fallback from
|
|
189
|
+
// committing against cached content, but that swapped the render tree
|
|
190
|
+
// from the LoaderBoundary branch to the plain OutletProvider branch
|
|
191
|
+
// inside renderSegments, causing React to unmount the entire chain
|
|
192
|
+
// (LoaderBoundary > Suspense > LoaderResolver > RouteContentWrapper >
|
|
193
|
+
// Suspender) every time the user opened an intercept or navigated back
|
|
194
|
+
// to a cached page. The flicker is now prevented by renderSegments'
|
|
195
|
+
// promise memoization keeping React's use() in "known fulfilled" state,
|
|
196
|
+
// so preserving `loading` keeps the element tree stable.
|
|
170
197
|
return fromCache;
|
|
171
198
|
})
|
|
172
199
|
.filter(Boolean) as ResolvedSegment[];
|
|
@@ -48,7 +48,7 @@ export function assertSegmentStructure(
|
|
|
48
48
|
|
|
49
49
|
if (cachedCategory !== incomingCategory) {
|
|
50
50
|
console.warn(
|
|
51
|
-
`[
|
|
51
|
+
`[Rango] Tree structure mismatch detected in ${context} ` +
|
|
52
52
|
`for segment "${cached.id}": loading category changed from ` +
|
|
53
53
|
`"${cachedCategory}" (${describeLoading(cached.loading)}) to ` +
|
|
54
54
|
`"${incomingCategory}" (${describeLoading(incoming.loading)}). ` +
|
|
@@ -64,7 +64,7 @@ export function assertSegmentStructure(
|
|
|
64
64
|
const incomingHasMount = !!incoming.mountPath;
|
|
65
65
|
if (cachedHasMount !== incomingHasMount) {
|
|
66
66
|
console.warn(
|
|
67
|
-
`[
|
|
67
|
+
`[Rango] MountContextProvider mismatch detected in ${context} ` +
|
|
68
68
|
`for segment "${cached.id}": mountPath changed from ` +
|
|
69
69
|
`${cachedHasMount ? `"${cached.mountPath}"` : "undefined"} to ` +
|
|
70
70
|
`${incomingHasMount ? `"${incoming.mountPath}"` : "undefined"}. ` +
|
|
@@ -25,10 +25,12 @@ import { validateRedirectOrigin } from "./validate-redirect-origin.js";
|
|
|
25
25
|
import {
|
|
26
26
|
extractRscHeaderUrl,
|
|
27
27
|
emptyResponse,
|
|
28
|
+
handleReloadHeader,
|
|
28
29
|
teeWithCompletion,
|
|
29
30
|
} from "./response-adapter.js";
|
|
30
31
|
import { mergeLocationState } from "./history-state.js";
|
|
31
32
|
import { classifyActionOutcome } from "./action-coordinator.js";
|
|
33
|
+
import { getAppVersion } from "./app-version.js";
|
|
32
34
|
|
|
33
35
|
// Polyfill Symbol.dispose/asyncDispose for Safari and older browsers
|
|
34
36
|
if (typeof Symbol.dispose === "undefined") {
|
|
@@ -43,8 +45,6 @@ if (typeof Symbol.asyncDispose === "undefined") {
|
|
|
43
45
|
*/
|
|
44
46
|
export interface ServerActionBridgeConfigWithController extends ServerActionBridgeConfig {
|
|
45
47
|
eventController: EventController;
|
|
46
|
-
/** RSC version from initial payload metadata */
|
|
47
|
-
version?: string;
|
|
48
48
|
/** Callback to trigger SPA navigation (for action redirects) */
|
|
49
49
|
onNavigate?: (
|
|
50
50
|
url: string,
|
|
@@ -75,10 +75,23 @@ export function createServerActionBridge(
|
|
|
75
75
|
deps,
|
|
76
76
|
onUpdate,
|
|
77
77
|
renderSegments,
|
|
78
|
-
version,
|
|
79
78
|
onNavigate,
|
|
80
79
|
} = config;
|
|
81
80
|
|
|
81
|
+
// SPA-navigate when onNavigate is set, else hard-reload. state is omitted (not
|
|
82
|
+
// passed as undefined) to match the header path's prior call shape.
|
|
83
|
+
async function dispatchRedirect(url: string, state?: unknown): Promise<void> {
|
|
84
|
+
if (onNavigate) {
|
|
85
|
+
await onNavigate(url, {
|
|
86
|
+
...(state !== undefined ? { state } : {}),
|
|
87
|
+
replace: true,
|
|
88
|
+
_skipCache: true,
|
|
89
|
+
});
|
|
90
|
+
} else {
|
|
91
|
+
window.location.href = url;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
82
95
|
let isRegistered = false;
|
|
83
96
|
|
|
84
97
|
const fetchPartialUpdate = createPartialUpdater({
|
|
@@ -86,7 +99,7 @@ export function createServerActionBridge(
|
|
|
86
99
|
client,
|
|
87
100
|
onUpdate,
|
|
88
101
|
renderSegments,
|
|
89
|
-
|
|
102
|
+
getVersion: getAppVersion,
|
|
90
103
|
});
|
|
91
104
|
|
|
92
105
|
/**
|
|
@@ -165,9 +178,15 @@ export function createServerActionBridge(
|
|
|
165
178
|
segmentState.currentSegmentIds.join(","),
|
|
166
179
|
);
|
|
167
180
|
// Add version param for version mismatch detection
|
|
181
|
+
const version = getAppVersion();
|
|
168
182
|
if (version) {
|
|
169
183
|
url.searchParams.set("_rsc_v", version);
|
|
170
184
|
}
|
|
185
|
+
// Add router ID for app switch detection
|
|
186
|
+
const rid = store.getRouterId?.();
|
|
187
|
+
if (rid) {
|
|
188
|
+
url.searchParams.set("_rsc_rid", rid);
|
|
189
|
+
}
|
|
171
190
|
|
|
172
191
|
// Encode arguments
|
|
173
192
|
const encodedBody = await deps.encodeReply(args, { temporaryReferences });
|
|
@@ -206,7 +225,6 @@ export function createServerActionBridge(
|
|
|
206
225
|
"rsc-action": id,
|
|
207
226
|
"X-RSC-Router-Client-Path": segmentState.currentUrl,
|
|
208
227
|
...(tx && { "X-RSC-Router-Request-Id": tx.requestId }),
|
|
209
|
-
// Send intercept source URL so server can maintain intercept context
|
|
210
228
|
...(interceptSourceUrl && {
|
|
211
229
|
"X-RSC-Router-Intercept-Source": interceptSourceUrl,
|
|
212
230
|
}),
|
|
@@ -219,18 +237,12 @@ export function createServerActionBridge(
|
|
|
219
237
|
handle.signal.removeEventListener("abort", onHandleAbort);
|
|
220
238
|
|
|
221
239
|
// Check for version mismatch - server wants us to reload
|
|
222
|
-
const
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
}
|
|
227
|
-
if (
|
|
228
|
-
log("version mismatch on action, reloading", {
|
|
229
|
-
reloadUrl: reload.url,
|
|
230
|
-
});
|
|
231
|
-
window.location.href = reload.url;
|
|
232
|
-
return new Promise<Response>(() => {});
|
|
233
|
-
}
|
|
240
|
+
const reloadResult = handleReloadHeader(response, {
|
|
241
|
+
onBlocked: resolveStreamComplete,
|
|
242
|
+
onReload: (url) =>
|
|
243
|
+
log("version mismatch on action, reloading", { reloadUrl: url }),
|
|
244
|
+
});
|
|
245
|
+
if (reloadResult) return reloadResult;
|
|
234
246
|
|
|
235
247
|
// Simple redirect from action (no state, no RSC payload).
|
|
236
248
|
// Short-circuits before createFromFetch — no Flight deserialization needed.
|
|
@@ -240,14 +252,7 @@ export function createServerActionBridge(
|
|
|
240
252
|
if (redirect && redirect !== "blocked" && !handle.signal.aborted) {
|
|
241
253
|
log("action simple redirect", { url: redirect.url });
|
|
242
254
|
handle.complete(undefined);
|
|
243
|
-
|
|
244
|
-
await onNavigate(redirect.url, {
|
|
245
|
-
replace: true,
|
|
246
|
-
_skipCache: true,
|
|
247
|
-
});
|
|
248
|
-
} else {
|
|
249
|
-
window.location.href = redirect.url;
|
|
250
|
-
}
|
|
255
|
+
await dispatchRedirect(redirect.url);
|
|
251
256
|
return new Promise<Response>(() => {});
|
|
252
257
|
}
|
|
253
258
|
if (redirect === "blocked") {
|
|
@@ -309,7 +314,6 @@ export function createServerActionBridge(
|
|
|
309
314
|
matchedCount: payload.metadata?.matched?.length ?? 0,
|
|
310
315
|
diffCount: payload.metadata?.diff?.length ?? 0,
|
|
311
316
|
});
|
|
312
|
-
|
|
313
317
|
// Guard: if the action was aborted while streaming (e.g., user navigated
|
|
314
318
|
// away or abortAllActions fired), bail out before any reconcile/render/cache
|
|
315
319
|
// writes to avoid overwriting the current UI with stale action results.
|
|
@@ -337,18 +341,9 @@ export function createServerActionBridge(
|
|
|
337
341
|
handle.complete(returnValue?.data);
|
|
338
342
|
return returnValue?.data;
|
|
339
343
|
}
|
|
340
|
-
const redirectState = metadata.locationState;
|
|
341
344
|
log("action redirect", { url: redirectUrl });
|
|
342
345
|
handle.complete(returnValue?.data);
|
|
343
|
-
|
|
344
|
-
await onNavigate(redirectUrl, {
|
|
345
|
-
state: redirectState,
|
|
346
|
-
replace: true,
|
|
347
|
-
_skipCache: true,
|
|
348
|
-
});
|
|
349
|
-
} else {
|
|
350
|
-
window.location.href = redirectUrl;
|
|
351
|
-
}
|
|
346
|
+
await dispatchRedirect(redirectUrl, metadata.locationState);
|
|
352
347
|
return returnValue?.data;
|
|
353
348
|
}
|
|
354
349
|
|
package/src/browser/types.ts
CHANGED
|
@@ -32,10 +32,19 @@ 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[];
|
|
38
41
|
diff?: string[];
|
|
42
|
+
/**
|
|
43
|
+
* All segment ids re-resolved on the server, including null-component
|
|
44
|
+
* ones excluded from `segments`/`diff`. Drives client-side handle-bucket
|
|
45
|
+
* cleanup. Superset of `diff`. See MatchResult.resolvedIds.
|
|
46
|
+
*/
|
|
47
|
+
resolvedIds?: string[];
|
|
39
48
|
/** Merged route params from the matched route */
|
|
40
49
|
params?: Record<string, string>;
|
|
41
50
|
/**
|
|
@@ -70,6 +79,8 @@ export interface RscMetadata {
|
|
|
70
79
|
* Included when theme is enabled in router config.
|
|
71
80
|
*/
|
|
72
81
|
initialTheme?: Theme;
|
|
82
|
+
/** URL prefix for all routes (from createRouter({ basename })). */
|
|
83
|
+
basename?: string;
|
|
73
84
|
/** Whether connection warmup is enabled */
|
|
74
85
|
warmupEnabled?: boolean;
|
|
75
86
|
/** Server-side redirect with optional state (for partial requests) */
|
|
@@ -215,6 +226,15 @@ export interface SegmentState {
|
|
|
215
226
|
export interface NavigationUpdate {
|
|
216
227
|
root: ReactNode | Promise<ReactNode>;
|
|
217
228
|
metadata: RscMetadata;
|
|
229
|
+
/** Scroll behavior to apply after React commits this update */
|
|
230
|
+
scroll?: {
|
|
231
|
+
/** For back/forward: restore saved position */
|
|
232
|
+
restore?: boolean;
|
|
233
|
+
/** Set to false to disable scrolling entirely */
|
|
234
|
+
enabled?: boolean;
|
|
235
|
+
/** Function to check if streaming is in progress */
|
|
236
|
+
isStreaming?: () => boolean;
|
|
237
|
+
};
|
|
218
238
|
}
|
|
219
239
|
|
|
220
240
|
/**
|
|
@@ -332,7 +352,13 @@ export type ReadonlyURLSearchParams = Omit<
|
|
|
332
352
|
export interface RscBrowserDependencies {
|
|
333
353
|
createFromFetch: <T>(
|
|
334
354
|
response: Promise<Response>,
|
|
335
|
-
options?: {
|
|
355
|
+
options?: {
|
|
356
|
+
temporaryReferences?: any;
|
|
357
|
+
findSourceMapURL?: (
|
|
358
|
+
filename: string,
|
|
359
|
+
environmentName: string,
|
|
360
|
+
) => string | null;
|
|
361
|
+
},
|
|
336
362
|
) => Promise<T>;
|
|
337
363
|
createFromReadableStream: <T>(stream: ReadableStream) => Promise<T>;
|
|
338
364
|
encodeReply: (
|
|
@@ -394,16 +420,25 @@ export interface NavigationStore {
|
|
|
394
420
|
segments: ResolvedSegment[],
|
|
395
421
|
handleData?: HandleData,
|
|
396
422
|
): void;
|
|
397
|
-
getCachedSegments(
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
423
|
+
getCachedSegments(historyKey: string):
|
|
424
|
+
| {
|
|
425
|
+
segments: ResolvedSegment[];
|
|
426
|
+
stale: boolean;
|
|
427
|
+
handleData?: HandleData;
|
|
428
|
+
routerId?: string;
|
|
429
|
+
}
|
|
401
430
|
| undefined;
|
|
402
431
|
hasHistoryCache(historyKey: string): boolean;
|
|
403
432
|
updateCacheHandleData(historyKey: string, handleData: HandleData): void;
|
|
404
433
|
markCacheAsStale(): void;
|
|
405
434
|
markCacheAsStaleAndBroadcast(): void;
|
|
406
435
|
clearHistoryCache(): void;
|
|
436
|
+
/**
|
|
437
|
+
* Clear this tab's nav + prefetch caches without broadcasting or rotating
|
|
438
|
+
* shared state. Intended for app-switch transitions that affect only this
|
|
439
|
+
* tab's session.
|
|
440
|
+
*/
|
|
441
|
+
clearHistoryCacheLocal(): void;
|
|
407
442
|
broadcastCacheInvalidation(): void;
|
|
408
443
|
|
|
409
444
|
// Cross-tab refresh callback (set by navigation bridge)
|
|
@@ -413,6 +448,10 @@ export interface NavigationStore {
|
|
|
413
448
|
getInterceptSourceUrl(): string | null;
|
|
414
449
|
setInterceptSourceUrl(url: string | null): void;
|
|
415
450
|
|
|
451
|
+
// Router identity tracking (for cross-app navigation detection)
|
|
452
|
+
getRouterId?(): string | undefined;
|
|
453
|
+
setRouterId?(id: string): void;
|
|
454
|
+
|
|
416
455
|
// UI update notifications
|
|
417
456
|
onUpdate(callback: UpdateSubscriber): () => void;
|
|
418
457
|
emitUpdate(update: NavigationUpdate): void;
|
|
@@ -443,6 +482,8 @@ export interface FetchPartialOptions {
|
|
|
443
482
|
interceptSourceUrl?: string;
|
|
444
483
|
/** RSC version for cache invalidation detection */
|
|
445
484
|
version?: string;
|
|
485
|
+
/** Current router ID — server detects app switch and returns full response */
|
|
486
|
+
routerId?: string;
|
|
446
487
|
/** If true, this is an HMR refetch - server should invalidate manifest cache */
|
|
447
488
|
hmr?: boolean;
|
|
448
489
|
}
|
|
@@ -511,6 +552,17 @@ export interface NavigationBridge {
|
|
|
511
552
|
refresh(): Promise<void>;
|
|
512
553
|
handlePopstate(): Promise<void>;
|
|
513
554
|
registerLinkInterception(): () => void;
|
|
555
|
+
/** Current RSC version (live, reflects the latest updateVersion). */
|
|
556
|
+
getVersion(): string | undefined;
|
|
557
|
+
/** Update the RSC version (e.g. after HMR). Clears prefetch cache. */
|
|
558
|
+
updateVersion(newVersion: string): void;
|
|
559
|
+
/**
|
|
560
|
+
* Replace the active app-shell snapshot (rootLayout, basename, version)
|
|
561
|
+
* atomically. Used on cross-app navigations when the response's routerId
|
|
562
|
+
* indicates the user entered a different app. Theme, warmup, and prefetch
|
|
563
|
+
* TTL are document-lifetime and not part of the shell.
|
|
564
|
+
*/
|
|
565
|
+
updateAppShell(next: import("./app-shell.js").AppShell): void;
|
|
514
566
|
}
|
|
515
567
|
|
|
516
568
|
/**
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
// Collect the `"use client"` client-reference keys reachable from an error /
|
|
2
|
+
// notFound boundary registration, for routing them into the dedicated
|
|
3
|
+
// `app-fallback` chunk (see vite/utils/client-chunks.ts).
|
|
4
|
+
//
|
|
5
|
+
// A boundary registration is not always a bare client element. The common,
|
|
6
|
+
// load-bearing pattern wraps the client boundary in providers a thrown handler
|
|
7
|
+
// needs (the layout that would normally supply them did not mount):
|
|
8
|
+
//
|
|
9
|
+
// defaultErrorBoundary: ({ error }) => (
|
|
10
|
+
// <FallbackIntl locales={...}>
|
|
11
|
+
// <ThemedError error={error} /> // <- the real "use client" boundary
|
|
12
|
+
// </FallbackIntl>
|
|
13
|
+
// )
|
|
14
|
+
//
|
|
15
|
+
// So the value may be (a) a handler FUNCTION returning a tree, or (b) an element
|
|
16
|
+
// tree with the client boundary nested below server wrappers. We:
|
|
17
|
+
// 1. If it's a function, CALL it with synthetic props to get the returned tree.
|
|
18
|
+
// This only constructs JSX — the inner components are element `type`s, never
|
|
19
|
+
// invoked — so no hooks run. Guarded: a boundary that needs a real render
|
|
20
|
+
// context (request globals, etc.) throws and is skipped (graceful: it simply
|
|
21
|
+
// stays on the default grouping, as before).
|
|
22
|
+
// 2. Walk the resulting tree and report every element whose `.type` is a
|
|
23
|
+
// plugin-rsc client reference.
|
|
24
|
+
//
|
|
25
|
+
// Limit: a boundary that *conditionally* renders different client components based
|
|
26
|
+
// on the runtime error cannot be resolved statically — only the branch taken with
|
|
27
|
+
// the synthetic error is seen. Such cases fall back to the default chunk; the
|
|
28
|
+
// custom `clientChunks` function is the escape hatch.
|
|
29
|
+
|
|
30
|
+
const CLIENT_REF = Symbol.for("react.client.reference");
|
|
31
|
+
const MAX_DEPTH = 40;
|
|
32
|
+
|
|
33
|
+
// Synthetic props covering the error-boundary (`{ error, reset }`) and notFound
|
|
34
|
+
// (`{ pathname }`) handler shapes. The handler destructures what it needs.
|
|
35
|
+
const SYNTHETIC_PROPS = {
|
|
36
|
+
error: new Error("rango: build-time fallback-chunk discovery"),
|
|
37
|
+
reset: () => {},
|
|
38
|
+
pathname: "/",
|
|
39
|
+
info: { componentStack: "" },
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
interface MaybeElement {
|
|
43
|
+
type?: { $$typeof?: symbol; $$id?: string };
|
|
44
|
+
props?: Record<string, unknown>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function isReactNodeLike(v: unknown): boolean {
|
|
48
|
+
return (
|
|
49
|
+
Array.isArray(v) ||
|
|
50
|
+
(typeof v === "object" && v !== null && "$$typeof" in (v as object))
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function walkElementTree(
|
|
55
|
+
node: unknown,
|
|
56
|
+
report: (refKey: string) => void,
|
|
57
|
+
depth: number,
|
|
58
|
+
): void {
|
|
59
|
+
if (node == null || depth > MAX_DEPTH) return;
|
|
60
|
+
if (Array.isArray(node)) {
|
|
61
|
+
for (const child of node) walkElementTree(child, report, depth + 1);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
if (typeof node !== "object") return;
|
|
65
|
+
|
|
66
|
+
const el = node as MaybeElement;
|
|
67
|
+
const type = el.type;
|
|
68
|
+
if (type?.$$typeof === CLIENT_REF && typeof type.$$id === "string") {
|
|
69
|
+
// $$id is `<referenceKey>#<exportName>` in build mode — keep the referenceKey.
|
|
70
|
+
report(type.$$id.split("#")[0]);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const props = el.props;
|
|
74
|
+
if (props && typeof props === "object") {
|
|
75
|
+
// Children are always nodes; other props are followed only when they look
|
|
76
|
+
// like React nodes (slots/icons), never arbitrary data objects.
|
|
77
|
+
walkElementTree(props.children, report, depth + 1);
|
|
78
|
+
for (const key in props) {
|
|
79
|
+
if (key === "children") continue;
|
|
80
|
+
const value = props[key];
|
|
81
|
+
if (isReactNodeLike(value)) walkElementTree(value, report, depth + 1);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Report every `"use client"` client-reference key reachable from a single
|
|
88
|
+
* error/notFound boundary registration (handler function or element tree).
|
|
89
|
+
*/
|
|
90
|
+
export function collectFallbackClientRefs(
|
|
91
|
+
boundary: unknown,
|
|
92
|
+
report: (refKey: string) => void,
|
|
93
|
+
): void {
|
|
94
|
+
try {
|
|
95
|
+
let node = boundary;
|
|
96
|
+
if (typeof node === "function") {
|
|
97
|
+
node = (node as (props: unknown) => unknown)(SYNTHETIC_PROPS);
|
|
98
|
+
}
|
|
99
|
+
walkElementTree(node, report, 0);
|
|
100
|
+
} catch {
|
|
101
|
+
// The boundary needs a real render context (request globals, hooks at the
|
|
102
|
+
// top level) or its tree has hostile getters. Its client refs can't be
|
|
103
|
+
// resolved statically — skip. It stays on the default grouping (no
|
|
104
|
+
// regression vs. not collecting), and the custom clientChunks fn is the
|
|
105
|
+
// escape hatch for such cases.
|
|
106
|
+
}
|
|
107
|
+
}
|