@rangojs/router 0.0.0-experimental.84 → 0.0.0-experimental.86
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 +50 -20
- package/dist/vite/index.js +19 -9
- package/package.json +14 -15
- package/skills/breadcrumbs/SKILL.md +3 -1
- package/skills/hooks/SKILL.md +4 -2
- package/skills/links/SKILL.md +88 -16
- package/skills/loader/SKILL.md +35 -2
- package/skills/typesafety/SKILL.md +3 -1
- package/src/browser/app-shell.ts +52 -0
- package/src/browser/navigation-bridge.ts +51 -2
- package/src/browser/navigation-store.ts +25 -1
- package/src/browser/partial-update.ts +20 -1
- package/src/browser/prefetch/cache.ts +16 -0
- package/src/browser/rango-state.ts +53 -13
- package/src/browser/react/NavigationProvider.tsx +44 -9
- package/src/browser/react/use-router.ts +8 -1
- package/src/browser/rsc-router.tsx +34 -6
- package/src/browser/types.ts +13 -0
- package/src/cache/cf/cf-cache-store.ts +5 -7
- package/src/index.rsc.ts +3 -0
- package/src/index.ts +3 -0
- package/src/outlet-context.ts +1 -1
- package/src/reverse.ts +3 -2
- package/src/router/handler-context.ts +20 -3
- package/src/router/lazy-includes.ts +1 -1
- package/src/router/loader-resolution.ts +3 -0
- package/src/router/match-api.ts +3 -3
- package/src/router/middleware-types.ts +2 -22
- package/src/router/middleware.ts +18 -3
- package/src/router/pattern-matching.ts +60 -9
- package/src/router/trie-matching.ts +10 -4
- package/src/router/url-params.ts +49 -0
- package/src/router.ts +1 -2
- package/src/rsc/handler.ts +2 -1
- package/src/rsc/response-route-handler.ts +3 -0
- package/src/server/request-context.ts +10 -42
- package/src/types/handler-context.ts +2 -34
- package/src/types/loader-types.ts +2 -6
- package/src/types/request-scope.ts +126 -0
- package/src/urls/response-types.ts +2 -10
- package/src/vite/rango.ts +23 -7
- package/src/vite/utils/banner.ts +1 -1
- package/src/vite/utils/package-resolution.ts +1 -1
|
@@ -12,7 +12,10 @@ import type {
|
|
|
12
12
|
ActionStateListener,
|
|
13
13
|
HandleData,
|
|
14
14
|
} from "./types.js";
|
|
15
|
-
import {
|
|
15
|
+
import {
|
|
16
|
+
clearPrefetchCache,
|
|
17
|
+
clearPrefetchCacheLocal,
|
|
18
|
+
} from "./prefetch/cache.js";
|
|
16
19
|
|
|
17
20
|
/**
|
|
18
21
|
* Default action state (idle with no payload)
|
|
@@ -335,6 +338,18 @@ export function createNavigationStore(
|
|
|
335
338
|
clearPrefetchCache();
|
|
336
339
|
}
|
|
337
340
|
|
|
341
|
+
/**
|
|
342
|
+
* Drop this tab's navigation + prefetch caches without broadcasting or
|
|
343
|
+
* rotating shared state. Used when the local session changes in a way that
|
|
344
|
+
* doesn't affect other tabs — e.g. this tab crosses into a different app
|
|
345
|
+
* via a cross-router navigation. Other tabs in the old app keep their
|
|
346
|
+
* caches and their X-Rango-State token.
|
|
347
|
+
*/
|
|
348
|
+
function clearCacheInternalLocal(): void {
|
|
349
|
+
historyCache.length = 0;
|
|
350
|
+
clearPrefetchCacheLocal();
|
|
351
|
+
}
|
|
352
|
+
|
|
338
353
|
/**
|
|
339
354
|
* Mark all cache entries as stale (internal - does not broadcast)
|
|
340
355
|
*/
|
|
@@ -668,6 +683,15 @@ export function createNavigationStore(
|
|
|
668
683
|
clearCacheAndBroadcast();
|
|
669
684
|
},
|
|
670
685
|
|
|
686
|
+
/**
|
|
687
|
+
* Drop this tab's navigation + prefetch caches locally without
|
|
688
|
+
* broadcasting or rotating shared state. Intended for cross-app
|
|
689
|
+
* transitions where the session state diverges for this tab only.
|
|
690
|
+
*/
|
|
691
|
+
clearHistoryCacheLocal(): void {
|
|
692
|
+
clearCacheInternalLocal();
|
|
693
|
+
},
|
|
694
|
+
|
|
671
695
|
/**
|
|
672
696
|
* Mark cache as stale and broadcast to other tabs
|
|
673
697
|
* Called after server actions - allows SWR pattern for popstate
|
|
@@ -41,6 +41,13 @@ export interface PartialUpdateConfig {
|
|
|
41
41
|
) => Promise<ReactNode> | ReactNode;
|
|
42
42
|
/** RSC version getter — returns the current version (may change after HMR) */
|
|
43
43
|
getVersion?: () => string | undefined;
|
|
44
|
+
/**
|
|
45
|
+
* Replace the active app-shell when a cross-app navigation is detected.
|
|
46
|
+
* Called before the full-update tree replacement renders, so the new
|
|
47
|
+
* payload's rootLayout, basename, and version are picked up. Theme,
|
|
48
|
+
* warmup, and prefetch TTL are not part of the shell — see AppShell.
|
|
49
|
+
*/
|
|
50
|
+
applyAppShell?: (next: import("./app-shell.js").AppShell) => void;
|
|
44
51
|
}
|
|
45
52
|
|
|
46
53
|
/**
|
|
@@ -110,6 +117,7 @@ export function createPartialUpdater(
|
|
|
110
117
|
onUpdate,
|
|
111
118
|
renderSegments,
|
|
112
119
|
getVersion = () => undefined,
|
|
120
|
+
applyAppShell,
|
|
113
121
|
} = config;
|
|
114
122
|
|
|
115
123
|
/**
|
|
@@ -228,7 +236,12 @@ export function createPartialUpdater(
|
|
|
228
236
|
// Detect app switch: if routerId changed, the navigation crossed into
|
|
229
237
|
// a different router (e.g., via host router path mount). Downgrade
|
|
230
238
|
// partial to full so the entire tree is replaced without reconciliation
|
|
231
|
-
// against stale segments from the previous app
|
|
239
|
+
// against stale segments from the previous app, and replace the app
|
|
240
|
+
// shell (rootLayout, basename, version) so the target app's document
|
|
241
|
+
// and router config take effect instead of remaining captured from the
|
|
242
|
+
// initial load. Theme, warmup, and prefetch TTL are intentionally
|
|
243
|
+
// document-lifetime (see AppShell doc); a new document navigation
|
|
244
|
+
// applies them.
|
|
232
245
|
if (payload.metadata?.routerId) {
|
|
233
246
|
const prevRouterId = store.getRouterId?.();
|
|
234
247
|
if (prevRouterId && prevRouterId !== payload.metadata.routerId) {
|
|
@@ -236,6 +249,12 @@ export function createPartialUpdater(
|
|
|
236
249
|
`[Browser] App switch detected (${prevRouterId} → ${payload.metadata.routerId}), forcing full update`,
|
|
237
250
|
);
|
|
238
251
|
payload.metadata.isPartial = false;
|
|
252
|
+
applyAppShell?.({
|
|
253
|
+
routerId: payload.metadata.routerId,
|
|
254
|
+
rootLayout: payload.metadata.rootLayout,
|
|
255
|
+
basename: payload.metadata.basename,
|
|
256
|
+
version: payload.metadata.version,
|
|
257
|
+
});
|
|
239
258
|
}
|
|
240
259
|
store.setRouterId?.(payload.metadata.routerId);
|
|
241
260
|
}
|
|
@@ -296,3 +296,19 @@ export function clearPrefetchCache(): void {
|
|
|
296
296
|
abortAllPrefetches();
|
|
297
297
|
invalidateRangoState();
|
|
298
298
|
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Drop all in-memory prefetch state for this tab without rotating rango-state.
|
|
302
|
+
*
|
|
303
|
+
* Use for local-only invalidations (e.g. app switch in this tab) where
|
|
304
|
+
* other tabs should NOT observe a state rotation. Unlike clearPrefetchCache,
|
|
305
|
+
* does not call invalidateRangoState, so the shared X-Rango-State token
|
|
306
|
+
* stays intact and siblings in the old app keep their prefetches.
|
|
307
|
+
*/
|
|
308
|
+
export function clearPrefetchCacheLocal(): void {
|
|
309
|
+
generation++;
|
|
310
|
+
inflight.clear();
|
|
311
|
+
inflightPromises.clear();
|
|
312
|
+
cache.clear();
|
|
313
|
+
abortAllPrefetches();
|
|
314
|
+
}
|
|
@@ -6,21 +6,37 @@
|
|
|
6
6
|
* navigation requests. The server responds with `Vary: X-Rango-State`,
|
|
7
7
|
* so the browser HTTP cache keys responses by (URL, X-Rango-State value).
|
|
8
8
|
*
|
|
9
|
-
*
|
|
9
|
+
* Value format: `{buildVersion}:{invalidationTimestamp}`
|
|
10
10
|
* - Build version changes on deploy, busting all cached prefetches.
|
|
11
11
|
* - Timestamp changes on server action invalidation.
|
|
12
12
|
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
13
|
+
* Storage key is namespaced per routerId (`rango-state:{routerId}`) so
|
|
14
|
+
* tabs in different apps on the same origin do not collide. Two tabs in
|
|
15
|
+
* the same app share a key → one tab's invalidation is picked up by the
|
|
16
|
+
* other via the `storage` event. A smooth cross-app transition in this
|
|
17
|
+
* tab rebinds to the target app's key; other tabs still in the old app
|
|
18
|
+
* keep their own key intact.
|
|
19
|
+
*
|
|
20
|
+
* If no routerId is supplied, falls back to a single legacy key for
|
|
21
|
+
* backward compatibility (single-app deployments unaffected).
|
|
16
22
|
*/
|
|
17
23
|
|
|
18
|
-
const
|
|
24
|
+
const LEGACY_STORAGE_KEY = "rango-state";
|
|
25
|
+
|
|
26
|
+
function buildStorageKey(routerId: string | undefined): string {
|
|
27
|
+
return routerId ? `${LEGACY_STORAGE_KEY}:${routerId}` : LEGACY_STORAGE_KEY;
|
|
28
|
+
}
|
|
19
29
|
|
|
20
30
|
// Module-level cache avoids hitting localStorage on every getRangoState() call.
|
|
21
31
|
// Initialized from localStorage on first access or by initRangoState().
|
|
22
32
|
let cachedState: string | null = null;
|
|
23
33
|
|
|
34
|
+
// The localStorage key this tab is currently bound to. Rebinds on
|
|
35
|
+
// initRangoState (document boot) and setRangoStateLocal (smooth app
|
|
36
|
+
// switch). The storage listener filters cross-tab events by this key so
|
|
37
|
+
// events from tabs in a different app are ignored.
|
|
38
|
+
let currentStorageKey: string = LEGACY_STORAGE_KEY;
|
|
39
|
+
|
|
24
40
|
// Cross-tab sync: the `storage` event fires in OTHER tabs when one tab writes
|
|
25
41
|
// to localStorage, keeping cachedState fresh without polling.
|
|
26
42
|
let storageListenerAttached = false;
|
|
@@ -28,7 +44,10 @@ let storageListenerAttached = false;
|
|
|
28
44
|
function attachStorageListener(): void {
|
|
29
45
|
if (storageListenerAttached || typeof window === "undefined") return;
|
|
30
46
|
window.addEventListener("storage", (e) => {
|
|
31
|
-
|
|
47
|
+
// Only react to events for this tab's current app namespace. Events
|
|
48
|
+
// under other routerId-scoped keys belong to other apps and must not
|
|
49
|
+
// clobber this tab's state.
|
|
50
|
+
if (e.key !== currentStorageKey) return;
|
|
32
51
|
cachedState = e.newValue;
|
|
33
52
|
});
|
|
34
53
|
storageListenerAttached = true;
|
|
@@ -37,16 +56,22 @@ function attachStorageListener(): void {
|
|
|
37
56
|
/**
|
|
38
57
|
* Initialize the Rango state key in localStorage.
|
|
39
58
|
* Called once at app startup with the build version from the server.
|
|
40
|
-
*
|
|
41
|
-
*
|
|
59
|
+
* The routerId scopes the storage key to this app; in multi-app setups
|
|
60
|
+
* each app owns its own `rango-state:{routerId}` key and cannot observe
|
|
61
|
+
* invalidations from sibling apps on the same origin.
|
|
62
|
+
*
|
|
63
|
+
* If localStorage already has a matching-version entry under the key,
|
|
64
|
+
* keeps it (preserves invalidation state across refresh). Otherwise
|
|
65
|
+
* writes a new value.
|
|
42
66
|
*/
|
|
43
|
-
export function initRangoState(version: string): void {
|
|
67
|
+
export function initRangoState(version: string, routerId?: string): void {
|
|
68
|
+
currentStorageKey = buildStorageKey(routerId);
|
|
44
69
|
if (typeof window === "undefined") return;
|
|
45
70
|
|
|
46
71
|
attachStorageListener();
|
|
47
72
|
|
|
48
73
|
try {
|
|
49
|
-
const existing = localStorage.getItem(
|
|
74
|
+
const existing = localStorage.getItem(currentStorageKey);
|
|
50
75
|
if (existing) {
|
|
51
76
|
const colonIdx = existing.indexOf(":");
|
|
52
77
|
if (colonIdx > 0) {
|
|
@@ -59,7 +84,7 @@ export function initRangoState(version: string): void {
|
|
|
59
84
|
}
|
|
60
85
|
// New version or first load
|
|
61
86
|
const newState = `${version}:${Date.now()}`;
|
|
62
|
-
localStorage.setItem(
|
|
87
|
+
localStorage.setItem(currentStorageKey, newState);
|
|
63
88
|
cachedState = newState;
|
|
64
89
|
} catch {
|
|
65
90
|
// localStorage may be unavailable (private browsing in some browsers)
|
|
@@ -77,7 +102,7 @@ export function getRangoState(): string {
|
|
|
77
102
|
if (typeof window === "undefined") return "0:0";
|
|
78
103
|
|
|
79
104
|
try {
|
|
80
|
-
const stored = localStorage.getItem(
|
|
105
|
+
const stored = localStorage.getItem(currentStorageKey);
|
|
81
106
|
if (stored) {
|
|
82
107
|
cachedState = stored;
|
|
83
108
|
return stored;
|
|
@@ -89,6 +114,21 @@ export function getRangoState(): string {
|
|
|
89
114
|
return "0:0";
|
|
90
115
|
}
|
|
91
116
|
|
|
117
|
+
/**
|
|
118
|
+
* Update the in-memory rango-state to a new version WITHOUT writing
|
|
119
|
+
* localStorage. Intended for smooth cross-app transitions in this tab only:
|
|
120
|
+
* subsequent requests from this tab send the new token, but other tabs
|
|
121
|
+
* still in the previous app do not observe a storage event. Rebinds this
|
|
122
|
+
* tab's storage key to the target app's namespace (`rango-state:{routerId}`)
|
|
123
|
+
* so subsequent storage events only reflect the new app. On the next hard
|
|
124
|
+
* reload, initRangoState reconciles localStorage from the server's
|
|
125
|
+
* authoritative version.
|
|
126
|
+
*/
|
|
127
|
+
export function setRangoStateLocal(version: string, routerId?: string): void {
|
|
128
|
+
currentStorageKey = buildStorageKey(routerId);
|
|
129
|
+
cachedState = `${version}:${Date.now()}`;
|
|
130
|
+
}
|
|
131
|
+
|
|
92
132
|
/**
|
|
93
133
|
* Invalidate the Rango state key. Called when server actions mutate data.
|
|
94
134
|
* Updates the timestamp portion while keeping the version prefix.
|
|
@@ -105,7 +145,7 @@ export function invalidateRangoState(): void {
|
|
|
105
145
|
if (typeof window === "undefined") return;
|
|
106
146
|
|
|
107
147
|
try {
|
|
108
|
-
localStorage.setItem(
|
|
148
|
+
localStorage.setItem(currentStorageKey, newState);
|
|
109
149
|
} catch {
|
|
110
150
|
// Silently handle localStorage errors
|
|
111
151
|
}
|
|
@@ -28,6 +28,7 @@ import { NonceContext } from "./nonce-context.js";
|
|
|
28
28
|
import type { ResolvedThemeConfig, Theme } from "../../theme/types.js";
|
|
29
29
|
import { cancelAllPrefetches } from "../prefetch/queue.js";
|
|
30
30
|
import { handleNavigationEnd } from "../scroll-restoration.js";
|
|
31
|
+
import type { AppShellRef } from "../app-shell.js";
|
|
31
32
|
|
|
32
33
|
/**
|
|
33
34
|
* Process handles from an async generator, updating the event controller
|
|
@@ -133,15 +134,23 @@ export interface NavigationProviderProps {
|
|
|
133
134
|
warmupEnabled?: boolean;
|
|
134
135
|
|
|
135
136
|
/**
|
|
136
|
-
* App version from server payload
|
|
137
|
-
*
|
|
137
|
+
* App version from server payload.
|
|
138
|
+
* Used only as a fallback when `appShellRef` is not supplied.
|
|
138
139
|
*/
|
|
139
140
|
version?: string;
|
|
140
141
|
|
|
141
142
|
/**
|
|
142
143
|
* URL prefix for all routes (from createRouter({ basename })).
|
|
144
|
+
* Used only as a fallback when `appShellRef` is not supplied.
|
|
143
145
|
*/
|
|
144
146
|
basename?: string;
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Live app-shell ref. When provided, the context's `basename` and `version`
|
|
150
|
+
* properties become live getters that track app-switch updates without
|
|
151
|
+
* invalidating the memoized context value.
|
|
152
|
+
*/
|
|
153
|
+
appShellRef?: AppShellRef;
|
|
145
154
|
}
|
|
146
155
|
|
|
147
156
|
/**
|
|
@@ -175,6 +184,7 @@ export function NavigationProvider({
|
|
|
175
184
|
warmupEnabled,
|
|
176
185
|
version,
|
|
177
186
|
basename,
|
|
187
|
+
appShellRef,
|
|
178
188
|
}: NavigationProviderProps): ReactNode {
|
|
179
189
|
// Track current payload for rendering (this triggers re-renders)
|
|
180
190
|
const [payload, setPayload] = useState(initialPayload);
|
|
@@ -196,18 +206,39 @@ export function NavigationProvider({
|
|
|
196
206
|
await bridge.refresh();
|
|
197
207
|
}, []);
|
|
198
208
|
|
|
199
|
-
// Context value is stable (store, eventController, navigate, refresh never
|
|
200
|
-
|
|
201
|
-
|
|
209
|
+
// Context value is stable (store, eventController, navigate, refresh never
|
|
210
|
+
// change). When an appShellRef is supplied, `basename` and `version` are
|
|
211
|
+
// installed as live getters so app-switch transitions (which update the ref)
|
|
212
|
+
// propagate to consumers without forcing a tree-wide rerender.
|
|
213
|
+
const contextValue = useMemo<NavigationStoreContextValue>(() => {
|
|
214
|
+
if (appShellRef) {
|
|
215
|
+
const value = {
|
|
216
|
+
store,
|
|
217
|
+
eventController,
|
|
218
|
+
navigate,
|
|
219
|
+
refresh,
|
|
220
|
+
} as NavigationStoreContextValue;
|
|
221
|
+
Object.defineProperty(value, "basename", {
|
|
222
|
+
configurable: true,
|
|
223
|
+
enumerable: true,
|
|
224
|
+
get: () => appShellRef.get().basename,
|
|
225
|
+
});
|
|
226
|
+
Object.defineProperty(value, "version", {
|
|
227
|
+
configurable: true,
|
|
228
|
+
enumerable: true,
|
|
229
|
+
get: () => appShellRef.get().version,
|
|
230
|
+
});
|
|
231
|
+
return value;
|
|
232
|
+
}
|
|
233
|
+
return {
|
|
202
234
|
store,
|
|
203
235
|
eventController,
|
|
204
236
|
navigate,
|
|
205
237
|
refresh,
|
|
206
238
|
version,
|
|
207
239
|
basename,
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
);
|
|
240
|
+
};
|
|
241
|
+
}, []);
|
|
211
242
|
|
|
212
243
|
// Connection warmup: keep TLS alive after idle periods.
|
|
213
244
|
// After 60s of no user interaction, marks connection as "cold".
|
|
@@ -402,7 +433,11 @@ export function NavigationProvider({
|
|
|
402
433
|
// Build the content tree
|
|
403
434
|
let content = <RootErrorBoundary>{root}</RootErrorBoundary>;
|
|
404
435
|
|
|
405
|
-
// Wrap with ThemeProvider when theme is enabled
|
|
436
|
+
// Wrap with ThemeProvider when theme is enabled. The ThemeProvider is
|
|
437
|
+
// document-lifetime: its config comes from the initial load and does NOT
|
|
438
|
+
// swap on cross-app transitions, because the ThemeProvider sits above the
|
|
439
|
+
// segment tree and a smooth (no-reload) app switch cannot safely remount
|
|
440
|
+
// it. A new theme config only takes effect on a full document load.
|
|
406
441
|
if (themeConfig) {
|
|
407
442
|
content = (
|
|
408
443
|
<ThemeProvider config={themeConfig} initialTheme={initialTheme}>
|
|
@@ -13,6 +13,10 @@ import type { RouterInstance, RouterNavigateOptions } from "../types.js";
|
|
|
13
13
|
* useRouter() do not re-render on navigation state changes.
|
|
14
14
|
* For reactive navigation state, use useNavigation() instead.
|
|
15
15
|
*
|
|
16
|
+
* Methods read `basename` from the live context on each call so that
|
|
17
|
+
* cross-app navigation (app-switch) sees the current app's basename
|
|
18
|
+
* rather than the one captured at mount time.
|
|
19
|
+
*
|
|
16
20
|
* @example
|
|
17
21
|
* ```tsx
|
|
18
22
|
* const router = useRouter();
|
|
@@ -29,7 +33,10 @@ export function useRouter(): RouterInstance {
|
|
|
29
33
|
throw new Error("useRouter must be used within NavigationProvider");
|
|
30
34
|
}
|
|
31
35
|
|
|
32
|
-
// Stable reference: ctx is
|
|
36
|
+
// Stable reference: ctx itself is stable, and reads on each method call
|
|
37
|
+
// pick up live basename values from the context (backed by a live ref
|
|
38
|
+
// in NavigationProvider), so app-switch transitions are reflected without
|
|
39
|
+
// recreating this object.
|
|
33
40
|
return useMemo<RouterInstance>(() => {
|
|
34
41
|
/** Prefix a root-relative path with basename if not already prefixed. */
|
|
35
42
|
function withBasename(url: string): string {
|
|
@@ -28,6 +28,7 @@ import {
|
|
|
28
28
|
isInterceptSegment,
|
|
29
29
|
splitInterceptSegments,
|
|
30
30
|
} from "./intercept-utils.js";
|
|
31
|
+
import { createAppShellRef } from "./app-shell.js";
|
|
31
32
|
|
|
32
33
|
// Vite HMR types are provided by vite/client
|
|
33
34
|
|
|
@@ -114,6 +115,13 @@ export interface BrowserAppContext {
|
|
|
114
115
|
warmupEnabled?: boolean;
|
|
115
116
|
/** App version for prefetch version mismatch detection */
|
|
116
117
|
version?: string;
|
|
118
|
+
/**
|
|
119
|
+
* Live app-shell ref. Cross-app navigations replace its contents so the
|
|
120
|
+
* NavigationProvider and renderSegments pick up the target app's
|
|
121
|
+
* rootLayout, basename, and version without consumer rerenders. Theme,
|
|
122
|
+
* warmup, and prefetch TTL are document-lifetime (see AppShell).
|
|
123
|
+
*/
|
|
124
|
+
appShellRef?: import("./app-shell.js").AppShellRef;
|
|
117
125
|
}
|
|
118
126
|
|
|
119
127
|
// Module-level state for the initialized app
|
|
@@ -204,13 +212,23 @@ export async function initBrowserApp(
|
|
|
204
212
|
// Create composable utilities
|
|
205
213
|
const client = createNavigationClient(deps);
|
|
206
214
|
|
|
207
|
-
//
|
|
208
|
-
|
|
215
|
+
// Capture the per-router app-shell so cross-app navigations can replace
|
|
216
|
+
// it atomically. rootLayout, basename, and version live here and are
|
|
217
|
+
// read through the ref at call time rather than closed over. Theme,
|
|
218
|
+
// warmup, and prefetch TTL are deliberately excluded — they are
|
|
219
|
+
// document-lifetime and stay stable across smooth cross-app transitions.
|
|
209
220
|
const version = initialPayload.metadata?.version;
|
|
221
|
+
const appShellRef = createAppShellRef({
|
|
222
|
+
routerId: initialPayload.metadata?.routerId,
|
|
223
|
+
rootLayout: initialPayload.metadata?.rootLayout,
|
|
224
|
+
basename: initialPayload.metadata?.basename,
|
|
225
|
+
version,
|
|
226
|
+
});
|
|
210
227
|
|
|
211
228
|
// Initialize the localStorage state key for cache invalidation.
|
|
212
|
-
//
|
|
213
|
-
|
|
229
|
+
// The build version busts cached prefetches on deploy; the routerId
|
|
230
|
+
// namespaces the key so sibling apps on the same origin don't collide.
|
|
231
|
+
initRangoState(version ?? "0", initialPayload.metadata?.routerId);
|
|
214
232
|
setAppVersion(version);
|
|
215
233
|
|
|
216
234
|
// Initialize the in-memory prefetch cache TTL from server config.
|
|
@@ -220,11 +238,17 @@ export async function initBrowserApp(
|
|
|
220
238
|
initPrefetchCache(prefetchCacheTTL);
|
|
221
239
|
}
|
|
222
240
|
|
|
223
|
-
// Create a bound renderSegments that
|
|
241
|
+
// Create a bound renderSegments that reads rootLayout through the shell
|
|
242
|
+
// ref. On app switch the ref is updated before the tree re-renders, so
|
|
243
|
+
// the new app's Document (rootLayout) replaces the previous one.
|
|
224
244
|
const renderSegments = (
|
|
225
245
|
segments: ResolvedSegment[],
|
|
226
246
|
options?: RenderSegmentsOptions,
|
|
227
|
-
) =>
|
|
247
|
+
) =>
|
|
248
|
+
baseRenderSegments(segments, {
|
|
249
|
+
...options,
|
|
250
|
+
rootLayout: appShellRef.get().rootLayout,
|
|
251
|
+
});
|
|
228
252
|
|
|
229
253
|
// Lazy reference for navigation bridge — the action bridge is created first
|
|
230
254
|
// but may need to trigger SPA navigation for action redirects.
|
|
@@ -256,6 +280,7 @@ export async function initBrowserApp(
|
|
|
256
280
|
onUpdate: (update) => store.emitUpdate(update),
|
|
257
281
|
renderSegments,
|
|
258
282
|
version: version,
|
|
283
|
+
appShellRef,
|
|
259
284
|
});
|
|
260
285
|
|
|
261
286
|
// Connect action redirect → navigation bridge (now that both are initialized)
|
|
@@ -416,6 +441,7 @@ export async function initBrowserApp(
|
|
|
416
441
|
initialTheme: effectiveInitialTheme,
|
|
417
442
|
warmupEnabled: initialPayload.metadata?.warmupEnabled ?? true,
|
|
418
443
|
version,
|
|
444
|
+
appShellRef,
|
|
419
445
|
};
|
|
420
446
|
browserAppContext = context;
|
|
421
447
|
|
|
@@ -481,6 +507,7 @@ export function RSCRouter(_props: RSCRouterProps): React.ReactElement {
|
|
|
481
507
|
initialTheme,
|
|
482
508
|
warmupEnabled,
|
|
483
509
|
version,
|
|
510
|
+
appShellRef,
|
|
484
511
|
} = getBrowserAppContext();
|
|
485
512
|
|
|
486
513
|
// Signal that the React tree has hydrated. useEffect only fires after
|
|
@@ -501,6 +528,7 @@ export function RSCRouter(_props: RSCRouterProps): React.ReactElement {
|
|
|
501
528
|
warmupEnabled={warmupEnabled}
|
|
502
529
|
version={version}
|
|
503
530
|
basename={initialPayload.metadata?.basename}
|
|
531
|
+
appShellRef={appShellRef}
|
|
504
532
|
/>
|
|
505
533
|
);
|
|
506
534
|
}
|
package/src/browser/types.ts
CHANGED
|
@@ -427,6 +427,12 @@ export interface NavigationStore {
|
|
|
427
427
|
markCacheAsStale(): void;
|
|
428
428
|
markCacheAsStaleAndBroadcast(): void;
|
|
429
429
|
clearHistoryCache(): void;
|
|
430
|
+
/**
|
|
431
|
+
* Clear this tab's nav + prefetch caches without broadcasting or rotating
|
|
432
|
+
* shared state. Intended for app-switch transitions that affect only this
|
|
433
|
+
* tab's session.
|
|
434
|
+
*/
|
|
435
|
+
clearHistoryCacheLocal(): void;
|
|
430
436
|
broadcastCacheInvalidation(): void;
|
|
431
437
|
|
|
432
438
|
// Cross-tab refresh callback (set by navigation bridge)
|
|
@@ -542,6 +548,13 @@ export interface NavigationBridge {
|
|
|
542
548
|
registerLinkInterception(): () => void;
|
|
543
549
|
/** Update the RSC version (e.g. after HMR). Clears prefetch cache. */
|
|
544
550
|
updateVersion(newVersion: string): void;
|
|
551
|
+
/**
|
|
552
|
+
* Replace the active app-shell snapshot (rootLayout, basename, version)
|
|
553
|
+
* atomically. Used on cross-app navigations when the response's routerId
|
|
554
|
+
* indicates the user entered a different app. Theme, warmup, and prefetch
|
|
555
|
+
* TTL are document-lifetime and not part of the shell.
|
|
556
|
+
*/
|
|
557
|
+
updateAppShell(next: import("./app-shell.js").AppShell): void;
|
|
545
558
|
}
|
|
546
559
|
|
|
547
560
|
/**
|
|
@@ -67,13 +67,11 @@ export const MAX_REVALIDATION_INTERVAL = 30;
|
|
|
67
67
|
// Types
|
|
68
68
|
// ============================================================================
|
|
69
69
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
export
|
|
74
|
-
|
|
75
|
-
passThroughOnException(): void;
|
|
76
|
-
}
|
|
70
|
+
// Re-exported from the canonical home so cf-cache-store consumers keep
|
|
71
|
+
// importing `ExecutionContext` from this module without a second interface
|
|
72
|
+
// drifting over time.
|
|
73
|
+
export type { ExecutionContext } from "../../types/request-scope.js";
|
|
74
|
+
import type { ExecutionContext } from "../../types/request-scope.js";
|
|
77
75
|
|
|
78
76
|
/**
|
|
79
77
|
* Minimal Cloudflare KV Namespace interface.
|
package/src/index.rsc.ts
CHANGED
|
@@ -172,6 +172,9 @@ export type { PublicRequestContext as RequestContext } from "./server/request-co
|
|
|
172
172
|
import type { PublicRequestContext } from "./server/request-context.js";
|
|
173
173
|
import type { DefaultEnv } from "./types/global-namespace.js";
|
|
174
174
|
|
|
175
|
+
// Shared base for every user-facing request context (mirrors index.ts).
|
|
176
|
+
export type { RequestScope, ExecutionContext } from "./types/request-scope.js";
|
|
177
|
+
|
|
175
178
|
export const getRequestContext: <
|
|
176
179
|
TEnv = DefaultEnv,
|
|
177
180
|
>() => PublicRequestContext<TEnv> = _getRequestContextInternal;
|
package/src/index.ts
CHANGED
|
@@ -264,6 +264,9 @@ export function transition(): never {
|
|
|
264
264
|
// Request context type (safe for client)
|
|
265
265
|
export type { PublicRequestContext as RequestContext } from "./server/request-context.js";
|
|
266
266
|
|
|
267
|
+
// Shared base for every user-facing request context.
|
|
268
|
+
export type { RequestScope, ExecutionContext } from "./types/request-scope.js";
|
|
269
|
+
|
|
267
270
|
// Cookie store types (safe for client)
|
|
268
271
|
export type {
|
|
269
272
|
CookieStore,
|
package/src/outlet-context.ts
CHANGED
package/src/reverse.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { ExtractParams } from "./types.js";
|
|
2
2
|
import type { SearchSchema, ResolveSearchSchema } from "./search-params.js";
|
|
3
3
|
import { serializeSearchParams } from "./search-params.js";
|
|
4
|
+
import { encodePathSegment } from "./router/url-params.js";
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* Sanitize prefix string by removing leading slash
|
|
@@ -318,7 +319,7 @@ export function createReverse<TRoutes extends Record<string, string>>(
|
|
|
318
319
|
hadOmittedOptional = true;
|
|
319
320
|
return "";
|
|
320
321
|
}
|
|
321
|
-
return
|
|
322
|
+
return encodePathSegment(value);
|
|
322
323
|
},
|
|
323
324
|
);
|
|
324
325
|
// Second pass: required params (no trailing ?)
|
|
@@ -329,7 +330,7 @@ export function createReverse<TRoutes extends Record<string, string>>(
|
|
|
329
330
|
if (value === undefined) {
|
|
330
331
|
throw new Error(`Missing param "${key}" for route "${name}"`);
|
|
331
332
|
}
|
|
332
|
-
return
|
|
333
|
+
return encodePathSegment(value);
|
|
333
334
|
},
|
|
334
335
|
);
|
|
335
336
|
// Clean up slashes only when an optional param was actually omitted,
|
|
@@ -18,6 +18,8 @@ import { isInsideCacheScope } from "../server/context.js";
|
|
|
18
18
|
import { NOCACHE_SYMBOL, assertNotInsideCacheExec } from "../cache/taint.js";
|
|
19
19
|
import { isAutoGeneratedRouteName } from "../route-name.js";
|
|
20
20
|
import { PRERENDER_PASSTHROUGH } from "../prerender.js";
|
|
21
|
+
import { encodePathSegment } from "./url-params.js";
|
|
22
|
+
import { fireAndForgetWaitUntil } from "../types/request-scope.js";
|
|
21
23
|
|
|
22
24
|
/**
|
|
23
25
|
* Strip internal _rsc* query params from a URL.
|
|
@@ -181,7 +183,7 @@ export function createReverseFunction(
|
|
|
181
183
|
hadOmittedOptional = true;
|
|
182
184
|
return "";
|
|
183
185
|
}
|
|
184
|
-
return
|
|
186
|
+
return encodePathSegment(value);
|
|
185
187
|
},
|
|
186
188
|
);
|
|
187
189
|
// Second pass: required params (no trailing ?)
|
|
@@ -192,7 +194,7 @@ export function createReverseFunction(
|
|
|
192
194
|
if (value === undefined) {
|
|
193
195
|
throw new Error(`Missing param "${key}" for route "${name}"`);
|
|
194
196
|
}
|
|
195
|
-
return
|
|
197
|
+
return encodePathSegment(value);
|
|
196
198
|
},
|
|
197
199
|
);
|
|
198
200
|
// Clean up slashes only when an optional param was actually omitted,
|
|
@@ -281,8 +283,12 @@ export function createHandlerContext<TEnv>(
|
|
|
281
283
|
search: searchSchema ? resolvedSearchParams : {},
|
|
282
284
|
pathname,
|
|
283
285
|
url,
|
|
284
|
-
originalUrl: new URL(request.url),
|
|
286
|
+
originalUrl: requestContext?.originalUrl ?? new URL(request.url),
|
|
285
287
|
env: bindings,
|
|
288
|
+
waitUntil: requestContext
|
|
289
|
+
? requestContext.waitUntil.bind(requestContext)
|
|
290
|
+
: fireAndForgetWaitUntil,
|
|
291
|
+
executionContext: requestContext?.executionContext,
|
|
286
292
|
_variables: variables,
|
|
287
293
|
get: ((keyOrVar: any) => {
|
|
288
294
|
// Read-time guard: non-cacheable var inside cache() → throw.
|
|
@@ -387,6 +393,12 @@ export function createPrerenderContext<TEnv>(
|
|
|
387
393
|
"Configure buildEnv in your rango() plugin options to enable build-time env access.",
|
|
388
394
|
);
|
|
389
395
|
},
|
|
396
|
+
// Build-time prerender has no live request. waitUntil is a true no-op
|
|
397
|
+
// (running fn() here would fire side effects during build, which is
|
|
398
|
+
// incorrect — these are meant to outlive the live response).
|
|
399
|
+
// executionContext is absent for the same reason.
|
|
400
|
+
waitUntil: () => {},
|
|
401
|
+
executionContext: undefined,
|
|
390
402
|
_variables: variables,
|
|
391
403
|
get: ((keyOrVar: any) => contextGet(variables, keyOrVar)) as any,
|
|
392
404
|
set: ((keyOrVar: any, value: any) => {
|
|
@@ -476,6 +488,11 @@ export function createStaticContext<TEnv>(
|
|
|
476
488
|
"Configure buildEnv in your rango() plugin options to enable build-time env access.",
|
|
477
489
|
);
|
|
478
490
|
},
|
|
491
|
+
// Static() handlers have no live request. waitUntil is a true no-op
|
|
492
|
+
// (running fn() here would fire side effects during build, which is
|
|
493
|
+
// incorrect). executionContext is absent for the same reason.
|
|
494
|
+
waitUntil: () => {},
|
|
495
|
+
executionContext: undefined,
|
|
479
496
|
_variables: variables,
|
|
480
497
|
get: ((keyOrVar: any) => contextGet(variables, keyOrVar)) as any,
|
|
481
498
|
set: ((keyOrVar: any, value: any) => {
|
|
@@ -266,7 +266,10 @@ function createLoaderExecutor<TEnv>(
|
|
|
266
266
|
search: (ctx as any).search,
|
|
267
267
|
pathname: ctx.pathname,
|
|
268
268
|
url: ctx.url,
|
|
269
|
+
originalUrl: ctx.originalUrl,
|
|
269
270
|
env: ctx.env,
|
|
271
|
+
waitUntil: ctx.waitUntil.bind(ctx),
|
|
272
|
+
executionContext: ctx.executionContext,
|
|
270
273
|
get: ((keyOrVar: any) =>
|
|
271
274
|
contextGet(variables, keyOrVar)) as typeof ctx.get,
|
|
272
275
|
use: ((item: LoaderDefinition<any, any> | Handle<any, any>) => {
|