@ivogt/rsc-router 0.0.0-experimental.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -0
- package/package.json +131 -0
- package/src/__mocks__/version.ts +6 -0
- package/src/__tests__/route-definition.test.ts +63 -0
- package/src/browser/event-controller.ts +876 -0
- package/src/browser/index.ts +18 -0
- package/src/browser/link-interceptor.ts +121 -0
- package/src/browser/lru-cache.ts +69 -0
- package/src/browser/merge-segment-loaders.ts +126 -0
- package/src/browser/navigation-bridge.ts +891 -0
- package/src/browser/navigation-client.ts +155 -0
- package/src/browser/navigation-store.ts +823 -0
- package/src/browser/partial-update.ts +545 -0
- package/src/browser/react/Link.tsx +248 -0
- package/src/browser/react/NavigationProvider.tsx +228 -0
- package/src/browser/react/ScrollRestoration.tsx +94 -0
- package/src/browser/react/context.ts +53 -0
- package/src/browser/react/index.ts +52 -0
- package/src/browser/react/location-state-shared.ts +120 -0
- package/src/browser/react/location-state.ts +62 -0
- package/src/browser/react/use-action.ts +240 -0
- package/src/browser/react/use-client-cache.ts +56 -0
- package/src/browser/react/use-handle.ts +178 -0
- package/src/browser/react/use-link-status.ts +134 -0
- package/src/browser/react/use-navigation.ts +150 -0
- package/src/browser/react/use-segments.ts +188 -0
- package/src/browser/request-controller.ts +149 -0
- package/src/browser/rsc-router.tsx +310 -0
- package/src/browser/scroll-restoration.ts +324 -0
- package/src/browser/server-action-bridge.ts +747 -0
- package/src/browser/shallow.ts +35 -0
- package/src/browser/types.ts +443 -0
- package/src/cache/__tests__/memory-segment-store.test.ts +487 -0
- package/src/cache/__tests__/memory-store.test.ts +484 -0
- package/src/cache/cache-scope.ts +565 -0
- package/src/cache/cf/__tests__/cf-cache-store.test.ts +361 -0
- package/src/cache/cf/cf-cache-store.ts +274 -0
- package/src/cache/cf/index.ts +19 -0
- package/src/cache/index.ts +52 -0
- package/src/cache/memory-segment-store.ts +150 -0
- package/src/cache/memory-store.ts +253 -0
- package/src/cache/types.ts +366 -0
- package/src/client.rsc.tsx +88 -0
- package/src/client.tsx +609 -0
- package/src/components/DefaultDocument.tsx +20 -0
- package/src/default-error-boundary.tsx +88 -0
- package/src/deps/browser.ts +8 -0
- package/src/deps/html-stream-client.ts +2 -0
- package/src/deps/html-stream-server.ts +2 -0
- package/src/deps/rsc.ts +10 -0
- package/src/deps/ssr.ts +2 -0
- package/src/errors.ts +259 -0
- package/src/handle.ts +120 -0
- package/src/handles/MetaTags.tsx +178 -0
- package/src/handles/index.ts +6 -0
- package/src/handles/meta.ts +247 -0
- package/src/href-client.ts +128 -0
- package/src/href.ts +139 -0
- package/src/index.rsc.ts +69 -0
- package/src/index.ts +84 -0
- package/src/loader.rsc.ts +204 -0
- package/src/loader.ts +47 -0
- package/src/network-error-thrower.tsx +21 -0
- package/src/outlet-context.ts +15 -0
- package/src/root-error-boundary.tsx +277 -0
- package/src/route-content-wrapper.tsx +198 -0
- package/src/route-definition.ts +1333 -0
- package/src/route-map-builder.ts +140 -0
- package/src/route-types.ts +148 -0
- package/src/route-utils.ts +89 -0
- package/src/router/__tests__/match-context.test.ts +104 -0
- package/src/router/__tests__/match-pipelines.test.ts +537 -0
- package/src/router/__tests__/match-result.test.ts +566 -0
- package/src/router/__tests__/on-error.test.ts +935 -0
- package/src/router/__tests__/pattern-matching.test.ts +577 -0
- package/src/router/error-handling.ts +287 -0
- package/src/router/handler-context.ts +60 -0
- package/src/router/loader-resolution.ts +326 -0
- package/src/router/manifest.ts +116 -0
- package/src/router/match-context.ts +261 -0
- package/src/router/match-middleware/background-revalidation.ts +236 -0
- package/src/router/match-middleware/cache-lookup.ts +261 -0
- package/src/router/match-middleware/cache-store.ts +250 -0
- package/src/router/match-middleware/index.ts +81 -0
- package/src/router/match-middleware/intercept-resolution.ts +268 -0
- package/src/router/match-middleware/segment-resolution.ts +174 -0
- package/src/router/match-pipelines.ts +214 -0
- package/src/router/match-result.ts +212 -0
- package/src/router/metrics.ts +62 -0
- package/src/router/middleware.test.ts +1355 -0
- package/src/router/middleware.ts +748 -0
- package/src/router/pattern-matching.ts +271 -0
- package/src/router/revalidation.ts +190 -0
- package/src/router/router-context.ts +299 -0
- package/src/router/types.ts +96 -0
- package/src/router.ts +3484 -0
- package/src/rsc/__tests__/helpers.test.ts +175 -0
- package/src/rsc/handler.ts +942 -0
- package/src/rsc/helpers.ts +64 -0
- package/src/rsc/index.ts +56 -0
- package/src/rsc/nonce.ts +18 -0
- package/src/rsc/types.ts +225 -0
- package/src/segment-system.tsx +405 -0
- package/src/server/__tests__/request-context.test.ts +171 -0
- package/src/server/context.ts +340 -0
- package/src/server/handle-store.ts +230 -0
- package/src/server/loader-registry.ts +174 -0
- package/src/server/request-context.ts +470 -0
- package/src/server/root-layout.tsx +10 -0
- package/src/server/tsconfig.json +14 -0
- package/src/server.ts +126 -0
- package/src/ssr/__tests__/ssr-handler.test.tsx +188 -0
- package/src/ssr/index.tsx +215 -0
- package/src/types.ts +1473 -0
- package/src/use-loader.tsx +346 -0
- package/src/vite/__tests__/expose-loader-id.test.ts +117 -0
- package/src/vite/expose-action-id.ts +344 -0
- package/src/vite/expose-handle-id.ts +209 -0
- package/src/vite/expose-loader-id.ts +357 -0
- package/src/vite/expose-location-state-id.ts +177 -0
- package/src/vite/index.ts +608 -0
- package/src/vite/version.d.ts +12 -0
- package/src/vite/virtual-entries.ts +109 -0
|
@@ -0,0 +1,891 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
NavigationBridge,
|
|
3
|
+
NavigationBridgeConfig,
|
|
4
|
+
NavigateOptions,
|
|
5
|
+
NavigationStore,
|
|
6
|
+
ResolvedSegment,
|
|
7
|
+
} from "./types.js";
|
|
8
|
+
import {
|
|
9
|
+
isLocationStateEntry,
|
|
10
|
+
resolveLocationStateEntries,
|
|
11
|
+
} from "./react/location-state-shared.js";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Check if state is from typed LocationStateEntry[] (has __rsc_ls_ keys)
|
|
15
|
+
*/
|
|
16
|
+
function isTypedLocationState(
|
|
17
|
+
state: unknown
|
|
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
|
+
}
|
|
57
|
+
|
|
58
|
+
// Add user state
|
|
59
|
+
if (userState !== undefined) {
|
|
60
|
+
if (isTypedLocationState(userState)) {
|
|
61
|
+
// Typed state: spread directly
|
|
62
|
+
Object.assign(result, userState);
|
|
63
|
+
} else {
|
|
64
|
+
// Legacy state: store in .state
|
|
65
|
+
result.state = userState;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return Object.keys(result).length > 0 ? result : null;
|
|
70
|
+
}
|
|
71
|
+
import { setupLinkInterception } from "./link-interceptor.js";
|
|
72
|
+
import { createPartialUpdater } from "./partial-update.js";
|
|
73
|
+
import { generateHistoryKey } from "./navigation-store.js";
|
|
74
|
+
import {
|
|
75
|
+
handleNavigationStart,
|
|
76
|
+
handleNavigationEnd,
|
|
77
|
+
ensureHistoryKey,
|
|
78
|
+
} from "./scroll-restoration.js";
|
|
79
|
+
import type { EventController, NavigationHandle } from "./event-controller.js";
|
|
80
|
+
import { NetworkError, isNetworkError } from "../errors.js";
|
|
81
|
+
import { NetworkErrorThrower } from "../network-error-thrower.js";
|
|
82
|
+
import { createElement, startTransition } from "react";
|
|
83
|
+
|
|
84
|
+
// Polyfill Symbol.dispose for Safari and older browsers
|
|
85
|
+
if (typeof Symbol.dispose === "undefined") {
|
|
86
|
+
(Symbol as any).dispose = Symbol("Symbol.dispose");
|
|
87
|
+
}
|
|
88
|
+
|
|
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
|
+
export { createNavigationTransaction };
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* Extended configuration for navigation bridge with event controller
|
|
444
|
+
*/
|
|
445
|
+
export interface NavigationBridgeConfigWithController extends NavigationBridgeConfig {
|
|
446
|
+
eventController: EventController;
|
|
447
|
+
/** RSC version from initial payload metadata */
|
|
448
|
+
version?: string;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/**
|
|
452
|
+
* Create a navigation bridge for handling client-side navigation
|
|
453
|
+
*
|
|
454
|
+
* The bridge coordinates all navigation operations:
|
|
455
|
+
* - Link click interception
|
|
456
|
+
* - Browser back/forward (popstate)
|
|
457
|
+
* - Programmatic navigation
|
|
458
|
+
*
|
|
459
|
+
* Uses the event controller for reactive state management.
|
|
460
|
+
*
|
|
461
|
+
* @param config - Bridge configuration
|
|
462
|
+
* @returns NavigationBridge instance
|
|
463
|
+
*/
|
|
464
|
+
export function createNavigationBridge(
|
|
465
|
+
config: NavigationBridgeConfigWithController
|
|
466
|
+
): NavigationBridge {
|
|
467
|
+
const { store, client, eventController, onUpdate, renderSegments, version } = config;
|
|
468
|
+
|
|
469
|
+
// Create shared partial updater
|
|
470
|
+
const fetchPartialUpdate = createPartialUpdater({
|
|
471
|
+
store,
|
|
472
|
+
client,
|
|
473
|
+
onUpdate,
|
|
474
|
+
renderSegments,
|
|
475
|
+
version,
|
|
476
|
+
});
|
|
477
|
+
|
|
478
|
+
return {
|
|
479
|
+
/**
|
|
480
|
+
* Navigate to a URL
|
|
481
|
+
* Uses optimistic rendering from cache when available (SWR pattern)
|
|
482
|
+
*/
|
|
483
|
+
async navigate(url: string, options?: NavigateOptions): Promise<void> {
|
|
484
|
+
// Resolve LocationStateEntry[] to flat object if needed
|
|
485
|
+
const resolvedState =
|
|
486
|
+
options?.state !== undefined
|
|
487
|
+
? resolveNavigationState(options.state)
|
|
488
|
+
: undefined;
|
|
489
|
+
|
|
490
|
+
// Only abort pending requests when navigating to a different route
|
|
491
|
+
// Same-route navigation (e.g., /todos -> /todos) should not cancel in-flight actions
|
|
492
|
+
const currentPath = new URL(window.location.href).pathname;
|
|
493
|
+
const targetPath = new URL(url, window.location.origin).pathname;
|
|
494
|
+
if (currentPath !== targetPath) {
|
|
495
|
+
eventController.abortNavigation();
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
// Before navigating away, update the source page's cache with the latest handleData.
|
|
499
|
+
// This ensures the cache has correct handleData even if handles were streaming.
|
|
500
|
+
const sourceHistoryKey = store.getHistoryKey();
|
|
501
|
+
const sourceCached = store.getCachedSegments(sourceHistoryKey);
|
|
502
|
+
if (sourceCached?.segments && sourceCached.segments.length > 0) {
|
|
503
|
+
const currentHandleData = eventController.getHandleState().data;
|
|
504
|
+
store.cacheSegmentsForHistory(
|
|
505
|
+
sourceHistoryKey,
|
|
506
|
+
sourceCached.segments,
|
|
507
|
+
currentHandleData
|
|
508
|
+
);
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
// Check if we have cached segments for target URL
|
|
512
|
+
const historyKey = generateHistoryKey(url);
|
|
513
|
+
const cached = store.getCachedSegments(historyKey);
|
|
514
|
+
|
|
515
|
+
// For shared segments (same ID on current and target), use current page's version
|
|
516
|
+
// since it may have fresher data after an action revalidation.
|
|
517
|
+
// This avoids unnecessary server round-trips for shared layout loaders.
|
|
518
|
+
let cachedSegments = cached?.segments;
|
|
519
|
+
if (cachedSegments && sourceCached?.segments) {
|
|
520
|
+
const sourceSegmentMap = new Map(
|
|
521
|
+
sourceCached.segments.map((s) => [s.id, s])
|
|
522
|
+
);
|
|
523
|
+
cachedSegments = cachedSegments.map((targetSeg) => {
|
|
524
|
+
const sourceSeg = sourceSegmentMap.get(targetSeg.id);
|
|
525
|
+
// Use source (current page) version for shared segments - it's fresher
|
|
526
|
+
return sourceSeg || targetSeg;
|
|
527
|
+
});
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
// Also check if there's an intercept cache entry for this URL
|
|
531
|
+
// If so, this URL CAN be intercepted, and we shouldn't use the non-intercept cache
|
|
532
|
+
// because the navigation might result in an intercept (depending on source URL)
|
|
533
|
+
const interceptHistoryKey = generateHistoryKey(url, { intercept: true });
|
|
534
|
+
const hasInterceptCache = store.hasHistoryCache(interceptHistoryKey);
|
|
535
|
+
|
|
536
|
+
// Skip optimistic rendering for:
|
|
537
|
+
// 1. intercept caches - interception depends on source page context
|
|
538
|
+
// 2. routes that CAN be intercepted - we don't know if this navigation will intercept
|
|
539
|
+
const hasUsableCache =
|
|
540
|
+
cachedSegments &&
|
|
541
|
+
cachedSegments.length > 0 &&
|
|
542
|
+
!isInterceptOnlyCache(cachedSegments) &&
|
|
543
|
+
!hasInterceptCache;
|
|
544
|
+
|
|
545
|
+
using tx = createNavigationTransaction(store, eventController, url, {
|
|
546
|
+
...options,
|
|
547
|
+
state: resolvedState,
|
|
548
|
+
skipLoadingState: hasUsableCache,
|
|
549
|
+
});
|
|
550
|
+
|
|
551
|
+
// REVALIDATE: Fetch fresh data from server
|
|
552
|
+
try {
|
|
553
|
+
await fetchPartialUpdate(
|
|
554
|
+
url,
|
|
555
|
+
hasUsableCache ? cachedSegments!.map((s) => s.id) : undefined,
|
|
556
|
+
false,
|
|
557
|
+
tx.handle.signal,
|
|
558
|
+
tx.with({
|
|
559
|
+
url,
|
|
560
|
+
replace: options?.replace,
|
|
561
|
+
scroll: options?.scroll,
|
|
562
|
+
state: resolvedState,
|
|
563
|
+
}),
|
|
564
|
+
// Pass cached segments (merged with current page's fresh segments for shared IDs)
|
|
565
|
+
// so the segment map is consistent with what we tell the server we have.
|
|
566
|
+
// Server decides what needs revalidation based on route matching and custom functions.
|
|
567
|
+
// No need for staleRevalidation flag - we're sending the freshest segments we have.
|
|
568
|
+
hasUsableCache ? { targetCacheSegments: cachedSegments } : undefined
|
|
569
|
+
);
|
|
570
|
+
} catch (error) {
|
|
571
|
+
// Ignore AbortError - navigation was cancelled by a newer navigation
|
|
572
|
+
if (error instanceof DOMException && error.name === "AbortError") {
|
|
573
|
+
console.log("[Browser] Navigation aborted by newer navigation");
|
|
574
|
+
return;
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
// Handle network errors by triggering root error boundary
|
|
578
|
+
if (error instanceof NetworkError || isNetworkError(error)) {
|
|
579
|
+
const networkError =
|
|
580
|
+
error instanceof NetworkError
|
|
581
|
+
? error
|
|
582
|
+
: new NetworkError(
|
|
583
|
+
"Unable to connect to server. Please check your connection.",
|
|
584
|
+
{ cause: error, url, operation: "navigation" }
|
|
585
|
+
);
|
|
586
|
+
|
|
587
|
+
console.error(
|
|
588
|
+
"[Browser] Network error during navigation:",
|
|
589
|
+
networkError
|
|
590
|
+
);
|
|
591
|
+
|
|
592
|
+
// Emit update with NetworkErrorThrower to trigger root error boundary
|
|
593
|
+
startTransition(() => {
|
|
594
|
+
onUpdate({
|
|
595
|
+
root: createElement(NetworkErrorThrower, { error: networkError }),
|
|
596
|
+
metadata: {
|
|
597
|
+
pathname: url,
|
|
598
|
+
segments: [],
|
|
599
|
+
isError: true,
|
|
600
|
+
},
|
|
601
|
+
});
|
|
602
|
+
});
|
|
603
|
+
return;
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
throw error;
|
|
607
|
+
}
|
|
608
|
+
},
|
|
609
|
+
|
|
610
|
+
/**
|
|
611
|
+
* Refresh current route
|
|
612
|
+
*/
|
|
613
|
+
async refresh(): Promise<void> {
|
|
614
|
+
eventController.abortNavigation();
|
|
615
|
+
|
|
616
|
+
using tx = createNavigationTransaction(
|
|
617
|
+
store,
|
|
618
|
+
eventController,
|
|
619
|
+
window.location.href,
|
|
620
|
+
{ replace: true }
|
|
621
|
+
);
|
|
622
|
+
|
|
623
|
+
try {
|
|
624
|
+
// Refetch with empty segments to get everything fresh
|
|
625
|
+
await fetchPartialUpdate(
|
|
626
|
+
window.location.href,
|
|
627
|
+
[],
|
|
628
|
+
false,
|
|
629
|
+
tx.handle.signal,
|
|
630
|
+
tx.with({ url: window.location.href, replace: true, scroll: false })
|
|
631
|
+
);
|
|
632
|
+
} catch (error) {
|
|
633
|
+
// Handle network errors by triggering root error boundary
|
|
634
|
+
if (error instanceof NetworkError || isNetworkError(error)) {
|
|
635
|
+
const networkError =
|
|
636
|
+
error instanceof NetworkError
|
|
637
|
+
? error
|
|
638
|
+
: new NetworkError(
|
|
639
|
+
"Unable to connect to server. Please check your connection.",
|
|
640
|
+
{
|
|
641
|
+
cause: error,
|
|
642
|
+
url: window.location.href,
|
|
643
|
+
operation: "revalidation",
|
|
644
|
+
}
|
|
645
|
+
);
|
|
646
|
+
|
|
647
|
+
console.error(
|
|
648
|
+
"[Browser] Network error during refresh:",
|
|
649
|
+
networkError
|
|
650
|
+
);
|
|
651
|
+
|
|
652
|
+
startTransition(() => {
|
|
653
|
+
onUpdate({
|
|
654
|
+
root: createElement(NetworkErrorThrower, { error: networkError }),
|
|
655
|
+
metadata: {
|
|
656
|
+
pathname: window.location.href,
|
|
657
|
+
segments: [],
|
|
658
|
+
isError: true,
|
|
659
|
+
},
|
|
660
|
+
});
|
|
661
|
+
});
|
|
662
|
+
return;
|
|
663
|
+
}
|
|
664
|
+
throw error;
|
|
665
|
+
}
|
|
666
|
+
},
|
|
667
|
+
|
|
668
|
+
/**
|
|
669
|
+
* Handle browser back/forward navigation
|
|
670
|
+
* Uses cached segments when available for instant restoration
|
|
671
|
+
*/
|
|
672
|
+
async handlePopstate(): Promise<void> {
|
|
673
|
+
// Abort any pending navigation to prevent race conditions
|
|
674
|
+
eventController.abortNavigation();
|
|
675
|
+
|
|
676
|
+
const url = window.location.href;
|
|
677
|
+
|
|
678
|
+
// Check if this history entry is an intercept
|
|
679
|
+
const historyState = window.history.state;
|
|
680
|
+
const isIntercept = historyState?.intercept === true;
|
|
681
|
+
const interceptSourceUrl = historyState?.sourceUrl;
|
|
682
|
+
|
|
683
|
+
// Check if intercept context is changing (same URL, different intercept state)
|
|
684
|
+
// If so, abort in-flight actions - their results would be for wrong context
|
|
685
|
+
const currentInterceptSource = store.getInterceptSourceUrl();
|
|
686
|
+
const newInterceptSource = interceptSourceUrl ?? null;
|
|
687
|
+
if (currentInterceptSource !== newInterceptSource) {
|
|
688
|
+
console.log(
|
|
689
|
+
`[Browser] Intercept context changing (${currentInterceptSource} -> ${newInterceptSource}), aborting in-flight actions`
|
|
690
|
+
);
|
|
691
|
+
eventController.abortAllActions();
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
// Compute history key from URL (with intercept suffix if applicable)
|
|
695
|
+
const historyKey = generateHistoryKey(url, { intercept: isIntercept });
|
|
696
|
+
|
|
697
|
+
console.log(
|
|
698
|
+
"[Browser] Popstate -",
|
|
699
|
+
isIntercept ? "intercept" : "normal",
|
|
700
|
+
"key:",
|
|
701
|
+
historyKey
|
|
702
|
+
);
|
|
703
|
+
|
|
704
|
+
// Update location in event controller
|
|
705
|
+
eventController.setLocation(new URL(url));
|
|
706
|
+
|
|
707
|
+
// If this is an intercept, restore the intercept context
|
|
708
|
+
if (isIntercept && interceptSourceUrl) {
|
|
709
|
+
store.setInterceptSourceUrl(interceptSourceUrl);
|
|
710
|
+
} else {
|
|
711
|
+
store.setInterceptSourceUrl(null);
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
// Helper to check if streaming is in progress
|
|
715
|
+
const isStreaming = () => eventController.getState().isStreaming;
|
|
716
|
+
|
|
717
|
+
// Check if we can restore from history cache
|
|
718
|
+
const cached = store.getCachedSegments(historyKey);
|
|
719
|
+
const cachedSegments = cached?.segments;
|
|
720
|
+
const cachedHandleData = cached?.handleData;
|
|
721
|
+
const isStale = cached?.stale ?? false;
|
|
722
|
+
|
|
723
|
+
if (cachedSegments && cachedSegments.length > 0) {
|
|
724
|
+
// Update store to point to this history entry
|
|
725
|
+
store.setHistoryKey(historyKey);
|
|
726
|
+
store.setSegmentIds(cachedSegments.map((s) => s.id));
|
|
727
|
+
store.setCurrentUrl(url);
|
|
728
|
+
store.setPath(new URL(url).pathname);
|
|
729
|
+
|
|
730
|
+
// Render from cache - force await to skip loading fallbacks
|
|
731
|
+
try {
|
|
732
|
+
const root = renderSegments(cachedSegments, {
|
|
733
|
+
forceAwait: true,
|
|
734
|
+
});
|
|
735
|
+
onUpdate({
|
|
736
|
+
root,
|
|
737
|
+
metadata: {
|
|
738
|
+
pathname: new URL(url).pathname,
|
|
739
|
+
segments: cachedSegments,
|
|
740
|
+
isPartial: true,
|
|
741
|
+
matched: cachedSegments.map((s) => s.id),
|
|
742
|
+
diff: [],
|
|
743
|
+
cachedHandleData,
|
|
744
|
+
},
|
|
745
|
+
});
|
|
746
|
+
|
|
747
|
+
// Restore scroll position for back/forward navigation
|
|
748
|
+
handleNavigationEnd({ restore: true, isStreaming });
|
|
749
|
+
|
|
750
|
+
// SWR: If stale, trigger background revalidation
|
|
751
|
+
if (isStale) {
|
|
752
|
+
console.log("[Browser] Cache is stale, background revalidating...");
|
|
753
|
+
// Background revalidation - don't await, just fire and forget
|
|
754
|
+
const segmentIds = cachedSegments.map((s) => s.id);
|
|
755
|
+
|
|
756
|
+
using tx = createNavigationTransaction(
|
|
757
|
+
store,
|
|
758
|
+
eventController,
|
|
759
|
+
url,
|
|
760
|
+
{ skipLoadingState: true, replace: true }
|
|
761
|
+
);
|
|
762
|
+
|
|
763
|
+
fetchPartialUpdate(
|
|
764
|
+
url,
|
|
765
|
+
segmentIds,
|
|
766
|
+
false,
|
|
767
|
+
tx.handle.signal,
|
|
768
|
+
tx.with({
|
|
769
|
+
url,
|
|
770
|
+
replace: true,
|
|
771
|
+
scroll: false,
|
|
772
|
+
intercept: isIntercept,
|
|
773
|
+
interceptSourceUrl,
|
|
774
|
+
cacheOnly: true,
|
|
775
|
+
}),
|
|
776
|
+
{ staleRevalidation: true, interceptSourceUrl }
|
|
777
|
+
).catch((error) => {
|
|
778
|
+
if (
|
|
779
|
+
error instanceof DOMException &&
|
|
780
|
+
error.name === "AbortError"
|
|
781
|
+
) {
|
|
782
|
+
console.log("[Browser] Background revalidation aborted");
|
|
783
|
+
return;
|
|
784
|
+
}
|
|
785
|
+
// For background revalidation, network errors are logged but don't trigger error boundary
|
|
786
|
+
// since the user is already seeing cached content
|
|
787
|
+
if (error instanceof NetworkError || isNetworkError(error)) {
|
|
788
|
+
console.warn(
|
|
789
|
+
"[Browser] Background revalidation network error (cached content preserved):",
|
|
790
|
+
error.message
|
|
791
|
+
);
|
|
792
|
+
return;
|
|
793
|
+
}
|
|
794
|
+
console.error("[Browser] Background revalidation failed:", error);
|
|
795
|
+
});
|
|
796
|
+
}
|
|
797
|
+
return;
|
|
798
|
+
} catch (error) {
|
|
799
|
+
console.warn(
|
|
800
|
+
"[Browser] Failed to render from cache, fetching:",
|
|
801
|
+
error
|
|
802
|
+
);
|
|
803
|
+
// Fall through to fetch
|
|
804
|
+
}
|
|
805
|
+
} else {
|
|
806
|
+
console.log("[Browser] History cache miss for key:", historyKey);
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
// Fetch if not cached
|
|
810
|
+
using tx = createNavigationTransaction(store, eventController, url, {
|
|
811
|
+
replace: true,
|
|
812
|
+
});
|
|
813
|
+
|
|
814
|
+
try {
|
|
815
|
+
await fetchPartialUpdate(
|
|
816
|
+
url,
|
|
817
|
+
undefined,
|
|
818
|
+
false,
|
|
819
|
+
tx.handle.signal,
|
|
820
|
+
tx.with({ url, replace: true, scroll: false })
|
|
821
|
+
);
|
|
822
|
+
// Restore scroll position after fetch completes
|
|
823
|
+
handleNavigationEnd({ restore: true, isStreaming });
|
|
824
|
+
} catch (error) {
|
|
825
|
+
if (error instanceof DOMException && error.name === "AbortError") {
|
|
826
|
+
console.log("[Browser] Popstate navigation aborted");
|
|
827
|
+
return;
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
// Handle network errors by triggering root error boundary
|
|
831
|
+
if (error instanceof NetworkError || isNetworkError(error)) {
|
|
832
|
+
const networkError =
|
|
833
|
+
error instanceof NetworkError
|
|
834
|
+
? error
|
|
835
|
+
: new NetworkError(
|
|
836
|
+
"Unable to connect to server. Please check your connection.",
|
|
837
|
+
{ cause: error, url, operation: "navigation" }
|
|
838
|
+
);
|
|
839
|
+
|
|
840
|
+
console.error(
|
|
841
|
+
"[Browser] Network error during popstate navigation:",
|
|
842
|
+
networkError
|
|
843
|
+
);
|
|
844
|
+
|
|
845
|
+
startTransition(() => {
|
|
846
|
+
onUpdate({
|
|
847
|
+
root: createElement(NetworkErrorThrower, { error: networkError }),
|
|
848
|
+
metadata: {
|
|
849
|
+
pathname: url,
|
|
850
|
+
segments: [],
|
|
851
|
+
isError: true,
|
|
852
|
+
},
|
|
853
|
+
});
|
|
854
|
+
});
|
|
855
|
+
return;
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
throw error;
|
|
859
|
+
}
|
|
860
|
+
},
|
|
861
|
+
|
|
862
|
+
/**
|
|
863
|
+
* Register link interception
|
|
864
|
+
* @returns Cleanup function
|
|
865
|
+
*/
|
|
866
|
+
registerLinkInterception(): () => void {
|
|
867
|
+
const cleanupLinks = setupLinkInterception((url, options) => {
|
|
868
|
+
this.navigate(url, options);
|
|
869
|
+
});
|
|
870
|
+
|
|
871
|
+
const handlePopstate = () => {
|
|
872
|
+
this.handlePopstate();
|
|
873
|
+
};
|
|
874
|
+
|
|
875
|
+
// Register cross-tab refresh callback with the store
|
|
876
|
+
store.setCrossTabRefreshCallback(() => {
|
|
877
|
+
this.refresh();
|
|
878
|
+
});
|
|
879
|
+
|
|
880
|
+
window.addEventListener("popstate", handlePopstate);
|
|
881
|
+
console.log("[Browser] Navigation bridge ready");
|
|
882
|
+
|
|
883
|
+
return () => {
|
|
884
|
+
cleanupLinks();
|
|
885
|
+
window.removeEventListener("popstate", handlePopstate);
|
|
886
|
+
};
|
|
887
|
+
},
|
|
888
|
+
};
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
export { createNavigationBridge as default };
|