@rangojs/router 0.35.0 → 0.36.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/dist/bin/{chunk-3CQXDX3A.js → chunk-THW7E5A2.js} +11 -5
- package/dist/bin/{client-urls-projection-2GBDM5YK.js → client-urls-projection-DHUUOJZM.js} +1 -1
- package/dist/bin/rango.js +1 -1
- package/dist/bin/{runtime-discovery-PA3V3IFQ.js → runtime-discovery-OPRGB3DF.js} +2 -2
- package/dist/bin/{temp-rsc-server-WDXEHDY7.js → temp-rsc-server-FV4ERMAS.js} +2 -2
- package/dist/types/browser/event-controller.d.ts +10 -0
- package/dist/types/browser/fetched-loader-handles.d.ts +33 -0
- package/dist/types/browser/react/use-link-status.d.ts +17 -8
- package/dist/types/browser/types.d.ts +2 -0
- package/dist/types/handles/deferred-resolution.d.ts +7 -0
- package/dist/types/handles/pushed-by.d.ts +25 -0
- package/dist/types/testing/flight.entry.d.ts +1 -1
- package/dist/types/testing/render-route.d.ts +5 -2
- package/dist/types/testing/serve-shell-request.d.ts +31 -0
- package/dist/vite/index.js +12 -6
- package/package.json +1 -1
- package/skills/hooks/navigation.md +10 -3
- package/skills/loader/SKILL.md +20 -0
- package/skills/testing/client-components.md +1 -1
- package/skills/testing/serve-shell-request.md +33 -14
- package/src/browser/event-controller.ts +49 -1
- package/src/browser/fetched-loader-handles.ts +96 -0
- package/src/browser/navigation-bridge.ts +2 -1
- package/src/browser/react/Link.tsx +13 -2
- package/src/browser/react/use-link-status.ts +40 -22
- package/src/browser/types.ts +2 -0
- package/src/handles/deferred-resolution.ts +29 -2
- package/src/handles/pushed-by.ts +75 -0
- package/src/rsc/loader-fetch.ts +47 -5
- package/src/ssr/ssr-root.tsx +1 -0
- package/src/testing/flight.entry.ts +1 -0
- package/src/testing/render-route.tsx +14 -3
- package/src/testing/serve-shell-request.ts +62 -1
- package/src/use-loader.tsx +33 -4
|
@@ -15,7 +15,11 @@ import {
|
|
|
15
15
|
filterRouteSegmentIds,
|
|
16
16
|
} from "./react/filter-segment-order.js";
|
|
17
17
|
import { notifyListeners } from "./notify-listeners.js";
|
|
18
|
-
import {
|
|
18
|
+
import {
|
|
19
|
+
mergeLoaderReadHandleData,
|
|
20
|
+
mergePartialHandleData,
|
|
21
|
+
type LoaderReadPushes,
|
|
22
|
+
} from "../handles/pushed-by.js";
|
|
19
23
|
import {
|
|
20
24
|
locationStateSnapshot,
|
|
21
25
|
type LocationStateSnapshot,
|
|
@@ -314,6 +318,15 @@ export interface EventController {
|
|
|
314
318
|
resolvedIds?: string[],
|
|
315
319
|
): void;
|
|
316
320
|
getHandleState(): HandleState;
|
|
321
|
+
/**
|
|
322
|
+
* Apply the handle values a fetched read of `loaderId` pushed to the page's
|
|
323
|
+
* handle data, keeping its segment order: they replace the loader's earlier
|
|
324
|
+
* values in its segment (handles/pushed-by.ts mergeLoaderReadHandleData).
|
|
325
|
+
* Waits like setHandleData while the hydration window is open. False, and
|
|
326
|
+
* nothing applied, when no segment of the page's handle data registers the
|
|
327
|
+
* loader.
|
|
328
|
+
*/
|
|
329
|
+
setLoaderReadHandleData(loaderId: string, pushes: LoaderReadPushes): boolean;
|
|
317
330
|
/**
|
|
318
331
|
* Open the hydration window. Until releaseHydrationLock(), getHandleState()
|
|
319
332
|
* returns the state the document's HTML was rendered with, and handle writes
|
|
@@ -601,6 +614,9 @@ export function createEventController(
|
|
|
601
614
|
|
|
602
615
|
let handleData: HandleData = {};
|
|
603
616
|
let handleSegmentOrder: string[] = [];
|
|
617
|
+
// The raw `matched` handleData was built from: which loaders each segment
|
|
618
|
+
// registers (setLoaderReadHandleData).
|
|
619
|
+
let handleMatched: string[] = [];
|
|
604
620
|
let routeSegmentIds: string[] = [];
|
|
605
621
|
// The hydration window (lockHydration): what the document's HTML was
|
|
606
622
|
// rendered with, and the handle writes waiting for its release.
|
|
@@ -1231,11 +1247,42 @@ export function createEventController(
|
|
|
1231
1247
|
handleData = data;
|
|
1232
1248
|
}
|
|
1233
1249
|
handleSegmentOrder = newSegmentOrder;
|
|
1250
|
+
handleMatched = rawMatched;
|
|
1234
1251
|
routeSegmentIds = newRouteSegmentIds;
|
|
1235
1252
|
|
|
1236
1253
|
notifyHandles.schedule();
|
|
1237
1254
|
}
|
|
1238
1255
|
|
|
1256
|
+
function setLoaderReadHandleData(
|
|
1257
|
+
loaderId: string,
|
|
1258
|
+
pushes: LoaderReadPushes,
|
|
1259
|
+
): boolean {
|
|
1260
|
+
const merge = () =>
|
|
1261
|
+
mergeLoaderReadHandleData(
|
|
1262
|
+
handleData,
|
|
1263
|
+
handleSegmentOrder,
|
|
1264
|
+
handleMatched,
|
|
1265
|
+
loaderId,
|
|
1266
|
+
pushes,
|
|
1267
|
+
);
|
|
1268
|
+
if (hydrationSnapshot) {
|
|
1269
|
+
// Decided on the data the waiting writes start from, applied after
|
|
1270
|
+
// them, in order, when the window closes.
|
|
1271
|
+
const applies = merge() !== undefined;
|
|
1272
|
+
if (applies) {
|
|
1273
|
+
writesAfterHydration.push(() => {
|
|
1274
|
+
setLoaderReadHandleData(loaderId, pushes);
|
|
1275
|
+
});
|
|
1276
|
+
}
|
|
1277
|
+
return applies;
|
|
1278
|
+
}
|
|
1279
|
+
const merged = merge();
|
|
1280
|
+
if (!merged) return false;
|
|
1281
|
+
handleData = merged;
|
|
1282
|
+
notifyHandles.schedule();
|
|
1283
|
+
return true;
|
|
1284
|
+
}
|
|
1285
|
+
|
|
1239
1286
|
function getHandleState(): HandleState {
|
|
1240
1287
|
if (hydrationSnapshot) return hydrationSnapshot.handles;
|
|
1241
1288
|
return {
|
|
@@ -1361,6 +1408,7 @@ export function createEventController(
|
|
|
1361
1408
|
// Handles
|
|
1362
1409
|
setHandleData,
|
|
1363
1410
|
getHandleState,
|
|
1411
|
+
setLoaderReadHandleData,
|
|
1364
1412
|
lockHydration,
|
|
1365
1413
|
getHydrationSnapshot: () => hydrationSnapshot,
|
|
1366
1414
|
releaseHydrationLock,
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import type { EventController } from "./event-controller.js";
|
|
2
|
+
import type { NavigationStore } from "./types.js";
|
|
3
|
+
import { resolveDeferredPushes } from "../handles/deferred-resolution.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The handle values a fetched loader read pushed (`_rsc_loader`: `load()`, a
|
|
7
|
+
* `refreshGroup` refresh), by handle $$id, as the reply carries them
|
|
8
|
+
* (rsc/loader-fetch.ts). A pending push is its promise.
|
|
9
|
+
*/
|
|
10
|
+
export type FetchedLoaderHandles = Record<string, unknown[]>;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Which loader's run pushed each of the reply's values, index-aligned per
|
|
14
|
+
* handle (null: the read loader's own body), as the reply carries it.
|
|
15
|
+
*/
|
|
16
|
+
export type FetchedLoaderPushedBy = Record<string, (string | null)[]>;
|
|
17
|
+
|
|
18
|
+
/** The router state a read's handle values are applied to. */
|
|
19
|
+
export interface FetchedLoaderPage {
|
|
20
|
+
eventController: EventController;
|
|
21
|
+
store: NavigationStore;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** The newest read sent for each loader: an older reply leaves the handles. */
|
|
25
|
+
const latestRead = new Map<string, number>();
|
|
26
|
+
let readSeq = 0;
|
|
27
|
+
const warnedUnregistered = new Set<string>();
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Start a read of `loaderId` from the page on screen. Call it when the request
|
|
31
|
+
* is sent; the returned function applies the reply's handle values.
|
|
32
|
+
*
|
|
33
|
+
* The values replace the loader's earlier ones in its segment once the
|
|
34
|
+
* pending ones settle (a rejected or nullish one dropped, as on the
|
|
35
|
+
* navigation lane), on their own after the data: a handle value never holds
|
|
36
|
+
* the loader's data. They apply only while the page the read was sent from is
|
|
37
|
+
* still on screen (same history entry, no commit since, as NavigationProvider's
|
|
38
|
+
* handle stream checks) and no newer read of the loader was sent, and the
|
|
39
|
+
* history entry keeps them for Back. A loader no segment of the page
|
|
40
|
+
* registers has no segment to own its pushes: they are not applied (warned
|
|
41
|
+
* once per loader in development).
|
|
42
|
+
*/
|
|
43
|
+
export function startFetchedLoaderRead(
|
|
44
|
+
loaderId: string,
|
|
45
|
+
page: FetchedLoaderPage | undefined,
|
|
46
|
+
): (
|
|
47
|
+
handles: FetchedLoaderHandles | undefined,
|
|
48
|
+
pushedBy?: FetchedLoaderPushedBy,
|
|
49
|
+
) => void {
|
|
50
|
+
const seq = ++readSeq;
|
|
51
|
+
latestRead.set(loaderId, seq);
|
|
52
|
+
const historyKey = page?.store.getHistoryKey();
|
|
53
|
+
const navInstance = page?.store.getNavInstance();
|
|
54
|
+
const current = (): boolean =>
|
|
55
|
+
page !== undefined &&
|
|
56
|
+
latestRead.get(loaderId) === seq &&
|
|
57
|
+
page.store.getHistoryKey() === historyKey &&
|
|
58
|
+
page.store.getNavInstance() === navInstance;
|
|
59
|
+
|
|
60
|
+
const apply = async (
|
|
61
|
+
handles: FetchedLoaderHandles | undefined,
|
|
62
|
+
pushedBy: FetchedLoaderPushedBy | undefined,
|
|
63
|
+
): Promise<void> => {
|
|
64
|
+
if (!handles || !current()) return;
|
|
65
|
+
const pushes = await resolveDeferredPushes(handles, pushedBy);
|
|
66
|
+
if (!current()) return;
|
|
67
|
+
const { eventController, store } = page!;
|
|
68
|
+
if (!eventController.setLoaderReadHandleData(loaderId, pushes)) {
|
|
69
|
+
if (
|
|
70
|
+
process.env.NODE_ENV !== "production" &&
|
|
71
|
+
Object.values(pushes.values).some((pushed) => pushed.length > 0) &&
|
|
72
|
+
!warnedUnregistered.has(loaderId)
|
|
73
|
+
) {
|
|
74
|
+
warnedUnregistered.add(loaderId);
|
|
75
|
+
console.warn(
|
|
76
|
+
`[rango] Loader "${loaderId}" pushed handle values on a fetched read ` +
|
|
77
|
+
`(load(), useRefreshLoaders()), but no segment on this page ` +
|
|
78
|
+
`registers it with loader(), so they have no segment to belong to ` +
|
|
79
|
+
`and are not applied. Register the loader on the route or layout ` +
|
|
80
|
+
`whose handle values it writes.`,
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
store.updateCacheHandleDataIfOwned(
|
|
86
|
+
historyKey!,
|
|
87
|
+
eventController.getHandleState().data,
|
|
88
|
+
navInstance!,
|
|
89
|
+
);
|
|
90
|
+
};
|
|
91
|
+
return (handles, pushedBy) => {
|
|
92
|
+
apply(handles, pushedBy).catch((error) =>
|
|
93
|
+
console.error("[rango] Error applying a loader read's handles:", error),
|
|
94
|
+
);
|
|
95
|
+
};
|
|
96
|
+
}
|
|
@@ -285,7 +285,7 @@ export function createNavigationBridge(
|
|
|
285
285
|
hops: number,
|
|
286
286
|
options: Pick<
|
|
287
287
|
NavigateOptionsInternal,
|
|
288
|
-
"replace" | "transition" | "_redirectFrom"
|
|
288
|
+
"replace" | "transition" | "_redirectFrom" | "_linkId"
|
|
289
289
|
>,
|
|
290
290
|
): Promise<void> {
|
|
291
291
|
const redirectUrl = validateRedirectOrigin(
|
|
@@ -715,6 +715,7 @@ export function createNavigationBridge(
|
|
|
715
715
|
replace: options?.replace,
|
|
716
716
|
transition: options?.transition,
|
|
717
717
|
_redirectFrom: options?._redirectFrom,
|
|
718
|
+
_linkId: options?._linkId,
|
|
718
719
|
},
|
|
719
720
|
);
|
|
720
721
|
}
|
|
@@ -5,13 +5,14 @@ import React, {
|
|
|
5
5
|
useCallback,
|
|
6
6
|
useContext,
|
|
7
7
|
useEffect,
|
|
8
|
+
useId,
|
|
8
9
|
useMemo,
|
|
9
10
|
useRef,
|
|
10
11
|
type ForwardRefExoticComponent,
|
|
11
12
|
type RefAttributes,
|
|
12
13
|
} from "react";
|
|
13
14
|
import { NavigationStoreContext } from "./context.js";
|
|
14
|
-
import { LinkContext } from "./use-link-status.js";
|
|
15
|
+
import { LinkContext, type LinkTarget } from "./use-link-status.js";
|
|
15
16
|
import type { HistoryState } from "../types.js";
|
|
16
17
|
import {
|
|
17
18
|
isHashOnlyNavigation,
|
|
@@ -258,6 +259,14 @@ export const Link: ForwardRefExoticComponent<
|
|
|
258
259
|
[to, isExternal, ctx?.basename],
|
|
259
260
|
);
|
|
260
261
|
|
|
262
|
+
// This instance's id: its click names it on the navigation, so
|
|
263
|
+
// useLinkStatus marks this link and not every link to the same URL.
|
|
264
|
+
const linkId = useId();
|
|
265
|
+
const linkTarget = useMemo<LinkTarget>(
|
|
266
|
+
() => ({ to: resolvedTo, id: linkId }),
|
|
267
|
+
[resolvedTo, linkId],
|
|
268
|
+
);
|
|
269
|
+
|
|
261
270
|
// No explicit `prefetch` prop: fall back to the router-wide default from
|
|
262
271
|
// the router store. Adaptive is resolved against the input capability when a
|
|
263
272
|
// prefetch is armed or triggered, never during render.
|
|
@@ -344,10 +353,12 @@ export const Link: ForwardRefExoticComponent<
|
|
|
344
353
|
state,
|
|
345
354
|
revalidate,
|
|
346
355
|
transition,
|
|
356
|
+
_linkId: linkId,
|
|
347
357
|
});
|
|
348
358
|
},
|
|
349
359
|
[
|
|
350
360
|
resolvedTo,
|
|
361
|
+
linkId,
|
|
351
362
|
isExternal,
|
|
352
363
|
reloadDocument,
|
|
353
364
|
replace,
|
|
@@ -494,7 +505,7 @@ export const Link: ForwardRefExoticComponent<
|
|
|
494
505
|
data-transition={transition === false ? "false" : undefined}
|
|
495
506
|
{...props}
|
|
496
507
|
>
|
|
497
|
-
<LinkContext.Provider value={
|
|
508
|
+
<LinkContext.Provider value={linkTarget}>{children}</LinkContext.Provider>
|
|
498
509
|
</a>
|
|
499
510
|
);
|
|
500
511
|
});
|
|
@@ -11,12 +11,20 @@ import { NavigationStoreContext } from "./context.js";
|
|
|
11
11
|
import type { EventController, NavigationEntry } from "../event-controller.js";
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
|
-
*
|
|
15
|
-
*
|
|
14
|
+
* What a Link hands to useLinkStatus: its resolved destination and an id for
|
|
15
|
+
* this Link instance, which its click puts on the navigation (`_linkId`).
|
|
16
16
|
*/
|
|
17
|
-
export
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
export interface LinkTarget {
|
|
18
|
+
readonly to: string;
|
|
19
|
+
readonly id: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Context for Link component to provide its destination and instance id.
|
|
24
|
+
* Used by useLinkStatus to determine if this specific link is pending.
|
|
25
|
+
*/
|
|
26
|
+
export const LinkContext: Context<LinkTarget | null> =
|
|
27
|
+
createContext<LinkTarget | null>(null);
|
|
20
28
|
|
|
21
29
|
/**
|
|
22
30
|
* Link status returned by useLinkStatus hook
|
|
@@ -53,18 +61,25 @@ function visibleNavigation(ec: EventController): NavigationEntry | null {
|
|
|
53
61
|
}
|
|
54
62
|
|
|
55
63
|
/**
|
|
56
|
-
* Whether `navigation`
|
|
57
|
-
*
|
|
64
|
+
* Whether `navigation` is this link's. A Link's click names the link
|
|
65
|
+
* (`_linkId`, kept through a server redirect chain), so only that link is
|
|
66
|
+
* pending, not every link to the same URL. A navigation no Link started
|
|
67
|
+
* (Back/Forward, router.push, an action's redirect) goes to every link to its
|
|
68
|
+
* URL or, after a server redirect, to the URL its chain started from.
|
|
58
69
|
*/
|
|
59
70
|
function isPendingFor(
|
|
60
|
-
|
|
71
|
+
link: LinkTarget | null,
|
|
61
72
|
navigation: NavigationEntry | null,
|
|
62
73
|
origin: string,
|
|
63
74
|
): boolean {
|
|
64
|
-
if (
|
|
75
|
+
if (link === null || !navigation) {
|
|
65
76
|
return false;
|
|
66
77
|
}
|
|
67
|
-
const
|
|
78
|
+
const linkId = navigation.options?._linkId;
|
|
79
|
+
if (linkId !== undefined) {
|
|
80
|
+
return linkId === link.id;
|
|
81
|
+
}
|
|
82
|
+
const to = normalizeUrl(link.to, origin);
|
|
68
83
|
const from = navigation.options?._redirectFrom;
|
|
69
84
|
return (
|
|
70
85
|
normalizeUrl(navigation.url, origin) === to ||
|
|
@@ -75,11 +90,12 @@ function isPendingFor(
|
|
|
75
90
|
/**
|
|
76
91
|
* Hook to track the pending state of a Link component
|
|
77
92
|
*
|
|
78
|
-
* Must be used inside a Link component. Returns `{ pending: true }` while
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
* navigation
|
|
93
|
+
* Must be used inside a Link component. Returns `{ pending: true }` while
|
|
94
|
+
* this link's navigation is in flight, until the page it brings commits:
|
|
95
|
+
* under transition() while React holds that page, and through a server
|
|
96
|
+
* redirect until the target page commits. A click marks only the clicked
|
|
97
|
+
* link; a navigation no link started (Back/Forward, router.push) marks every
|
|
98
|
+
* link to its URL. A newer navigation ends it at once.
|
|
83
99
|
*
|
|
84
100
|
* Useful for showing inline loading indicators on individual links.
|
|
85
101
|
*
|
|
@@ -98,7 +114,7 @@ function isPendingFor(
|
|
|
98
114
|
* ```
|
|
99
115
|
*/
|
|
100
116
|
export function useLinkStatus(): LinkStatus {
|
|
101
|
-
const
|
|
117
|
+
const link = useContext(LinkContext);
|
|
102
118
|
const ctx = useContext(NavigationStoreContext);
|
|
103
119
|
|
|
104
120
|
const origin = ctx?.origin ?? "http://localhost";
|
|
@@ -108,7 +124,7 @@ export function useLinkStatus(): LinkStatus {
|
|
|
108
124
|
() =>
|
|
109
125
|
!!ctx &&
|
|
110
126
|
!ctx.eventController.getHydrationSnapshot() &&
|
|
111
|
-
isPendingFor(
|
|
127
|
+
isPendingFor(link, visibleNavigation(ctx.eventController), origin),
|
|
112
128
|
);
|
|
113
129
|
|
|
114
130
|
// The value commits in the lane of the notification that carries it, as
|
|
@@ -117,9 +133,11 @@ export function useLinkStatus(): LinkStatus {
|
|
|
117
133
|
// notification inside the page's update). Set once per navigation: a
|
|
118
134
|
// repeated urgent set would commit ahead of a held page's settle. A
|
|
119
135
|
// background navigation (an action's refetch) is not a newer one, so a
|
|
120
|
-
// held link stays pending.
|
|
136
|
+
// held link stays pending. Deps are the link's fields, not the object: a
|
|
137
|
+
// provider's new object with the same fields must not resubscribe and set
|
|
138
|
+
// the value again.
|
|
121
139
|
useEffect(() => {
|
|
122
|
-
if (!ctx ||
|
|
140
|
+
if (!ctx || link === null) {
|
|
123
141
|
return;
|
|
124
142
|
}
|
|
125
143
|
|
|
@@ -128,7 +146,7 @@ export function useLinkStatus(): LinkStatus {
|
|
|
128
146
|
const navigation = visibleNavigation(ctx.eventController);
|
|
129
147
|
if (navigation === last) return;
|
|
130
148
|
last = navigation;
|
|
131
|
-
setPending(isPendingFor(
|
|
149
|
+
setPending(isPendingFor(link, navigation, origin));
|
|
132
150
|
};
|
|
133
151
|
|
|
134
152
|
// Catch-up: re-read state synchronously on mount before subscribing, so a
|
|
@@ -138,10 +156,10 @@ export function useLinkStatus(): LinkStatus {
|
|
|
138
156
|
update();
|
|
139
157
|
|
|
140
158
|
return ctx.eventController.subscribe(update);
|
|
141
|
-
}, [
|
|
159
|
+
}, [link?.to, link?.id, origin]);
|
|
142
160
|
|
|
143
161
|
// If not inside a Link, return not pending
|
|
144
|
-
if (
|
|
162
|
+
if (link === null) {
|
|
145
163
|
return { pending: false };
|
|
146
164
|
}
|
|
147
165
|
|
package/src/browser/types.ts
CHANGED
|
@@ -525,6 +525,8 @@ export interface NavigateOptionsInternal extends Omit<
|
|
|
525
525
|
_redirectHops?: number;
|
|
526
526
|
/** The URL a redirected navigation chain started from: a link to it stays pending (useLinkStatus) */
|
|
527
527
|
_redirectFrom?: string;
|
|
528
|
+
/** The Link instance whose click started this navigation chain: only that link is pending (useLinkStatus) */
|
|
529
|
+
_linkId?: string;
|
|
528
530
|
}
|
|
529
531
|
|
|
530
532
|
/**
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import type { HandleData, HandleStore } from "../server/handle-store.js";
|
|
2
2
|
import { isThenable } from "./is-thenable.js";
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
isPushedByKey,
|
|
5
|
+
pushedByKey,
|
|
6
|
+
type LoaderReadPushes,
|
|
7
|
+
} from "./pushed-by.js";
|
|
4
8
|
|
|
5
9
|
/**
|
|
6
10
|
* Runtime-neutral resolution of deferred (Promise) handle values. Shared by the
|
|
@@ -68,6 +72,29 @@ export async function resolveDeferredHandleValues(
|
|
|
68
72
|
return out;
|
|
69
73
|
}
|
|
70
74
|
|
|
75
|
+
/**
|
|
76
|
+
* A fetched loader read's pushes, as its reply carries them, with deferred
|
|
77
|
+
* values awaited as resolveDeferredHandleValues awaits them; `pushedBy` loses
|
|
78
|
+
* the entries of the dropped values.
|
|
79
|
+
*/
|
|
80
|
+
export async function resolveDeferredPushes(
|
|
81
|
+
pushes: Readonly<Record<string, readonly unknown[]>>,
|
|
82
|
+
pushedBy: Readonly<Record<string, readonly (string | null)[]>> = {},
|
|
83
|
+
): Promise<LoaderReadPushes> {
|
|
84
|
+
const out: LoaderReadPushes = { values: {}, pushedBy: {} };
|
|
85
|
+
await Promise.all(
|
|
86
|
+
Object.entries(pushes).map(async ([handleName, values]) => {
|
|
87
|
+
const resolved = await resolveValues(values);
|
|
88
|
+
out.values[handleName] = keptValues(resolved);
|
|
89
|
+
const by = pushedBy[handleName];
|
|
90
|
+
if (by) {
|
|
91
|
+
out.pushedBy[handleName] = by.filter((_, i) => resolved[i].keep);
|
|
92
|
+
}
|
|
93
|
+
}),
|
|
94
|
+
);
|
|
95
|
+
return out;
|
|
96
|
+
}
|
|
97
|
+
|
|
71
98
|
interface ResolvedValue {
|
|
72
99
|
keep: boolean;
|
|
73
100
|
value: unknown;
|
|
@@ -78,7 +105,7 @@ interface ResolvedValue {
|
|
|
78
105
|
// A thenable is awaited and dropped when it rejects OR resolves to null/undefined
|
|
79
106
|
// (the `.defer({ else })` skip semantics: a forgotten resolver times out to
|
|
80
107
|
// `else ?? undefined`), so a nullish deferred slot never reaches a collector.
|
|
81
|
-
function resolveValues(values: unknown[]): Promise<ResolvedValue[]> {
|
|
108
|
+
function resolveValues(values: readonly unknown[]): Promise<ResolvedValue[]> {
|
|
82
109
|
return Promise.all(
|
|
83
110
|
values.map(async (v) => {
|
|
84
111
|
if (!isThenable(v)) return { keep: true, value: v };
|
package/src/handles/pushed-by.ts
CHANGED
|
@@ -75,6 +75,81 @@ export function withSegmentHandles(
|
|
|
75
75
|
return out;
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
+
/**
|
|
79
|
+
* What a fetched loader read pushed, settled: the values by handle $$id, and
|
|
80
|
+
* the loader whose run pushed each (index-aligned; null, or no entry for the
|
|
81
|
+
* handle, for the read loader's own body).
|
|
82
|
+
*/
|
|
83
|
+
export interface LoaderReadPushes {
|
|
84
|
+
values: Record<string, unknown[]>;
|
|
85
|
+
pushedBy: Record<string, (string | null)[]>;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The handle data after a fetched read of loader `loaderId` (the `_rsc_loader`
|
|
90
|
+
* lane: `load()`, a `refreshGroup` refresh) pushed `pushes`. The read renders
|
|
91
|
+
* no segment, so its pushes go where the page's run of the loader put its
|
|
92
|
+
* own: the first segment of `segmentOrder` holding a value the loader pushed,
|
|
93
|
+
* else the first segment of `matched` (the raw list `current` was built from)
|
|
94
|
+
* that registers it, where a render files them (loader-resolution.ts
|
|
95
|
+
* loaderOwner). The loader's earlier values give way to these in their
|
|
96
|
+
* position, in every segment that registers it, and every other run keeps its
|
|
97
|
+
* own (mergePartialHandleData, with the loader as the run that ran). A value
|
|
98
|
+
* pushed by another loader the page registers, one the body read with
|
|
99
|
+
* ctx.use, belongs to that loader's run, which the read did not refresh: the
|
|
100
|
+
* page keeps that run's values, as it keeps its data. Undefined when no
|
|
101
|
+
* segment registers the loader: its pushes have no segment to belong to.
|
|
102
|
+
*/
|
|
103
|
+
export function mergeLoaderReadHandleData(
|
|
104
|
+
current: HandleData,
|
|
105
|
+
segmentOrder: readonly string[],
|
|
106
|
+
matched: readonly string[],
|
|
107
|
+
loaderId: string,
|
|
108
|
+
pushes: LoaderReadPushes,
|
|
109
|
+
): HandleData | undefined {
|
|
110
|
+
const registrations: string[] = [];
|
|
111
|
+
const registers = new Set<string>();
|
|
112
|
+
const otherLoaders = new Set<string | null>();
|
|
113
|
+
for (const id of matched) {
|
|
114
|
+
const marker = LOADER_SEGMENT_ID.exec(id);
|
|
115
|
+
if (!marker) continue;
|
|
116
|
+
const loader = id.slice(marker.index + marker[0].length);
|
|
117
|
+
if (loader !== loaderId) {
|
|
118
|
+
otherLoaders.add(loader);
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
registrations.push(id);
|
|
122
|
+
registers.add(id.slice(0, marker.index));
|
|
123
|
+
}
|
|
124
|
+
if (registrations.length === 0) return undefined;
|
|
125
|
+
const pushedHere = (segmentId: string): boolean =>
|
|
126
|
+
Object.keys(current).some(
|
|
127
|
+
(name) =>
|
|
128
|
+
isPushedByKey(name) &&
|
|
129
|
+
current[name][segmentId]?.includes(loaderId) === true,
|
|
130
|
+
);
|
|
131
|
+
const owner =
|
|
132
|
+
segmentOrder.find((id) => registers.has(id) && pushedHere(id)) ??
|
|
133
|
+
[...registers][0]!;
|
|
134
|
+
const incoming: HandleData = {};
|
|
135
|
+
for (const name in pushes.values) {
|
|
136
|
+
const by = pushes.pushedBy[name];
|
|
137
|
+
const own = pushes.values[name].filter(
|
|
138
|
+
(_, i) => !otherLoaders.has(by?.[i] ?? null),
|
|
139
|
+
);
|
|
140
|
+
if (own.length === 0) continue;
|
|
141
|
+
incoming[name] = { [owner]: own };
|
|
142
|
+
incoming[pushedByKey(name)] = { [owner]: own.map(() => loaderId) };
|
|
143
|
+
}
|
|
144
|
+
return mergePartialHandleData(
|
|
145
|
+
current,
|
|
146
|
+
incoming,
|
|
147
|
+
segmentOrder,
|
|
148
|
+
matched,
|
|
149
|
+
registrations,
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
|
|
78
153
|
/**
|
|
79
154
|
* The handle data after a partial response's snapshot `incoming` (each stream
|
|
80
155
|
* yield holds everything the response pushed so far). A segment outside
|
package/src/rsc/loader-fetch.ts
CHANGED
|
@@ -38,6 +38,20 @@ import {
|
|
|
38
38
|
} from "./helpers.js";
|
|
39
39
|
import type { HandlerContext } from "./handler-context.js";
|
|
40
40
|
import { WIRE } from "../wire-params.js";
|
|
41
|
+
import { isHandle } from "../handle.js";
|
|
42
|
+
import { runInsideLoaderBodyScope } from "../server/context.js";
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* A successful read's reply: the loader's value, the values its body pushed
|
|
46
|
+
* to each handle (by handle $$id, in push order), and which loader's run
|
|
47
|
+
* pushed each (index-aligned, null for the body's own; a handle with no entry
|
|
48
|
+
* holds only the body's own).
|
|
49
|
+
*/
|
|
50
|
+
interface LoaderFetchPayload {
|
|
51
|
+
loaderResult: unknown;
|
|
52
|
+
handles: Record<string, unknown[]>;
|
|
53
|
+
pushedBy: Record<string, (string | null)[]>;
|
|
54
|
+
}
|
|
41
55
|
|
|
42
56
|
export interface LoaderErrorMeta<TEnv> {
|
|
43
57
|
request: Request;
|
|
@@ -255,6 +269,17 @@ export async function handleLoaderFetch<TEnv>(
|
|
|
255
269
|
variables,
|
|
256
270
|
async () => {
|
|
257
271
|
const reqCtx = getRequestContext();
|
|
272
|
+
// ctx.use(Handle) pushes file under the request context's segment
|
|
273
|
+
// (request-context.ts createUseFunction), which throws without one,
|
|
274
|
+
// and this lane renders no segment: the loader's id stands in. The
|
|
275
|
+
// body's pushes, and those of the loaders it reads with ctx.use,
|
|
276
|
+
// file under the loader's id and ride the reply; the browser files
|
|
277
|
+
// them under the segments that register the loader
|
|
278
|
+
// (browser/fetched-loader-handles.ts). Set before the copy below:
|
|
279
|
+
// a "use cache" function handed the loader's ctx replays a HIT's
|
|
280
|
+
// pushes into its segment (cache-runtime.ts callerSegmentId).
|
|
281
|
+
(reqCtx as { _currentSegmentId?: string })._currentSegmentId =
|
|
282
|
+
loaderId;
|
|
258
283
|
// Merge route params (from previewMatch) with explicit loader params.
|
|
259
284
|
// Explicit params take precedence over route-matched params.
|
|
260
285
|
const resolvedRouteParams = routeParams ?? {};
|
|
@@ -290,6 +315,17 @@ export async function handleLoaderFetch<TEnv>(
|
|
|
290
315
|
: undefined,
|
|
291
316
|
),
|
|
292
317
|
...(loaderFormData ? { formData: loaderFormData } : {}),
|
|
318
|
+
// A loader the body reads runs as its own run, as a render runs
|
|
319
|
+
// it (loader-resolution.ts): its pushes name it
|
|
320
|
+
// (getHandlePushOwner), so the reply's pushedBy tells the browser
|
|
321
|
+
// they are that loader's, not this one's.
|
|
322
|
+
use: ((item: any) =>
|
|
323
|
+
isHandle(item)
|
|
324
|
+
? reqCtx.use(item)
|
|
325
|
+
: runInsideLoaderBodyScope(
|
|
326
|
+
() => reqCtx.use(item),
|
|
327
|
+
item.$$id,
|
|
328
|
+
)) as typeof reqCtx.use,
|
|
293
329
|
};
|
|
294
330
|
|
|
295
331
|
// Meter the fetchable-loader execution via observePhase, the sole
|
|
@@ -299,13 +335,19 @@ export async function handleLoaderFetch<TEnv>(
|
|
|
299
335
|
fn(loaderCtx),
|
|
300
336
|
);
|
|
301
337
|
|
|
302
|
-
|
|
303
|
-
loaderResult:
|
|
304
|
-
|
|
305
|
-
|
|
338
|
+
const loaderPayload: LoaderFetchPayload = {
|
|
339
|
+
loaderResult: result,
|
|
340
|
+
// The pushes the body made, a pending one as its promise: the
|
|
341
|
+
// browser applies them once they settle, after the data.
|
|
342
|
+
handles: reqCtx._handleStore.getDataForSegment(loaderId),
|
|
343
|
+
// A loader the body read with ctx.use pushed as its own run: the
|
|
344
|
+
// browser leaves the page's run of it alone when the page
|
|
345
|
+
// registers it (handles/pushed-by.ts mergeLoaderReadHandleData).
|
|
346
|
+
pushedBy: reqCtx._handleStore.getPushedBy(loaderId) ?? {},
|
|
347
|
+
};
|
|
306
348
|
// Read once here: onError can fire outside the ALS frame.
|
|
307
349
|
const requestContext = _getRequestContext();
|
|
308
|
-
const rscStream = ctx.renderToReadableStream<
|
|
350
|
+
const rscStream = ctx.renderToReadableStream<LoaderFetchPayload>(
|
|
309
351
|
loaderPayload,
|
|
310
352
|
{
|
|
311
353
|
onError: (error: unknown) => {
|
package/src/ssr/ssr-root.tsx
CHANGED
|
@@ -119,6 +119,7 @@ function createSsrEventController(opts: {
|
|
|
119
119
|
releaseRouteState: () => {},
|
|
120
120
|
setHandleData: () => {},
|
|
121
121
|
getHandleState: () => handleState,
|
|
122
|
+
setLoaderReadHandleData: () => false,
|
|
122
123
|
lockHydration: () => {},
|
|
123
124
|
getHydrationSnapshot: () => undefined,
|
|
124
125
|
releaseHydrationLock: () => {},
|
|
@@ -25,8 +25,9 @@
|
|
|
25
25
|
* This exercises the context read path, not the run path.
|
|
26
26
|
* - navigate() drives the navigation lifecycle as a click does:
|
|
27
27
|
* useNavigation() reads loading from the call and idle with the commit,
|
|
28
|
-
* and useLinkStatus().pending is true for
|
|
29
|
-
*
|
|
28
|
+
* and useLinkStatus().pending is true for every link to the destination
|
|
29
|
+
* (for a <Link> click, the clicked link only) until the commit, which
|
|
30
|
+
* React holds with it. A `transition` spec plus a pending
|
|
30
31
|
* Promise seeded via navigate(url, { loaders }) commits through
|
|
31
32
|
* production's commitInTransition (browser/partial-update.ts), which React
|
|
32
33
|
* holds: useNavigation() stays loading and the held reader's
|
|
@@ -52,6 +53,8 @@
|
|
|
52
53
|
* rejection is the loader's error. Needs the testing kit's
|
|
53
54
|
* `@vitejs/plugin-rsc/browser` alias (rangoTestAliases). A load() with params
|
|
54
55
|
* is answered with a loaderError; a body and non-GET loads are not answered.
|
|
56
|
+
* The answer carries no handle values: what a loader pushes on a read is
|
|
57
|
+
* serveShellRequest's readLoader() and e2e territory.
|
|
55
58
|
* - A spec's transition({ when }) decides every commit with production's
|
|
56
59
|
* browser decision: navigate() and useRouter().push model kind "push",
|
|
57
60
|
* useRouter().replace kind "replace", refresh() kind "revalidate", and a
|
|
@@ -1057,6 +1060,8 @@ export async function renderRoute(
|
|
|
1057
1060
|
/** The loader data the response carries: a prefetch's (prefetch()). */
|
|
1058
1061
|
seeds?: Record<string, unknown>;
|
|
1059
1062
|
history?: Pick<NavigateOptionsInternal, "state" | "replace" | "transition">;
|
|
1063
|
+
/** A <Link> click: the clicked instance, as production's entry carries it. */
|
|
1064
|
+
linkId?: string;
|
|
1060
1065
|
kind?: TransitionWhenKind;
|
|
1061
1066
|
transition?: boolean;
|
|
1062
1067
|
streamUntil?: Promise<unknown>;
|
|
@@ -1152,6 +1157,9 @@ export async function renderRoute(
|
|
|
1152
1157
|
}
|
|
1153
1158
|
const handle = eventController.startNavigation(
|
|
1154
1159
|
new URL(target, TEST_ORIGIN).href,
|
|
1160
|
+
navOptions?.linkId === undefined
|
|
1161
|
+
? undefined
|
|
1162
|
+
: { _linkId: navOptions.linkId },
|
|
1155
1163
|
);
|
|
1156
1164
|
// An action made the prefetch stale: once this navigation settled (its
|
|
1157
1165
|
// commit and its stream), the page revalidates. Production's own guards
|
|
@@ -1546,7 +1554,10 @@ export async function renderRoute(
|
|
|
1546
1554
|
let prefetchRoot: HTMLElement | undefined;
|
|
1547
1555
|
const bridge: NavigationBridge = {
|
|
1548
1556
|
navigate: (target, navigateOptions) =>
|
|
1549
|
-
navigate(target, {
|
|
1557
|
+
navigate(target, {
|
|
1558
|
+
history: navigateOptions ?? {},
|
|
1559
|
+
linkId: navigateOptions?._linkId,
|
|
1560
|
+
}),
|
|
1550
1561
|
refresh: () => refresh(),
|
|
1551
1562
|
// Back/forward onto the entry history.state holds. Its page is restored
|
|
1552
1563
|
// when the mount or a navigation wrote the entry, history still holds that
|