@rangojs/router 0.0.0-experimental.10 → 0.0.0-experimental.100
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 +9 -0
- package/README.md +1037 -4
- package/dist/bin/rango.js +1619 -157
- package/dist/vite/index.js +5762 -2301
- package/dist/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
- package/package.json +71 -63
- package/skills/breadcrumbs/SKILL.md +252 -0
- package/skills/cache-guide/SKILL.md +294 -0
- package/skills/caching/SKILL.md +93 -23
- package/skills/composability/SKILL.md +172 -0
- package/skills/debug-manifest/SKILL.md +12 -8
- package/skills/document-cache/SKILL.md +18 -16
- package/skills/fonts/SKILL.md +6 -4
- package/skills/handler-use/SKILL.md +364 -0
- package/skills/hooks/SKILL.md +367 -71
- package/skills/host-router/SKILL.md +218 -0
- package/skills/i18n/SKILL.md +276 -0
- package/skills/intercept/SKILL.md +176 -8
- package/skills/layout/SKILL.md +124 -3
- package/skills/links/SKILL.md +304 -25
- package/skills/loader/SKILL.md +474 -47
- package/skills/middleware/SKILL.md +207 -37
- package/skills/migrate-nextjs/SKILL.md +562 -0
- package/skills/migrate-react-router/SKILL.md +769 -0
- package/skills/mime-routes/SKILL.md +15 -11
- package/skills/parallel/SKILL.md +272 -1
- package/skills/prerender/SKILL.md +467 -65
- package/skills/rango/SKILL.md +89 -21
- package/skills/response-routes/SKILL.md +152 -91
- package/skills/route/SKILL.md +305 -14
- package/skills/router-setup/SKILL.md +210 -32
- package/skills/server-actions/SKILL.md +739 -0
- package/skills/streams-and-websockets/SKILL.md +283 -0
- package/skills/theme/SKILL.md +9 -8
- package/skills/typesafety/SKILL.md +333 -86
- package/skills/use-cache/SKILL.md +324 -0
- package/skills/view-transitions/SKILL.md +212 -0
- package/src/__internal.ts +102 -4
- package/src/bin/rango.ts +312 -15
- package/src/browser/action-coordinator.ts +97 -0
- package/src/browser/action-response-classifier.ts +99 -0
- package/src/browser/app-shell.ts +52 -0
- package/src/browser/app-version.ts +14 -0
- package/src/browser/event-controller.ts +136 -68
- package/src/browser/history-state.ts +80 -0
- package/src/browser/intercept-utils.ts +52 -0
- package/src/browser/link-interceptor.ts +24 -4
- package/src/browser/logging.ts +55 -0
- package/src/browser/merge-segment-loaders.ts +20 -12
- package/src/browser/navigation-bridge.ts +374 -561
- package/src/browser/navigation-client.ts +228 -70
- package/src/browser/navigation-store.ts +97 -55
- package/src/browser/navigation-transaction.ts +297 -0
- package/src/browser/network-error-handler.ts +61 -0
- package/src/browser/partial-update.ts +376 -315
- package/src/browser/prefetch/cache.ts +314 -0
- package/src/browser/prefetch/fetch.ts +282 -0
- package/src/browser/prefetch/observer.ts +65 -0
- package/src/browser/prefetch/policy.ts +48 -0
- package/src/browser/prefetch/queue.ts +191 -0
- package/src/browser/prefetch/resource-ready.ts +77 -0
- package/src/browser/rango-state.ts +152 -0
- package/src/browser/react/Link.tsx +255 -71
- package/src/browser/react/NavigationProvider.tsx +152 -24
- package/src/browser/react/context.ts +11 -0
- package/src/browser/react/filter-segment-order.ts +55 -0
- package/src/browser/react/index.ts +15 -12
- package/src/browser/react/location-state-shared.ts +95 -53
- package/src/browser/react/location-state.ts +60 -15
- package/src/browser/react/mount-context.ts +6 -1
- package/src/browser/react/nonce-context.ts +23 -0
- package/src/browser/react/shallow-equal.ts +27 -0
- package/src/browser/react/use-action.ts +29 -51
- package/src/browser/react/use-client-cache.ts +5 -3
- package/src/browser/react/use-handle.ts +30 -120
- package/src/browser/react/use-link-status.ts +6 -5
- package/src/browser/react/use-navigation.ts +44 -65
- package/src/browser/react/use-params.ts +78 -0
- package/src/browser/react/use-pathname.ts +47 -0
- package/src/browser/react/use-reverse.ts +99 -0
- package/src/browser/react/use-router.ts +83 -0
- package/src/browser/react/use-search-params.ts +56 -0
- package/src/browser/react/use-segments.ts +85 -99
- package/src/browser/response-adapter.ts +73 -0
- package/src/browser/rsc-router.tsx +246 -64
- package/src/browser/scroll-restoration.ts +127 -52
- package/src/browser/segment-reconciler.ts +243 -0
- package/src/browser/segment-structure-assert.ts +16 -0
- package/src/browser/server-action-bridge.ts +510 -603
- package/src/browser/shallow.ts +6 -1
- package/src/browser/types.ts +158 -48
- package/src/browser/validate-redirect-origin.ts +29 -0
- package/src/build/generate-manifest.ts +84 -23
- package/src/build/generate-route-types.ts +39 -828
- package/src/build/index.ts +4 -5
- package/src/build/route-trie.ts +85 -32
- package/src/build/route-types/ast-helpers.ts +25 -0
- package/src/build/route-types/ast-route-extraction.ts +98 -0
- package/src/build/route-types/codegen.ts +102 -0
- package/src/build/route-types/include-resolution.ts +418 -0
- package/src/build/route-types/param-extraction.ts +48 -0
- package/src/build/route-types/per-module-writer.ts +128 -0
- package/src/build/route-types/router-processing.ts +618 -0
- package/src/build/route-types/scan-filter.ts +85 -0
- package/src/build/runtime-discovery.ts +231 -0
- package/src/cache/background-task.ts +34 -0
- package/src/cache/cache-key-utils.ts +44 -0
- package/src/cache/cache-policy.ts +125 -0
- package/src/cache/cache-runtime.ts +342 -0
- package/src/cache/cache-scope.ts +167 -307
- package/src/cache/cf/cf-cache-store.ts +573 -21
- package/src/cache/cf/index.ts +13 -3
- package/src/cache/document-cache.ts +116 -77
- package/src/cache/handle-capture.ts +81 -0
- package/src/cache/handle-snapshot.ts +41 -0
- package/src/cache/index.ts +1 -15
- package/src/cache/memory-segment-store.ts +191 -13
- package/src/cache/profile-registry.ts +73 -0
- package/src/cache/read-through-swr.ts +134 -0
- package/src/cache/segment-codec.ts +256 -0
- package/src/cache/taint.ts +153 -0
- package/src/cache/types.ts +72 -122
- package/src/client.rsc.tsx +6 -1
- package/src/client.tsx +118 -302
- package/src/component-utils.ts +4 -4
- package/src/components/DefaultDocument.tsx +5 -1
- package/src/context-var.ts +156 -0
- package/src/debug.ts +19 -9
- package/src/errors.ts +77 -7
- package/src/handle.ts +55 -10
- package/src/handles/MetaTags.tsx +73 -20
- package/src/handles/breadcrumbs.ts +66 -0
- package/src/handles/index.ts +1 -0
- package/src/handles/meta.ts +30 -13
- package/src/host/cookie-handler.ts +21 -15
- package/src/host/errors.ts +8 -8
- package/src/host/index.ts +4 -7
- package/src/host/pattern-matcher.ts +27 -27
- package/src/host/router.ts +61 -39
- package/src/host/testing.ts +8 -8
- package/src/host/types.ts +15 -7
- package/src/host/utils.ts +1 -1
- package/src/href-client.ts +65 -45
- package/src/index.rsc.ts +138 -21
- package/src/index.ts +206 -51
- package/src/internal-debug.ts +11 -0
- package/src/loader.rsc.ts +25 -143
- package/src/loader.ts +27 -10
- package/src/network-error-thrower.tsx +3 -1
- package/src/outlet-context.ts +1 -1
- package/src/outlet-provider.tsx +45 -0
- package/src/prerender/param-hash.ts +4 -2
- package/src/prerender/store.ts +159 -13
- package/src/prerender.ts +397 -29
- package/src/response-utils.ts +28 -0
- package/src/reverse.ts +231 -121
- package/src/root-error-boundary.tsx +41 -29
- package/src/route-content-wrapper.tsx +7 -4
- package/src/route-definition/dsl-helpers.ts +1134 -0
- package/src/route-definition/helper-factories.ts +200 -0
- package/src/route-definition/helpers-types.ts +483 -0
- package/src/route-definition/index.ts +55 -0
- package/src/route-definition/redirect.ts +101 -0
- package/src/route-definition/resolve-handler-use.ts +155 -0
- package/src/route-definition.ts +1 -1431
- package/src/route-map-builder.ts +162 -123
- package/src/route-name.ts +53 -0
- package/src/route-types.ts +66 -9
- package/src/router/content-negotiation.ts +215 -0
- package/src/router/debug-manifest.ts +72 -0
- package/src/router/error-handling.ts +9 -9
- package/src/router/find-match.ts +160 -0
- package/src/router/handler-context.ts +418 -86
- package/src/router/intercept-resolution.ts +35 -20
- package/src/router/lazy-includes.ts +237 -0
- package/src/router/loader-resolution.ts +359 -128
- package/src/router/logging.ts +251 -0
- package/src/router/manifest.ts +98 -32
- package/src/router/match-api.ts +196 -261
- package/src/router/match-context.ts +4 -2
- package/src/router/match-handlers.ts +441 -0
- package/src/router/match-middleware/background-revalidation.ts +108 -93
- package/src/router/match-middleware/cache-lookup.ts +415 -86
- package/src/router/match-middleware/cache-store.ts +91 -29
- package/src/router/match-middleware/intercept-resolution.ts +48 -21
- package/src/router/match-middleware/segment-resolution.ts +73 -9
- package/src/router/match-pipelines.ts +10 -45
- package/src/router/match-result.ts +154 -35
- package/src/router/metrics.ts +240 -15
- package/src/router/middleware-cookies.ts +55 -0
- package/src/router/middleware-types.ts +209 -0
- package/src/router/middleware.ts +373 -371
- package/src/router/navigation-snapshot.ts +182 -0
- package/src/router/pattern-matching.ts +292 -52
- package/src/router/prerender-match.ts +502 -0
- package/src/router/preview-match.ts +98 -0
- package/src/router/request-classification.ts +310 -0
- package/src/router/revalidation.ts +152 -39
- package/src/router/route-snapshot.ts +245 -0
- package/src/router/router-context.ts +41 -21
- package/src/router/router-interfaces.ts +484 -0
- package/src/router/router-options.ts +618 -0
- package/src/router/router-registry.ts +24 -0
- package/src/router/segment-resolution/fresh.ts +756 -0
- package/src/router/segment-resolution/helpers.ts +268 -0
- package/src/router/segment-resolution/loader-cache.ts +199 -0
- package/src/router/segment-resolution/revalidation.ts +1407 -0
- package/src/router/segment-resolution/static-store.ts +67 -0
- package/src/router/segment-resolution.ts +21 -1315
- package/src/router/segment-wrappers.ts +291 -0
- package/src/router/substitute-pattern-params.ts +56 -0
- package/src/router/telemetry-otel.ts +299 -0
- package/src/router/telemetry.ts +300 -0
- package/src/router/timeout.ts +148 -0
- package/src/router/trie-matching.ts +111 -39
- package/src/router/types.ts +17 -9
- package/src/router/url-params.ts +49 -0
- package/src/router.ts +642 -2011
- package/src/rsc/handler-context.ts +45 -0
- package/src/rsc/handler.ts +864 -1114
- package/src/rsc/helpers.ts +181 -19
- package/src/rsc/index.ts +0 -20
- package/src/rsc/loader-fetch.ts +229 -0
- package/src/rsc/manifest-init.ts +90 -0
- package/src/rsc/nonce.ts +14 -0
- package/src/rsc/origin-guard.ts +141 -0
- package/src/rsc/progressive-enhancement.ts +395 -0
- package/src/rsc/response-error.ts +37 -0
- package/src/rsc/response-route-handler.ts +360 -0
- package/src/rsc/rsc-rendering.ts +256 -0
- package/src/rsc/runtime-warnings.ts +42 -0
- package/src/rsc/server-action.ts +360 -0
- package/src/rsc/ssr-setup.ts +128 -0
- package/src/rsc/types.ts +52 -11
- package/src/search-params.ts +230 -0
- package/src/segment-content-promise.ts +67 -0
- package/src/segment-loader-promise.ts +122 -0
- package/src/segment-system.tsx +187 -38
- package/src/server/context.ts +333 -59
- package/src/server/cookie-store.ts +190 -0
- package/src/server/fetchable-loader-store.ts +37 -0
- package/src/server/handle-store.ts +113 -15
- package/src/server/loader-registry.ts +24 -64
- package/src/server/request-context.ts +603 -109
- package/src/server.ts +35 -155
- package/src/ssr/index.tsx +107 -30
- package/src/static-handler.ts +126 -0
- package/src/theme/ThemeProvider.tsx +21 -15
- package/src/theme/ThemeScript.tsx +5 -5
- package/src/theme/constants.ts +5 -2
- package/src/theme/index.ts +4 -14
- package/src/theme/theme-context.ts +4 -30
- package/src/theme/theme-script.ts +21 -18
- package/src/types/boundaries.ts +158 -0
- package/src/types/cache-types.ts +198 -0
- package/src/types/error-types.ts +192 -0
- package/src/types/global-namespace.ts +100 -0
- package/src/types/handler-context.ts +764 -0
- package/src/types/index.ts +88 -0
- package/src/types/loader-types.ts +209 -0
- package/src/types/request-scope.ts +126 -0
- package/src/types/route-config.ts +170 -0
- package/src/types/route-entry.ts +120 -0
- package/src/types/segments.ts +167 -0
- package/src/types.ts +1 -1757
- package/src/urls/include-helper.ts +207 -0
- package/src/urls/index.ts +53 -0
- package/src/urls/path-helper-types.ts +372 -0
- package/src/urls/path-helper.ts +364 -0
- package/src/urls/pattern-types.ts +107 -0
- package/src/urls/response-types.ts +108 -0
- package/src/urls/type-extraction.ts +372 -0
- package/src/urls/urls-function.ts +98 -0
- package/src/urls.ts +1 -1282
- package/src/use-loader.tsx +161 -81
- package/src/vite/debug.ts +184 -0
- package/src/vite/discovery/bundle-postprocess.ts +181 -0
- package/src/vite/discovery/discover-routers.ts +376 -0
- package/src/vite/discovery/gate-state.ts +171 -0
- package/src/vite/discovery/prerender-collection.ts +486 -0
- package/src/vite/discovery/route-types-writer.ts +258 -0
- package/src/vite/discovery/self-gen-tracking.ts +73 -0
- package/src/vite/discovery/state.ts +117 -0
- package/src/vite/discovery/virtual-module-codegen.ts +203 -0
- package/src/vite/index.ts +15 -2063
- package/src/vite/plugin-types.ts +103 -0
- package/src/vite/plugins/cjs-to-esm.ts +98 -0
- package/src/vite/plugins/client-ref-dedup.ts +131 -0
- package/src/vite/plugins/client-ref-hashing.ts +117 -0
- 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/{expose-action-id.ts → plugins/expose-action-id.ts} +107 -64
- package/src/vite/plugins/expose-id-utils.ts +299 -0
- package/src/vite/plugins/expose-ids/export-analysis.ts +296 -0
- package/src/vite/plugins/expose-ids/handler-transform.ts +209 -0
- package/src/vite/plugins/expose-ids/loader-transform.ts +74 -0
- package/src/vite/plugins/expose-ids/router-transform.ts +127 -0
- package/src/vite/plugins/expose-ids/types.ts +45 -0
- package/src/vite/plugins/expose-internal-ids.ts +816 -0
- package/src/vite/plugins/performance-tracks.ts +96 -0
- package/src/vite/plugins/refresh-cmd.ts +127 -0
- package/src/vite/plugins/use-cache-transform.ts +336 -0
- package/src/vite/plugins/version-injector.ts +109 -0
- package/src/vite/plugins/version-plugin.ts +266 -0
- package/src/vite/{virtual-entries.ts → plugins/virtual-entries.ts} +23 -14
- package/src/vite/plugins/virtual-stub-plugin.ts +29 -0
- package/src/vite/rango.ts +497 -0
- package/src/vite/router-discovery.ts +1423 -0
- package/src/vite/utils/ast-handler-extract.ts +517 -0
- package/src/vite/utils/banner.ts +36 -0
- package/src/vite/utils/bundle-analysis.ts +137 -0
- package/src/vite/utils/manifest-utils.ts +70 -0
- package/src/vite/utils/package-resolution.ts +161 -0
- package/src/vite/utils/prerender-utils.ts +222 -0
- package/src/vite/utils/shared-utils.ts +170 -0
- package/CLAUDE.md +0 -43
- package/src/browser/lru-cache.ts +0 -69
- package/src/browser/request-controller.ts +0 -164
- package/src/cache/memory-store.ts +0 -253
- package/src/href-context.ts +0 -33
- package/src/router.gen.ts +0 -6
- package/src/urls.gen.ts +0 -8
- package/src/vite/expose-handle-id.ts +0 -209
- package/src/vite/expose-loader-id.ts +0 -426
- package/src/vite/expose-location-state-id.ts +0 -177
- package/src/vite/expose-prerender-handler-id.ts +0 -429
- package/src/vite/package-resolution.ts +0 -125
- /package/src/vite/{version.d.ts → plugins/version.d.ts} +0 -0
|
@@ -1,442 +1,48 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
NavigationBridge,
|
|
3
3
|
NavigationBridgeConfig,
|
|
4
|
-
|
|
5
|
-
NavigationStore,
|
|
4
|
+
NavigateOptionsInternal,
|
|
6
5
|
ResolvedSegment,
|
|
7
6
|
} from "./types.js";
|
|
7
|
+
import { setAppVersion } from "./app-version.js";
|
|
8
|
+
import { setRangoStateLocal } from "./rango-state.js";
|
|
9
|
+
import type { AppShell, AppShellRef } from "./app-shell.js";
|
|
10
|
+
import * as React from "react";
|
|
11
|
+
import { startTransition } from "react";
|
|
8
12
|
import {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
} from "./
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
): state is Record<string, unknown> {
|
|
19
|
-
if (state === null || typeof state !== "object") return false;
|
|
20
|
-
return Object.keys(state).some((key) => key.startsWith("__rsc_ls_"));
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Resolve navigation state - handles both LocationStateEntry[] and legacy formats
|
|
25
|
-
*/
|
|
26
|
-
function resolveNavigationState(state: unknown): unknown {
|
|
27
|
-
// Check if it's an array of LocationStateEntry
|
|
28
|
-
if (
|
|
29
|
-
Array.isArray(state) &&
|
|
30
|
-
state.length > 0 &&
|
|
31
|
-
isLocationStateEntry(state[0])
|
|
32
|
-
) {
|
|
33
|
-
return resolveLocationStateEntries(state);
|
|
34
|
-
}
|
|
35
|
-
// Return as-is for legacy formats
|
|
36
|
-
return state;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Build history state object from user state
|
|
41
|
-
* - Typed state: spread directly into history.state
|
|
42
|
-
* - Legacy state: store in history.state.state
|
|
43
|
-
*/
|
|
44
|
-
function buildHistoryState(
|
|
45
|
-
userState: unknown,
|
|
46
|
-
routerState?: { intercept?: boolean; sourceUrl?: string }
|
|
47
|
-
): Record<string, unknown> | null {
|
|
48
|
-
const result: Record<string, unknown> = {};
|
|
49
|
-
|
|
50
|
-
// Add router internal state
|
|
51
|
-
if (routerState?.intercept) {
|
|
52
|
-
result.intercept = true;
|
|
53
|
-
if (routerState.sourceUrl) {
|
|
54
|
-
result.sourceUrl = routerState.sourceUrl;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
13
|
+
createNavigationTransaction,
|
|
14
|
+
resolveNavigationState,
|
|
15
|
+
} from "./navigation-transaction.js";
|
|
16
|
+
import { buildHistoryState } from "./history-state.js";
|
|
17
|
+
import {
|
|
18
|
+
handleNavigationStart,
|
|
19
|
+
handleNavigationEnd,
|
|
20
|
+
ensureHistoryKey,
|
|
21
|
+
} from "./scroll-restoration.js";
|
|
57
22
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
// Typed state: spread directly
|
|
62
|
-
Object.assign(result, userState);
|
|
63
|
-
} else {
|
|
64
|
-
// Legacy state: store in .state
|
|
65
|
-
result.state = userState;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
23
|
+
// addTransitionType is only available in React experimental
|
|
24
|
+
const addTransitionType: ((type: string) => void) | undefined =
|
|
25
|
+
"addTransitionType" in React ? (React as any).addTransitionType : undefined;
|
|
68
26
|
|
|
69
|
-
return Object.keys(result).length > 0 ? result : null;
|
|
70
|
-
}
|
|
71
27
|
import { setupLinkInterception } from "./link-interceptor.js";
|
|
72
28
|
import { createPartialUpdater } from "./partial-update.js";
|
|
73
29
|
import { generateHistoryKey } from "./navigation-store.js";
|
|
30
|
+
import type { EventController } from "./event-controller.js";
|
|
31
|
+
import { isInterceptOnlyCache } from "./intercept-utils.js";
|
|
74
32
|
import {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
} from "./
|
|
79
|
-
import
|
|
80
|
-
import {
|
|
81
|
-
import {
|
|
82
|
-
import { createElement, startTransition } from "react";
|
|
33
|
+
toNetworkError,
|
|
34
|
+
emitNetworkError,
|
|
35
|
+
isBackgroundSuppressible,
|
|
36
|
+
} from "./network-error-handler.js";
|
|
37
|
+
import { debugLog } from "./logging.js";
|
|
38
|
+
import { ServerRedirect } from "../errors.js";
|
|
39
|
+
import { validateRedirectOrigin } from "./validate-redirect-origin.js";
|
|
83
40
|
|
|
84
41
|
// Polyfill Symbol.dispose for Safari and older browsers
|
|
85
42
|
if (typeof Symbol.dispose === "undefined") {
|
|
86
43
|
(Symbol as any).dispose = Symbol("Symbol.dispose");
|
|
87
44
|
}
|
|
88
45
|
|
|
89
|
-
/**
|
|
90
|
-
* Check if a segment is an intercept segment
|
|
91
|
-
* Intercept segments have namespace starting with "intercept:" or ID containing .@
|
|
92
|
-
*/
|
|
93
|
-
function isInterceptSegment(s: ResolvedSegment): boolean {
|
|
94
|
-
return (
|
|
95
|
-
s.namespace?.startsWith("intercept:") ||
|
|
96
|
-
(s.type === "parallel" && s.id.includes(".@"))
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Check if cached segments are intercept-only (no main route segments)
|
|
102
|
-
* Intercept responses shouldn't be used for optimistic rendering since
|
|
103
|
-
* whether interception happens depends on the current page context
|
|
104
|
-
*/
|
|
105
|
-
function isInterceptOnlyCache(segments: ResolvedSegment[]): boolean {
|
|
106
|
-
return segments.some(isInterceptSegment);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* Options for committing a navigation transaction
|
|
111
|
-
*/
|
|
112
|
-
interface CommitOptions {
|
|
113
|
-
url: string;
|
|
114
|
-
segmentIds: string[];
|
|
115
|
-
segments: ResolvedSegment[];
|
|
116
|
-
replace?: boolean;
|
|
117
|
-
scroll?: boolean;
|
|
118
|
-
/** User-provided state to store in history.state */
|
|
119
|
-
state?: unknown;
|
|
120
|
-
/** If true, only update store without changing URL/history (for server actions) */
|
|
121
|
-
storeOnly?: boolean;
|
|
122
|
-
/** If true, this is an intercept route - store in history.state for popstate handling */
|
|
123
|
-
intercept?: boolean;
|
|
124
|
-
/** Source URL where the intercept was triggered from (stored in history.state) */
|
|
125
|
-
interceptSourceUrl?: string;
|
|
126
|
-
/** If true, only update cache without touching store or history (for background stale revalidation) */
|
|
127
|
-
cacheOnly?: boolean;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Options that can override the pre-configured commit settings
|
|
132
|
-
*/
|
|
133
|
-
interface BoundCommitOverrides {
|
|
134
|
-
/** Override scroll behavior (e.g., disable for intercepts) */
|
|
135
|
-
scroll?: boolean;
|
|
136
|
-
/** Override replace behavior (e.g., force replace for intercepts) */
|
|
137
|
-
replace?: boolean;
|
|
138
|
-
/** Override user-provided state */
|
|
139
|
-
state?: unknown;
|
|
140
|
-
/** Mark this as an intercept route */
|
|
141
|
-
intercept?: boolean;
|
|
142
|
-
/** Source URL where intercept was triggered from */
|
|
143
|
-
interceptSourceUrl?: string;
|
|
144
|
-
/** If true, only update cache (for stale revalidation) */
|
|
145
|
-
cacheOnly?: boolean;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* Token for tracking an active stream - call end() when stream completes
|
|
150
|
-
*/
|
|
151
|
-
export interface StreamingToken {
|
|
152
|
-
end(): void;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Bound transaction with pre-configured commit options (without segmentIds/segments)
|
|
157
|
-
*/
|
|
158
|
-
export interface BoundTransaction {
|
|
159
|
-
readonly currentUrl: string;
|
|
160
|
-
/** Start streaming and get a token to end it when the stream completes */
|
|
161
|
-
startStreaming(): StreamingToken;
|
|
162
|
-
commit(
|
|
163
|
-
segmentIds: string[],
|
|
164
|
-
segments: ResolvedSegment[],
|
|
165
|
-
overrides?: BoundCommitOverrides
|
|
166
|
-
): void;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* Navigation transaction for managing state during navigation
|
|
171
|
-
* Uses the event controller handle for lifecycle management
|
|
172
|
-
*/
|
|
173
|
-
interface NavigationTransaction extends Disposable {
|
|
174
|
-
/** Optimistically commit from cache - instant render before revalidation */
|
|
175
|
-
optimisticCommit(options: CommitOptions): void;
|
|
176
|
-
/** Final commit with server data (or reconciliation after optimistic) */
|
|
177
|
-
commit(options: CommitOptions): void;
|
|
178
|
-
with(
|
|
179
|
-
options: Omit<CommitOptions, "segmentIds" | "segments">
|
|
180
|
-
): BoundTransaction;
|
|
181
|
-
/** The navigation handle from the event controller */
|
|
182
|
-
handle: NavigationHandle;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
/**
|
|
186
|
-
* Creates a navigation transaction that coordinates with the event controller.
|
|
187
|
-
* Handles loading state transitions and cleanup on completion/abort.
|
|
188
|
-
*
|
|
189
|
-
* Supports optimistic navigation: render from cache immediately,
|
|
190
|
-
* then revalidate in background and reconcile if data changed.
|
|
191
|
-
*/
|
|
192
|
-
function createNavigationTransaction(
|
|
193
|
-
store: NavigationStore,
|
|
194
|
-
eventController: EventController,
|
|
195
|
-
url: string,
|
|
196
|
-
options?: NavigateOptions & { skipLoadingState?: boolean }
|
|
197
|
-
): NavigationTransaction {
|
|
198
|
-
let committed = false;
|
|
199
|
-
let optimisticallyCommitted = false;
|
|
200
|
-
let earlyStatePushed = false;
|
|
201
|
-
const currentUrl = window.location.href;
|
|
202
|
-
|
|
203
|
-
// Start navigation in event controller (this sets loading state)
|
|
204
|
-
const handle = eventController.startNavigation(url, options);
|
|
205
|
-
|
|
206
|
-
// If state is provided, push it to history immediately so loading UI can access it
|
|
207
|
-
// This enables "optimistic state" - showing product names in skeletons etc.
|
|
208
|
-
if (options?.state !== undefined && !options?.replace) {
|
|
209
|
-
const earlyHistoryState = buildHistoryState(options.state);
|
|
210
|
-
window.history.pushState(earlyHistoryState, "", url);
|
|
211
|
-
earlyStatePushed = true;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
/**
|
|
215
|
-
* Optimistically commit from cache - renders immediately before revalidation
|
|
216
|
-
* Sets optimisticallyCommitted flag so final commit() knows to reconcile
|
|
217
|
-
*/
|
|
218
|
-
function optimisticCommit(opts: CommitOptions): void {
|
|
219
|
-
optimisticallyCommitted = true;
|
|
220
|
-
|
|
221
|
-
const { url, segmentIds, segments, replace, scroll } = opts;
|
|
222
|
-
const parsedUrl = new URL(url, window.location.origin);
|
|
223
|
-
|
|
224
|
-
// Save current scroll position before navigating
|
|
225
|
-
handleNavigationStart();
|
|
226
|
-
|
|
227
|
-
// Update segment state
|
|
228
|
-
store.setSegmentIds(segmentIds);
|
|
229
|
-
store.setCurrentUrl(url);
|
|
230
|
-
store.setPath(parsedUrl.pathname);
|
|
231
|
-
|
|
232
|
-
// Generate history key from URL
|
|
233
|
-
const historyKey = generateHistoryKey(url);
|
|
234
|
-
store.setHistoryKey(historyKey);
|
|
235
|
-
|
|
236
|
-
// Cache segments with current handleData (will be overwritten by fresh data on final commit)
|
|
237
|
-
const currentHandleData = eventController.getHandleState().data;
|
|
238
|
-
store.cacheSegmentsForHistory(historyKey, segments, currentHandleData);
|
|
239
|
-
|
|
240
|
-
// Build history state with user state if provided
|
|
241
|
-
const historyState = buildHistoryState(opts.state);
|
|
242
|
-
|
|
243
|
-
// Update browser URL
|
|
244
|
-
// Use replaceState if we already pushed early (for optimistic state access)
|
|
245
|
-
if (replace || earlyStatePushed) {
|
|
246
|
-
window.history.replaceState(historyState, "", url);
|
|
247
|
-
} else {
|
|
248
|
-
window.history.pushState(historyState, "", url);
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
// Ensure new history entry has a scroll restoration key
|
|
252
|
-
ensureHistoryKey();
|
|
253
|
-
|
|
254
|
-
// Complete the navigation in event controller (sets idle state)
|
|
255
|
-
handle.complete(parsedUrl);
|
|
256
|
-
|
|
257
|
-
// Handle scroll after navigation
|
|
258
|
-
handleNavigationEnd({ scroll });
|
|
259
|
-
|
|
260
|
-
console.log(
|
|
261
|
-
"[Browser] Optimistic commit from cache, historyKey:",
|
|
262
|
-
historyKey
|
|
263
|
-
);
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
/**
|
|
267
|
-
* Commit the navigation - updates store and URL atomically
|
|
268
|
-
* If optimisticCommit was called, this becomes a reconciliation
|
|
269
|
-
*/
|
|
270
|
-
function commit(opts: CommitOptions): void {
|
|
271
|
-
committed = true;
|
|
272
|
-
|
|
273
|
-
// If optimistic commit already done, adjust options for reconciliation
|
|
274
|
-
const isReconciliation = optimisticallyCommitted;
|
|
275
|
-
const {
|
|
276
|
-
url,
|
|
277
|
-
segmentIds,
|
|
278
|
-
segments,
|
|
279
|
-
storeOnly,
|
|
280
|
-
intercept,
|
|
281
|
-
interceptSourceUrl,
|
|
282
|
-
cacheOnly,
|
|
283
|
-
} = opts;
|
|
284
|
-
// For reconciliation: always replace (URL already pushed), no scroll
|
|
285
|
-
const replace = isReconciliation ? true : opts.replace;
|
|
286
|
-
const scroll = isReconciliation ? false : opts.scroll;
|
|
287
|
-
|
|
288
|
-
const parsedUrl = new URL(url, window.location.origin);
|
|
289
|
-
|
|
290
|
-
// Generate history key from URL (with intercept suffix for separate caching)
|
|
291
|
-
const historyKey = generateHistoryKey(url, { intercept });
|
|
292
|
-
|
|
293
|
-
// For cache-only commits (stale revalidation), only update cache and return
|
|
294
|
-
// Don't touch store state or history - user may have navigated elsewhere
|
|
295
|
-
if (cacheOnly) {
|
|
296
|
-
const currentHandleData = eventController.getHandleState().data;
|
|
297
|
-
store.cacheSegmentsForHistory(historyKey, segments, currentHandleData);
|
|
298
|
-
console.log("[Browser] Cache-only commit, historyKey:", historyKey);
|
|
299
|
-
return;
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
// Save current scroll position before navigating (only for non-reconciliation)
|
|
303
|
-
if (!isReconciliation) {
|
|
304
|
-
handleNavigationStart();
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
// Update segment state atomically
|
|
308
|
-
store.setSegmentIds(segmentIds);
|
|
309
|
-
store.setCurrentUrl(url);
|
|
310
|
-
store.setPath(parsedUrl.pathname);
|
|
311
|
-
|
|
312
|
-
store.setHistoryKey(historyKey);
|
|
313
|
-
|
|
314
|
-
// Cache segments with current handleData for this history entry (fresh data overwrites optimistic)
|
|
315
|
-
const currentHandleData = eventController.getHandleState().data;
|
|
316
|
-
store.cacheSegmentsForHistory(historyKey, segments, currentHandleData);
|
|
317
|
-
|
|
318
|
-
// For server actions, skip URL/history updates but still complete navigation
|
|
319
|
-
if (storeOnly) {
|
|
320
|
-
console.log("[Browser] Store updated (action)");
|
|
321
|
-
// Complete navigation to clear loading state
|
|
322
|
-
handle.complete(parsedUrl);
|
|
323
|
-
return;
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
// Build history state - include user state and intercept info for popstate handling
|
|
327
|
-
const historyState = buildHistoryState(opts.state, {
|
|
328
|
-
intercept,
|
|
329
|
-
sourceUrl: interceptSourceUrl,
|
|
330
|
-
});
|
|
331
|
-
|
|
332
|
-
// Update browser URL (skip if reconciliation - already done in optimisticCommit)
|
|
333
|
-
if (!isReconciliation) {
|
|
334
|
-
// Use replaceState if we already pushed early (for optimistic state access) or replace requested
|
|
335
|
-
if (replace || earlyStatePushed) {
|
|
336
|
-
window.history.replaceState(historyState, "", url);
|
|
337
|
-
} else {
|
|
338
|
-
window.history.pushState(historyState, "", url);
|
|
339
|
-
}
|
|
340
|
-
// Ensure new history entry has a scroll restoration key
|
|
341
|
-
ensureHistoryKey();
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
// Complete the navigation in event controller (sets idle state, updates location)
|
|
345
|
-
handle.complete(parsedUrl);
|
|
346
|
-
|
|
347
|
-
// Handle scroll after navigation (skip if reconciliation)
|
|
348
|
-
if (!isReconciliation) {
|
|
349
|
-
handleNavigationEnd({ scroll });
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
if (isReconciliation) {
|
|
353
|
-
console.log("[Browser] Reconciliation commit, historyKey:", historyKey);
|
|
354
|
-
} else {
|
|
355
|
-
console.log(
|
|
356
|
-
"[Browser] Navigation committed, historyKey:",
|
|
357
|
-
historyKey,
|
|
358
|
-
intercept ? "(intercept)" : ""
|
|
359
|
-
);
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
return {
|
|
364
|
-
handle,
|
|
365
|
-
optimisticCommit,
|
|
366
|
-
commit,
|
|
367
|
-
|
|
368
|
-
/**
|
|
369
|
-
* Create a bound transaction with pre-configured URL options
|
|
370
|
-
* segmentIds and segments provided at commit time (after they're resolved)
|
|
371
|
-
*/
|
|
372
|
-
with(
|
|
373
|
-
opts: Omit<CommitOptions, "segmentIds" | "segments">
|
|
374
|
-
): BoundTransaction {
|
|
375
|
-
return {
|
|
376
|
-
get currentUrl() {
|
|
377
|
-
return currentUrl;
|
|
378
|
-
},
|
|
379
|
-
startStreaming() {
|
|
380
|
-
return handle.startStreaming();
|
|
381
|
-
},
|
|
382
|
-
commit: (
|
|
383
|
-
segmentIds: string[],
|
|
384
|
-
segments: ResolvedSegment[],
|
|
385
|
-
overrides?: BoundCommitOverrides
|
|
386
|
-
) => {
|
|
387
|
-
// Allow overrides to disable scroll (e.g., for intercepts)
|
|
388
|
-
const finalScroll =
|
|
389
|
-
overrides?.scroll !== undefined ? overrides.scroll : opts.scroll;
|
|
390
|
-
// Allow overrides to force replace (e.g., for intercepts)
|
|
391
|
-
const finalReplace =
|
|
392
|
-
overrides?.replace !== undefined ? overrides.replace : opts.replace;
|
|
393
|
-
// Intercept info: overrides take precedence, fallback to opts
|
|
394
|
-
const intercept =
|
|
395
|
-
overrides?.intercept !== undefined
|
|
396
|
-
? overrides.intercept
|
|
397
|
-
: opts.intercept;
|
|
398
|
-
const interceptSourceUrl =
|
|
399
|
-
overrides?.interceptSourceUrl !== undefined
|
|
400
|
-
? overrides.interceptSourceUrl
|
|
401
|
-
: opts.interceptSourceUrl;
|
|
402
|
-
// Cache-only mode: overrides take precedence, fallback to opts
|
|
403
|
-
const cacheOnly =
|
|
404
|
-
overrides?.cacheOnly !== undefined
|
|
405
|
-
? overrides.cacheOnly
|
|
406
|
-
: opts.cacheOnly;
|
|
407
|
-
// User state: overrides take precedence, fallback to opts
|
|
408
|
-
const state =
|
|
409
|
-
overrides?.state !== undefined ? overrides.state : opts.state;
|
|
410
|
-
commit({
|
|
411
|
-
...opts,
|
|
412
|
-
segmentIds,
|
|
413
|
-
segments,
|
|
414
|
-
scroll: finalScroll,
|
|
415
|
-
replace: finalReplace,
|
|
416
|
-
state,
|
|
417
|
-
intercept,
|
|
418
|
-
interceptSourceUrl,
|
|
419
|
-
cacheOnly,
|
|
420
|
-
});
|
|
421
|
-
},
|
|
422
|
-
};
|
|
423
|
-
},
|
|
424
|
-
|
|
425
|
-
[Symbol.dispose]() {
|
|
426
|
-
// If aborted, another navigation took over - don't touch state
|
|
427
|
-
if (handle.signal.aborted) return;
|
|
428
|
-
|
|
429
|
-
// If not committed (and not optimistically committed), the handle's dispose
|
|
430
|
-
// will reset state to idle via the event controller
|
|
431
|
-
if (!committed && !optimisticallyCommitted) {
|
|
432
|
-
handle[Symbol.dispose]();
|
|
433
|
-
// The NavigationHandle's [Symbol.dispose] handles this
|
|
434
|
-
}
|
|
435
|
-
},
|
|
436
|
-
};
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
// Export for use by server-action-bridge
|
|
440
46
|
export { createNavigationTransaction };
|
|
441
47
|
|
|
442
48
|
/**
|
|
@@ -444,8 +50,13 @@ export { createNavigationTransaction };
|
|
|
444
50
|
*/
|
|
445
51
|
export interface NavigationBridgeConfigWithController extends NavigationBridgeConfig {
|
|
446
52
|
eventController: EventController;
|
|
447
|
-
/** RSC version from initial payload metadata */
|
|
53
|
+
/** RSC version from initial payload metadata (fallback when appShellRef is not provided) */
|
|
448
54
|
version?: string;
|
|
55
|
+
/**
|
|
56
|
+
* Live app-shell ref. When supplied, the bridge reads version/basename
|
|
57
|
+
* from this ref so cross-app navigations propagate correctly.
|
|
58
|
+
*/
|
|
59
|
+
appShellRef?: AppShellRef;
|
|
449
60
|
}
|
|
450
61
|
|
|
451
62
|
/**
|
|
@@ -462,9 +73,47 @@ export interface NavigationBridgeConfigWithController extends NavigationBridgeCo
|
|
|
462
73
|
* @returns NavigationBridge instance
|
|
463
74
|
*/
|
|
464
75
|
export function createNavigationBridge(
|
|
465
|
-
config: NavigationBridgeConfigWithController
|
|
76
|
+
config: NavigationBridgeConfigWithController,
|
|
466
77
|
): NavigationBridge {
|
|
467
|
-
const {
|
|
78
|
+
const {
|
|
79
|
+
store,
|
|
80
|
+
client,
|
|
81
|
+
eventController,
|
|
82
|
+
onUpdate,
|
|
83
|
+
renderSegments,
|
|
84
|
+
appShellRef,
|
|
85
|
+
} = config;
|
|
86
|
+
let version = config.version;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Replace the active app-shell snapshot atomically. Called by the partial
|
|
90
|
+
* updater when a response's routerId indicates the navigation crossed
|
|
91
|
+
* into a different app. Runs the local-only side-effects tied to
|
|
92
|
+
* app-shell fields (app version, rango-state namespace) so the new app
|
|
93
|
+
* owns them after the swap. Theme, warmup, and prefetch TTL are
|
|
94
|
+
* document-lifetime and are NOT touched here.
|
|
95
|
+
*/
|
|
96
|
+
function applyAppShell(next: AppShell): void {
|
|
97
|
+
if (appShellRef) {
|
|
98
|
+
appShellRef.update(next);
|
|
99
|
+
}
|
|
100
|
+
if (next.version !== undefined) {
|
|
101
|
+
version = next.version;
|
|
102
|
+
setAppVersion(next.version);
|
|
103
|
+
// Use the local-only setter — initRangoState writes the shared
|
|
104
|
+
// localStorage key and fires a storage event in other tabs still in
|
|
105
|
+
// the old app. setRangoStateLocal only mutates this tab's in-memory
|
|
106
|
+
// cache and rebinds it to the target app's routerId-scoped key,
|
|
107
|
+
// preserving the "local-only, no broadcast/rotation" contract for
|
|
108
|
+
// smooth app-switch transitions.
|
|
109
|
+
setRangoStateLocal(next.version, next.routerId);
|
|
110
|
+
}
|
|
111
|
+
// Cross-app: prior cache entries belong to a different app's segments.
|
|
112
|
+
// Drop them locally only — do NOT broadcast invalidation or rotate the
|
|
113
|
+
// shared X-Rango-State token, since other tabs still in the old app are
|
|
114
|
+
// unaffected by this tab's transition.
|
|
115
|
+
store.clearHistoryCacheLocal();
|
|
116
|
+
}
|
|
468
117
|
|
|
469
118
|
// Create shared partial updater
|
|
470
119
|
const fetchPartialUpdate = createPartialUpdater({
|
|
@@ -472,25 +121,128 @@ export function createNavigationBridge(
|
|
|
472
121
|
client,
|
|
473
122
|
onUpdate,
|
|
474
123
|
renderSegments,
|
|
475
|
-
version,
|
|
124
|
+
getVersion: () => version,
|
|
125
|
+
applyAppShell,
|
|
476
126
|
});
|
|
477
127
|
|
|
478
128
|
return {
|
|
479
129
|
/**
|
|
480
130
|
* Navigate to a URL
|
|
481
|
-
* Uses
|
|
131
|
+
* Uses cached segments for SWR revalidation when available
|
|
482
132
|
*/
|
|
483
|
-
async navigate(
|
|
133
|
+
async navigate(
|
|
134
|
+
url: string,
|
|
135
|
+
options?: NavigateOptionsInternal,
|
|
136
|
+
): Promise<void> {
|
|
484
137
|
// Resolve LocationStateEntry[] to flat object if needed
|
|
485
138
|
const resolvedState =
|
|
486
139
|
options?.state !== undefined
|
|
487
140
|
? resolveNavigationState(options.state)
|
|
488
141
|
: undefined;
|
|
489
142
|
|
|
143
|
+
// Cross-origin URLs are not handled by SPA navigation.
|
|
144
|
+
// Fall back to a full browser navigation for http/https only.
|
|
145
|
+
let targetUrl: URL;
|
|
146
|
+
try {
|
|
147
|
+
targetUrl = new URL(url, window.location.origin);
|
|
148
|
+
} catch {
|
|
149
|
+
console.warn(`[rango] navigate() ignored: malformed URL "${url}"`);
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
if (targetUrl.origin !== window.location.origin) {
|
|
153
|
+
if (targetUrl.protocol !== "http:" && targetUrl.protocol !== "https:") {
|
|
154
|
+
console.error(
|
|
155
|
+
`[rango] navigate() blocked: unsupported scheme "${targetUrl.protocol}"`,
|
|
156
|
+
);
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
window.location.href = targetUrl.href;
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Shallow navigation: skip RSC fetch when revalidate is false
|
|
164
|
+
// and the pathname hasn't changed (search param / hash only change).
|
|
165
|
+
if (
|
|
166
|
+
options?.revalidate === false &&
|
|
167
|
+
targetUrl.pathname === new URL(window.location.href).pathname
|
|
168
|
+
) {
|
|
169
|
+
// Preserve intercept context from the current history entry so that
|
|
170
|
+
// popstate uses the correct cache key (:intercept suffix) and restores
|
|
171
|
+
// the right full-page vs modal semantics.
|
|
172
|
+
const currentHistoryState = window.history.state;
|
|
173
|
+
const isIntercept = currentHistoryState?.intercept === true;
|
|
174
|
+
const interceptSourceUrl = isIntercept
|
|
175
|
+
? currentHistoryState?.sourceUrl
|
|
176
|
+
: undefined;
|
|
177
|
+
|
|
178
|
+
const historyKey = generateHistoryKey(url, { intercept: isIntercept });
|
|
179
|
+
|
|
180
|
+
// Copy current segments to the new history key so back/forward restores instantly
|
|
181
|
+
const currentKey = store.getHistoryKey();
|
|
182
|
+
const currentCache = store.getCachedSegments(currentKey);
|
|
183
|
+
if (currentCache?.segments) {
|
|
184
|
+
const currentHandleData = eventController.getHandleState().data;
|
|
185
|
+
store.cacheSegmentsForHistory(
|
|
186
|
+
historyKey,
|
|
187
|
+
currentCache.segments,
|
|
188
|
+
currentHandleData,
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// Save current scroll position before changing URL
|
|
193
|
+
handleNavigationStart();
|
|
194
|
+
|
|
195
|
+
// Snapshot old state before pushState/replaceState overwrites it
|
|
196
|
+
const oldState = window.history.state;
|
|
197
|
+
|
|
198
|
+
// Update browser URL (carry intercept context into history state)
|
|
199
|
+
const historyState = buildHistoryState(
|
|
200
|
+
resolvedState,
|
|
201
|
+
{
|
|
202
|
+
intercept: isIntercept || undefined,
|
|
203
|
+
sourceUrl: interceptSourceUrl,
|
|
204
|
+
},
|
|
205
|
+
{},
|
|
206
|
+
);
|
|
207
|
+
if (options.replace) {
|
|
208
|
+
window.history.replaceState(historyState, "", url);
|
|
209
|
+
} else {
|
|
210
|
+
window.history.pushState(historyState, "", url);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// Ensure new history entry has a scroll restoration key
|
|
214
|
+
ensureHistoryKey();
|
|
215
|
+
|
|
216
|
+
// Notify useLocationState() hooks when state changes
|
|
217
|
+
const hasOldState =
|
|
218
|
+
oldState &&
|
|
219
|
+
typeof oldState === "object" &&
|
|
220
|
+
("state" in oldState ||
|
|
221
|
+
Object.keys(oldState).some((k) => k.startsWith("__rsc_ls_")));
|
|
222
|
+
const hasNewState =
|
|
223
|
+
historyState &&
|
|
224
|
+
("state" in historyState ||
|
|
225
|
+
Object.keys(historyState).some((k) => k.startsWith("__rsc_ls_")));
|
|
226
|
+
if (hasOldState || hasNewState) {
|
|
227
|
+
window.dispatchEvent(new Event("__rsc_locationstate"));
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// Update store history key so future navigations reference the right cache
|
|
231
|
+
store.setHistoryKey(historyKey);
|
|
232
|
+
store.setCurrentUrl(url);
|
|
233
|
+
|
|
234
|
+
// Notify hooks — location updates, state stays idle
|
|
235
|
+
eventController.setLocation(targetUrl);
|
|
236
|
+
|
|
237
|
+
// Handle post-navigation scroll
|
|
238
|
+
handleNavigationEnd({ scroll: options.scroll });
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
|
|
490
242
|
// Only abort pending requests when navigating to a different route
|
|
491
243
|
// Same-route navigation (e.g., /todos -> /todos) should not cancel in-flight actions
|
|
492
244
|
const currentPath = new URL(window.location.href).pathname;
|
|
493
|
-
const targetPath =
|
|
245
|
+
const targetPath = targetUrl.pathname;
|
|
494
246
|
if (currentPath !== targetPath) {
|
|
495
247
|
eventController.abortNavigation();
|
|
496
248
|
}
|
|
@@ -503,7 +255,9 @@ export function createNavigationBridge(
|
|
|
503
255
|
const isLeavingIntercept = isCurrentlyIntercept && isSamePathNavigation;
|
|
504
256
|
|
|
505
257
|
if (isLeavingIntercept) {
|
|
506
|
-
|
|
258
|
+
debugLog(
|
|
259
|
+
"[Browser] Leaving intercept - same URL navigation from intercept",
|
|
260
|
+
);
|
|
507
261
|
// Clear intercept source URL to ensure server doesn't treat this as intercept
|
|
508
262
|
store.setInterceptSourceUrl(null);
|
|
509
263
|
}
|
|
@@ -517,7 +271,7 @@ export function createNavigationBridge(
|
|
|
517
271
|
store.cacheSegmentsForHistory(
|
|
518
272
|
sourceHistoryKey,
|
|
519
273
|
sourceCached.segments,
|
|
520
|
-
currentHandleData
|
|
274
|
+
currentHandleData,
|
|
521
275
|
);
|
|
522
276
|
}
|
|
523
277
|
|
|
@@ -532,7 +286,7 @@ export function createNavigationBridge(
|
|
|
532
286
|
const cachedHandleData = cached?.handleData;
|
|
533
287
|
if (cachedSegments && sourceCached?.segments) {
|
|
534
288
|
const sourceSegmentMap = new Map(
|
|
535
|
-
sourceCached.segments.map((s) => [s.id, s])
|
|
289
|
+
sourceCached.segments.map((s) => [s.id, s]),
|
|
536
290
|
);
|
|
537
291
|
cachedSegments = cachedSegments.map((targetSeg) => {
|
|
538
292
|
const sourceSeg = sourceSegmentMap.get(targetSeg.id);
|
|
@@ -547,28 +301,40 @@ export function createNavigationBridge(
|
|
|
547
301
|
const interceptHistoryKey = generateHistoryKey(url, { intercept: true });
|
|
548
302
|
const hasInterceptCache = store.hasHistoryCache(interceptHistoryKey);
|
|
549
303
|
|
|
550
|
-
// Skip
|
|
304
|
+
// Skip cached SWR for:
|
|
551
305
|
// 1. intercept caches - interception depends on source page context
|
|
552
306
|
// 2. routes that CAN be intercepted - we don't know if this navigation will intercept
|
|
553
307
|
// 3. when leaving intercept - we need fresh non-intercept segments from server
|
|
308
|
+
// 4. redirect-with-state - force re-render so hooks read fresh state
|
|
309
|
+
// 5. stale cache - server action invalidated it, need fresh data with loading state
|
|
554
310
|
const hasUsableCache =
|
|
555
311
|
cachedSegments &&
|
|
556
312
|
cachedSegments.length > 0 &&
|
|
557
313
|
!isInterceptOnlyCache(cachedSegments) &&
|
|
558
314
|
!hasInterceptCache &&
|
|
559
|
-
!isLeavingIntercept
|
|
560
|
-
|
|
561
|
-
|
|
315
|
+
!isLeavingIntercept &&
|
|
316
|
+
!cached?.stale &&
|
|
317
|
+
!options?._skipCache;
|
|
318
|
+
|
|
319
|
+
// Forward navigations always await fetchPartialUpdate before rendering,
|
|
320
|
+
// so useNavigation should always report "loading". skipLoadingState is
|
|
321
|
+
// only used for popstate background revalidation (line ~526) where
|
|
322
|
+
// cached content renders instantly without a network wait.
|
|
323
|
+
const tx = createNavigationTransaction(store, eventController, url, {
|
|
562
324
|
...options,
|
|
563
325
|
state: resolvedState,
|
|
564
|
-
skipLoadingState:
|
|
326
|
+
skipLoadingState: false,
|
|
565
327
|
});
|
|
566
328
|
|
|
567
329
|
// REVALIDATE: Fetch fresh data from server
|
|
568
330
|
try {
|
|
569
331
|
await fetchPartialUpdate(
|
|
570
332
|
url,
|
|
571
|
-
hasUsableCache
|
|
333
|
+
hasUsableCache
|
|
334
|
+
? cachedSegments!.map((s) => s.id)
|
|
335
|
+
: options?._skipCache
|
|
336
|
+
? [] // Action redirect: send no segments so server renders everything fresh
|
|
337
|
+
: undefined,
|
|
572
338
|
false,
|
|
573
339
|
tx.handle.signal,
|
|
574
340
|
tx.with({
|
|
@@ -577,55 +343,56 @@ export function createNavigationBridge(
|
|
|
577
343
|
scroll: options?.scroll,
|
|
578
344
|
state: resolvedState,
|
|
579
345
|
}),
|
|
580
|
-
// Pass cached segments (merged with current page's fresh segments for shared IDs)
|
|
581
|
-
// so the segment map is consistent with what we tell the server we have.
|
|
582
|
-
// Server decides what needs revalidation based on route matching and custom functions.
|
|
583
|
-
// No need for staleRevalidation flag - we're sending the freshest segments we have.
|
|
584
|
-
// Also pass cached handle data for restoring breadcrumbs when server returns empty diff.
|
|
585
|
-
// When leaving intercept, pass the flag so fetchPartialUpdate knows to filter segments.
|
|
586
346
|
hasUsableCache
|
|
587
|
-
? {
|
|
347
|
+
? {
|
|
348
|
+
type: "navigate" as const,
|
|
349
|
+
targetCacheSegments: cachedSegments,
|
|
350
|
+
targetCacheHandleData: cachedHandleData,
|
|
351
|
+
}
|
|
588
352
|
: isLeavingIntercept
|
|
589
|
-
? {
|
|
590
|
-
: undefined
|
|
353
|
+
? { type: "leave-intercept" as const }
|
|
354
|
+
: undefined,
|
|
591
355
|
);
|
|
592
356
|
} catch (error) {
|
|
593
|
-
//
|
|
357
|
+
// Server-side redirect with location state: the current transaction's
|
|
358
|
+
// cleanup resets loading state. Re-navigate to the redirect
|
|
359
|
+
// target carrying the server-set state into history.pushState.
|
|
360
|
+
if (error instanceof ServerRedirect) {
|
|
361
|
+
const redirectUrl = validateRedirectOrigin(
|
|
362
|
+
error.url,
|
|
363
|
+
window.location.origin,
|
|
364
|
+
);
|
|
365
|
+
if (!redirectUrl) {
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
return this.navigate(redirectUrl, {
|
|
369
|
+
state: error.state,
|
|
370
|
+
replace: options?.replace,
|
|
371
|
+
_skipCache: true,
|
|
372
|
+
} as NavigateOptionsInternal);
|
|
373
|
+
}
|
|
374
|
+
|
|
594
375
|
if (error instanceof DOMException && error.name === "AbortError") {
|
|
595
|
-
|
|
376
|
+
debugLog("[Browser] Navigation aborted by newer navigation");
|
|
596
377
|
return;
|
|
597
378
|
}
|
|
598
379
|
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
: new NetworkError(
|
|
605
|
-
"Unable to connect to server. Please check your connection.",
|
|
606
|
-
{ cause: error, url, operation: "navigation" }
|
|
607
|
-
);
|
|
608
|
-
|
|
380
|
+
const networkError = toNetworkError(error, {
|
|
381
|
+
url,
|
|
382
|
+
operation: "navigation",
|
|
383
|
+
});
|
|
384
|
+
if (networkError) {
|
|
609
385
|
console.error(
|
|
610
386
|
"[Browser] Network error during navigation:",
|
|
611
|
-
networkError
|
|
387
|
+
networkError,
|
|
612
388
|
);
|
|
613
|
-
|
|
614
|
-
// Emit update with NetworkErrorThrower to trigger root error boundary
|
|
615
|
-
startTransition(() => {
|
|
616
|
-
onUpdate({
|
|
617
|
-
root: createElement(NetworkErrorThrower, { error: networkError }),
|
|
618
|
-
metadata: {
|
|
619
|
-
pathname: url,
|
|
620
|
-
segments: [],
|
|
621
|
-
isError: true,
|
|
622
|
-
},
|
|
623
|
-
});
|
|
624
|
-
});
|
|
389
|
+
emitNetworkError(onUpdate, networkError, url);
|
|
625
390
|
return;
|
|
626
391
|
}
|
|
627
392
|
|
|
628
393
|
throw error;
|
|
394
|
+
} finally {
|
|
395
|
+
tx[Symbol.dispose]();
|
|
629
396
|
}
|
|
630
397
|
},
|
|
631
398
|
|
|
@@ -635,11 +402,11 @@ export function createNavigationBridge(
|
|
|
635
402
|
async refresh(): Promise<void> {
|
|
636
403
|
eventController.abortNavigation();
|
|
637
404
|
|
|
638
|
-
|
|
405
|
+
const tx = createNavigationTransaction(
|
|
639
406
|
store,
|
|
640
407
|
eventController,
|
|
641
408
|
window.location.href,
|
|
642
|
-
{ replace: true }
|
|
409
|
+
{ replace: true },
|
|
643
410
|
);
|
|
644
411
|
|
|
645
412
|
try {
|
|
@@ -649,41 +416,24 @@ export function createNavigationBridge(
|
|
|
649
416
|
[],
|
|
650
417
|
false,
|
|
651
418
|
tx.handle.signal,
|
|
652
|
-
tx.with({ url: window.location.href, replace: true, scroll: false })
|
|
419
|
+
tx.with({ url: window.location.href, replace: true, scroll: false }),
|
|
653
420
|
);
|
|
654
421
|
} catch (error) {
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
: new NetworkError(
|
|
661
|
-
"Unable to connect to server. Please check your connection.",
|
|
662
|
-
{
|
|
663
|
-
cause: error,
|
|
664
|
-
url: window.location.href,
|
|
665
|
-
operation: "revalidation",
|
|
666
|
-
}
|
|
667
|
-
);
|
|
668
|
-
|
|
422
|
+
const networkError = toNetworkError(error, {
|
|
423
|
+
url: window.location.href,
|
|
424
|
+
operation: "revalidation",
|
|
425
|
+
});
|
|
426
|
+
if (networkError) {
|
|
669
427
|
console.error(
|
|
670
428
|
"[Browser] Network error during refresh:",
|
|
671
|
-
networkError
|
|
429
|
+
networkError,
|
|
672
430
|
);
|
|
673
|
-
|
|
674
|
-
startTransition(() => {
|
|
675
|
-
onUpdate({
|
|
676
|
-
root: createElement(NetworkErrorThrower, { error: networkError }),
|
|
677
|
-
metadata: {
|
|
678
|
-
pathname: window.location.href,
|
|
679
|
-
segments: [],
|
|
680
|
-
isError: true,
|
|
681
|
-
},
|
|
682
|
-
});
|
|
683
|
-
});
|
|
431
|
+
emitNetworkError(onUpdate, networkError, window.location.href);
|
|
684
432
|
return;
|
|
685
433
|
}
|
|
686
434
|
throw error;
|
|
435
|
+
} finally {
|
|
436
|
+
tx[Symbol.dispose]();
|
|
687
437
|
}
|
|
688
438
|
},
|
|
689
439
|
|
|
@@ -707,20 +457,29 @@ export function createNavigationBridge(
|
|
|
707
457
|
const currentInterceptSource = store.getInterceptSourceUrl();
|
|
708
458
|
const newInterceptSource = interceptSourceUrl ?? null;
|
|
709
459
|
if (currentInterceptSource !== newInterceptSource) {
|
|
710
|
-
|
|
711
|
-
`[Browser] Intercept context changing (${currentInterceptSource} -> ${newInterceptSource}), aborting in-flight actions
|
|
460
|
+
debugLog(
|
|
461
|
+
`[Browser] Intercept context changing (${currentInterceptSource} -> ${newInterceptSource}), aborting in-flight actions`,
|
|
712
462
|
);
|
|
713
463
|
eventController.abortAllActions();
|
|
714
464
|
}
|
|
715
465
|
|
|
466
|
+
// Popstate that exits an intercept to a non-intercept destination. The
|
|
467
|
+
// fallback fetch path below needs `leave-intercept` mode so it filters
|
|
468
|
+
// the cached @modal segment from the request and forces a re-render —
|
|
469
|
+
// otherwise a cache-miss popstate whose server response has an empty
|
|
470
|
+
// diff hits the "no changes" branch in partial-update and the modal
|
|
471
|
+
// stays on screen.
|
|
472
|
+
const isLeavingIntercept =
|
|
473
|
+
!isIntercept && currentInterceptSource !== null;
|
|
474
|
+
|
|
716
475
|
// Compute history key from URL (with intercept suffix if applicable)
|
|
717
476
|
const historyKey = generateHistoryKey(url, { intercept: isIntercept });
|
|
718
477
|
|
|
719
|
-
|
|
478
|
+
debugLog(
|
|
720
479
|
"[Browser] Popstate -",
|
|
721
480
|
isIntercept ? "intercept" : "normal",
|
|
722
481
|
"key:",
|
|
723
|
-
historyKey
|
|
482
|
+
historyKey,
|
|
724
483
|
);
|
|
725
484
|
|
|
726
485
|
// Update location in event controller
|
|
@@ -749,12 +508,27 @@ export function createNavigationBridge(
|
|
|
749
508
|
store.setCurrentUrl(url);
|
|
750
509
|
store.setPath(new URL(url).pathname);
|
|
751
510
|
|
|
511
|
+
// Restore router identity from cache so subsequent navigations
|
|
512
|
+
// don't falsely detect an app switch.
|
|
513
|
+
if (cached?.routerId) {
|
|
514
|
+
store.setRouterId?.(cached.routerId);
|
|
515
|
+
}
|
|
516
|
+
|
|
752
517
|
// Render from cache - force await to skip loading fallbacks
|
|
753
518
|
try {
|
|
754
|
-
const root = renderSegments(cachedSegments, {
|
|
519
|
+
const root = await renderSegments(cachedSegments, {
|
|
755
520
|
forceAwait: true,
|
|
756
521
|
});
|
|
757
|
-
|
|
522
|
+
// Merge params from cached segments for useParams restoration.
|
|
523
|
+
// Set params on event controller before onUpdate so both location
|
|
524
|
+
// and params are current when the debounced notify() fires.
|
|
525
|
+
const cachedParams: Record<string, string> = {};
|
|
526
|
+
for (const s of cachedSegments) {
|
|
527
|
+
if (s.params) Object.assign(cachedParams, s.params);
|
|
528
|
+
}
|
|
529
|
+
eventController.setParams(cachedParams);
|
|
530
|
+
|
|
531
|
+
const popstateUpdate = {
|
|
758
532
|
root,
|
|
759
533
|
metadata: {
|
|
760
534
|
pathname: new URL(url).pathname,
|
|
@@ -763,23 +537,40 @@ export function createNavigationBridge(
|
|
|
763
537
|
matched: cachedSegments.map((s) => s.id),
|
|
764
538
|
diff: [],
|
|
765
539
|
cachedHandleData,
|
|
540
|
+
params: cachedParams,
|
|
766
541
|
},
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
//
|
|
770
|
-
|
|
542
|
+
scroll: { restore: true, isStreaming },
|
|
543
|
+
};
|
|
544
|
+
// Intercept-driven popstate (entering OR leaving an intercept) only
|
|
545
|
+
// mutates the parallel slot; the main outlet shows the same content.
|
|
546
|
+
// Skip startViewTransition in those cases — same rationale as the
|
|
547
|
+
// intercept guard in partial-update.ts's hasTransition computation.
|
|
548
|
+
const hasTransition =
|
|
549
|
+
!isIntercept &&
|
|
550
|
+
!isLeavingIntercept &&
|
|
551
|
+
cachedSegments.some((s) => s.transition);
|
|
552
|
+
if (hasTransition) {
|
|
553
|
+
startTransition(() => {
|
|
554
|
+
if (addTransitionType) {
|
|
555
|
+
addTransitionType("navigation-back");
|
|
556
|
+
}
|
|
557
|
+
onUpdate(popstateUpdate);
|
|
558
|
+
});
|
|
559
|
+
} else {
|
|
560
|
+
onUpdate(popstateUpdate);
|
|
561
|
+
}
|
|
771
562
|
|
|
772
563
|
// SWR: If stale, trigger background revalidation
|
|
773
564
|
if (isStale) {
|
|
774
|
-
|
|
565
|
+
debugLog("[Browser] Cache is stale, background revalidating...");
|
|
775
566
|
// Background revalidation - don't await, just fire and forget
|
|
776
567
|
const segmentIds = cachedSegments.map((s) => s.id);
|
|
777
568
|
|
|
778
|
-
|
|
569
|
+
const tx = createNavigationTransaction(
|
|
779
570
|
store,
|
|
780
571
|
eventController,
|
|
781
572
|
url,
|
|
782
|
-
{ skipLoadingState: true, replace: true }
|
|
573
|
+
{ skipLoadingState: true, replace: true },
|
|
783
574
|
);
|
|
784
575
|
|
|
785
576
|
fetchPartialUpdate(
|
|
@@ -795,41 +586,33 @@ export function createNavigationBridge(
|
|
|
795
586
|
interceptSourceUrl,
|
|
796
587
|
cacheOnly: true,
|
|
797
588
|
}),
|
|
798
|
-
{
|
|
799
|
-
)
|
|
800
|
-
|
|
801
|
-
error
|
|
802
|
-
error
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
return;
|
|
806
|
-
}
|
|
807
|
-
// For background revalidation, network errors are logged but don't trigger error boundary
|
|
808
|
-
// since the user is already seeing cached content
|
|
809
|
-
if (error instanceof NetworkError || isNetworkError(error)) {
|
|
810
|
-
console.warn(
|
|
811
|
-
"[Browser] Background revalidation network error (cached content preserved):",
|
|
812
|
-
error.message
|
|
589
|
+
{ type: "stale-revalidation", interceptSourceUrl },
|
|
590
|
+
)
|
|
591
|
+
.catch((error) => {
|
|
592
|
+
if (isBackgroundSuppressible(error)) return;
|
|
593
|
+
console.error(
|
|
594
|
+
"[Browser] Background revalidation failed:",
|
|
595
|
+
error,
|
|
813
596
|
);
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
597
|
+
})
|
|
598
|
+
.finally(() => {
|
|
599
|
+
tx[Symbol.dispose]();
|
|
600
|
+
});
|
|
818
601
|
}
|
|
819
602
|
return;
|
|
820
603
|
} catch (error) {
|
|
821
604
|
console.warn(
|
|
822
605
|
"[Browser] Failed to render from cache, fetching:",
|
|
823
|
-
error
|
|
606
|
+
error,
|
|
824
607
|
);
|
|
825
608
|
// Fall through to fetch
|
|
826
609
|
}
|
|
827
610
|
} else {
|
|
828
|
-
|
|
611
|
+
debugLog("[Browser] History cache miss for key:", historyKey);
|
|
829
612
|
}
|
|
830
613
|
|
|
831
614
|
// Fetch if not cached
|
|
832
|
-
|
|
615
|
+
const tx = createNavigationTransaction(store, eventController, url, {
|
|
833
616
|
replace: true,
|
|
834
617
|
});
|
|
835
618
|
|
|
@@ -839,45 +622,43 @@ export function createNavigationBridge(
|
|
|
839
622
|
undefined,
|
|
840
623
|
false,
|
|
841
624
|
tx.handle.signal,
|
|
842
|
-
tx.with({
|
|
625
|
+
tx.with({
|
|
626
|
+
url,
|
|
627
|
+
replace: true,
|
|
628
|
+
scroll: false,
|
|
629
|
+
intercept: isIntercept,
|
|
630
|
+
interceptSourceUrl,
|
|
631
|
+
}),
|
|
632
|
+
isIntercept
|
|
633
|
+
? { type: "navigate", interceptSourceUrl }
|
|
634
|
+
: isLeavingIntercept
|
|
635
|
+
? { type: "leave-intercept" }
|
|
636
|
+
: undefined,
|
|
843
637
|
);
|
|
844
638
|
// Restore scroll position after fetch completes
|
|
845
639
|
handleNavigationEnd({ restore: true, isStreaming });
|
|
846
640
|
} catch (error) {
|
|
847
641
|
if (error instanceof DOMException && error.name === "AbortError") {
|
|
848
|
-
|
|
642
|
+
debugLog("[Browser] Popstate navigation aborted");
|
|
849
643
|
return;
|
|
850
644
|
}
|
|
851
645
|
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
: new NetworkError(
|
|
858
|
-
"Unable to connect to server. Please check your connection.",
|
|
859
|
-
{ cause: error, url, operation: "navigation" }
|
|
860
|
-
);
|
|
861
|
-
|
|
646
|
+
const networkError = toNetworkError(error, {
|
|
647
|
+
url,
|
|
648
|
+
operation: "navigation",
|
|
649
|
+
});
|
|
650
|
+
if (networkError) {
|
|
862
651
|
console.error(
|
|
863
|
-
"[Browser] Network error during popstate
|
|
864
|
-
networkError
|
|
652
|
+
"[Browser] Network error during popstate:",
|
|
653
|
+
networkError,
|
|
865
654
|
);
|
|
866
|
-
|
|
867
|
-
startTransition(() => {
|
|
868
|
-
onUpdate({
|
|
869
|
-
root: createElement(NetworkErrorThrower, { error: networkError }),
|
|
870
|
-
metadata: {
|
|
871
|
-
pathname: url,
|
|
872
|
-
segments: [],
|
|
873
|
-
isError: true,
|
|
874
|
-
},
|
|
875
|
-
});
|
|
876
|
-
});
|
|
655
|
+
emitNetworkError(onUpdate, networkError, url);
|
|
877
656
|
return;
|
|
878
657
|
}
|
|
879
658
|
|
|
880
659
|
throw error;
|
|
660
|
+
} finally {
|
|
661
|
+
tx[Symbol.dispose]();
|
|
881
662
|
}
|
|
882
663
|
},
|
|
883
664
|
|
|
@@ -894,19 +675,51 @@ export function createNavigationBridge(
|
|
|
894
675
|
this.handlePopstate();
|
|
895
676
|
};
|
|
896
677
|
|
|
678
|
+
// When the browser restores a page from bfcache (back-forward cache),
|
|
679
|
+
// any in-flight navigation state is stale. This happens when:
|
|
680
|
+
// 1. A navigation triggers X-RSC-Reload (e.g., response route hit via SPA)
|
|
681
|
+
// 2. window.location.href does a hard navigation
|
|
682
|
+
// 3. The user presses back and the browser restores from bfcache
|
|
683
|
+
// At that point, currentNavigation is still set from step 1, so
|
|
684
|
+
// getState() returns "loading" and the progress bar shows.
|
|
685
|
+
// Abort the stale navigation to reset state to idle.
|
|
686
|
+
const handlePageShow = (event: PageTransitionEvent) => {
|
|
687
|
+
if (event.persisted) {
|
|
688
|
+
debugLog(
|
|
689
|
+
"[Browser] Page restored from bfcache, resetting navigation state",
|
|
690
|
+
);
|
|
691
|
+
eventController.abortNavigation();
|
|
692
|
+
// pagehide flips scrollRestoration to "auto" for bfcache compat;
|
|
693
|
+
// restore "manual" so the router controls scroll on SPA navigations.
|
|
694
|
+
window.history.scrollRestoration = "manual";
|
|
695
|
+
}
|
|
696
|
+
};
|
|
697
|
+
|
|
897
698
|
// Register cross-tab refresh callback with the store
|
|
898
699
|
store.setCrossTabRefreshCallback(() => {
|
|
899
700
|
this.refresh();
|
|
900
701
|
});
|
|
901
702
|
|
|
902
703
|
window.addEventListener("popstate", handlePopstate);
|
|
903
|
-
|
|
704
|
+
window.addEventListener("pageshow", handlePageShow);
|
|
705
|
+
debugLog("[Browser] Navigation bridge ready");
|
|
904
706
|
|
|
905
707
|
return () => {
|
|
906
708
|
cleanupLinks();
|
|
907
709
|
window.removeEventListener("popstate", handlePopstate);
|
|
710
|
+
window.removeEventListener("pageshow", handlePageShow);
|
|
908
711
|
};
|
|
909
712
|
},
|
|
713
|
+
|
|
714
|
+
updateVersion(newVersion: string): void {
|
|
715
|
+
version = newVersion;
|
|
716
|
+
setAppVersion(newVersion);
|
|
717
|
+
store.clearHistoryCache();
|
|
718
|
+
},
|
|
719
|
+
|
|
720
|
+
updateAppShell(next: AppShell): void {
|
|
721
|
+
applyAppShell(next);
|
|
722
|
+
},
|
|
910
723
|
};
|
|
911
724
|
}
|
|
912
725
|
|