@rangojs/router 0.0.0-experimental.debug-cache-fix → 0.0.0-experimental.dfdb0387
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 +76 -18
- package/dist/bin/rango.js +130 -47
- package/dist/vite/index.js +702 -231
- package/package.json +2 -2
- package/skills/cache-guide/SKILL.md +32 -0
- package/skills/caching/SKILL.md +8 -0
- package/skills/links/SKILL.md +3 -1
- package/skills/loader/SKILL.md +53 -43
- package/skills/middleware/SKILL.md +2 -0
- package/skills/prerender/SKILL.md +110 -68
- package/skills/route/SKILL.md +31 -0
- package/skills/router-setup/SKILL.md +87 -2
- package/skills/typesafety/SKILL.md +10 -0
- package/src/__internal.ts +1 -1
- package/src/browser/app-version.ts +14 -0
- package/src/browser/navigation-bridge.ts +16 -3
- package/src/browser/navigation-client.ts +98 -46
- package/src/browser/navigation-store.ts +43 -8
- package/src/browser/partial-update.ts +32 -5
- package/src/browser/prefetch/cache.ts +16 -6
- package/src/browser/prefetch/fetch.ts +52 -6
- package/src/browser/prefetch/queue.ts +61 -29
- package/src/browser/prefetch/resource-ready.ts +77 -0
- package/src/browser/react/Link.tsx +67 -8
- package/src/browser/react/NavigationProvider.tsx +13 -4
- package/src/browser/react/context.ts +7 -2
- package/src/browser/react/use-handle.ts +9 -58
- package/src/browser/react/use-router.ts +21 -8
- package/src/browser/rsc-router.tsx +26 -3
- package/src/browser/scroll-restoration.ts +10 -8
- package/src/browser/segment-reconciler.ts +26 -0
- package/src/browser/server-action-bridge.ts +8 -6
- package/src/browser/types.ts +27 -5
- package/src/build/generate-manifest.ts +6 -6
- package/src/build/generate-route-types.ts +3 -0
- package/src/build/route-types/include-resolution.ts +8 -1
- package/src/build/route-types/router-processing.ts +211 -72
- package/src/build/route-types/scan-filter.ts +8 -1
- package/src/cache/cache-scope.ts +12 -14
- package/src/cache/taint.ts +55 -0
- package/src/client.tsx +2 -56
- package/src/context-var.ts +72 -2
- package/src/handle.ts +40 -0
- package/src/index.rsc.ts +3 -1
- package/src/index.ts +12 -0
- package/src/prerender/store.ts +5 -4
- package/src/prerender.ts +138 -77
- package/src/reverse.ts +22 -1
- package/src/route-definition/dsl-helpers.ts +42 -19
- package/src/route-definition/helpers-types.ts +10 -6
- package/src/route-definition/index.ts +3 -0
- package/src/route-definition/redirect.ts +9 -1
- package/src/route-definition/resolve-handler-use.ts +149 -0
- package/src/route-types.ts +11 -0
- package/src/router/content-negotiation.ts +100 -1
- package/src/router/handler-context.ts +79 -23
- package/src/router/intercept-resolution.ts +9 -4
- package/src/router/loader-resolution.ts +156 -21
- package/src/router/match-api.ts +124 -189
- package/src/router/match-middleware/cache-lookup.ts +26 -7
- package/src/router/match-middleware/segment-resolution.ts +53 -0
- package/src/router/match-result.ts +82 -4
- package/src/router/middleware-types.ts +6 -8
- package/src/router/middleware.ts +2 -5
- package/src/router/navigation-snapshot.ts +182 -0
- package/src/router/prerender-match.ts +110 -10
- package/src/router/preview-match.ts +30 -102
- package/src/router/request-classification.ts +310 -0
- package/src/router/route-snapshot.ts +245 -0
- package/src/router/router-interfaces.ts +36 -4
- package/src/router/router-options.ts +37 -11
- package/src/router/segment-resolution/fresh.ts +80 -9
- package/src/router/segment-resolution/helpers.ts +29 -24
- package/src/router/segment-resolution/revalidation.ts +91 -8
- package/src/router/types.ts +1 -0
- package/src/router.ts +54 -5
- package/src/rsc/handler.ts +472 -372
- package/src/rsc/loader-fetch.ts +23 -3
- package/src/rsc/manifest-init.ts +5 -1
- package/src/rsc/progressive-enhancement.ts +14 -2
- package/src/rsc/rsc-rendering.ts +10 -1
- package/src/rsc/server-action.ts +8 -0
- package/src/rsc/ssr-setup.ts +2 -2
- package/src/rsc/types.ts +9 -1
- package/src/server/context.ts +50 -1
- package/src/server/handle-store.ts +19 -0
- package/src/server/loader-registry.ts +9 -8
- package/src/server/request-context.ts +175 -15
- package/src/ssr/index.tsx +3 -0
- package/src/static-handler.ts +18 -6
- package/src/types/cache-types.ts +4 -4
- package/src/types/handler-context.ts +37 -19
- package/src/types/loader-types.ts +36 -9
- package/src/types/route-entry.ts +1 -1
- package/src/types/segments.ts +1 -0
- package/src/urls/path-helper-types.ts +9 -2
- package/src/urls/path-helper.ts +47 -12
- package/src/urls/pattern-types.ts +12 -0
- package/src/urls/response-types.ts +16 -6
- package/src/use-loader.tsx +77 -5
- package/src/vite/discovery/bundle-postprocess.ts +30 -33
- package/src/vite/discovery/discover-routers.ts +5 -1
- package/src/vite/discovery/prerender-collection.ts +128 -74
- package/src/vite/discovery/state.ts +13 -4
- package/src/vite/index.ts +4 -0
- package/src/vite/plugin-types.ts +60 -5
- package/src/vite/plugins/expose-id-utils.ts +12 -0
- package/src/vite/plugins/expose-ids/handler-transform.ts +30 -0
- package/src/vite/plugins/expose-internal-ids.ts +257 -40
- package/src/vite/plugins/performance-tracks.ts +88 -0
- package/src/vite/plugins/refresh-cmd.ts +88 -26
- package/src/vite/rango.ts +19 -2
- package/src/vite/router-discovery.ts +178 -37
- package/src/vite/utils/prerender-utils.ts +18 -0
- package/src/vite/utils/shared-utils.ts +3 -2
|
@@ -23,6 +23,7 @@ import {
|
|
|
23
23
|
import { getRangoState } from "../rango-state.js";
|
|
24
24
|
import { enqueuePrefetch } from "./queue.js";
|
|
25
25
|
import { shouldPrefetch } from "./policy.js";
|
|
26
|
+
import { debugLog } from "../logging.js";
|
|
26
27
|
|
|
27
28
|
/**
|
|
28
29
|
* Build an RSC partial URL for prefetching.
|
|
@@ -34,6 +35,7 @@ function buildPrefetchUrl(
|
|
|
34
35
|
url: string,
|
|
35
36
|
segmentIds: string[],
|
|
36
37
|
version?: string,
|
|
38
|
+
routerId?: string,
|
|
37
39
|
): URL | null {
|
|
38
40
|
let targetUrl: URL;
|
|
39
41
|
try {
|
|
@@ -51,6 +53,9 @@ function buildPrefetchUrl(
|
|
|
51
53
|
if (version) {
|
|
52
54
|
targetUrl.searchParams.set("_rsc_v", version);
|
|
53
55
|
}
|
|
56
|
+
if (routerId) {
|
|
57
|
+
targetUrl.searchParams.set("_rsc_rid", routerId);
|
|
58
|
+
}
|
|
54
59
|
return targetUrl;
|
|
55
60
|
}
|
|
56
61
|
|
|
@@ -108,13 +113,33 @@ export function prefetchDirect(
|
|
|
108
113
|
url: string,
|
|
109
114
|
segmentIds: string[],
|
|
110
115
|
version?: string,
|
|
116
|
+
routerId?: string,
|
|
117
|
+
prefetchKey?: string | ((from: string) => string),
|
|
111
118
|
): void {
|
|
112
119
|
if (!shouldPrefetch()) return;
|
|
113
120
|
|
|
114
|
-
const targetUrl = buildPrefetchUrl(url, segmentIds, version);
|
|
121
|
+
const targetUrl = buildPrefetchUrl(url, segmentIds, version, routerId);
|
|
115
122
|
if (!targetUrl) return;
|
|
116
|
-
|
|
117
|
-
|
|
123
|
+
// Skip same-page prefetch with prefetchKey — a same-page diff is trivial
|
|
124
|
+
// and would corrupt the wildcard cache entry for cross-page navigation.
|
|
125
|
+
if (prefetchKey != null && targetUrl.pathname === window.location.pathname) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
const key = buildPrefetchKey(window.location.href, targetUrl, prefetchKey);
|
|
129
|
+
if (hasPrefetch(key)) {
|
|
130
|
+
debugLog("[prefetch] direct dedup (key already exists)", {
|
|
131
|
+
url,
|
|
132
|
+
key,
|
|
133
|
+
prefetchKey: prefetchKey != null ? String(prefetchKey) : undefined,
|
|
134
|
+
});
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
debugLog("[prefetch] direct fetch", {
|
|
138
|
+
url,
|
|
139
|
+
key,
|
|
140
|
+
source: window.location.href,
|
|
141
|
+
prefetchKey: prefetchKey != null ? String(prefetchKey) : undefined,
|
|
142
|
+
});
|
|
118
143
|
executePrefetchFetch(key, targetUrl.toString());
|
|
119
144
|
}
|
|
120
145
|
|
|
@@ -127,17 +152,38 @@ export function prefetchQueued(
|
|
|
127
152
|
url: string,
|
|
128
153
|
segmentIds: string[],
|
|
129
154
|
version?: string,
|
|
155
|
+
routerId?: string,
|
|
156
|
+
prefetchKey?: string | ((from: string) => string),
|
|
130
157
|
): string {
|
|
131
158
|
if (!shouldPrefetch()) return "";
|
|
132
|
-
const targetUrl = buildPrefetchUrl(url, segmentIds, version);
|
|
159
|
+
const targetUrl = buildPrefetchUrl(url, segmentIds, version, routerId);
|
|
133
160
|
if (!targetUrl) return "";
|
|
134
|
-
|
|
135
|
-
|
|
161
|
+
// Skip same-page prefetch with prefetchKey — a same-page diff is trivial
|
|
162
|
+
// and would corrupt the wildcard cache entry for cross-page navigation.
|
|
163
|
+
if (prefetchKey != null && targetUrl.pathname === window.location.pathname) {
|
|
164
|
+
return "";
|
|
165
|
+
}
|
|
166
|
+
const key = buildPrefetchKey(window.location.href, targetUrl, prefetchKey);
|
|
167
|
+
if (hasPrefetch(key)) {
|
|
168
|
+
debugLog("[prefetch] queued dedup (key already exists)", {
|
|
169
|
+
url,
|
|
170
|
+
key,
|
|
171
|
+
prefetchKey: prefetchKey != null ? String(prefetchKey) : undefined,
|
|
172
|
+
});
|
|
173
|
+
return key;
|
|
174
|
+
}
|
|
136
175
|
const fetchUrlStr = targetUrl.toString();
|
|
176
|
+
const targetPathname = targetUrl.pathname;
|
|
137
177
|
enqueuePrefetch(key, (signal) => {
|
|
138
178
|
// Re-check at execution time: a hover-triggered prefetchDirect may
|
|
139
179
|
// have started or completed this key while the item sat in the queue.
|
|
140
180
|
if (hasPrefetch(key)) return Promise.resolve();
|
|
181
|
+
// By execution time, the user may have navigated to the target page.
|
|
182
|
+
// A same-page prefetch produces a trivial diff that would overwrite
|
|
183
|
+
// the useful cross-page entry in the wildcard cache.
|
|
184
|
+
if (prefetchKey != null && targetPathname === window.location.pathname) {
|
|
185
|
+
return Promise.resolve();
|
|
186
|
+
}
|
|
141
187
|
return executePrefetchFetch(key, fetchUrlStr, signal).then(() => {});
|
|
142
188
|
});
|
|
143
189
|
return key;
|
|
@@ -5,21 +5,19 @@
|
|
|
5
5
|
* Hover prefetches bypass this queue — they fire directly for immediate response
|
|
6
6
|
* to user intent.
|
|
7
7
|
*
|
|
8
|
-
* Draining
|
|
9
|
-
*
|
|
10
|
-
*
|
|
8
|
+
* Draining waits for an idle main-thread moment and for viewport images to
|
|
9
|
+
* finish loading, so prefetch fetch() calls never compete with critical
|
|
10
|
+
* resources for the browser's connection pool.
|
|
11
11
|
*
|
|
12
12
|
* When a navigation starts, queued prefetches are cancelled but executing ones
|
|
13
13
|
* are left running. Navigation can reuse their in-flight responses via the
|
|
14
14
|
* prefetch cache's inflight promise map, avoiding duplicate requests.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
import { wait, waitForIdle, waitForViewportImages } from "./resource-ready.js";
|
|
18
18
|
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
? requestAnimationFrame
|
|
22
|
-
: (fn) => setTimeout(fn, 0);
|
|
19
|
+
const MAX_CONCURRENT = 2;
|
|
20
|
+
const IMAGE_WAIT_TIMEOUT = 2000;
|
|
23
21
|
|
|
24
22
|
let active = 0;
|
|
25
23
|
const queue: Array<{
|
|
@@ -28,8 +26,9 @@ const queue: Array<{
|
|
|
28
26
|
}> = [];
|
|
29
27
|
const queued = new Set<string>();
|
|
30
28
|
const executing = new Set<string>();
|
|
31
|
-
|
|
29
|
+
const abortControllers = new Map<string, AbortController>();
|
|
32
30
|
let drainScheduled = false;
|
|
31
|
+
let drainGeneration = 0;
|
|
33
32
|
|
|
34
33
|
function startExecution(
|
|
35
34
|
key: string,
|
|
@@ -37,8 +36,10 @@ function startExecution(
|
|
|
37
36
|
): void {
|
|
38
37
|
active++;
|
|
39
38
|
executing.add(key);
|
|
40
|
-
|
|
41
|
-
|
|
39
|
+
const ac = new AbortController();
|
|
40
|
+
abortControllers.set(key, ac);
|
|
41
|
+
execute(ac.signal).finally(() => {
|
|
42
|
+
abortControllers.delete(key);
|
|
42
43
|
// Only decrement if this key wasn't already cleared by cancelAllPrefetches.
|
|
43
44
|
// Without this guard, cancelled tasks' .finally() would underflow active
|
|
44
45
|
// below zero, breaking the MAX_CONCURRENT guarantee.
|
|
@@ -50,18 +51,32 @@ function startExecution(
|
|
|
50
51
|
}
|
|
51
52
|
|
|
52
53
|
/**
|
|
53
|
-
* Schedule a drain
|
|
54
|
-
* Coalesces multiple drain requests into a single
|
|
55
|
-
* batch completion doesn't schedule redundant
|
|
54
|
+
* Schedule a drain after the browser is idle and viewport images are loaded.
|
|
55
|
+
* Coalesces multiple drain requests into a single deferred callback so
|
|
56
|
+
* batch completion doesn't schedule redundant waits.
|
|
57
|
+
*
|
|
58
|
+
* The two-step wait ensures prefetch fetch() calls don't compete with
|
|
59
|
+
* images for the browser's connection pool:
|
|
60
|
+
* 1. waitForIdle — yield until the main thread has a quiet moment
|
|
61
|
+
* 2. waitForViewportImages OR 2s timeout — yield until visible images
|
|
62
|
+
* finish loading, but don't let slow/broken images block indefinitely
|
|
56
63
|
*/
|
|
57
64
|
function scheduleDrain(): void {
|
|
58
65
|
if (drainScheduled) return;
|
|
59
66
|
if (active >= MAX_CONCURRENT || queue.length === 0) return;
|
|
60
67
|
drainScheduled = true;
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
68
|
+
const gen = drainGeneration;
|
|
69
|
+
waitForIdle()
|
|
70
|
+
.then(() =>
|
|
71
|
+
Promise.race([waitForViewportImages(), wait(IMAGE_WAIT_TIMEOUT)]),
|
|
72
|
+
)
|
|
73
|
+
.then(() => {
|
|
74
|
+
drainScheduled = false;
|
|
75
|
+
// Stale drain: a cancel/abort happened while we were waiting.
|
|
76
|
+
// A fresh scheduleDrain will be called by whatever enqueues next.
|
|
77
|
+
if (gen !== drainGeneration) return;
|
|
78
|
+
if (queue.length > 0) drain();
|
|
79
|
+
});
|
|
65
80
|
}
|
|
66
81
|
|
|
67
82
|
function drain(): void {
|
|
@@ -74,9 +89,10 @@ function drain(): void {
|
|
|
74
89
|
|
|
75
90
|
/**
|
|
76
91
|
* Enqueue a prefetch for concurrency-limited execution.
|
|
77
|
-
* Execution is
|
|
78
|
-
*
|
|
79
|
-
* Deduplicates by key — items already queued or executing
|
|
92
|
+
* Execution is deferred until the browser is idle and viewport images
|
|
93
|
+
* have finished loading, so prefetches never compete with critical
|
|
94
|
+
* resources. Deduplicates by key — items already queued or executing
|
|
95
|
+
* are skipped.
|
|
80
96
|
*
|
|
81
97
|
* The executor receives an AbortSignal that is aborted when
|
|
82
98
|
* cancelAllPrefetches() is called (e.g. on navigation start).
|
|
@@ -93,19 +109,32 @@ export function enqueuePrefetch(
|
|
|
93
109
|
}
|
|
94
110
|
|
|
95
111
|
/**
|
|
96
|
-
* Cancel queued prefetches
|
|
97
|
-
* navigation
|
|
98
|
-
*
|
|
99
|
-
*
|
|
100
|
-
* with navigation fetches under HTTP/2 multiplexing.
|
|
112
|
+
* Cancel queued prefetches and abort in-flight ones that don't match
|
|
113
|
+
* the current navigation target. If `keepUrl` is provided, the
|
|
114
|
+
* executing prefetch whose key contains that URL is kept alive so
|
|
115
|
+
* navigation can reuse its response via consumeInflightPrefetch.
|
|
101
116
|
*
|
|
102
117
|
* Called when a navigation starts via the NavigationProvider's
|
|
103
118
|
* event controller subscription.
|
|
104
119
|
*/
|
|
105
|
-
export function cancelAllPrefetches(): void {
|
|
120
|
+
export function cancelAllPrefetches(keepUrl?: string | null): void {
|
|
106
121
|
queue.length = 0;
|
|
107
122
|
queued.clear();
|
|
108
123
|
drainScheduled = false;
|
|
124
|
+
drainGeneration++;
|
|
125
|
+
|
|
126
|
+
// Abort in-flight prefetches that aren't for the navigation target.
|
|
127
|
+
// Keys use format "sourceHref\0targetPathname+search" — match the
|
|
128
|
+
// target portion (after \0) against keepUrl.
|
|
129
|
+
for (const [key, ac] of abortControllers) {
|
|
130
|
+
const target = key.split("\0")[1];
|
|
131
|
+
if (keepUrl && target && keepUrl.startsWith(target)) continue;
|
|
132
|
+
ac.abort();
|
|
133
|
+
abortControllers.delete(key);
|
|
134
|
+
if (executing.delete(key)) {
|
|
135
|
+
active--;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
109
138
|
}
|
|
110
139
|
|
|
111
140
|
/**
|
|
@@ -114,8 +143,10 @@ export function cancelAllPrefetches(): void {
|
|
|
114
143
|
* in-flight responses would be stale.
|
|
115
144
|
*/
|
|
116
145
|
export function abortAllPrefetches(): void {
|
|
117
|
-
|
|
118
|
-
|
|
146
|
+
for (const ac of abortControllers.values()) {
|
|
147
|
+
ac.abort();
|
|
148
|
+
}
|
|
149
|
+
abortControllers.clear();
|
|
119
150
|
|
|
120
151
|
queue.length = 0;
|
|
121
152
|
queued.clear();
|
|
@@ -125,4 +156,5 @@ export function abortAllPrefetches(): void {
|
|
|
125
156
|
executing.clear();
|
|
126
157
|
active = 0;
|
|
127
158
|
drainScheduled = false;
|
|
159
|
+
drainGeneration++;
|
|
128
160
|
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resource Readiness
|
|
3
|
+
*
|
|
4
|
+
* Utilities to defer speculative prefetches until critical resources
|
|
5
|
+
* (viewport images) have finished loading. Prevents prefetch fetch()
|
|
6
|
+
* calls from competing with images for the browser's connection pool.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Resolve when all in-viewport images have finished loading.
|
|
11
|
+
* Returns immediately if no images are pending.
|
|
12
|
+
*
|
|
13
|
+
* Only checks images that exist at call time — does not observe
|
|
14
|
+
* dynamically added images. For SPA navigations where new images
|
|
15
|
+
* appear after render, call this after the navigation settles.
|
|
16
|
+
*/
|
|
17
|
+
export function waitForViewportImages(): Promise<void> {
|
|
18
|
+
if (typeof document === "undefined") return Promise.resolve();
|
|
19
|
+
|
|
20
|
+
const pending = Array.from(document.querySelectorAll("img")).filter((img) => {
|
|
21
|
+
if (img.complete) return false;
|
|
22
|
+
const rect = img.getBoundingClientRect();
|
|
23
|
+
return (
|
|
24
|
+
rect.bottom > 0 &&
|
|
25
|
+
rect.right > 0 &&
|
|
26
|
+
rect.top < window.innerHeight &&
|
|
27
|
+
rect.left < window.innerWidth
|
|
28
|
+
);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
if (pending.length === 0) return Promise.resolve();
|
|
32
|
+
|
|
33
|
+
return new Promise((resolve) => {
|
|
34
|
+
const settled = new Set<HTMLImageElement>();
|
|
35
|
+
|
|
36
|
+
const settle = (img: HTMLImageElement) => {
|
|
37
|
+
if (settled.has(img)) return;
|
|
38
|
+
settled.add(img);
|
|
39
|
+
if (settled.size >= pending.length) resolve();
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
for (const img of pending) {
|
|
43
|
+
img.addEventListener("load", () => settle(img), { once: true });
|
|
44
|
+
img.addEventListener("error", () => settle(img), { once: true });
|
|
45
|
+
// Re-check: image may have completed between the initial filter
|
|
46
|
+
// and listener attachment. settle() is idempotent per image, so
|
|
47
|
+
// a queued load event firing afterward is harmless.
|
|
48
|
+
if (img.complete) settle(img);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Resolve after the given number of milliseconds.
|
|
55
|
+
*/
|
|
56
|
+
export function wait(ms: number): Promise<void> {
|
|
57
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Resolve when the browser has an idle main-thread moment.
|
|
62
|
+
* Uses requestIdleCallback where available, falls back to setTimeout.
|
|
63
|
+
*
|
|
64
|
+
* This is a scheduling hint, not an asset-loaded detector — combine
|
|
65
|
+
* with waitForViewportImages() for full resource readiness.
|
|
66
|
+
*/
|
|
67
|
+
export function waitForIdle(timeout = 200): Promise<void> {
|
|
68
|
+
if (typeof window !== "undefined" && "requestIdleCallback" in window) {
|
|
69
|
+
return new Promise((resolve) => {
|
|
70
|
+
window.requestIdleCallback(() => resolve(), { timeout });
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return new Promise((resolve) => {
|
|
75
|
+
setTimeout(resolve, 0);
|
|
76
|
+
});
|
|
77
|
+
}
|
|
@@ -5,6 +5,7 @@ import React, {
|
|
|
5
5
|
useCallback,
|
|
6
6
|
useContext,
|
|
7
7
|
useEffect,
|
|
8
|
+
useMemo,
|
|
8
9
|
useRef,
|
|
9
10
|
type ForwardRefExoticComponent,
|
|
10
11
|
type RefAttributes,
|
|
@@ -32,6 +33,7 @@ export type LinkState =
|
|
|
32
33
|
| StateOrGetter<Record<string, unknown>>;
|
|
33
34
|
|
|
34
35
|
import { prefetchDirect, prefetchQueued } from "../prefetch/fetch.js";
|
|
36
|
+
import { getAppVersion } from "../app-version.js";
|
|
35
37
|
import {
|
|
36
38
|
observeForPrefetch,
|
|
37
39
|
unobserveForPrefetch,
|
|
@@ -95,6 +97,26 @@ export interface LinkProps extends Omit<
|
|
|
95
97
|
* @default "none"
|
|
96
98
|
*/
|
|
97
99
|
prefetch?: PrefetchStrategy;
|
|
100
|
+
/**
|
|
101
|
+
* Custom prefetch cache key for source-agnostic cache reuse.
|
|
102
|
+
* When set, prefetch responses are cached independently of the current
|
|
103
|
+
* page URL, so navigating to the same target from different source pages
|
|
104
|
+
* reuses the cached prefetch.
|
|
105
|
+
*
|
|
106
|
+
* - String: static group name (e.g., `"pages"`)
|
|
107
|
+
* - Function: receives current URL (`window.location.href`), returns a
|
|
108
|
+
* normalized source key
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* ```tsx
|
|
112
|
+
* // Static group — all "pages" links share one cache entry per target
|
|
113
|
+
* <Link to="/page/3" prefetch="hover" prefetchKey="pages" />
|
|
114
|
+
*
|
|
115
|
+
* // Normalize — strip trailing page number from source URL
|
|
116
|
+
* <Link to="/page/3" prefetch="hover" prefetchKey={(from) => from.replace(/\/\d+$/, '')} />
|
|
117
|
+
* ```
|
|
118
|
+
*/
|
|
119
|
+
prefetchKey?: string | ((from: string) => string);
|
|
98
120
|
/**
|
|
99
121
|
* State to pass to history.pushState/replaceState.
|
|
100
122
|
* Accessible via useLocationState() hook.
|
|
@@ -182,6 +204,7 @@ export const Link: ForwardRefExoticComponent<
|
|
|
182
204
|
reloadDocument = false,
|
|
183
205
|
revalidate,
|
|
184
206
|
prefetch = "none",
|
|
207
|
+
prefetchKey,
|
|
185
208
|
state,
|
|
186
209
|
children,
|
|
187
210
|
onClick,
|
|
@@ -192,6 +215,16 @@ export const Link: ForwardRefExoticComponent<
|
|
|
192
215
|
const ctx = useContext(NavigationStoreContext);
|
|
193
216
|
const isExternal = isExternalUrl(to);
|
|
194
217
|
|
|
218
|
+
// Auto-prefix with basename for app-local paths.
|
|
219
|
+
// Skip if external, already prefixed, or not a root-relative path.
|
|
220
|
+
const resolvedTo = useMemo(() => {
|
|
221
|
+
if (isExternal) return to;
|
|
222
|
+
const bn = ctx?.basename;
|
|
223
|
+
if (!bn || !to.startsWith("/") || to.startsWith(bn + "/") || to === bn)
|
|
224
|
+
return to;
|
|
225
|
+
return to === "/" ? bn : bn + to;
|
|
226
|
+
}, [to, isExternal, ctx?.basename]);
|
|
227
|
+
|
|
195
228
|
// Resolve adaptive: viewport on touch devices, hover on pointer devices
|
|
196
229
|
const resolvedStrategy =
|
|
197
230
|
prefetch === "adaptive" ? (isTouchDevice ? "viewport" : "hover") : prefetch;
|
|
@@ -273,9 +306,23 @@ export const Link: ForwardRefExoticComponent<
|
|
|
273
306
|
resolvedState = currentState;
|
|
274
307
|
}
|
|
275
308
|
|
|
276
|
-
ctx.navigate(
|
|
309
|
+
ctx.navigate(resolvedTo, {
|
|
310
|
+
replace,
|
|
311
|
+
scroll,
|
|
312
|
+
state: resolvedState,
|
|
313
|
+
revalidate,
|
|
314
|
+
});
|
|
277
315
|
},
|
|
278
|
-
[
|
|
316
|
+
[
|
|
317
|
+
resolvedTo,
|
|
318
|
+
isExternal,
|
|
319
|
+
reloadDocument,
|
|
320
|
+
replace,
|
|
321
|
+
scroll,
|
|
322
|
+
revalidate,
|
|
323
|
+
ctx,
|
|
324
|
+
onClick,
|
|
325
|
+
],
|
|
279
326
|
);
|
|
280
327
|
|
|
281
328
|
const handleMouseEnter = useCallback(() => {
|
|
@@ -289,9 +336,15 @@ export const Link: ForwardRefExoticComponent<
|
|
|
289
336
|
// prefetch — prefetchDirect bypasses the queue, and hasPrefetch
|
|
290
337
|
// deduplicates if the viewport prefetch already completed.
|
|
291
338
|
const segmentState = ctx.store.getSegmentState();
|
|
292
|
-
prefetchDirect(
|
|
339
|
+
prefetchDirect(
|
|
340
|
+
resolvedTo,
|
|
341
|
+
segmentState.currentSegmentIds,
|
|
342
|
+
getAppVersion(),
|
|
343
|
+
ctx.store.getRouterId?.(),
|
|
344
|
+
prefetchKey,
|
|
345
|
+
);
|
|
293
346
|
}
|
|
294
|
-
}, [resolvedStrategy,
|
|
347
|
+
}, [resolvedStrategy, resolvedTo, isExternal, ctx, prefetchKey]);
|
|
295
348
|
|
|
296
349
|
// Viewport/render prefetch: waits for idle before starting,
|
|
297
350
|
// uses concurrency-limited queue to avoid flooding.
|
|
@@ -308,7 +361,13 @@ export const Link: ForwardRefExoticComponent<
|
|
|
308
361
|
const triggerPrefetch = () => {
|
|
309
362
|
if (cancelled) return;
|
|
310
363
|
const segmentState = ctx.store.getSegmentState();
|
|
311
|
-
prefetchQueued(
|
|
364
|
+
prefetchQueued(
|
|
365
|
+
resolvedTo,
|
|
366
|
+
segmentState.currentSegmentIds,
|
|
367
|
+
getAppVersion(),
|
|
368
|
+
ctx.store.getRouterId?.(),
|
|
369
|
+
prefetchKey,
|
|
370
|
+
);
|
|
312
371
|
};
|
|
313
372
|
|
|
314
373
|
// Schedule prefetch only when the app is idle (no navigation/streaming).
|
|
@@ -347,12 +406,12 @@ export const Link: ForwardRefExoticComponent<
|
|
|
347
406
|
unobserveForPrefetch(observedElement);
|
|
348
407
|
}
|
|
349
408
|
};
|
|
350
|
-
}, [resolvedStrategy,
|
|
409
|
+
}, [resolvedStrategy, resolvedTo, isExternal, ctx, prefetchKey]);
|
|
351
410
|
|
|
352
411
|
return (
|
|
353
412
|
<a
|
|
354
413
|
ref={setRef}
|
|
355
|
-
href={
|
|
414
|
+
href={resolvedTo}
|
|
356
415
|
onClick={handleClick}
|
|
357
416
|
onMouseEnter={handleMouseEnter}
|
|
358
417
|
data-link-component
|
|
@@ -362,7 +421,7 @@ export const Link: ForwardRefExoticComponent<
|
|
|
362
421
|
data-revalidate={revalidate === false ? "false" : undefined}
|
|
363
422
|
{...props}
|
|
364
423
|
>
|
|
365
|
-
<LinkContext.Provider value={
|
|
424
|
+
<LinkContext.Provider value={resolvedTo}>{children}</LinkContext.Provider>
|
|
366
425
|
</a>
|
|
367
426
|
);
|
|
368
427
|
});
|
|
@@ -134,9 +134,14 @@ export interface NavigationProviderProps {
|
|
|
134
134
|
|
|
135
135
|
/**
|
|
136
136
|
* App version from server payload (stable, immutable).
|
|
137
|
-
* Forwarded to
|
|
137
|
+
* Forwarded to context for cache key building.
|
|
138
138
|
*/
|
|
139
139
|
version?: string;
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* URL prefix for all routes (from createRouter({ basename })).
|
|
143
|
+
*/
|
|
144
|
+
basename?: string;
|
|
140
145
|
}
|
|
141
146
|
|
|
142
147
|
/**
|
|
@@ -169,6 +174,7 @@ export function NavigationProvider({
|
|
|
169
174
|
initialTheme,
|
|
170
175
|
warmupEnabled,
|
|
171
176
|
version,
|
|
177
|
+
basename,
|
|
172
178
|
}: NavigationProviderProps): ReactNode {
|
|
173
179
|
// Track current payload for rendering (this triggers re-renders)
|
|
174
180
|
const [payload, setPayload] = useState(initialPayload);
|
|
@@ -198,6 +204,7 @@ export function NavigationProvider({
|
|
|
198
204
|
navigate,
|
|
199
205
|
refresh,
|
|
200
206
|
version,
|
|
207
|
+
basename,
|
|
201
208
|
}),
|
|
202
209
|
[],
|
|
203
210
|
);
|
|
@@ -289,15 +296,17 @@ export function NavigationProvider({
|
|
|
289
296
|
};
|
|
290
297
|
}, [warmupEnabled]);
|
|
291
298
|
|
|
292
|
-
// Cancel
|
|
293
|
-
//
|
|
299
|
+
// Cancel non-matching prefetches when navigation starts.
|
|
300
|
+
// Frees connections so the navigation fetch isn't competing with
|
|
301
|
+
// speculative prefetches. The prefetch matching the navigation target
|
|
302
|
+
// is kept alive so it can be reused via consumeInflightPrefetch.
|
|
294
303
|
useEffect(() => {
|
|
295
304
|
let wasIdle = true;
|
|
296
305
|
const unsub = eventController.subscribe(() => {
|
|
297
306
|
const state = eventController.getState();
|
|
298
307
|
const isIdle = state.state === "idle" && !state.isStreaming;
|
|
299
308
|
if (wasIdle && !isIdle) {
|
|
300
|
-
cancelAllPrefetches();
|
|
309
|
+
cancelAllPrefetches(state.pendingUrl);
|
|
301
310
|
}
|
|
302
311
|
wasIdle = isIdle;
|
|
303
312
|
});
|
|
@@ -43,10 +43,15 @@ export interface NavigationStoreContextValue {
|
|
|
43
43
|
refresh: () => Promise<void>;
|
|
44
44
|
|
|
45
45
|
/**
|
|
46
|
-
* App version from server payload
|
|
47
|
-
* Used in prefetch requests for version mismatch detection.
|
|
46
|
+
* App version from the initial server payload.
|
|
48
47
|
*/
|
|
49
48
|
version: string | undefined;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* URL prefix for all routes (from createRouter({ basename })).
|
|
52
|
+
* Used by Link and useRouter() to auto-prefix app-local paths.
|
|
53
|
+
*/
|
|
54
|
+
basename: string | undefined;
|
|
50
55
|
}
|
|
51
56
|
|
|
52
57
|
/**
|
|
@@ -9,64 +9,11 @@ import {
|
|
|
9
9
|
startTransition,
|
|
10
10
|
} from "react";
|
|
11
11
|
import type { Handle } from "../../handle.js";
|
|
12
|
-
import {
|
|
12
|
+
import { collectHandleData } from "../../handle.js";
|
|
13
13
|
import type { HandleData } from "../types.js";
|
|
14
14
|
import { NavigationStoreContext } from "./context.js";
|
|
15
15
|
import { shallowEqual } from "./shallow-equal.js";
|
|
16
16
|
|
|
17
|
-
/**
|
|
18
|
-
* Resolve the collect function for a handle.
|
|
19
|
-
* Handle objects are plain { __brand, $$id } - collect is stored in the registry
|
|
20
|
-
* (populated when createHandle runs on the client).
|
|
21
|
-
*/
|
|
22
|
-
function resolveCollect<T, A>(handle: Handle<T, A>): (segments: T[][]) => A {
|
|
23
|
-
// Look up collect from the registry (populated when the handle module is imported).
|
|
24
|
-
const registered = getCollectFn(handle.$$id);
|
|
25
|
-
if (registered) {
|
|
26
|
-
return registered as (segments: T[][]) => A;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
// Fall back to default flat collect with a dev warning.
|
|
30
|
-
if (process.env.NODE_ENV !== "production") {
|
|
31
|
-
console.warn(
|
|
32
|
-
`[rsc-router] Handle "${handle.$$id}" was passed as a prop but its collect ` +
|
|
33
|
-
`function could not be resolved. Falling back to flat array. ` +
|
|
34
|
-
`Import the handle module in a client component to register its collect function.`,
|
|
35
|
-
);
|
|
36
|
-
}
|
|
37
|
-
return ((segments: unknown[][]) => segments.flat()) as unknown as (
|
|
38
|
-
segments: T[][],
|
|
39
|
-
) => A;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Collect handle data from segments and transform to final value.
|
|
44
|
-
*/
|
|
45
|
-
function collectHandle<T, A>(
|
|
46
|
-
handle: Handle<T, A>,
|
|
47
|
-
data: HandleData,
|
|
48
|
-
segmentOrder: string[],
|
|
49
|
-
): A {
|
|
50
|
-
const collect = resolveCollect(handle);
|
|
51
|
-
const segmentData = data[handle.$$id];
|
|
52
|
-
|
|
53
|
-
if (!segmentData) {
|
|
54
|
-
return collect([]);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
// Build array of segment arrays in parent -> child order
|
|
58
|
-
const segmentArrays: T[][] = [];
|
|
59
|
-
for (const segmentId of segmentOrder) {
|
|
60
|
-
const entries = segmentData[segmentId];
|
|
61
|
-
if (entries && entries.length > 0) {
|
|
62
|
-
segmentArrays.push(entries as T[]);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
// Call collect once with all segment data
|
|
67
|
-
return collect(segmentArrays);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
17
|
/**
|
|
71
18
|
* Hook to access collected handle data.
|
|
72
19
|
*
|
|
@@ -99,13 +46,13 @@ export function useHandle<T, A, S>(
|
|
|
99
46
|
// Initial state from context event controller, or empty fallback without provider.
|
|
100
47
|
const [value, setValue] = useState<A | S>(() => {
|
|
101
48
|
if (!ctx) {
|
|
102
|
-
const collected =
|
|
49
|
+
const collected = collectHandleData(handle, {}, []);
|
|
103
50
|
return selector ? selector(collected) : collected;
|
|
104
51
|
}
|
|
105
52
|
|
|
106
53
|
// On client, use event controller state
|
|
107
54
|
const state = ctx.eventController.getHandleState();
|
|
108
|
-
const collected =
|
|
55
|
+
const collected = collectHandleData(handle, state.data, state.segmentOrder);
|
|
109
56
|
return selector ? selector(collected) : collected;
|
|
110
57
|
});
|
|
111
58
|
const [optimisticValue, setOptimisticValue] = useOptimistic(value);
|
|
@@ -125,7 +72,7 @@ export function useHandle<T, A, S>(
|
|
|
125
72
|
// Sync current state for the (possibly new) handle so that switching
|
|
126
73
|
// handles on an idle page doesn't leave stale data from the old handle.
|
|
127
74
|
const currentHandleState = ctx.eventController.getHandleState();
|
|
128
|
-
const currentCollected =
|
|
75
|
+
const currentCollected = collectHandleData(
|
|
129
76
|
handle,
|
|
130
77
|
currentHandleState.data,
|
|
131
78
|
currentHandleState.segmentOrder,
|
|
@@ -142,7 +89,11 @@ export function useHandle<T, A, S>(
|
|
|
142
89
|
const state = ctx.eventController.getHandleState();
|
|
143
90
|
const isAction =
|
|
144
91
|
ctx.eventController.getState().inflightActions.length > 0;
|
|
145
|
-
const collected =
|
|
92
|
+
const collected = collectHandleData(
|
|
93
|
+
handle,
|
|
94
|
+
state.data,
|
|
95
|
+
state.segmentOrder,
|
|
96
|
+
);
|
|
146
97
|
const nextValue = selectorRef.current
|
|
147
98
|
? selectorRef.current(collected)
|
|
148
99
|
: collected;
|