@real-router/react 0.27.2 → 0.27.4
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/cjs/{Link-CyDwbrFA.js → Link-D4K4T0Ef.js} +2 -2
- package/dist/cjs/{Link-CyDwbrFA.js.map → Link-D4K4T0Ef.js.map} +1 -1
- package/dist/cjs/{RouterErrorBoundary-3UhUelqY.d.ts → RouterErrorBoundary-k17kf8Rr.d.ts} +3 -3
- package/dist/cjs/{RouterErrorBoundary-3UhUelqY.d.ts.map → RouterErrorBoundary-k17kf8Rr.d.ts.map} +1 -1
- package/dist/cjs/{RouterProvider-CPsCmrRw.js → RouterProvider-CAaKExt_.js} +2 -2
- package/dist/cjs/RouterProvider-CAaKExt_.js.map +1 -0
- package/dist/cjs/{RouterProvider-lgNarto1.d.ts → RouterProvider-xbLo90sc.d.ts} +2 -2
- package/dist/cjs/{RouterProvider-lgNarto1.d.ts.map → RouterProvider-xbLo90sc.d.ts.map} +1 -1
- package/dist/cjs/index.d.ts +3 -3
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/index.react-server.d.ts +1 -1
- package/dist/cjs/ink.d.ts +2 -2
- package/dist/cjs/ink.js +1 -1
- package/dist/cjs/legacy.d.ts +3 -3
- package/dist/cjs/legacy.js +1 -1
- package/dist/cjs/{useRouterTransition-D5nwRENd.d.ts → useRouterTransition-CRFKfwuP.d.ts} +2 -2
- package/dist/cjs/{useRouterTransition-D5nwRENd.d.ts.map → useRouterTransition-CRFKfwuP.d.ts.map} +1 -1
- package/dist/esm/{Link-uERoM4yk.mjs → Link-PQIMnFd0.mjs} +2 -2
- package/dist/esm/{Link-uERoM4yk.mjs.map → Link-PQIMnFd0.mjs.map} +1 -1
- package/dist/esm/{RouterErrorBoundary-3UhUelqY.d.mts → RouterErrorBoundary-k17kf8Rr.d.mts} +3 -3
- package/dist/esm/{RouterErrorBoundary-3UhUelqY.d.mts.map → RouterErrorBoundary-k17kf8Rr.d.mts.map} +1 -1
- package/dist/esm/{RouterProvider-DCchvq4n.d.mts → RouterProvider-CG1cqrjE.d.mts} +2 -2
- package/dist/esm/{RouterProvider-DCchvq4n.d.mts.map → RouterProvider-CG1cqrjE.d.mts.map} +1 -1
- package/dist/esm/{RouterProvider-DgHypQTJ.mjs → RouterProvider-DhZoi6au.mjs} +2 -2
- package/dist/esm/RouterProvider-DhZoi6au.mjs.map +1 -0
- package/dist/esm/index.d.mts +3 -3
- package/dist/esm/index.mjs +1 -1
- package/dist/esm/index.react-server.d.mts +1 -1
- package/dist/esm/ink.d.mts +2 -2
- package/dist/esm/ink.mjs +1 -1
- package/dist/esm/legacy.d.mts +3 -3
- package/dist/esm/legacy.mjs +1 -1
- package/dist/esm/{useRouterTransition-PCwA_qj1.d.mts → useRouterTransition-C5Ubt7rP.d.mts} +2 -2
- package/dist/esm/{useRouterTransition-PCwA_qj1.d.mts.map → useRouterTransition-C5Ubt7rP.d.mts.map} +1 -1
- package/package.json +6 -7
- package/dist/cjs/RouterProvider-CPsCmrRw.js.map +0 -1
- package/dist/esm/RouterProvider-DgHypQTJ.mjs.map +0 -1
- package/src/RouterProvider.tsx +0 -148
- package/src/components/Await.tsx +0 -46
- package/src/components/ClientOnly.tsx +0 -25
- package/src/components/HttpStatusCode.tsx +0 -66
- package/src/components/HttpStatusProvider.tsx +0 -26
- package/src/components/InkLink.tsx +0 -134
- package/src/components/InkRouterProvider.tsx +0 -9
- package/src/components/Link.tsx +0 -117
- package/src/components/RouterErrorBoundary.tsx +0 -69
- package/src/components/ServerOnly.tsx +0 -26
- package/src/components/Streamed.tsx +0 -30
- package/src/components/modern/RouteView/RouteView.tsx +0 -69
- package/src/components/modern/RouteView/components.tsx +0 -19
- package/src/components/modern/RouteView/helpers.tsx +0 -231
- package/src/components/modern/RouteView/index.ts +0 -8
- package/src/components/modern/RouteView/types.ts +0 -38
- package/src/constants.ts +0 -10
- package/src/context.ts +0 -14
- package/src/hooks/useDeferred.tsx +0 -34
- package/src/hooks/useIsActiveRoute.tsx +0 -47
- package/src/hooks/useNavigator.tsx +0 -15
- package/src/hooks/useRoute.tsx +0 -32
- package/src/hooks/useRouteEnter.tsx +0 -148
- package/src/hooks/useRouteExit.tsx +0 -159
- package/src/hooks/useRouteNode.tsx +0 -37
- package/src/hooks/useRouteUtils.tsx +0 -35
- package/src/hooks/useRouter.tsx +0 -15
- package/src/hooks/useRouterTransition.tsx +0 -17
- package/src/index.react-server.ts +0 -33
- package/src/index.ts +0 -61
- package/src/ink-types.ts +0 -25
- package/src/ink.ts +0 -30
- package/src/legacy.ssr.ts +0 -35
- package/src/legacy.ts +0 -35
- package/src/ssr.react-server.ts +0 -21
- package/src/ssr.ts +0 -43
- package/src/types.ts +0 -40
- package/src/utils/createHttpStatusSink.ts +0 -27
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { Suspense } from "react";
|
|
2
|
-
|
|
3
|
-
import type { ReactNode } from "react";
|
|
4
|
-
|
|
5
|
-
export interface StreamedProps {
|
|
6
|
-
/** Shown while any descendant `use(promise)` / `<Await>` is pending. */
|
|
7
|
-
readonly fallback: ReactNode;
|
|
8
|
-
readonly children: ReactNode;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Cross-adapter alias for `<Suspense fallback={…}>`. Pairs with `<Await>`
|
|
13
|
-
* for symmetry with `<Streamed>` boundaries in the SvelteKit / Solid
|
|
14
|
-
* deferred-data conventions.
|
|
15
|
-
*
|
|
16
|
-
* ```tsx
|
|
17
|
-
* <Streamed fallback={<Spinner />}>
|
|
18
|
-
* <Await<Review[]> name="reviews">
|
|
19
|
-
* {(reviews) => <ReviewList items={reviews} />}
|
|
20
|
-
* </Await>
|
|
21
|
-
* </Streamed>
|
|
22
|
-
* ```
|
|
23
|
-
*
|
|
24
|
-
* The component is a thin wrapper around React's native `<Suspense>` — no
|
|
25
|
-
* additional behaviour. Use plain `<Suspense>` directly if you don't need
|
|
26
|
-
* the cross-framework naming alignment.
|
|
27
|
-
*/
|
|
28
|
-
export function Streamed({ fallback, children }: StreamedProps): ReactNode {
|
|
29
|
-
return <Suspense fallback={fallback}>{children}</Suspense>;
|
|
30
|
-
}
|
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
import { useMemo, useRef } from "react";
|
|
2
|
-
|
|
3
|
-
import { Match, NotFound, Self } from "./components";
|
|
4
|
-
import { buildRenderList, collectElements } from "./helpers";
|
|
5
|
-
import { useRouteNode } from "../../../hooks/useRouteNode";
|
|
6
|
-
|
|
7
|
-
import type { RouteViewProps } from "./types";
|
|
8
|
-
import type { ReactElement } from "react";
|
|
9
|
-
|
|
10
|
-
function RouteViewRoot({
|
|
11
|
-
nodeName,
|
|
12
|
-
children,
|
|
13
|
-
}: Readonly<RouteViewProps>): ReactElement | null {
|
|
14
|
-
const { route } = useRouteNode(nodeName);
|
|
15
|
-
const hasBeenActivatedRef = useRef<Set<string> | null>(null);
|
|
16
|
-
|
|
17
|
-
// eslint-disable-next-line @eslint-react/refs -- lazy init: assign once when null to avoid `new Set()` allocation on every render
|
|
18
|
-
hasBeenActivatedRef.current ??= new Set();
|
|
19
|
-
|
|
20
|
-
// Skip the Children.forEach + collectElements traversal when the children
|
|
21
|
-
// reference is unchanged. The common SPA case is a stable JSX tree across
|
|
22
|
-
// re-renders, so the cache hits on every render except the first.
|
|
23
|
-
//
|
|
24
|
-
// Streaming SSR caveat: with `renderToReadableStream`, React may invoke
|
|
25
|
-
// RouteView multiple times across chunks with a fresh `children` reference
|
|
26
|
-
// each time. The useMemo misses on each new render and the traversal runs
|
|
27
|
-
// again — this is the expected SSR cost; the alternative would be build-
|
|
28
|
-
// time codegen of the static route tree, which is out of scope here.
|
|
29
|
-
const elements = useMemo(() => {
|
|
30
|
-
const collected: ReactElement[] = [];
|
|
31
|
-
|
|
32
|
-
collectElements(children, collected);
|
|
33
|
-
|
|
34
|
-
return collected;
|
|
35
|
-
}, [children]);
|
|
36
|
-
|
|
37
|
-
if (!route) {
|
|
38
|
-
return null;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const { rendered } = buildRenderList(
|
|
42
|
-
elements,
|
|
43
|
-
route.name,
|
|
44
|
-
nodeName,
|
|
45
|
-
// eslint-disable-next-line @eslint-react/refs -- stable Set ref read for keepAlive tracking (never reassigned)
|
|
46
|
-
hasBeenActivatedRef.current,
|
|
47
|
-
);
|
|
48
|
-
|
|
49
|
-
if (rendered.length > 0) {
|
|
50
|
-
return <>{rendered}</>;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
return null;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
RouteViewRoot.displayName = "RouteView";
|
|
57
|
-
|
|
58
|
-
export const RouteView = Object.assign(RouteViewRoot, {
|
|
59
|
-
Match,
|
|
60
|
-
Self,
|
|
61
|
-
NotFound,
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
export type {
|
|
65
|
-
RouteViewProps,
|
|
66
|
-
MatchProps as RouteViewMatchProps,
|
|
67
|
-
SelfProps as RouteViewSelfProps,
|
|
68
|
-
NotFoundProps as RouteViewNotFoundProps,
|
|
69
|
-
} from "./types";
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import type { MatchProps, NotFoundProps, SelfProps } from "./types";
|
|
2
|
-
|
|
3
|
-
export function Match(_props: MatchProps): null {
|
|
4
|
-
return null;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
Match.displayName = "RouteView.Match";
|
|
8
|
-
|
|
9
|
-
export function Self(_props: SelfProps): null {
|
|
10
|
-
return null;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
Self.displayName = "RouteView.Self";
|
|
14
|
-
|
|
15
|
-
export function NotFound(_props: NotFoundProps): null {
|
|
16
|
-
return null;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
NotFound.displayName = "RouteView.NotFound";
|
|
@@ -1,231 +0,0 @@
|
|
|
1
|
-
import { UNKNOWN_ROUTE } from "@real-router/core";
|
|
2
|
-
import { startsWithSegment } from "@real-router/route-utils";
|
|
3
|
-
import { Activity, Children, Fragment, Suspense, isValidElement } from "react";
|
|
4
|
-
|
|
5
|
-
import { Match, NotFound, Self } from "./components";
|
|
6
|
-
|
|
7
|
-
import type { MatchProps, NotFoundProps, SelfProps } from "./types";
|
|
8
|
-
import type { ReactElement, ReactNode } from "react";
|
|
9
|
-
|
|
10
|
-
const MARKER_TYPES: ReadonlySet<unknown> = new Set([Match, Self, NotFound]);
|
|
11
|
-
|
|
12
|
-
interface FallbackSlots {
|
|
13
|
-
selfChildren: ReactNode;
|
|
14
|
-
selfFallback: ReactNode | undefined;
|
|
15
|
-
selfFound: boolean;
|
|
16
|
-
notFoundChildren: ReactNode;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
// Fixed keys used by appendFallback to distinguish the Self / NotFound
|
|
20
|
-
// render slots from user-supplied <Match> children. Match render slots key
|
|
21
|
-
// off `fullSegmentName` instead — these two are the only synthetic keys.
|
|
22
|
-
const SELF_KEY = "__route-view-self__";
|
|
23
|
-
const NOT_FOUND_KEY = "__route-view-not-found__";
|
|
24
|
-
|
|
25
|
-
function isSegmentMatch(
|
|
26
|
-
routeName: string,
|
|
27
|
-
fullSegmentName: string,
|
|
28
|
-
exact: boolean,
|
|
29
|
-
): boolean {
|
|
30
|
-
if (fullSegmentName === "") {
|
|
31
|
-
return false;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
if (exact) {
|
|
35
|
-
return routeName === fullSegmentName;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
return startsWithSegment(routeName, fullSegmentName);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export function collectElements(
|
|
42
|
-
children: ReactNode,
|
|
43
|
-
result: ReactElement[],
|
|
44
|
-
): void {
|
|
45
|
-
// Recurses into Fragment-like wrappers (anything that isn't Match / Self /
|
|
46
|
-
// NotFound) to flatten the slot tree. No explicit depth guard: typical
|
|
47
|
-
// RouteView shape is `<RouteView><Match/>...<NotFound/></RouteView>` —
|
|
48
|
-
// depth ≤ 3 in real apps. A pathological hand-written tree of N Fragments
|
|
49
|
-
// recurses N times; the call stack, not this function, is the bound.
|
|
50
|
-
//
|
|
51
|
-
// `Children.forEach` iterates without `Children.toArray`'s array allocation
|
|
52
|
-
// and per-child clone-with-synthetic-key step. We don't read child.key here
|
|
53
|
-
// (Match/Self/NotFound carry their own segment-derived keys further down),
|
|
54
|
-
// so the cheaper iterator is functionally equivalent.
|
|
55
|
-
// eslint-disable-next-line @eslint-react/no-children-for-each -- intentional: collectElements is a render-hot pipeline; toArray's array+key clone is wasteful here
|
|
56
|
-
Children.forEach(children, (child) => {
|
|
57
|
-
if (!isValidElement(child)) {
|
|
58
|
-
return;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
if (MARKER_TYPES.has(child.type)) {
|
|
62
|
-
result.push(child);
|
|
63
|
-
} else {
|
|
64
|
-
collectElements(
|
|
65
|
-
(child.props as { readonly children: ReactNode }).children,
|
|
66
|
-
result,
|
|
67
|
-
);
|
|
68
|
-
}
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
function renderSlotElement(
|
|
73
|
-
slotChildren: ReactNode,
|
|
74
|
-
key: string,
|
|
75
|
-
keepAlive: boolean,
|
|
76
|
-
mode: "visible" | "hidden",
|
|
77
|
-
fallback?: ReactNode,
|
|
78
|
-
): ReactElement {
|
|
79
|
-
const content =
|
|
80
|
-
fallback === undefined ? (
|
|
81
|
-
slotChildren
|
|
82
|
-
) : (
|
|
83
|
-
<Suspense fallback={fallback}>{slotChildren}</Suspense>
|
|
84
|
-
);
|
|
85
|
-
|
|
86
|
-
if (keepAlive) {
|
|
87
|
-
return (
|
|
88
|
-
<Activity mode={mode} key={key}>
|
|
89
|
-
{content}
|
|
90
|
-
</Activity>
|
|
91
|
-
);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
return <Fragment key={key}>{content}</Fragment>;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
function recordFallback(child: ReactElement, slots: FallbackSlots): boolean {
|
|
98
|
-
if (child.type === NotFound) {
|
|
99
|
-
slots.notFoundChildren = (child.props as NotFoundProps).children;
|
|
100
|
-
|
|
101
|
-
return true;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
if (child.type === Self) {
|
|
105
|
-
// First-wins: subsequent <Self> elements are ignored, mirroring NotFound.
|
|
106
|
-
if (!slots.selfFound) {
|
|
107
|
-
slots.selfChildren = (child.props as SelfProps).children;
|
|
108
|
-
slots.selfFallback = (child.props as SelfProps).fallback;
|
|
109
|
-
slots.selfFound = true;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
return true;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
return false;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
function processMatch(
|
|
119
|
-
child: ReactElement,
|
|
120
|
-
routeName: string,
|
|
121
|
-
nodeName: string,
|
|
122
|
-
hasBeenActivated: Set<string>,
|
|
123
|
-
alreadyActive: boolean,
|
|
124
|
-
): { rendered: ReactElement | null; matched: boolean } {
|
|
125
|
-
const matchProps = child.props as MatchProps;
|
|
126
|
-
const { segment, exact = false, keepAlive = false, fallback } = matchProps;
|
|
127
|
-
const fullSegmentName = nodeName ? `${nodeName}.${segment}` : segment;
|
|
128
|
-
const isActive =
|
|
129
|
-
!alreadyActive && isSegmentMatch(routeName, fullSegmentName, exact);
|
|
130
|
-
|
|
131
|
-
if (isActive) {
|
|
132
|
-
hasBeenActivated.add(fullSegmentName);
|
|
133
|
-
|
|
134
|
-
return {
|
|
135
|
-
rendered: renderSlotElement(
|
|
136
|
-
matchProps.children,
|
|
137
|
-
fullSegmentName,
|
|
138
|
-
keepAlive,
|
|
139
|
-
"visible",
|
|
140
|
-
fallback,
|
|
141
|
-
),
|
|
142
|
-
matched: true,
|
|
143
|
-
};
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
if (keepAlive && hasBeenActivated.has(fullSegmentName)) {
|
|
147
|
-
return {
|
|
148
|
-
rendered: renderSlotElement(
|
|
149
|
-
matchProps.children,
|
|
150
|
-
fullSegmentName,
|
|
151
|
-
keepAlive,
|
|
152
|
-
"hidden",
|
|
153
|
-
fallback,
|
|
154
|
-
),
|
|
155
|
-
matched: false,
|
|
156
|
-
};
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
return { rendered: null, matched: false };
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
function appendFallback(
|
|
163
|
-
rendered: ReactElement[],
|
|
164
|
-
routeName: string,
|
|
165
|
-
nodeName: string,
|
|
166
|
-
slots: FallbackSlots,
|
|
167
|
-
): void {
|
|
168
|
-
if (slots.selfFound && routeName === nodeName) {
|
|
169
|
-
rendered.push(
|
|
170
|
-
renderSlotElement(
|
|
171
|
-
slots.selfChildren,
|
|
172
|
-
SELF_KEY,
|
|
173
|
-
false,
|
|
174
|
-
"visible",
|
|
175
|
-
slots.selfFallback,
|
|
176
|
-
),
|
|
177
|
-
);
|
|
178
|
-
|
|
179
|
-
return;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
if (routeName === UNKNOWN_ROUTE && slots.notFoundChildren !== null) {
|
|
183
|
-
rendered.push(
|
|
184
|
-
<Fragment key={NOT_FOUND_KEY}>{slots.notFoundChildren}</Fragment>,
|
|
185
|
-
);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
export function buildRenderList(
|
|
190
|
-
elements: ReactElement[],
|
|
191
|
-
routeName: string,
|
|
192
|
-
nodeName: string,
|
|
193
|
-
hasBeenActivated: Set<string>,
|
|
194
|
-
): { rendered: ReactElement[]; activeMatchFound: boolean } {
|
|
195
|
-
const slots: FallbackSlots = {
|
|
196
|
-
selfChildren: null,
|
|
197
|
-
selfFallback: undefined,
|
|
198
|
-
selfFound: false,
|
|
199
|
-
notFoundChildren: null,
|
|
200
|
-
};
|
|
201
|
-
let activeMatchFound = false;
|
|
202
|
-
const rendered: ReactElement[] = [];
|
|
203
|
-
|
|
204
|
-
for (const child of elements) {
|
|
205
|
-
if (recordFallback(child, slots)) {
|
|
206
|
-
continue;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
const result = processMatch(
|
|
210
|
-
child,
|
|
211
|
-
routeName,
|
|
212
|
-
nodeName,
|
|
213
|
-
hasBeenActivated,
|
|
214
|
-
activeMatchFound,
|
|
215
|
-
);
|
|
216
|
-
|
|
217
|
-
if (result.matched) {
|
|
218
|
-
activeMatchFound = true;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
if (result.rendered !== null) {
|
|
222
|
-
rendered.push(result.rendered);
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
if (!activeMatchFound) {
|
|
227
|
-
appendFallback(rendered, routeName, nodeName, slots);
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
return { rendered, activeMatchFound };
|
|
231
|
-
}
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import type { ReactNode } from "react";
|
|
2
|
-
|
|
3
|
-
export interface RouteViewProps {
|
|
4
|
-
/** Route tree node name to subscribe to. "" for root. */
|
|
5
|
-
readonly nodeName: string;
|
|
6
|
-
/** <RouteView.Match>, <RouteView.Self>, and <RouteView.NotFound> elements. */
|
|
7
|
-
readonly children: ReactNode;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export interface MatchProps {
|
|
11
|
-
/** Route segment to match against. */
|
|
12
|
-
readonly segment: string;
|
|
13
|
-
/** Exact match only (no descendants). Defaults to false. */
|
|
14
|
-
readonly exact?: boolean;
|
|
15
|
-
/** Preserve component state when deactivated (React Activity). Defaults to false. */
|
|
16
|
-
readonly keepAlive?: boolean;
|
|
17
|
-
/** Fallback content to show while children are suspended. */
|
|
18
|
-
readonly fallback?: ReactNode;
|
|
19
|
-
/** Content to render when matched. */
|
|
20
|
-
readonly children: ReactNode;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export interface SelfProps {
|
|
24
|
-
/**
|
|
25
|
-
* Fallback content to show while children are suspended.
|
|
26
|
-
*
|
|
27
|
-
* Symmetric with `<RouteView.Match fallback>` — wraps children in
|
|
28
|
-
* `<Suspense>` when defined.
|
|
29
|
-
*/
|
|
30
|
-
readonly fallback?: ReactNode;
|
|
31
|
-
/** Content to render when the active route name equals the parent RouteView's nodeName. */
|
|
32
|
-
readonly children: ReactNode;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export interface NotFoundProps {
|
|
36
|
-
/** Content to render on UNKNOWN_ROUTE. */
|
|
37
|
-
readonly children: ReactNode;
|
|
38
|
-
}
|
package/src/constants.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
export const EMPTY_PARAMS = Object.freeze({});
|
|
2
|
-
|
|
3
|
-
export const EMPTY_OPTIONS = Object.freeze({});
|
|
4
|
-
|
|
5
|
-
// Singleton forever-pending promise — module-scope so `useDeferred(unknownKey)`
|
|
6
|
-
// returns a stable reference across calls (Suspense boundary doesn't retry).
|
|
7
|
-
export const NEVER_PROMISE = new Promise<never>(() => {
|
|
8
|
-
// Intentionally never resolves — surfaces a forever-pending Suspense boundary
|
|
9
|
-
// when a key is requested that the loader never declared.
|
|
10
|
-
});
|
package/src/context.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { createContext } from "react";
|
|
2
|
-
|
|
3
|
-
import type { RouteContext as RouteContextType } from "./types";
|
|
4
|
-
import type { Router, Navigator } from "@real-router/core";
|
|
5
|
-
|
|
6
|
-
// All three contexts use the `<T | null>` shape with a `null` default. Hooks
|
|
7
|
-
// (useRouter / useRoute / useNavigator) treat a `null` read as
|
|
8
|
-
// "RouterProvider missing" and throw — this surfaces wiring mistakes loudly
|
|
9
|
-
// instead of letting consumers chase an undefined router instance.
|
|
10
|
-
export const RouteContext = createContext<RouteContextType | null>(null);
|
|
11
|
-
|
|
12
|
-
export const RouterContext = createContext<Router | null>(null);
|
|
13
|
-
|
|
14
|
-
export const NavigatorContext = createContext<Navigator | null>(null);
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { NEVER_PROMISE } from "../constants";
|
|
2
|
-
import { useRoute } from "./useRoute";
|
|
3
|
-
|
|
4
|
-
interface DeferredContext {
|
|
5
|
-
ssrDataDeferred?: Record<string, Promise<unknown>>;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Read a deferred promise published by `defer({ deferred: { <key>: Promise } })`
|
|
10
|
-
* inside an SSR data loader.
|
|
11
|
-
*
|
|
12
|
-
* - **Server render**: returns the actual loader-returned promise. Combine
|
|
13
|
-
* with `<Suspense>` + `use(promise)` for native streaming via React 19's
|
|
14
|
-
* `renderToReadableStream`.
|
|
15
|
-
* - **Post-hydration**: returns a registry-backed promise; the inline
|
|
16
|
-
* `<script>__rrDefer__("key", json)</script>` tags emitted by the server
|
|
17
|
-
* stream resolve it. `use()` returns synchronously once the registry
|
|
18
|
-
* settles.
|
|
19
|
-
* - **Unknown key**: returns a never-resolving promise — Suspense boundary
|
|
20
|
-
* stays in fallback. This surfaces consumer/loader key drift as a visible
|
|
21
|
-
* loading state instead of a silent runtime error.
|
|
22
|
-
*
|
|
23
|
-
* The hook subscribes to `RouteContext`, so it re-runs on every navigation.
|
|
24
|
-
* Promise reference identity is stable across renders within the same
|
|
25
|
-
* navigation — `use()` will not re-suspend on rerenders.
|
|
26
|
-
*/
|
|
27
|
-
export function useDeferred<T = unknown>(key: string): Promise<T> {
|
|
28
|
-
const { route } = useRoute();
|
|
29
|
-
const context = route.context as DeferredContext;
|
|
30
|
-
const deferred = context.ssrDataDeferred;
|
|
31
|
-
const promise = deferred?.[key];
|
|
32
|
-
|
|
33
|
-
return (promise ?? NEVER_PROMISE) as Promise<T>;
|
|
34
|
-
}
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { createActiveRouteSource } from "@real-router/sources";
|
|
2
|
-
import { useMemo, useSyncExternalStore } from "react";
|
|
3
|
-
|
|
4
|
-
import { useRouter } from "./useRouter";
|
|
5
|
-
|
|
6
|
-
import type { Params } from "@real-router/core";
|
|
7
|
-
|
|
8
|
-
export function useIsActiveRoute(
|
|
9
|
-
routeName: string,
|
|
10
|
-
params?: Params,
|
|
11
|
-
strict = false,
|
|
12
|
-
ignoreQueryParams = true,
|
|
13
|
-
hash?: string,
|
|
14
|
-
): boolean {
|
|
15
|
-
const router = useRouter();
|
|
16
|
-
|
|
17
|
-
// createActiveRouteSource is per-router + canonical-args cached in
|
|
18
|
-
// @real-router/sources, so passing params by reference is safe — equivalent
|
|
19
|
-
// param shapes hit the same cache entry regardless of key order. The
|
|
20
|
-
// `hash` argument (#532) is part of the cache key when defined: a Link
|
|
21
|
-
// pointing to `/settings#account` shares its source only with other
|
|
22
|
-
// consumers using the same routeName + params + hash.
|
|
23
|
-
//
|
|
24
|
-
// The useMemo wrap skips `canonicalJson(params)` + cache lookup on every
|
|
25
|
-
// render when all primitive deps and the `params` reference are stable —
|
|
26
|
-
// the common case once memo()+shallowEqual has bailed out further up
|
|
27
|
-
// (or when the parent re-renders for a non-Link reason). For inline
|
|
28
|
-
// `params={{id:1}}` the dep changes per render and the lookup still
|
|
29
|
-
// runs, but that path was already the slow path before this memo.
|
|
30
|
-
// exactOptionalPropertyTypes forbids `{ hash: undefined }` literally, so
|
|
31
|
-
// we conditionally spread the key only when the caller passed a value.
|
|
32
|
-
const store = useMemo(
|
|
33
|
-
() =>
|
|
34
|
-
createActiveRouteSource(router, routeName, params, {
|
|
35
|
-
strict,
|
|
36
|
-
ignoreQueryParams,
|
|
37
|
-
...(hash !== undefined && { hash }),
|
|
38
|
-
}),
|
|
39
|
-
[router, routeName, params, strict, ignoreQueryParams, hash],
|
|
40
|
-
);
|
|
41
|
-
|
|
42
|
-
return useSyncExternalStore(
|
|
43
|
-
store.subscribe,
|
|
44
|
-
store.getSnapshot,
|
|
45
|
-
store.getSnapshot,
|
|
46
|
-
);
|
|
47
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { useContext } from "react";
|
|
2
|
-
|
|
3
|
-
import { NavigatorContext } from "../context";
|
|
4
|
-
|
|
5
|
-
import type { Navigator } from "@real-router/core";
|
|
6
|
-
|
|
7
|
-
export const useNavigator = (): Navigator => {
|
|
8
|
-
const navigator = useContext(NavigatorContext);
|
|
9
|
-
|
|
10
|
-
if (!navigator) {
|
|
11
|
-
throw new Error("useNavigator must be used within a RouterProvider");
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
return navigator;
|
|
15
|
-
};
|
package/src/hooks/useRoute.tsx
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { useContext } from "react";
|
|
2
|
-
|
|
3
|
-
import { RouteContext } from "../context";
|
|
4
|
-
|
|
5
|
-
import type { RouteContext as RouteContextType } from "../types";
|
|
6
|
-
import type { Params, State } from "@real-router/core";
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Return shape of `useRoute<P>()` — the context with `route` narrowed to
|
|
10
|
-
* `State<P>` (non-nullable). Promoting the intersection to a named alias
|
|
11
|
-
* keeps the function signature and the cast site in sync.
|
|
12
|
-
*/
|
|
13
|
-
type RouteHookResult<P extends Params = Params> = Omit<
|
|
14
|
-
RouteContextType<P>,
|
|
15
|
-
"route"
|
|
16
|
-
> & { route: State<P> };
|
|
17
|
-
|
|
18
|
-
export const useRoute = <P extends Params = Params>(): RouteHookResult<P> => {
|
|
19
|
-
const routeContext = useContext(RouteContext);
|
|
20
|
-
|
|
21
|
-
if (!routeContext) {
|
|
22
|
-
throw new Error("useRoute must be used within a RouterProvider");
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
if (!routeContext.route) {
|
|
26
|
-
throw new Error(
|
|
27
|
-
"useRoute called with no active route. Did you forget to await router.start() before rendering, or is the router stopped/disposed?",
|
|
28
|
-
);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
return routeContext as RouteHookResult<P>;
|
|
32
|
-
};
|