@rangojs/router 0.36.1 → 0.37.0
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-THW7E5A2.js → chunk-A64EG6MI.js} +22 -19
- package/dist/bin/{client-urls-projection-DHUUOJZM.js → client-urls-projection-RLDSA3BL.js} +1 -1
- package/dist/bin/rango.js +1 -1
- package/dist/bin/{runtime-discovery-OPRGB3DF.js → runtime-discovery-T7ZLA6E3.js} +2 -2
- package/dist/bin/{temp-rsc-server-FV4ERMAS.js → temp-rsc-server-FGGXPKVQ.js} +2 -2
- package/dist/types/browser/event-controller.d.ts +53 -2
- package/dist/types/browser/fetched-loader-handles.d.ts +57 -14
- package/dist/types/browser/navigation-store.d.ts +3 -2
- package/dist/types/browser/react/use-handle.d.ts +18 -5
- package/dist/types/browser/react/use-push-handle.d.ts +44 -0
- package/dist/types/browser/types.d.ts +44 -0
- package/dist/types/client.d.ts +1 -0
- package/dist/types/client.rsc.d.ts +1 -0
- package/dist/types/handle.d.ts +13 -5
- package/dist/types/handles/client-pushes.d.ts +51 -0
- package/dist/types/handles/deferred-resolution.d.ts +7 -0
- package/dist/types/handles/pushed-by.d.ts +11 -0
- package/dist/types/loader-store.d.ts +13 -0
- package/dist/types/rsc/prefetch-render-error.d.ts +2 -1
- package/dist/types/server/handle-store.d.ts +8 -0
- package/dist/types/testing/dom.entry.d.ts +1 -1
- package/dist/types/testing/render-route.d.ts +74 -14
- package/dist/vite/index.js +23 -20
- package/package.json +1 -1
- package/skills/breadcrumbs/SKILL.md +68 -6
- package/skills/caching/SKILL.md +4 -2
- package/skills/catalog.json +3 -3
- package/skills/client-urls/SKILL.md +13 -1
- package/skills/document-cache/SKILL.md +31 -1
- package/skills/hooks/SKILL.md +8 -4
- package/skills/hooks/data.md +10 -1
- package/skills/hooks/handle-and-actions.md +402 -26
- package/skills/hooks/navigation.md +4 -0
- package/skills/hooks/urls.md +3 -0
- package/skills/links/SKILL.md +5 -3
- package/skills/loader/SKILL.md +28 -9
- package/skills/migrate-nextjs/SKILL.md +5 -0
- package/skills/migrate-react-router/component-migration.md +4 -1
- package/skills/parallel/SKILL.md +8 -0
- package/skills/ppr/SKILL.md +1 -0
- package/skills/rango/SKILL.md +6 -3
- package/skills/testing/client-components.md +35 -32
- package/skills/testing/handles.md +58 -6
- package/skills/ux-architecture/SKILL.md +4 -0
- package/src/browser/event-controller.ts +265 -4
- package/src/browser/fetched-loader-handles.ts +227 -54
- package/src/browser/navigation-store.ts +261 -2
- package/src/browser/partial-update.ts +3 -2
- package/src/browser/react/use-handle.ts +74 -27
- package/src/browser/react/use-push-handle.ts +113 -0
- package/src/browser/rsc-router.tsx +11 -14
- package/src/browser/types.ts +48 -0
- package/src/cache/document-cache-policy.ts +6 -5
- package/src/client.rsc.tsx +1 -0
- package/src/client.tsx +31 -1
- package/src/handle.ts +13 -5
- package/src/handles/client-pushes.ts +120 -0
- package/src/handles/deferred-resolution.ts +12 -0
- package/src/handles/pushed-by.ts +55 -1
- package/src/loader-store.ts +26 -0
- package/src/rsc/loader-fetch.ts +17 -0
- package/src/rsc/prefetch-render-error.ts +2 -1
- package/src/rsc/request-stages.ts +7 -6
- package/src/rsc/rsc-rendering.ts +32 -14
- package/src/server/handle-store.ts +23 -0
- package/src/ssr/ssr-root.tsx +5 -0
- package/src/testing/dom.entry.ts +2 -0
- package/src/testing/render-route.tsx +317 -72
- package/src/use-loader.tsx +16 -5
package/src/browser/types.ts
CHANGED
|
@@ -615,6 +615,32 @@ export type UpdateSubscriber = (update: NavigationUpdate) => void;
|
|
|
615
615
|
*/
|
|
616
616
|
export type StateListener = () => void;
|
|
617
617
|
|
|
618
|
+
/**
|
|
619
|
+
* A page read in flight (NavigationStore.beginPageRead): the page it was
|
|
620
|
+
* sent from and the loader data that page held for the loader then. The
|
|
621
|
+
* page's entries are those of the key it was sent from, those a shallow
|
|
622
|
+
* navigation shares its segments with, and those a later commit wrote in
|
|
623
|
+
* their place.
|
|
624
|
+
*/
|
|
625
|
+
export interface PageRead {
|
|
626
|
+
/**
|
|
627
|
+
* The keys of the page's entries the read is for: those holding the data
|
|
628
|
+
* it was requested for, and once it landed, those holding its result.
|
|
629
|
+
*/
|
|
630
|
+
historyKeys(): string[];
|
|
631
|
+
/** An entry of the page still holds the data the read was requested for. */
|
|
632
|
+
current(): boolean;
|
|
633
|
+
/**
|
|
634
|
+
* Write `result` into every entry of the page that holds the data the read
|
|
635
|
+
* was requested for: each of their loader segments for the loader becomes
|
|
636
|
+
* a copy carrying it, in place, and the page's read record names it.
|
|
637
|
+
* False, and nothing written, when none does.
|
|
638
|
+
*/
|
|
639
|
+
land(result: unknown): boolean;
|
|
640
|
+
/** The read landed and an entry of the page still holds its result. */
|
|
641
|
+
holds(): boolean;
|
|
642
|
+
}
|
|
643
|
+
|
|
618
644
|
/**
|
|
619
645
|
* Navigation store interface
|
|
620
646
|
*
|
|
@@ -652,6 +678,28 @@ export interface NavigationStore {
|
|
|
652
678
|
getHistoryEntryMemory(entryKey?: string): HistoryEntryMemory | undefined;
|
|
653
679
|
/** Monotonic token of the most recently committed navigation. */
|
|
654
680
|
getNavInstance(): number;
|
|
681
|
+
/**
|
|
682
|
+
* Begin a page read of `loaderId` (`load()`, a group refresh) from the
|
|
683
|
+
* entry on screen: it captures, by identity, the loader data that entry
|
|
684
|
+
* holds in every loader segment of the loader
|
|
685
|
+
* (browser/fetched-loader-handles.ts).
|
|
686
|
+
*/
|
|
687
|
+
beginPageRead(loaderId: string): PageRead;
|
|
688
|
+
/**
|
|
689
|
+
* The loaders whose data a landed page read replaced on the page of
|
|
690
|
+
* `historyKey`'s entry, since the server last sent them: the run of each
|
|
691
|
+
* is newer on the page than the document's (a late handle push keeps it).
|
|
692
|
+
*/
|
|
693
|
+
getPageReadLoaders(historyKey: string): ReadonlySet<string>;
|
|
694
|
+
/**
|
|
695
|
+
* Rewrite an entry's segments array in place (a `prefetch: false` fill):
|
|
696
|
+
* the array keeps its identity, and the page's read record is carried over
|
|
697
|
+
* as cacheSegmentsForHistory carries it.
|
|
698
|
+
*/
|
|
699
|
+
replaceEntrySegments(
|
|
700
|
+
segments: ResolvedSegment[],
|
|
701
|
+
next: readonly ResolvedSegment[],
|
|
702
|
+
): void;
|
|
655
703
|
cacheSegmentsForHistory(
|
|
656
704
|
historyKey: string,
|
|
657
705
|
segments: ResolvedSegment[],
|
|
@@ -112,17 +112,18 @@ export function shouldCacheResponse(
|
|
|
112
112
|
|
|
113
113
|
// prefetch: false. The payload carries deferred units: it answers one
|
|
114
114
|
// page's prefetch and nothing else. parseCacheControl refuses the
|
|
115
|
-
// Cache-Control it is sent with (rsc-rendering.ts NOT_REUSED
|
|
116
|
-
// middleware can replace that after next(),
|
|
117
|
-
// with this body would show a fallback no fill
|
|
115
|
+
// Cache-Control it is sent with (rsc-rendering.ts NOT_REUSED, or the
|
|
116
|
+
// route's own no-store), but a middleware can replace that after next(),
|
|
117
|
+
// and a navigation answered with this body would show a fallback no fill
|
|
118
|
+
// ever replaces.
|
|
118
119
|
if (requestCtx?._payloadDeferred) {
|
|
119
120
|
return null;
|
|
120
121
|
}
|
|
121
122
|
|
|
122
123
|
// A partial scoped to its source page: this cache keys it by
|
|
123
124
|
// URL and the ids the client holds, so it would answer another page with
|
|
124
|
-
// the same ids. rsc-rendering.ts sends it NOT_REUSED
|
|
125
|
-
// replace that after next().
|
|
125
|
+
// the same ids. rsc-rendering.ts sends it NOT_REUSED (or keeps the route's
|
|
126
|
+
// no-store); a middleware can replace that after next().
|
|
126
127
|
if (requestCtx?._payloadSourceScoped) {
|
|
127
128
|
return null;
|
|
128
129
|
}
|
package/src/client.rsc.tsx
CHANGED
|
@@ -124,5 +124,6 @@ export { useHref } from "./browser/react/use-href.js";
|
|
|
124
124
|
export { useReverse } from "./browser/react/use-reverse.js";
|
|
125
125
|
|
|
126
126
|
export { useHandle } from "./browser/react/use-handle.js";
|
|
127
|
+
export { usePushHandle } from "./browser/react/use-push-handle.js";
|
|
127
128
|
|
|
128
129
|
export { useLocationState } from "./browser/react/location-state.js";
|
package/src/client.tsx
CHANGED
|
@@ -93,7 +93,7 @@ function renderSlotContent(segment: ResolvedSegment | null): ReactNode {
|
|
|
93
93
|
// Common for intercept routes that use useLoader without a custom layout.
|
|
94
94
|
result = loaderWrapped;
|
|
95
95
|
} else {
|
|
96
|
-
result = content
|
|
96
|
+
result = <SlotSegment segment={segment}>{content}</SlotSegment>;
|
|
97
97
|
}
|
|
98
98
|
|
|
99
99
|
if (process.env.NODE_ENV !== "production" && INTERNAL_RANGO_SUSPENSE_AUDIT) {
|
|
@@ -111,6 +111,29 @@ function renderSlotContent(segment: ResolvedSegment | null): ReactNode {
|
|
|
111
111
|
return result;
|
|
112
112
|
}
|
|
113
113
|
|
|
114
|
+
/**
|
|
115
|
+
* The slot's own segment for what its content reads from OutletContext
|
|
116
|
+
* (usePushHandle files its values under it). A slot with neither a layout nor
|
|
117
|
+
* loaders has no OutletProvider of its own and would read its parent's; the
|
|
118
|
+
* rest of the parent's context (loaders, parallel, content) stays.
|
|
119
|
+
*/
|
|
120
|
+
function SlotSegment({
|
|
121
|
+
segment,
|
|
122
|
+
children,
|
|
123
|
+
}: {
|
|
124
|
+
segment: ResolvedSegment;
|
|
125
|
+
children: ReactNode;
|
|
126
|
+
}): ReactNode {
|
|
127
|
+
const context = useContext(OutletContext);
|
|
128
|
+
const value = useMemo(
|
|
129
|
+
() => (context ? { ...context, segment } : null),
|
|
130
|
+
[context, segment],
|
|
131
|
+
);
|
|
132
|
+
return (
|
|
133
|
+
<OutletContext.Provider value={value}>{children}</OutletContext.Provider>
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
114
137
|
// Dev only (suspense-audit.ts): the boundaries renderSlotContent built, as
|
|
115
138
|
// their audited components with the same props. After the fact, not a choice
|
|
116
139
|
// per JSX element: a build must fold to the product elements alone (see
|
|
@@ -136,6 +159,12 @@ function auditedSlot(node: ReactNode): ReactNode {
|
|
|
136
159
|
props as ComponentProps<typeof RouteContentWrapper>,
|
|
137
160
|
);
|
|
138
161
|
}
|
|
162
|
+
if (node.type === SlotSegment) {
|
|
163
|
+
return createElement(SlotSegment, {
|
|
164
|
+
segment: props.segment,
|
|
165
|
+
children: auditedSlot(props.children),
|
|
166
|
+
});
|
|
167
|
+
}
|
|
139
168
|
return node;
|
|
140
169
|
}
|
|
141
170
|
|
|
@@ -491,6 +520,7 @@ export {
|
|
|
491
520
|
|
|
492
521
|
export { type Handle } from "./handle.js";
|
|
493
522
|
export { useHandle } from "./browser/react/use-handle.js";
|
|
523
|
+
export { usePushHandle } from "./browser/react/use-push-handle.js";
|
|
494
524
|
|
|
495
525
|
export { Meta } from "./handles/meta.js";
|
|
496
526
|
export type { MetaDescriptor, MetaDescriptorBase } from "./router/types.js";
|
package/src/handle.ts
CHANGED
|
@@ -4,9 +4,14 @@ import { isUnderTestRunner } from "./runtime-env.js";
|
|
|
4
4
|
/**
|
|
5
5
|
* Handle definition for accumulating data across route segments.
|
|
6
6
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
7
|
+
* A handle accumulates values across the matched route segments: each
|
|
8
|
+
* segment's values, in tree order, are collected into what `useHandle`
|
|
9
|
+
* returns. Values are pushed by the server, from a handler or a loader
|
|
10
|
+
* (`ctx.use(handle)`), and by client components, for data they learn or
|
|
11
|
+
* change after the server rendered (`usePushHandle(handle)`). In each
|
|
12
|
+
* segment the client's values follow the server's. Unlike loaders (which
|
|
13
|
+
* fetch data for specific routes), handles accumulate data across all
|
|
14
|
+
* matched route segments.
|
|
10
15
|
*
|
|
11
16
|
* @example
|
|
12
17
|
* ```ts
|
|
@@ -16,11 +21,14 @@ import { isUnderTestRunner } from "./runtime-env.js";
|
|
|
16
21
|
* (segments) => segments.flat(), // opt into a single flat list
|
|
17
22
|
* );
|
|
18
23
|
*
|
|
19
|
-
* //
|
|
24
|
+
* // Push from a handler or a loader
|
|
20
25
|
* const push = ctx.use(Breadcrumbs);
|
|
21
26
|
* push({ label: "Home", href: "/" });
|
|
22
27
|
*
|
|
23
|
-
* //
|
|
28
|
+
* // Push from a client component, during render
|
|
29
|
+
* usePushHandle(Breadcrumbs)({ label: draft.name, href: "/draft" });
|
|
30
|
+
*
|
|
31
|
+
* // Read in a client component
|
|
24
32
|
* const crumbs = useHandle(Breadcrumbs);
|
|
25
33
|
* ```
|
|
26
34
|
*/
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import type { HandleData } from "../server/handle-store.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A usePushHandle component's owner object, as setClientHandle sees it:
|
|
5
|
+
* `resync` renders it again; `generation` counts the renders resync asked
|
|
6
|
+
* for, as of the render that published. Both are set by the hook before it
|
|
7
|
+
* publishes; a bare owner has neither and is never asked to resync.
|
|
8
|
+
*/
|
|
9
|
+
export interface ClientPushOwner {
|
|
10
|
+
resync?: () => void;
|
|
11
|
+
generation?: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The client handle layer (usePushHandle): handle id -> segment id -> pusher
|
|
16
|
+
* -> the values that pusher's last committed render pushed, its promises
|
|
17
|
+
* resolved. Pushers are kept in tree order (Map insertion order, which
|
|
18
|
+
* EventController.setClientHandle rebuilds when a pusher joins).
|
|
19
|
+
*/
|
|
20
|
+
export type ClientPushes = Map<
|
|
21
|
+
string,
|
|
22
|
+
Map<string, Map<ClientPushOwner, unknown[]>>
|
|
23
|
+
>;
|
|
24
|
+
|
|
25
|
+
/** Object pairs a samePushValue walk compares before it remembers equal ones. */
|
|
26
|
+
const UNREMEMBERED_PAIRS = 64;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Structural equality for pushed values: arrays and plain objects by their
|
|
30
|
+
* entries, React elements by type, key and props, dates by time, anything
|
|
31
|
+
* else (a promise, a function, a Map, a Set, a class instance, a
|
|
32
|
+
* null-prototype object) by Object.is. A component that pushes a fresh
|
|
33
|
+
* literal on each render (nested objects included) and reads the same handle
|
|
34
|
+
* would otherwise re-render itself without end: each render's push would
|
|
35
|
+
* look new. useCommitAnchoredState (browser/react/use-handle.ts) compares
|
|
36
|
+
* what useHandle and useSegments return with it too, so a re-delivery of
|
|
37
|
+
* equal content keeps the reader's reference. The walk goes no deeper than
|
|
38
|
+
* 32 levels: what it cannot compare within them, a cyclic value included,
|
|
39
|
+
* counts as different.
|
|
40
|
+
*
|
|
41
|
+
* Each pair of objects is walked once. A shared sub-object (Flight keeps a
|
|
42
|
+
* shared reference shared) is reached once per path to it, so a walk that
|
|
43
|
+
* remembers nothing is exponential in the sharing: an equal binary DAG 20
|
|
44
|
+
* levels deep took 164 ms, 22 levels 685 ms. Pairs found equal are remembered once the walk has
|
|
45
|
+
* visited UNREMEMBERED_PAIRS of them, so a small value such as a breadcrumb
|
|
46
|
+
* trail allocates no map. A pair found different needs no entry: `every`
|
|
47
|
+
* stops there and the whole walk is false. A pair still being walked is not
|
|
48
|
+
* remembered yet, so a cycle still runs into the depth bound; a pair already
|
|
49
|
+
* found equal stays equal wherever the walk meets it again, past the bound
|
|
50
|
+
* too.
|
|
51
|
+
*/
|
|
52
|
+
export function samePushValue(a: unknown, b: unknown): boolean {
|
|
53
|
+
let visits = 0;
|
|
54
|
+
let equal: Map<object, Set<object>> | undefined;
|
|
55
|
+
const same = (a: unknown, b: unknown, depth: number): boolean => {
|
|
56
|
+
if (Object.is(a, b)) return true;
|
|
57
|
+
if (a instanceof Date)
|
|
58
|
+
return b instanceof Date && a.getTime() === b.getTime();
|
|
59
|
+
const proto = a && typeof a === "object" && Object.getPrototypeOf(a);
|
|
60
|
+
if (
|
|
61
|
+
depth > 32 ||
|
|
62
|
+
!proto ||
|
|
63
|
+
!b ||
|
|
64
|
+
typeof b !== "object" ||
|
|
65
|
+
proto !== Object.getPrototypeOf(b) ||
|
|
66
|
+
(proto !== Object.prototype && proto !== Array.prototype)
|
|
67
|
+
) {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
if (++visits > UNREMEMBERED_PAIRS) equal ??= new Map();
|
|
71
|
+
const partners = equal?.get(a);
|
|
72
|
+
if (partners?.has(b)) return true;
|
|
73
|
+
// A React element's dev-only `_owner` is a fiber: compare what renders.
|
|
74
|
+
const element = "$$typeof" in a;
|
|
75
|
+
const keys = element
|
|
76
|
+
? ["$$typeof", "type", "key", "props"]
|
|
77
|
+
: Object.keys(a);
|
|
78
|
+
const result =
|
|
79
|
+
(element || keys.length === Object.keys(b).length) &&
|
|
80
|
+
keys.every(
|
|
81
|
+
(key) =>
|
|
82
|
+
key in b &&
|
|
83
|
+
same(
|
|
84
|
+
(a as Record<string, unknown>)[key],
|
|
85
|
+
(b as Record<string, unknown>)[key],
|
|
86
|
+
depth + 1,
|
|
87
|
+
),
|
|
88
|
+
);
|
|
89
|
+
if (result && equal) {
|
|
90
|
+
if (partners) partners.add(b);
|
|
91
|
+
else equal.set(a, new Set([b]));
|
|
92
|
+
}
|
|
93
|
+
return result;
|
|
94
|
+
};
|
|
95
|
+
return same(a, b, 0);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* `data` (the server's handle data) with `handleId`'s client pushes after the
|
|
100
|
+
* server's values in each segment: `[...server, ...pusher1, ...pusher2]`. A
|
|
101
|
+
* fixed position, so what arrives first never decides what shows. A segment
|
|
102
|
+
* only the client pushes into is collected in segment order like any other;
|
|
103
|
+
* one outside the order is skipped by collectHandleData.
|
|
104
|
+
*/
|
|
105
|
+
export function withClientPushes(
|
|
106
|
+
data: HandleData,
|
|
107
|
+
handleId: string,
|
|
108
|
+
pushes: ClientPushes,
|
|
109
|
+
): HandleData {
|
|
110
|
+
const bySegment = pushes.get(handleId);
|
|
111
|
+
if (!bySegment) return data;
|
|
112
|
+
const merged: Record<string, unknown[]> = { ...data[handleId] };
|
|
113
|
+
for (const [segmentId, owners] of bySegment) {
|
|
114
|
+
merged[segmentId] = [
|
|
115
|
+
...(merged[segmentId] ?? []),
|
|
116
|
+
...[...owners.values()].flat(),
|
|
117
|
+
];
|
|
118
|
+
}
|
|
119
|
+
return { ...data, [handleId]: merged };
|
|
120
|
+
}
|
|
@@ -123,6 +123,18 @@ function keptValues(resolved: ResolvedValue[]): unknown[] {
|
|
|
123
123
|
return resolved.filter((r) => r.keep).map((r) => r.value);
|
|
124
124
|
}
|
|
125
125
|
|
|
126
|
+
/**
|
|
127
|
+
* One list of pushed values with its deferred entries awaited, as
|
|
128
|
+
* resolveDeferredHandleValues awaits a segment's: shallow, in order, a
|
|
129
|
+
* rejected or nullish deferred dropped. A client component's pushes
|
|
130
|
+
* (usePushHandle) resolve through it, so they follow the server's rules.
|
|
131
|
+
*/
|
|
132
|
+
export async function resolveDeferredList(
|
|
133
|
+
values: readonly unknown[],
|
|
134
|
+
): Promise<unknown[]> {
|
|
135
|
+
return keptValues(await resolveValues(values));
|
|
136
|
+
}
|
|
137
|
+
|
|
126
138
|
/**
|
|
127
139
|
* Full-render handle stream: a one-shot async generator that yields the FINAL
|
|
128
140
|
* handle snapshot with every top-level deferred value resolved. Drop-in for
|
package/src/handles/pushed-by.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
LOADER_SEGMENT_ID,
|
|
3
|
+
filterSegmentOrder,
|
|
4
|
+
} from "../browser/react/filter-segment-order.js";
|
|
2
5
|
import type { HandleData } from "../server/handle-store.js";
|
|
3
6
|
|
|
4
7
|
/**
|
|
@@ -246,3 +249,54 @@ export function mergePartialHandleData(
|
|
|
246
249
|
}
|
|
247
250
|
return out;
|
|
248
251
|
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* The handle data after a late handle yield of the document (`late`: the
|
|
255
|
+
* document render's full state so far, `matched` its segment ids) lands on
|
|
256
|
+
* `current`, the page's data. Swapped in whole, except the runs of the
|
|
257
|
+
* loaders in `refreshed`: a fetched read landed for each since the document
|
|
258
|
+
* rendered (NavigationStore.getPageReadLoaders), so the page's values of that
|
|
259
|
+
* run are newer than the yield's and stay. The yield's values of those runs
|
|
260
|
+
* are dropped and the rest goes in as a partial response in which every
|
|
261
|
+
* other run ran (mergePartialHandleData). `late` itself when no read landed.
|
|
262
|
+
*/
|
|
263
|
+
export function mergeLateHandleData(
|
|
264
|
+
current: HandleData,
|
|
265
|
+
late: HandleData,
|
|
266
|
+
matched: readonly string[],
|
|
267
|
+
refreshed: ReadonlySet<string>,
|
|
268
|
+
): HandleData {
|
|
269
|
+
if (refreshed.size === 0) return late;
|
|
270
|
+
const incoming: HandleData = {};
|
|
271
|
+
for (const name in late) {
|
|
272
|
+
if (isPushedByKey(name)) continue;
|
|
273
|
+
const byName = pushedByKey(name);
|
|
274
|
+
for (const [segmentId, values] of Object.entries(late[name])) {
|
|
275
|
+
const by = late[byName]?.[segmentId] as PushedBy;
|
|
276
|
+
const keptValues: unknown[] = [];
|
|
277
|
+
const keptBy: (string | null)[] = [];
|
|
278
|
+
values.forEach((value, i) => {
|
|
279
|
+
const run = by?.[i] ?? null;
|
|
280
|
+
if (run !== null && refreshed.has(run)) return;
|
|
281
|
+
keptValues.push(value);
|
|
282
|
+
keptBy.push(run);
|
|
283
|
+
});
|
|
284
|
+
if (keptValues.length === 0) continue;
|
|
285
|
+
(incoming[name] ??= {})[segmentId] = keptValues;
|
|
286
|
+
if (keptBy.some((run) => run !== null)) {
|
|
287
|
+
(incoming[byName] ??= {})[segmentId] = keptBy;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
const ran = matched.filter((id) => {
|
|
292
|
+
const marker = LOADER_SEGMENT_ID.exec(id);
|
|
293
|
+
return !marker || !refreshed.has(id.slice(marker.index + marker[0].length));
|
|
294
|
+
});
|
|
295
|
+
return mergePartialHandleData(
|
|
296
|
+
current,
|
|
297
|
+
incoming,
|
|
298
|
+
filterSegmentOrder([...matched]),
|
|
299
|
+
matched,
|
|
300
|
+
ran,
|
|
301
|
+
);
|
|
302
|
+
}
|
package/src/loader-store.ts
CHANGED
|
@@ -494,6 +494,32 @@ export class LoaderStore {
|
|
|
494
494
|
this.dropIfSettled(bucketKey, e);
|
|
495
495
|
}
|
|
496
496
|
|
|
497
|
+
/**
|
|
498
|
+
* True while a reader with route context subscribes to the bucket: a value
|
|
499
|
+
* committed to it shows on the page (browser/fetched-loader-handles.ts
|
|
500
|
+
* treats a read into it as the page's own).
|
|
501
|
+
*/
|
|
502
|
+
hasRouteReaders(bucketKey: string): boolean {
|
|
503
|
+
return (this.entries.get(bucketKey)?.routeDataSources.size ?? 0) > 0;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/**
|
|
507
|
+
* True while `requestId`'s result is what the bucket holds: finishData
|
|
508
|
+
* committed it and no newer request or clear() (a route-data change) has
|
|
509
|
+
* moved the bucket on since. A fetched read's handle values and history
|
|
510
|
+
* write land exactly when this holds (browser/fetched-loader-handles.ts).
|
|
511
|
+
*/
|
|
512
|
+
holdsRequest(bucketKey: string, requestId: number): boolean {
|
|
513
|
+
const e = this.entries.get(bucketKey);
|
|
514
|
+
return (
|
|
515
|
+
e !== undefined &&
|
|
516
|
+
e.latestRequestId === requestId &&
|
|
517
|
+
e.snapshot.requestId === requestId &&
|
|
518
|
+
e.snapshot.hasValue &&
|
|
519
|
+
e.snapshot.error === null
|
|
520
|
+
);
|
|
521
|
+
}
|
|
522
|
+
|
|
497
523
|
/**
|
|
498
524
|
* Commit an error. Preserves the last good `value` so consumers can keep
|
|
499
525
|
* showing previous data while displaying the error if they choose. No-op
|
package/src/rsc/loader-fetch.ts
CHANGED
|
@@ -345,6 +345,23 @@ export async function handleLoaderFetch<TEnv>(
|
|
|
345
345
|
// registers it (handles/pushed-by.ts mergeLoaderReadHandleData).
|
|
346
346
|
pushedBy: reqCtx._handleStore.getPushedBy(loaderId) ?? {},
|
|
347
347
|
};
|
|
348
|
+
// The reply holds what the body pushed by the time it returned. A
|
|
349
|
+
// push it makes later (a timer, a promise it did not await) cannot
|
|
350
|
+
// reach the browser: warned in development, once per read.
|
|
351
|
+
let warnedLate = false;
|
|
352
|
+
reqCtx._handleStore.closeSegment(loaderId, (handleName) => {
|
|
353
|
+
if (process.env.NODE_ENV === "production" || warnedLate) return;
|
|
354
|
+
warnedLate = true;
|
|
355
|
+
console.warn(
|
|
356
|
+
`[rango] Loader "${loaderId}" pushed handle "${handleName}" ` +
|
|
357
|
+
`after its body returned, on a fetched read (load(), ` +
|
|
358
|
+
`useRefreshLoaders()). The reply carries only the values ` +
|
|
359
|
+
`pushed before the body returned, so this push came too late ` +
|
|
360
|
+
`and is not applied. Push before returning: await the work, ` +
|
|
361
|
+
`or push a promise (or reserve the slot with ` +
|
|
362
|
+
`ctx.use(Handle).defer()) and settle it later.`,
|
|
363
|
+
);
|
|
364
|
+
});
|
|
348
365
|
// Read once here: onError can fire outside the ALS frame.
|
|
349
366
|
const requestContext = _getRequestContext();
|
|
350
367
|
const rscStream = ctx.renderToReadableStream<LoaderFetchPayload>(
|
|
@@ -17,7 +17,8 @@
|
|
|
17
17
|
* a prefetch with nothing live in it carries `private, max-age`
|
|
18
18
|
* (request-stages.ts applyStoredPrefetchCacheControl); a prefetch
|
|
19
19
|
* scoped to its source page or carrying deferred units is sent
|
|
20
|
-
* `private, no-cache` (rsc-rendering.ts NOT_REUSED)
|
|
20
|
+
* `private, no-cache` (rsc-rendering.ts NOT_REUSED), or the route's own
|
|
21
|
+
* `no-store`.
|
|
21
22
|
*
|
|
22
23
|
* The errors are the render's `_renderErrors`: a Flight error row, a streamed
|
|
23
24
|
* handler recovered into its boundary, a loader that threw (redirect() and
|
|
@@ -697,12 +697,13 @@ export interface FinalizeRequestOptions<TEnv> {
|
|
|
697
697
|
* reads the route's own header when it stores the response (s-maxage), the
|
|
698
698
|
* browser gets this one, in place of any other the route set.
|
|
699
699
|
*
|
|
700
|
-
* Left as it is: a partial
|
|
701
|
-
*
|
|
702
|
-
* `no-cache`, a zero `max-age`), a redirect
|
|
703
|
-
*
|
|
704
|
-
* client's state (a per-client
|
|
705
|
-
* stored copy would replay to later
|
|
700
|
+
* Left as it is: a partial scoped to its source page or carrying deferred
|
|
701
|
+
* units (sent NOT_REUSED, or the route's own no-store), a header that
|
|
702
|
+
* forbids storing it (`no-store`, `no-cache`, a zero `max-age`), a redirect
|
|
703
|
+
* a middleware returned after the render (`redirect`, converted to a Flight
|
|
704
|
+
* redirect above), and a response carrying one client's state (a per-client
|
|
705
|
+
* signal header, location state), which a stored copy would replay to later
|
|
706
|
+
* requests.
|
|
706
707
|
*/
|
|
707
708
|
function applyStoredPrefetchCacheControl(
|
|
708
709
|
response: Response,
|
package/src/rsc/rsc-rendering.ts
CHANGED
|
@@ -1141,19 +1141,20 @@ function renderPreparedRscResponse<TEnv>(
|
|
|
1141
1141
|
reqCtx._payloadSourceScoped = true;
|
|
1142
1142
|
}
|
|
1143
1143
|
// Not reused by a shared cache or the document cache (which also refuses a
|
|
1144
|
-
// payload marked `_payloadDeferred` or `_payloadSourceScoped`)
|
|
1145
|
-
//
|
|
1146
|
-
//
|
|
1147
|
-
//
|
|
1148
|
-
//
|
|
1149
|
-
//
|
|
1150
|
-
// its headers.
|
|
1144
|
+
// payload marked `_payloadDeferred` or `_payloadSourceScoped`): a partial
|
|
1145
|
+
// scoped to its source page (above); a response that carries deferred
|
|
1146
|
+
// units, which answers one page's prefetch, and a fill, the second half of
|
|
1147
|
+
// one adoption (prefetch: false, docs/design/prefetch-false.md, R7); and a
|
|
1148
|
+
// stale partial render, which a shared cache keyed by URL must not store or
|
|
1149
|
+
// serve. A route's own `no-store` already forbids all of that and more, so
|
|
1150
|
+
// it is kept. Any other route keeps its headers.
|
|
1151
1151
|
if (
|
|
1152
|
-
reqCtx._payloadSourceScoped ||
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1152
|
+
(reqCtx._payloadSourceScoped ||
|
|
1153
|
+
reqCtx._payloadDeferred ||
|
|
1154
|
+
(isPartial &&
|
|
1155
|
+
(reqCtx._requestKind === "fill" ||
|
|
1156
|
+
url.searchParams.get(WIRE.stale) === "true"))) &&
|
|
1157
|
+
!keepsRouteNoStore(reqCtx.res.headers.get("cache-control"))
|
|
1157
1158
|
) {
|
|
1158
1159
|
rscHeaders["cache-control"] = NOT_REUSED;
|
|
1159
1160
|
}
|
|
@@ -1650,8 +1651,9 @@ function publishTailTiming(
|
|
|
1650
1651
|
const PENDING = Symbol("pending");
|
|
1651
1652
|
|
|
1652
1653
|
/**
|
|
1653
|
-
* Cache-Control of a response that is not to be reused:
|
|
1654
|
-
* deferred units (`prefetch: false`),
|
|
1654
|
+
* Cache-Control of a response that is not to be reused: a partial scoped to
|
|
1655
|
+
* its source page, one that carries deferred units (`prefetch: false`), a
|
|
1656
|
+
* fill, and a stale partial render. `private` keeps it out of
|
|
1655
1657
|
* shared caches; `no-cache` makes the browser ask again, and with no
|
|
1656
1658
|
* validator on the response that is a full request. The browser may keep the
|
|
1657
1659
|
* body all the same (Chromium does) and answers from it, without asking,
|
|
@@ -1661,9 +1663,25 @@ const PENDING = Symbol("pending");
|
|
|
1661
1663
|
* Chromium reports a `no-store` fetch whose body is read through a stream
|
|
1662
1664
|
* as canceled (net::ERR_ABORTED in DevTools, and Playwright's
|
|
1663
1665
|
* response.finished() never settles), though every byte arrived.
|
|
1666
|
+
* A route that set `no-store` itself keeps it (keepsRouteNoStore) and takes
|
|
1667
|
+
* that report, as its navigations already do: replacing it with `no-cache`
|
|
1668
|
+
* would let the browser keep a page the route said must never be stored.
|
|
1664
1669
|
*/
|
|
1665
1670
|
const NOT_REUSED = "private, no-cache";
|
|
1666
1671
|
|
|
1672
|
+
const NO_STORE_DIRECTIVE = /(^|[\s,;])no-store(?=$|[\s,;=])/i;
|
|
1673
|
+
|
|
1674
|
+
/**
|
|
1675
|
+
* Whether the route's own Cache-Control (set by a middleware before next()
|
|
1676
|
+
* or by a handler, read from the stub response) says `no-store`. Read at the
|
|
1677
|
+
* NOT_REUSED decision: every handler and middleware-before-next() write has
|
|
1678
|
+
* landed, and createResponseWithMergedHeaders copies the stub value onto the
|
|
1679
|
+
* response because the render then sets none.
|
|
1680
|
+
*/
|
|
1681
|
+
function keepsRouteNoStore(cacheControl: string | null): boolean {
|
|
1682
|
+
return cacheControl !== null && NO_STORE_DIRECTIVE.test(cacheControl);
|
|
1683
|
+
}
|
|
1684
|
+
|
|
1667
1685
|
/**
|
|
1668
1686
|
* Whether `value` is not a promise, or a promise that has already settled:
|
|
1669
1687
|
* racing it against an already-resolved promise, a settled one's reaction is
|
|
@@ -335,6 +335,15 @@ export interface HandleStore {
|
|
|
335
335
|
*/
|
|
336
336
|
releaseRecordedValues(segmentId: string): void;
|
|
337
337
|
|
|
338
|
+
/**
|
|
339
|
+
* Stop recording pushes into `segmentId`: a later push() into it, a
|
|
340
|
+
* replayed copy included, records nothing and calls `onLate` with the
|
|
341
|
+
* handle's name. The fetchable-loader lane (rsc/loader-fetch.ts) closes
|
|
342
|
+
* its loader's segment once the reply holds the body's pushes, so a push
|
|
343
|
+
* the body makes after it returned is reported instead of lost unseen.
|
|
344
|
+
*/
|
|
345
|
+
closeSegment(segmentId: string, onLate: (handleName: string) => void): void;
|
|
346
|
+
|
|
338
347
|
/**
|
|
339
348
|
* Get all collected handle data after all handlers have settled.
|
|
340
349
|
* Waits for `settled` (handler lane), then returns the data at that point.
|
|
@@ -508,6 +517,8 @@ export function createHandleStore(): HandleStore {
|
|
|
508
517
|
const placeholderLoaders = new Set<string>();
|
|
509
518
|
// Segments whose restored record values a push still goes in front of.
|
|
510
519
|
const held = new Set<string>();
|
|
520
|
+
// closeSegment: segments that record no more pushes, with what to call.
|
|
521
|
+
const closed = new Map<string, (handleName: string) => void>();
|
|
511
522
|
let authority: ((loaderId: string) => RecordAuthority) | undefined;
|
|
512
523
|
const isRestored = (loaderId: string): boolean =>
|
|
513
524
|
restoredLoaders.has(loaderId) || authority?.(loaderId) === "pin";
|
|
@@ -914,6 +925,11 @@ export function createHandleStore(): HandleStore {
|
|
|
914
925
|
loaderPush?: boolean,
|
|
915
926
|
owner?: string,
|
|
916
927
|
): void {
|
|
928
|
+
const onLate = closed.get(segmentId);
|
|
929
|
+
if (onLate) {
|
|
930
|
+
onLate(handleName);
|
|
931
|
+
return;
|
|
932
|
+
}
|
|
917
933
|
if (completed) {
|
|
918
934
|
const error = createLateHandlePushError(handleName, segmentId);
|
|
919
935
|
if (this.onError) this.onError(error);
|
|
@@ -1115,6 +1131,13 @@ export function createHandleStore(): HandleStore {
|
|
|
1115
1131
|
held.delete(segmentId);
|
|
1116
1132
|
},
|
|
1117
1133
|
|
|
1134
|
+
closeSegment(
|
|
1135
|
+
segmentId: string,
|
|
1136
|
+
onLate: (handleName: string) => void,
|
|
1137
|
+
): void {
|
|
1138
|
+
closed.set(segmentId, onLate);
|
|
1139
|
+
},
|
|
1140
|
+
|
|
1118
1141
|
replacePlaceholders(
|
|
1119
1142
|
loaderIds: readonly string[],
|
|
1120
1143
|
deliver: () => void,
|
package/src/ssr/ssr-root.tsx
CHANGED
|
@@ -120,10 +120,15 @@ function createSsrEventController(opts: {
|
|
|
120
120
|
setHandleData: () => {},
|
|
121
121
|
getHandleState: () => handleState,
|
|
122
122
|
setLoaderReadHandleData: () => false,
|
|
123
|
+
setLateHandleData: () => {},
|
|
123
124
|
lockHydration: () => {},
|
|
124
125
|
getHydrationSnapshot: () => undefined,
|
|
125
126
|
releaseHydrationLock: () => {},
|
|
126
127
|
setRouteSegmentIds: () => {},
|
|
128
|
+
setClientHandle: () => {},
|
|
129
|
+
withClientHandles: (data) => data,
|
|
130
|
+
subscribeToClientHandles: () => () => {},
|
|
131
|
+
getHeldHandleState: () => undefined,
|
|
127
132
|
setParams: (nextParams) => {
|
|
128
133
|
params = nextParams;
|
|
129
134
|
},
|