@real-router/react 0.24.0 → 0.25.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/README.md +127 -7
- package/dist/cjs/Await-whr-pcvo.d.ts +41 -0
- package/dist/cjs/Await-whr-pcvo.d.ts.map +1 -0
- package/dist/cjs/HttpStatusProvider-Dh6cMr95.d.ts +143 -0
- package/dist/cjs/HttpStatusProvider-Dh6cMr95.d.ts.map +1 -0
- package/dist/cjs/Link-BspioC76.js +2 -0
- package/dist/cjs/Link-BspioC76.js.map +1 -0
- package/dist/cjs/RouterErrorBoundary-BlJmaoyr.d.ts +47 -0
- package/dist/cjs/RouterErrorBoundary-BlJmaoyr.d.ts.map +1 -0
- package/dist/cjs/{RouterProvider-Bj1_NbpA.d.ts → RouterProvider-DvC9mViF.d.ts} +2 -2
- package/dist/cjs/RouterProvider-DvC9mViF.d.ts.map +1 -0
- package/dist/cjs/RouterProvider-fJvMO5Tc.js +2 -0
- package/dist/cjs/RouterProvider-fJvMO5Tc.js.map +1 -0
- package/dist/cjs/createHttpStatusSink-D_6IiR5E.js +2 -0
- package/dist/cjs/createHttpStatusSink-D_6IiR5E.js.map +1 -0
- package/dist/cjs/index.d.ts +6 -281
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/index.react-server.d.ts +5 -0
- package/dist/cjs/index.react-server.js +0 -0
- package/dist/cjs/ink.d.ts +2 -1
- package/dist/cjs/ink.d.ts.map +1 -1
- package/dist/cjs/ink.js +1 -1
- package/dist/cjs/ink.js.map +1 -1
- package/dist/cjs/legacy.d.ts +3 -2
- package/dist/cjs/legacy.js +1 -1
- package/dist/cjs/legacy.ssr.d.ts +3 -0
- package/dist/cjs/legacy.ssr.js +1 -0
- package/dist/cjs/ssr.d.ts +4 -0
- package/dist/cjs/ssr.js +2 -0
- package/dist/cjs/ssr.js.map +1 -0
- package/dist/cjs/ssr.react-server.d.ts +3 -0
- package/dist/cjs/ssr.react-server.js +0 -0
- package/dist/cjs/useDeferred-DE5YUsfa.d.ts +24 -0
- package/dist/cjs/useDeferred-DE5YUsfa.d.ts.map +1 -0
- package/dist/cjs/useRoute-Dudo2K_0.js +2 -0
- package/dist/cjs/useRoute-Dudo2K_0.js.map +1 -0
- package/dist/cjs/useRouteEnter-gfy65W0D.d.ts +279 -0
- package/dist/cjs/useRouteEnter-gfy65W0D.d.ts.map +1 -0
- package/dist/cjs/useRouterTransition-BfayPs0h.d.ts +56 -0
- package/dist/cjs/useRouterTransition-BfayPs0h.d.ts.map +1 -0
- package/dist/esm/Await-qSmJ-ZuN.d.mts +41 -0
- package/dist/esm/Await-qSmJ-ZuN.d.mts.map +1 -0
- package/dist/esm/HttpStatusProvider-Cy9GSANm.d.mts +143 -0
- package/dist/esm/HttpStatusProvider-Cy9GSANm.d.mts.map +1 -0
- package/dist/esm/Link-BQol2GYb.mjs +2 -0
- package/dist/esm/Link-BQol2GYb.mjs.map +1 -0
- package/dist/esm/RouterErrorBoundary-C-MH_yrP.d.mts +47 -0
- package/dist/esm/RouterErrorBoundary-C-MH_yrP.d.mts.map +1 -0
- package/dist/esm/{RouterProvider-Dr3X_vbc.d.mts → RouterProvider-D_ZlPVAC.d.mts} +2 -2
- package/dist/esm/RouterProvider-D_ZlPVAC.d.mts.map +1 -0
- package/dist/esm/RouterProvider-a9y9bqji.mjs +2 -0
- package/dist/esm/RouterProvider-a9y9bqji.mjs.map +1 -0
- package/dist/esm/createHttpStatusSink-BXWVamHE.mjs +2 -0
- package/dist/esm/createHttpStatusSink-BXWVamHE.mjs.map +1 -0
- package/dist/esm/index.d.mts +6 -281
- package/dist/esm/index.mjs +1 -1
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/index.react-server.d.mts +5 -0
- package/dist/esm/index.react-server.mjs +0 -0
- package/dist/esm/ink.d.mts +2 -1
- package/dist/esm/ink.d.mts.map +1 -1
- package/dist/esm/ink.mjs +1 -1
- package/dist/esm/ink.mjs.map +1 -1
- package/dist/esm/legacy.d.mts +3 -2
- package/dist/esm/legacy.mjs +1 -1
- package/dist/esm/legacy.ssr.d.mts +3 -0
- package/dist/esm/legacy.ssr.mjs +1 -0
- package/dist/esm/ssr.d.mts +4 -0
- package/dist/esm/ssr.mjs +2 -0
- package/dist/esm/ssr.mjs.map +1 -0
- package/dist/esm/ssr.react-server.d.mts +3 -0
- package/dist/esm/ssr.react-server.mjs +0 -0
- package/dist/esm/useDeferred-B5Qy7ttQ.d.mts +24 -0
- package/dist/esm/useDeferred-B5Qy7ttQ.d.mts.map +1 -0
- package/dist/esm/useRoute-Bta1jyl2.mjs +2 -0
- package/dist/esm/useRoute-Bta1jyl2.mjs.map +1 -0
- package/dist/esm/useRouteEnter-CxjYofFa.d.mts +279 -0
- package/dist/esm/useRouteEnter-CxjYofFa.d.mts.map +1 -0
- package/dist/esm/useRouterTransition-CI1P95ZQ.d.mts +56 -0
- package/dist/esm/useRouterTransition-CI1P95ZQ.d.mts.map +1 -0
- package/package.json +46 -4
- package/src/components/Await.tsx +46 -0
- package/src/components/ClientOnly.tsx +25 -0
- package/src/components/HttpStatusCode.tsx +66 -0
- package/src/components/HttpStatusProvider.tsx +26 -0
- package/src/components/InkLink.tsx +44 -22
- package/src/components/Link.tsx +32 -36
- package/src/components/RouterErrorBoundary.tsx +21 -6
- package/src/components/ServerOnly.tsx +26 -0
- package/src/components/Streamed.tsx +30 -0
- package/src/components/modern/RouteView/RouteView.tsx +9 -3
- package/src/components/modern/RouteView/helpers.tsx +26 -16
- package/src/constants.ts +7 -8
- package/src/context.ts +4 -2
- package/src/hooks/useDeferred.tsx +34 -0
- package/src/hooks/useIsActiveRoute.tsx +17 -9
- package/src/hooks/useNavigator.tsx +0 -2
- package/src/hooks/useRoute.tsx +10 -7
- package/src/hooks/useRouter.tsx +0 -2
- package/src/index.react-server.ts +33 -0
- package/src/index.ts +5 -0
- package/src/legacy.ssr.ts +35 -0
- package/src/legacy.ts +4 -0
- package/src/ssr.react-server.ts +21 -0
- package/src/ssr.ts +43 -0
- package/src/utils/createHttpStatusSink.ts +27 -0
- package/dist/cjs/Link-DYvAnJlk.js +0 -2
- package/dist/cjs/Link-DYvAnJlk.js.map +0 -1
- package/dist/cjs/RouterProvider-Bj1_NbpA.d.ts.map +0 -1
- package/dist/cjs/RouterProvider-CfBNR-k7.js +0 -2
- package/dist/cjs/RouterProvider-CfBNR-k7.js.map +0 -1
- package/dist/cjs/index.d.ts.map +0 -1
- package/dist/cjs/useRouterTransition-BYit_9Mt.d.ts +0 -92
- package/dist/cjs/useRouterTransition-BYit_9Mt.d.ts.map +0 -1
- package/dist/esm/Link-C7upxMc8.mjs +0 -2
- package/dist/esm/Link-C7upxMc8.mjs.map +0 -1
- package/dist/esm/RouterProvider-CS2ZoONm.mjs +0 -2
- package/dist/esm/RouterProvider-CS2ZoONm.mjs.map +0 -1
- package/dist/esm/RouterProvider-Dr3X_vbc.d.mts.map +0 -1
- package/dist/esm/index.d.mts.map +0 -1
- package/dist/esm/useRouterTransition-B65Wvngj.d.mts +0 -92
- package/dist/esm/useRouterTransition-B65Wvngj.d.mts.map +0 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Text, useFocus, useInput } from "ink";
|
|
2
|
-
import { memo
|
|
2
|
+
import { memo } from "react";
|
|
3
3
|
|
|
4
4
|
import { EMPTY_OPTIONS, EMPTY_PARAMS } from "../constants";
|
|
5
5
|
import { shallowEqual } from "../dom-utils";
|
|
@@ -9,6 +9,30 @@ import { useRouter } from "../hooks/useRouter";
|
|
|
9
9
|
import type { InkLinkProps } from "../ink-types";
|
|
10
10
|
import type { FC } from "react";
|
|
11
11
|
|
|
12
|
+
/**
|
|
13
|
+
* Pick the most specific value across focus / active / base priority.
|
|
14
|
+
* Focus wins over active; active wins over base. Each layer falls through
|
|
15
|
+
* to the next when its value is undefined, matching the original explicit
|
|
16
|
+
* if/else cascade for both `color` and `inverse`.
|
|
17
|
+
*/
|
|
18
|
+
function pickPriority<T>(
|
|
19
|
+
isFocused: boolean,
|
|
20
|
+
isRouteActive: boolean,
|
|
21
|
+
focusValue: T | undefined,
|
|
22
|
+
activeValue: T | undefined,
|
|
23
|
+
baseValue: T | undefined,
|
|
24
|
+
): T | undefined {
|
|
25
|
+
if (isFocused) {
|
|
26
|
+
return focusValue ?? activeValue ?? baseValue;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (isRouteActive) {
|
|
30
|
+
return activeValue ?? baseValue;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return baseValue;
|
|
34
|
+
}
|
|
35
|
+
|
|
12
36
|
function areInkLinkPropsEqual(
|
|
13
37
|
prev: Readonly<InkLinkProps>,
|
|
14
38
|
next: Readonly<InkLinkProps>,
|
|
@@ -61,35 +85,33 @@ const InkLinkImpl: FC<InkLinkProps> = ({
|
|
|
61
85
|
ignoreQueryParams,
|
|
62
86
|
);
|
|
63
87
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}, [onSelect, router, routeName, routeParams, routeOptions]);
|
|
68
|
-
|
|
88
|
+
// No useCallback: `useInput` consumes the handler via its own internal
|
|
89
|
+
// ref; a stable identity provides no cache-bail-out benefit here. Same
|
|
90
|
+
// reasoning as Link's handleClick.
|
|
69
91
|
useInput(
|
|
70
92
|
(_input, key) => {
|
|
71
93
|
if (key.return) {
|
|
72
|
-
|
|
94
|
+
onSelect?.();
|
|
95
|
+
router.navigate(routeName, routeParams, routeOptions).catch(() => {});
|
|
73
96
|
}
|
|
74
97
|
},
|
|
75
98
|
{ isActive: isFocused },
|
|
76
99
|
);
|
|
77
100
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
}
|
|
101
|
+
const finalColor = pickPriority(
|
|
102
|
+
isFocused,
|
|
103
|
+
isRouteActive,
|
|
104
|
+
focusColor,
|
|
105
|
+
activeColor,
|
|
106
|
+
color,
|
|
107
|
+
);
|
|
108
|
+
const finalInverse = pickPriority(
|
|
109
|
+
isFocused,
|
|
110
|
+
isRouteActive,
|
|
111
|
+
focusInverse,
|
|
112
|
+
activeInverse,
|
|
113
|
+
inverse,
|
|
114
|
+
);
|
|
93
115
|
|
|
94
116
|
const textProps: { color?: string; inverse?: boolean } = {};
|
|
95
117
|
|
package/src/components/Link.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { memo,
|
|
1
|
+
import { memo, useMemo } from "react";
|
|
2
2
|
|
|
3
3
|
import { EMPTY_PARAMS, EMPTY_OPTIONS } from "../constants";
|
|
4
4
|
import {
|
|
@@ -66,47 +66,43 @@ const LinkImpl: FC<LinkProps> = ({
|
|
|
66
66
|
hash,
|
|
67
67
|
);
|
|
68
68
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
),
|
|
77
|
-
[router, routeName, routeParams, hash],
|
|
78
|
-
);
|
|
79
|
-
|
|
80
|
-
const handleClick = useCallback(
|
|
81
|
-
(evt: MouseEvent<HTMLAnchorElement>) => {
|
|
82
|
-
if (onClick) {
|
|
83
|
-
onClick(evt);
|
|
69
|
+
// No useMemo: outer memo()+shallowEqual prevents Link re-render unless a
|
|
70
|
+
// prop actually changed. When this body runs, either `hash` differs from
|
|
71
|
+
// last render or another prop changed — in both cases the `{ hash }` alloc
|
|
72
|
+
// is unavoidable. The useMemo wrapper added one closure + deps slot per
|
|
73
|
+
// render without saving an allocation.
|
|
74
|
+
const hashOption = hash === undefined ? undefined : { hash };
|
|
75
|
+
const href = buildHref(router, routeName, routeParams, hashOption);
|
|
84
76
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
77
|
+
// useCallback was wasteful: 7 deps recreated the closure on every meaningful
|
|
78
|
+
// render anyway, and `<a onClick>` does not benefit from a stable function
|
|
79
|
+
// identity (no child-memo-bail-out chain past it). Inline arrow function is
|
|
80
|
+
// what React Compiler emits automatically for this shape.
|
|
81
|
+
const handleClick = (evt: MouseEvent<HTMLAnchorElement>) => {
|
|
82
|
+
if (onClick) {
|
|
83
|
+
onClick(evt);
|
|
89
84
|
|
|
90
|
-
if (
|
|
85
|
+
if (evt.defaultPrevented) {
|
|
91
86
|
return;
|
|
92
87
|
}
|
|
88
|
+
}
|
|
93
89
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
[onClick, target, router, routeName, routeParams, routeOptions, hash],
|
|
104
|
-
);
|
|
90
|
+
if (!shouldNavigate(evt.nativeEvent) || target === "_blank") {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
evt.preventDefault();
|
|
95
|
+
navigateWithHash(router, routeName, routeParams, hash, routeOptions).catch(
|
|
96
|
+
() => {},
|
|
97
|
+
);
|
|
98
|
+
};
|
|
105
99
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
100
|
+
// Memoize the joined class string. parseTokens + Set + join on every render
|
|
101
|
+
// adds up on pages with N Links navigating frequently; deps cover every
|
|
102
|
+
// input the function reads so cache invalidation is exact.
|
|
103
|
+
const finalClassName = useMemo(
|
|
104
|
+
() => buildActiveClassName(isActive, activeClassName, className),
|
|
105
|
+
[isActive, activeClassName, className],
|
|
110
106
|
);
|
|
111
107
|
|
|
112
108
|
return (
|
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
import { createDismissableError } from "@real-router/sources";
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
useEffect,
|
|
4
|
+
useLayoutEffect,
|
|
5
|
+
useMemo,
|
|
6
|
+
useRef,
|
|
7
|
+
useSyncExternalStore,
|
|
8
|
+
} from "react";
|
|
3
9
|
|
|
4
10
|
import { useRouter } from "../hooks/useRouter";
|
|
5
11
|
|
|
6
12
|
import type { RouterError, State } from "@real-router/core";
|
|
7
|
-
import type {
|
|
13
|
+
import type { ReactElement, ReactNode } from "react";
|
|
8
14
|
|
|
9
15
|
export interface RouterErrorBoundaryProps {
|
|
10
16
|
readonly children: ReactNode;
|
|
@@ -20,9 +26,12 @@ export function RouterErrorBoundary({
|
|
|
20
26
|
children,
|
|
21
27
|
fallback,
|
|
22
28
|
onError,
|
|
23
|
-
}: RouterErrorBoundaryProps):
|
|
29
|
+
}: RouterErrorBoundaryProps): ReactElement {
|
|
24
30
|
const router = useRouter();
|
|
25
|
-
|
|
31
|
+
// Per-router cached in @real-router/sources — the WeakMap lookup is cheap,
|
|
32
|
+
// but useMemo avoids it entirely on stable-router re-renders (the common
|
|
33
|
+
// case for boundaries mounted in app shells).
|
|
34
|
+
const store = useMemo(() => createDismissableError(router), [router]);
|
|
26
35
|
const snapshot = useSyncExternalStore(
|
|
27
36
|
store.subscribe,
|
|
28
37
|
store.getSnapshot,
|
|
@@ -31,8 +40,14 @@ export function RouterErrorBoundary({
|
|
|
31
40
|
|
|
32
41
|
const onErrorRef = useRef(onError);
|
|
33
42
|
|
|
34
|
-
//
|
|
35
|
-
|
|
43
|
+
// "Latest ref" pattern shared with useRouteEnter/useRouteExit: sync the
|
|
44
|
+
// callback to the ref via useLayoutEffect (synchronous, post-render,
|
|
45
|
+
// pre-paint) so the snapshot effect below reads the freshest callback
|
|
46
|
+
// without listing `onError` as a dep — and StrictMode's double-effect
|
|
47
|
+
// pass writes the same value twice harmlessly.
|
|
48
|
+
useLayoutEffect(() => {
|
|
49
|
+
onErrorRef.current = onError;
|
|
50
|
+
});
|
|
36
51
|
|
|
37
52
|
useEffect(() => {
|
|
38
53
|
if (snapshot.error) {
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { useEffect, useState } from "react";
|
|
2
|
+
|
|
3
|
+
import type { ReactNode } from "react";
|
|
4
|
+
|
|
5
|
+
export interface ServerOnlyProps {
|
|
6
|
+
readonly children: ReactNode;
|
|
7
|
+
readonly fallback?: ReactNode;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function ServerOnly({
|
|
11
|
+
children,
|
|
12
|
+
fallback = null,
|
|
13
|
+
}: ServerOnlyProps): ReactNode {
|
|
14
|
+
const [mounted, setMounted] = useState(false);
|
|
15
|
+
|
|
16
|
+
useEffect(() => {
|
|
17
|
+
// SSR/hydration boundary: server emits the children branch, client matches
|
|
18
|
+
// it on first paint, then this effect flips state to swap in the fallback
|
|
19
|
+
// (or hide entirely). The intentional re-render keeps markup consistent
|
|
20
|
+
// across renders.
|
|
21
|
+
// eslint-disable-next-line @eslint-react/set-state-in-effect -- intentional post-hydration swap
|
|
22
|
+
setMounted(true);
|
|
23
|
+
}, []);
|
|
24
|
+
|
|
25
|
+
return mounted ? fallback : children;
|
|
26
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
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
|
+
}
|
|
@@ -17,9 +17,15 @@ function RouteViewRoot({
|
|
|
17
17
|
// eslint-disable-next-line @eslint-react/refs -- lazy init: assign once when null to avoid `new Set()` allocation on every render
|
|
18
18
|
hasBeenActivatedRef.current ??= new Set();
|
|
19
19
|
|
|
20
|
-
// Skip the Children.
|
|
21
|
-
// reference is unchanged.
|
|
22
|
-
//
|
|
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.
|
|
23
29
|
const elements = useMemo(() => {
|
|
24
30
|
const collected: ReactElement[] = [];
|
|
25
31
|
|
|
@@ -14,6 +14,12 @@ interface FallbackSlots {
|
|
|
14
14
|
notFoundChildren: ReactNode;
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
+
// Fixed keys used by appendFallback to distinguish the Self / NotFound
|
|
18
|
+
// render slots from user-supplied <Match> children. Match render slots key
|
|
19
|
+
// off `fullSegmentName` instead — these two are the only synthetic keys.
|
|
20
|
+
const SELF_KEY = "__route-view-self__";
|
|
21
|
+
const NOT_FOUND_KEY = "__route-view-not-found__";
|
|
22
|
+
|
|
17
23
|
function isSegmentMatch(
|
|
18
24
|
routeName: string,
|
|
19
25
|
fullSegmentName: string,
|
|
@@ -34,10 +40,20 @@ export function collectElements(
|
|
|
34
40
|
children: ReactNode,
|
|
35
41
|
result: ReactElement[],
|
|
36
42
|
): void {
|
|
37
|
-
//
|
|
38
|
-
|
|
43
|
+
// Recurses into Fragment-like wrappers (anything that isn't Match / Self /
|
|
44
|
+
// NotFound) to flatten the slot tree. No explicit depth guard: typical
|
|
45
|
+
// RouteView shape is `<RouteView><Match/>...<NotFound/></RouteView>` —
|
|
46
|
+
// depth ≤ 3 in real apps. A pathological hand-written tree of N Fragments
|
|
47
|
+
// recurses N times; the call stack, not this function, is the bound.
|
|
48
|
+
//
|
|
49
|
+
// `Children.forEach` iterates without `Children.toArray`'s array allocation
|
|
50
|
+
// and per-child clone-with-synthetic-key step. We don't read child.key here
|
|
51
|
+
// (Match/Self/NotFound carry their own segment-derived keys further down),
|
|
52
|
+
// so the cheaper iterator is functionally equivalent.
|
|
53
|
+
// 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
|
|
54
|
+
Children.forEach(children, (child) => {
|
|
39
55
|
if (!isValidElement(child)) {
|
|
40
|
-
|
|
56
|
+
return;
|
|
41
57
|
}
|
|
42
58
|
|
|
43
59
|
if (
|
|
@@ -52,7 +68,7 @@ export function collectElements(
|
|
|
52
68
|
result,
|
|
53
69
|
);
|
|
54
70
|
}
|
|
55
|
-
}
|
|
71
|
+
});
|
|
56
72
|
}
|
|
57
73
|
|
|
58
74
|
function renderSlotElement(
|
|
@@ -108,12 +124,8 @@ function processMatch(
|
|
|
108
124
|
hasBeenActivated: Set<string>,
|
|
109
125
|
alreadyActive: boolean,
|
|
110
126
|
): { rendered: ReactElement | null; matched: boolean } {
|
|
111
|
-
const
|
|
112
|
-
|
|
113
|
-
exact = false,
|
|
114
|
-
keepAlive = false,
|
|
115
|
-
fallback,
|
|
116
|
-
} = child.props as MatchProps;
|
|
127
|
+
const matchProps = child.props as MatchProps;
|
|
128
|
+
const { segment, exact = false, keepAlive = false, fallback } = matchProps;
|
|
117
129
|
const fullSegmentName = nodeName ? `${nodeName}.${segment}` : segment;
|
|
118
130
|
const isActive =
|
|
119
131
|
!alreadyActive && isSegmentMatch(routeName, fullSegmentName, exact);
|
|
@@ -123,7 +135,7 @@ function processMatch(
|
|
|
123
135
|
|
|
124
136
|
return {
|
|
125
137
|
rendered: renderSlotElement(
|
|
126
|
-
|
|
138
|
+
matchProps.children,
|
|
127
139
|
fullSegmentName,
|
|
128
140
|
keepAlive,
|
|
129
141
|
"visible",
|
|
@@ -136,7 +148,7 @@ function processMatch(
|
|
|
136
148
|
if (keepAlive && hasBeenActivated.has(fullSegmentName)) {
|
|
137
149
|
return {
|
|
138
150
|
rendered: renderSlotElement(
|
|
139
|
-
|
|
151
|
+
matchProps.children,
|
|
140
152
|
fullSegmentName,
|
|
141
153
|
keepAlive,
|
|
142
154
|
"hidden",
|
|
@@ -159,7 +171,7 @@ function appendFallback(
|
|
|
159
171
|
rendered.push(
|
|
160
172
|
renderSlotElement(
|
|
161
173
|
slots.selfChildren,
|
|
162
|
-
|
|
174
|
+
SELF_KEY,
|
|
163
175
|
false,
|
|
164
176
|
"visible",
|
|
165
177
|
slots.selfFallback,
|
|
@@ -171,9 +183,7 @@ function appendFallback(
|
|
|
171
183
|
|
|
172
184
|
if (routeName === UNKNOWN_ROUTE && slots.notFoundChildren !== null) {
|
|
173
185
|
rendered.push(
|
|
174
|
-
<Fragment key=
|
|
175
|
-
{slots.notFoundChildren}
|
|
176
|
-
</Fragment>,
|
|
186
|
+
<Fragment key={NOT_FOUND_KEY}>{slots.notFoundChildren}</Fragment>,
|
|
177
187
|
);
|
|
178
188
|
}
|
|
179
189
|
}
|
package/src/constants.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
// packages/react/src/constants.ts
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Stable empty object for default params
|
|
5
|
-
*/
|
|
6
1
|
export const EMPTY_PARAMS = Object.freeze({});
|
|
7
2
|
|
|
8
|
-
/**
|
|
9
|
-
* Stable empty options object
|
|
10
|
-
*/
|
|
11
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
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
// packages/react/src/context.ts
|
|
2
|
-
|
|
3
1
|
import { createContext } from "react";
|
|
4
2
|
|
|
5
3
|
import type { RouteContext as RouteContextType } from "./types";
|
|
6
4
|
import type { Router, Navigator } from "@real-router/core";
|
|
7
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.
|
|
8
10
|
export const RouteContext = createContext<RouteContextType | null>(null);
|
|
9
11
|
|
|
10
12
|
export const RouterContext = createContext<Router | null>(null);
|
|
@@ -0,0 +1,34 @@
|
|
|
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,5 +1,5 @@
|
|
|
1
1
|
import { createActiveRouteSource } from "@real-router/sources";
|
|
2
|
-
import { useSyncExternalStore } from "react";
|
|
2
|
+
import { useMemo, useSyncExternalStore } from "react";
|
|
3
3
|
|
|
4
4
|
import { useRouter } from "./useRouter";
|
|
5
5
|
|
|
@@ -20,15 +20,23 @@ export function useIsActiveRoute(
|
|
|
20
20
|
// `hash` argument (#532) is part of the cache key when defined: a Link
|
|
21
21
|
// pointing to `/settings#account` shares its source only with other
|
|
22
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.
|
|
23
30
|
// exactOptionalPropertyTypes forbids `{ hash: undefined }` literally, so
|
|
24
|
-
// we conditionally
|
|
25
|
-
const store =
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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],
|
|
32
40
|
);
|
|
33
41
|
|
|
34
42
|
return useSyncExternalStore(
|
package/src/hooks/useRoute.tsx
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
// packages/react/src/hooks/useRoute.tsx
|
|
2
|
-
|
|
3
1
|
import { useContext } from "react";
|
|
4
2
|
|
|
5
3
|
import { RouteContext } from "../context";
|
|
@@ -7,10 +5,17 @@ import { RouteContext } from "../context";
|
|
|
7
5
|
import type { RouteContext as RouteContextType } from "../types";
|
|
8
6
|
import type { Params, State } from "@real-router/core";
|
|
9
7
|
|
|
10
|
-
|
|
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<
|
|
11
14
|
RouteContextType<P>,
|
|
12
15
|
"route"
|
|
13
|
-
> & { route: State<P> }
|
|
16
|
+
> & { route: State<P> };
|
|
17
|
+
|
|
18
|
+
export const useRoute = <P extends Params = Params>(): RouteHookResult<P> => {
|
|
14
19
|
const routeContext = useContext(RouteContext);
|
|
15
20
|
|
|
16
21
|
if (!routeContext) {
|
|
@@ -23,7 +28,5 @@ export const useRoute = <P extends Params = Params>(): Omit<
|
|
|
23
28
|
);
|
|
24
29
|
}
|
|
25
30
|
|
|
26
|
-
return routeContext as
|
|
27
|
-
route: State<P>;
|
|
28
|
-
};
|
|
31
|
+
return routeContext as RouteHookResult<P>;
|
|
29
32
|
};
|
package/src/hooks/useRouter.tsx
CHANGED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// React Server entry — type-only re-exports under `react-server` condition (RSC bundlers). See CLAUDE.md.
|
|
2
|
+
//
|
|
3
|
+
// SSR-feature prop types (`AwaitProps`, `StreamedProps`, `ClientOnlyProps`,
|
|
4
|
+
// `ServerOnlyProps`) live in the `/ssr.react-server` entry — Server
|
|
5
|
+
// Components that need them import from `@real-router/react/ssr` (resolves
|
|
6
|
+
// to types only under the `react-server` condition).
|
|
7
|
+
|
|
8
|
+
export type { LinkProps } from "./types";
|
|
9
|
+
|
|
10
|
+
export type {
|
|
11
|
+
RouteExitContext,
|
|
12
|
+
RouteExitHandler,
|
|
13
|
+
UseRouteExitOptions,
|
|
14
|
+
} from "./hooks/useRouteExit";
|
|
15
|
+
|
|
16
|
+
export type {
|
|
17
|
+
RouteEnterContext,
|
|
18
|
+
RouteEnterHandler,
|
|
19
|
+
UseRouteEnterOptions,
|
|
20
|
+
} from "./hooks/useRouteEnter";
|
|
21
|
+
|
|
22
|
+
export type {
|
|
23
|
+
RouteViewProps,
|
|
24
|
+
RouteViewMatchProps,
|
|
25
|
+
RouteViewSelfProps,
|
|
26
|
+
RouteViewNotFoundProps,
|
|
27
|
+
} from "./components/modern/RouteView";
|
|
28
|
+
|
|
29
|
+
export type { RouterErrorBoundaryProps } from "./components/RouterErrorBoundary";
|
|
30
|
+
|
|
31
|
+
export type { Navigator } from "@real-router/core";
|
|
32
|
+
|
|
33
|
+
export type { RouterTransitionSnapshot } from "@real-router/sources";
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
// Main entry point — React 19.2+
|
|
2
|
+
//
|
|
3
|
+
// Client-side hooks + components. SSR-aware components and hooks
|
|
4
|
+
// (`<ClientOnly>`, `<ServerOnly>`, `<Await>`, `<Streamed>`, `useDeferred`)
|
|
5
|
+
// have moved to the `/ssr` subpath — import them from
|
|
6
|
+
// `@real-router/react/ssr` to opt into the SSR-feature surface.
|
|
2
7
|
|
|
3
8
|
// Components
|
|
4
9
|
export { RouteView } from "./components/modern/RouteView";
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// SSR-feature entry for the legacy (React 18+) entry point.
|
|
2
|
+
//
|
|
3
|
+
// `<Await>` is excluded — it depends on React 19's `use(promise)` which is
|
|
4
|
+
// not available in React 18. React 18 consumers can compose the same
|
|
5
|
+
// pattern manually with `<Suspense>` + a Suspense-aware data library.
|
|
6
|
+
|
|
7
|
+
// Components
|
|
8
|
+
export { ClientOnly } from "./components/ClientOnly";
|
|
9
|
+
|
|
10
|
+
export { ServerOnly } from "./components/ServerOnly";
|
|
11
|
+
|
|
12
|
+
export { Streamed } from "./components/Streamed";
|
|
13
|
+
|
|
14
|
+
export { HttpStatusCode } from "./components/HttpStatusCode";
|
|
15
|
+
|
|
16
|
+
export { HttpStatusProvider } from "./components/HttpStatusProvider";
|
|
17
|
+
|
|
18
|
+
// Hooks
|
|
19
|
+
export { useDeferred } from "./hooks/useDeferred";
|
|
20
|
+
|
|
21
|
+
// Utilities
|
|
22
|
+
export { createHttpStatusSink } from "./utils/createHttpStatusSink";
|
|
23
|
+
|
|
24
|
+
// Types
|
|
25
|
+
export type { ClientOnlyProps } from "./components/ClientOnly";
|
|
26
|
+
|
|
27
|
+
export type { ServerOnlyProps } from "./components/ServerOnly";
|
|
28
|
+
|
|
29
|
+
export type { StreamedProps } from "./components/Streamed";
|
|
30
|
+
|
|
31
|
+
export type { HttpStatusCodeProps } from "./components/HttpStatusCode";
|
|
32
|
+
|
|
33
|
+
export type { HttpStatusProviderProps } from "./components/HttpStatusProvider";
|
|
34
|
+
|
|
35
|
+
export type { HttpStatusSink } from "./utils/createHttpStatusSink";
|
package/src/legacy.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
// Legacy entry point — React 18+
|
|
2
|
+
//
|
|
3
|
+
// SSR-aware components/hooks live at `@real-router/react/legacy/ssr`
|
|
4
|
+
// (subset of the React 19+ `/ssr` surface — `<Await>` is excluded since
|
|
5
|
+
// it depends on `use(promise)`).
|
|
2
6
|
|
|
3
7
|
// Components
|
|
4
8
|
export { Link } from "./components/Link";
|