@ivogt/rsc-router 0.0.0-experimental.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.
Files changed (123) hide show
  1. package/README.md +19 -0
  2. package/package.json +131 -0
  3. package/src/__mocks__/version.ts +6 -0
  4. package/src/__tests__/route-definition.test.ts +63 -0
  5. package/src/browser/event-controller.ts +876 -0
  6. package/src/browser/index.ts +18 -0
  7. package/src/browser/link-interceptor.ts +121 -0
  8. package/src/browser/lru-cache.ts +69 -0
  9. package/src/browser/merge-segment-loaders.ts +126 -0
  10. package/src/browser/navigation-bridge.ts +891 -0
  11. package/src/browser/navigation-client.ts +155 -0
  12. package/src/browser/navigation-store.ts +823 -0
  13. package/src/browser/partial-update.ts +545 -0
  14. package/src/browser/react/Link.tsx +248 -0
  15. package/src/browser/react/NavigationProvider.tsx +228 -0
  16. package/src/browser/react/ScrollRestoration.tsx +94 -0
  17. package/src/browser/react/context.ts +53 -0
  18. package/src/browser/react/index.ts +52 -0
  19. package/src/browser/react/location-state-shared.ts +120 -0
  20. package/src/browser/react/location-state.ts +62 -0
  21. package/src/browser/react/use-action.ts +240 -0
  22. package/src/browser/react/use-client-cache.ts +56 -0
  23. package/src/browser/react/use-handle.ts +178 -0
  24. package/src/browser/react/use-link-status.ts +134 -0
  25. package/src/browser/react/use-navigation.ts +150 -0
  26. package/src/browser/react/use-segments.ts +188 -0
  27. package/src/browser/request-controller.ts +149 -0
  28. package/src/browser/rsc-router.tsx +310 -0
  29. package/src/browser/scroll-restoration.ts +324 -0
  30. package/src/browser/server-action-bridge.ts +747 -0
  31. package/src/browser/shallow.ts +35 -0
  32. package/src/browser/types.ts +443 -0
  33. package/src/cache/__tests__/memory-segment-store.test.ts +487 -0
  34. package/src/cache/__tests__/memory-store.test.ts +484 -0
  35. package/src/cache/cache-scope.ts +565 -0
  36. package/src/cache/cf/__tests__/cf-cache-store.test.ts +361 -0
  37. package/src/cache/cf/cf-cache-store.ts +274 -0
  38. package/src/cache/cf/index.ts +19 -0
  39. package/src/cache/index.ts +52 -0
  40. package/src/cache/memory-segment-store.ts +150 -0
  41. package/src/cache/memory-store.ts +253 -0
  42. package/src/cache/types.ts +366 -0
  43. package/src/client.rsc.tsx +88 -0
  44. package/src/client.tsx +609 -0
  45. package/src/components/DefaultDocument.tsx +20 -0
  46. package/src/default-error-boundary.tsx +88 -0
  47. package/src/deps/browser.ts +8 -0
  48. package/src/deps/html-stream-client.ts +2 -0
  49. package/src/deps/html-stream-server.ts +2 -0
  50. package/src/deps/rsc.ts +10 -0
  51. package/src/deps/ssr.ts +2 -0
  52. package/src/errors.ts +259 -0
  53. package/src/handle.ts +120 -0
  54. package/src/handles/MetaTags.tsx +178 -0
  55. package/src/handles/index.ts +6 -0
  56. package/src/handles/meta.ts +247 -0
  57. package/src/href-client.ts +128 -0
  58. package/src/href.ts +139 -0
  59. package/src/index.rsc.ts +69 -0
  60. package/src/index.ts +84 -0
  61. package/src/loader.rsc.ts +204 -0
  62. package/src/loader.ts +47 -0
  63. package/src/network-error-thrower.tsx +21 -0
  64. package/src/outlet-context.ts +15 -0
  65. package/src/root-error-boundary.tsx +277 -0
  66. package/src/route-content-wrapper.tsx +198 -0
  67. package/src/route-definition.ts +1333 -0
  68. package/src/route-map-builder.ts +140 -0
  69. package/src/route-types.ts +148 -0
  70. package/src/route-utils.ts +89 -0
  71. package/src/router/__tests__/match-context.test.ts +104 -0
  72. package/src/router/__tests__/match-pipelines.test.ts +537 -0
  73. package/src/router/__tests__/match-result.test.ts +566 -0
  74. package/src/router/__tests__/on-error.test.ts +935 -0
  75. package/src/router/__tests__/pattern-matching.test.ts +577 -0
  76. package/src/router/error-handling.ts +287 -0
  77. package/src/router/handler-context.ts +60 -0
  78. package/src/router/loader-resolution.ts +326 -0
  79. package/src/router/manifest.ts +116 -0
  80. package/src/router/match-context.ts +261 -0
  81. package/src/router/match-middleware/background-revalidation.ts +236 -0
  82. package/src/router/match-middleware/cache-lookup.ts +261 -0
  83. package/src/router/match-middleware/cache-store.ts +250 -0
  84. package/src/router/match-middleware/index.ts +81 -0
  85. package/src/router/match-middleware/intercept-resolution.ts +268 -0
  86. package/src/router/match-middleware/segment-resolution.ts +174 -0
  87. package/src/router/match-pipelines.ts +214 -0
  88. package/src/router/match-result.ts +212 -0
  89. package/src/router/metrics.ts +62 -0
  90. package/src/router/middleware.test.ts +1355 -0
  91. package/src/router/middleware.ts +748 -0
  92. package/src/router/pattern-matching.ts +271 -0
  93. package/src/router/revalidation.ts +190 -0
  94. package/src/router/router-context.ts +299 -0
  95. package/src/router/types.ts +96 -0
  96. package/src/router.ts +3484 -0
  97. package/src/rsc/__tests__/helpers.test.ts +175 -0
  98. package/src/rsc/handler.ts +942 -0
  99. package/src/rsc/helpers.ts +64 -0
  100. package/src/rsc/index.ts +56 -0
  101. package/src/rsc/nonce.ts +18 -0
  102. package/src/rsc/types.ts +225 -0
  103. package/src/segment-system.tsx +405 -0
  104. package/src/server/__tests__/request-context.test.ts +171 -0
  105. package/src/server/context.ts +340 -0
  106. package/src/server/handle-store.ts +230 -0
  107. package/src/server/loader-registry.ts +174 -0
  108. package/src/server/request-context.ts +470 -0
  109. package/src/server/root-layout.tsx +10 -0
  110. package/src/server/tsconfig.json +14 -0
  111. package/src/server.ts +126 -0
  112. package/src/ssr/__tests__/ssr-handler.test.tsx +188 -0
  113. package/src/ssr/index.tsx +215 -0
  114. package/src/types.ts +1473 -0
  115. package/src/use-loader.tsx +346 -0
  116. package/src/vite/__tests__/expose-loader-id.test.ts +117 -0
  117. package/src/vite/expose-action-id.ts +344 -0
  118. package/src/vite/expose-handle-id.ts +209 -0
  119. package/src/vite/expose-loader-id.ts +357 -0
  120. package/src/vite/expose-location-state-id.ts +177 -0
  121. package/src/vite/index.ts +608 -0
  122. package/src/vite/version.d.ts +12 -0
  123. package/src/vite/virtual-entries.ts +109 -0
@@ -0,0 +1,120 @@
1
+ /**
2
+ * Shared location state utilities - works in both RSC and client contexts
3
+ * No "use client" directive so it can be imported from RSC
4
+ */
5
+
6
+ /**
7
+ * Internal entry representing a state value with its unique key
8
+ */
9
+ export interface LocationStateEntry {
10
+ readonly __rsc_ls_key: string;
11
+ readonly __rsc_ls_value: unknown;
12
+ }
13
+
14
+ /**
15
+ * Type-safe location state definition
16
+ *
17
+ * Created via createLocationState(), used with Link's state prop
18
+ * and useLocationState() hook.
19
+ */
20
+ export interface LocationStateDefinition<TArgs extends unknown[], TState> {
21
+ (...args: TArgs): LocationStateEntry;
22
+ readonly __rsc_ls_key: string;
23
+ }
24
+
25
+ // Track used keys to detect duplicates in development
26
+ const usedKeys = new Set<string>();
27
+
28
+ /**
29
+ * Create a type-safe location state definition
30
+ *
31
+ * The key is auto-generated by the Vite exposeLocationStateId plugin based on
32
+ * file path and export name. No manual key required.
33
+ *
34
+ * @param key Auto-injected by Vite plugin, do not provide manually
35
+ * @returns A typed state definition for use with Link and useLocationState
36
+ *
37
+ * @example
38
+ * ```typescript
39
+ * // Define typed state (key auto-generated from file + export)
40
+ * export const ProductState = createLocationState<{ name: string; price: number }>();
41
+ *
42
+ * // Use in Link - state is captured at click time
43
+ * <Link to="/product/123" state={[ProductState({ name: product.name, price: product.price })]}>
44
+ * View Product
45
+ * </Link>
46
+ *
47
+ * // Multiple states
48
+ * <Link to="/checkout" state={[ProductState(productData), CartState(cartData)]}>
49
+ * Checkout
50
+ * </Link>
51
+ *
52
+ * // For lazy evaluation (click-time), pass a getter
53
+ * <Link to="/product" state={[ProductState(() => ({ name: product.name }))]}>
54
+ *
55
+ * // Read with type safety
56
+ * const productState = useLocationState(ProductState);
57
+ * // productState: { name: string; price: number } | undefined
58
+ * ```
59
+ */
60
+ export function createLocationState<TState>(
61
+ key?: string
62
+ ): LocationStateDefinition<[TState | (() => TState)], TState> {
63
+ if (!key && process.env.NODE_ENV !== "production") {
64
+ console.warn(
65
+ "[rsc-router] createLocationState is missing a key. " +
66
+ "Make sure the exposeLocationStateId Vite plugin is enabled and " +
67
+ "the state is exported with: export const MyState = createLocationState(...)"
68
+ );
69
+ }
70
+ const fullKey = `__rsc_ls_${key}`;
71
+
72
+ // Warn about duplicate keys in development
73
+ if (process.env.NODE_ENV !== "production" && usedKeys.has(fullKey)) {
74
+ console.warn(
75
+ `[rsc-router] Duplicate location state key "${key}". ` +
76
+ `Each createLocationState call should have a unique key.`
77
+ );
78
+ }
79
+ usedKeys.add(fullKey);
80
+
81
+ const definition = Object.assign(
82
+ (stateOrGetter: TState | (() => TState)): LocationStateEntry => ({
83
+ __rsc_ls_key: fullKey,
84
+ // Resolve getter immediately - lazy evaluation happens via Link's stateRef pattern
85
+ __rsc_ls_value:
86
+ typeof stateOrGetter === "function"
87
+ ? (stateOrGetter as () => TState)()
88
+ : stateOrGetter,
89
+ }),
90
+ { __rsc_ls_key: fullKey }
91
+ );
92
+
93
+ return definition as LocationStateDefinition<[TState | (() => TState)], TState>;
94
+ }
95
+
96
+ /**
97
+ * Check if a value is a LocationStateEntry
98
+ */
99
+ export function isLocationStateEntry(value: unknown): value is LocationStateEntry {
100
+ return (
101
+ value !== null &&
102
+ typeof value === "object" &&
103
+ "__rsc_ls_key" in value &&
104
+ "__rsc_ls_value" in value &&
105
+ typeof (value as LocationStateEntry).__rsc_ls_key === "string"
106
+ );
107
+ }
108
+
109
+ /**
110
+ * Resolve state entries into a flat object for history.state
111
+ */
112
+ export function resolveLocationStateEntries(
113
+ entries: LocationStateEntry[]
114
+ ): Record<string, unknown> {
115
+ const result: Record<string, unknown> = {};
116
+ for (const entry of entries) {
117
+ result[entry.__rsc_ls_key] = entry.__rsc_ls_value;
118
+ }
119
+ return result;
120
+ }
@@ -0,0 +1,62 @@
1
+ "use client";
2
+
3
+ import { useState, useEffect } from "react";
4
+ import type { LocationStateDefinition } from "./location-state-shared.js";
5
+
6
+ // Re-export shared utilities and types
7
+ export {
8
+ createLocationState,
9
+ isLocationStateEntry,
10
+ resolveLocationStateEntries,
11
+ type LocationStateEntry,
12
+ type LocationStateDefinition,
13
+ } from "./location-state-shared.js";
14
+
15
+ /**
16
+ * Hook to read location state from history.state
17
+ *
18
+ * Overloaded:
19
+ * - With definition: Returns typed state from the specific key
20
+ * - With type param only: Returns legacy state from history.state.state (backwards compat)
21
+ *
22
+ * @example
23
+ * ```typescript
24
+ * // Typed access with definition (recommended)
25
+ * const ProductState = createLocationState<{ name: string }>("product");
26
+ * const state = useLocationState(ProductState);
27
+ * // state: { name: string } | undefined
28
+ *
29
+ * // Legacy typed access (backwards compatible)
30
+ * const legacyState = useLocationState<{ from?: string }>();
31
+ * ```
32
+ */
33
+ export function useLocationState<TArgs extends unknown[], TState>(
34
+ definition: LocationStateDefinition<TArgs, TState>
35
+ ): TState | undefined;
36
+ export function useLocationState<T = unknown>(): T | undefined;
37
+ export function useLocationState<TArgs extends unknown[], TState>(
38
+ definition?: LocationStateDefinition<TArgs, TState>
39
+ ): TState | undefined {
40
+ const [state, setState] = useState<TState | undefined>(() => {
41
+ if (typeof window === "undefined") return undefined;
42
+ if (definition) {
43
+ return window.history.state?.[definition.__rsc_ls_key] as TState | undefined;
44
+ }
45
+ // Legacy: return history.state.state for backwards compatibility
46
+ return window.history.state?.state as TState | undefined;
47
+ });
48
+
49
+ useEffect(() => {
50
+ const handlePopstate = () => {
51
+ if (definition) {
52
+ setState(window.history.state?.[definition.__rsc_ls_key] as TState | undefined);
53
+ } else {
54
+ setState(window.history.state?.state as TState | undefined);
55
+ }
56
+ };
57
+ window.addEventListener("popstate", handlePopstate);
58
+ return () => window.removeEventListener("popstate", handlePopstate);
59
+ }, [definition]);
60
+
61
+ return state;
62
+ }
@@ -0,0 +1,240 @@
1
+ "use client";
2
+
3
+ import {
4
+ useContext,
5
+ useState,
6
+ useEffect,
7
+ useRef,
8
+ useOptimistic,
9
+ startTransition,
10
+ } from "react";
11
+ import { NavigationStoreContext } from "./context.js";
12
+ import type { TrackedActionState, ActionLifecycleState } from "../types.js";
13
+ import { invariant } from "../../errors.js";
14
+
15
+ /**
16
+ * Default action state (idle with no payload)
17
+ */
18
+ const DEFAULT_ACTION_STATE: TrackedActionState = {
19
+ state: "idle",
20
+ actionId: null,
21
+ payload: null,
22
+ error: null,
23
+ result: null,
24
+ };
25
+
26
+ /**
27
+ * Normalize action ID - returns the ID as-is
28
+ *
29
+ * Server actions have IDs like "hash#actionName" or "src/actions.ts#actionName".
30
+ * When using function references, we use the full ID for exact matching.
31
+ * When using strings, the event controller supports suffix matching
32
+ * (e.g., "addToCart" matches "hash#addToCart").
33
+ */
34
+ function normalizeActionId(actionId: string): string {
35
+ return actionId;
36
+ }
37
+
38
+ /**
39
+ * Extract action ID from a server action function or string.
40
+ *
41
+ * Actions passed as props from server components lose their metadata
42
+ * during RSC serialization - use a string action name instead.
43
+ */
44
+ export function getActionId(action: ServerActionFunction | string): string {
45
+ invariant(
46
+ typeof action === "function" || typeof action === "string",
47
+ `useAction: action must be a function or string, got ${typeof action}`
48
+ );
49
+ const actionId = (action as any)?.$$id;
50
+ if (actionId) {
51
+ return normalizeActionId(actionId);
52
+ }
53
+
54
+ // If action is a string, use it directly
55
+ if (typeof action === "string") {
56
+ return action;
57
+ }
58
+
59
+ // If we get here, this is likely an action passed as prop from a server component
60
+ // These lose their metadata during RSC serialization
61
+ throw new Error(
62
+ `useAction: Cannot extract action ID from function reference.
63
+
64
+ This typically happens when an action is passed as a prop from a server component.
65
+ Actions passed through RSC lose their metadata during serialization.
66
+
67
+ Solutions:
68
+ 1. Import the action directly in your client component:
69
+ import { myAction } from './actions';
70
+ const state = useAction(myAction);
71
+
72
+ 2. Use the action name as a string:
73
+ const state = useAction("myAction");
74
+
75
+ The string must match the exported function name from your "use server" file.`
76
+ );
77
+ }
78
+
79
+ /**
80
+ * Server action function type
81
+ * Server actions have a $$id property added by the RSC compiler
82
+ */
83
+ export type ServerActionFunction = ((...args: any[]) => Promise<any>) & {
84
+ $$id?: string;
85
+ };
86
+
87
+ /**
88
+ * Hook to track the lifecycle of a specific server action
89
+ *
90
+ * Unlike useNavigation which tracks global navigation state, useAction
91
+ * tracks the state of individual server action invocations.
92
+ *
93
+ * Uses the event controller for reactive state management.
94
+ * State is derived from the inflight actions tracked by the controller.
95
+ *
96
+ * Features:
97
+ * - Tracks action lifecycle: idle → loading → streaming → idle
98
+ * - Captures result/error locally (React handles cleanup)
99
+ * - If multiple actions fire, tracks only the last one
100
+ * - Supports selector pattern like useNavigation
101
+ *
102
+ * Matching behavior:
103
+ * - **Function reference**: Uses full $$id for exact matching. This is precise
104
+ * and distinguishes between actions with the same name in different files.
105
+ * - **String**: Matches by suffix (action name after #). This is convenient
106
+ * but may be ambiguous if multiple files export the same action name.
107
+ *
108
+ * @param action - Either a server action function or a string action name.
109
+ * - **Function**: Must be directly imported in the client component.
110
+ * Actions passed as props from server components will throw an error.
111
+ * - **String**: The exported function name from your "use server" file.
112
+ * Matches any action ending with "#actionName" (suffix match).
113
+ *
114
+ * @example
115
+ * ```tsx
116
+ * // Option 1: Direct import (precise matching)
117
+ * import { addToCart } from './actions';
118
+ * const actionState = useAction(addToCart);
119
+ *
120
+ * // Option 2: String-based (suffix matching)
121
+ * // Matches "hash#addToCart" or "src/actions.ts#addToCart"
122
+ * const actionState = useAction('addToCart');
123
+ *
124
+ * // With selector for specific values
125
+ * const isLoading = useAction(addToCart, state => state.state === 'loading');
126
+ * const error = useAction(addToCart, state => state.error);
127
+ * ```
128
+ *
129
+ * @note Actions passed as props from server components lose their metadata
130
+ * during RSC serialization. Use a string action name or import directly.
131
+ */
132
+ export function useAction(
133
+ action: ServerActionFunction | string
134
+ ): TrackedActionState;
135
+ export function useAction<T>(
136
+ action: ServerActionFunction | string,
137
+ selector: (state: TrackedActionState) => T
138
+ ): T;
139
+ export function useAction<T>(
140
+ action: ServerActionFunction | string,
141
+ selector?: (state: TrackedActionState) => T
142
+ ): T | TrackedActionState {
143
+ const ctx = useContext(NavigationStoreContext);
144
+ const actionId =
145
+ typeof window !== "undefined" && typeof document !== "undefined"
146
+ ? getActionId(action)
147
+ : "";
148
+
149
+ // Base state for useOptimistic
150
+ const [baseState, setBaseState] = useState<T | TrackedActionState>(() => {
151
+ if (!ctx) {
152
+ return selector ? selector(DEFAULT_ACTION_STATE) : DEFAULT_ACTION_STATE;
153
+ }
154
+ const state = ctx.eventController.getActionState(actionId);
155
+ return selector ? selector(state) : state;
156
+ });
157
+ const prevSelected = useRef(baseState);
158
+ prevSelected.current = baseState;
159
+ // useOptimistic allows immediate updates during transitions/actions
160
+ const [optimisticState, setOptimisticState] = useOptimistic<
161
+ T | TrackedActionState
162
+ >(null!);
163
+
164
+ // Memoize the selector to avoid unnecessary re-subscriptions
165
+ const selectorRef = useRef(selector);
166
+ selectorRef.current = selector;
167
+
168
+ // Subscribe to action state changes from event controller
169
+ useEffect(() => {
170
+ if (!ctx) return;
171
+
172
+ // Subscribe to action-specific updates
173
+ const unsubscribe = ctx.eventController.subscribeToAction(
174
+ actionId,
175
+ (state) => {
176
+ const selectedState = selectorRef.current
177
+ ? selectorRef.current(state)
178
+ : state;
179
+
180
+ if (!isShallowEqual(selectedState, prevSelected.current)) {
181
+ prevSelected.current = selectedState;
182
+ setBaseState(selectedState);
183
+ startTransition(() => {
184
+ setOptimisticState(selectedState);
185
+ });
186
+ }
187
+ }
188
+ );
189
+
190
+ return () => {
191
+ unsubscribe();
192
+ };
193
+ }, [actionId]);
194
+
195
+ return (optimisticState ?? baseState) as T | TrackedActionState;
196
+ }
197
+
198
+ function isShallowEqual<T, U>(selectedState: T, baseState: U): boolean {
199
+ // If references are equal, they're shallow equal
200
+ //@ts-expect-error -- TS doesn't like comparing generics
201
+ if (selectedState === baseState) {
202
+ return true;
203
+ }
204
+
205
+ // If either is null/undefined and they're not equal, they're not shallow equal
206
+ if (selectedState == null || baseState == null) {
207
+ return false;
208
+ }
209
+
210
+ // If types are different, they're not shallow equal
211
+ if (typeof selectedState !== typeof baseState) {
212
+ return false;
213
+ }
214
+
215
+ // For primitives, === comparison is sufficient (already checked above)
216
+ if (typeof selectedState !== "object") {
217
+ return false;
218
+ }
219
+
220
+ // For objects, compare keys and values shallowly
221
+ const keysA = Object.keys(selectedState as object);
222
+ const keysB = Object.keys(baseState as object);
223
+
224
+ if (keysA.length !== keysB.length) {
225
+ return false;
226
+ }
227
+
228
+ for (const key of keysA) {
229
+ if (
230
+ !Object.prototype.hasOwnProperty.call(baseState, key) ||
231
+ (selectedState as any)[key] !== (baseState as any)[key]
232
+ ) {
233
+ return false;
234
+ }
235
+ }
236
+
237
+ return true;
238
+ }
239
+
240
+ export type { TrackedActionState };
@@ -0,0 +1,56 @@
1
+ "use client";
2
+
3
+ import { useContext, useCallback } from "react";
4
+ import { NavigationStoreContext } from "./context.js";
5
+
6
+ /**
7
+ * Return type for useClientCache hook
8
+ */
9
+ export interface ClientCacheControls {
10
+ /**
11
+ * Clear the client-side navigation cache.
12
+ * Call this after data changes that happen outside of server actions
13
+ * (e.g., REST API calls, WebSocket updates, etc.)
14
+ *
15
+ * This will also broadcast to other tabs to clear their caches.
16
+ */
17
+ clear: () => void;
18
+ }
19
+
20
+ /**
21
+ * Hook to access client-side cache controls
22
+ *
23
+ * Use this when you need to manually invalidate the navigation cache
24
+ * after data changes that happen outside of server actions.
25
+ *
26
+ * Server actions automatically clear the cache, so you only need this for:
27
+ * - REST API mutations
28
+ * - WebSocket-driven updates
29
+ * - Other non-RSC data changes
30
+ *
31
+ * @example
32
+ * ```tsx
33
+ * function DataEditor() {
34
+ * const { clear } = useClientCache();
35
+ *
36
+ * async function handleSave() {
37
+ * await fetch('/api/data', { method: 'POST', body: JSON.stringify(data) });
38
+ * // Clear cache so back/forward navigation shows fresh data
39
+ * clear();
40
+ * }
41
+ *
42
+ * return <button onClick={handleSave}>Save</button>;
43
+ * }
44
+ * ```
45
+ */
46
+ export function useClientCache(): ClientCacheControls {
47
+ const ctx = useContext(NavigationStoreContext);
48
+
49
+ const clear = useCallback(() => {
50
+ if (ctx?.store) {
51
+ ctx.store.clearHistoryCache();
52
+ }
53
+ }, [ctx]);
54
+
55
+ return { clear };
56
+ }
@@ -0,0 +1,178 @@
1
+ "use client";
2
+
3
+ import {
4
+ useContext,
5
+ useState,
6
+ useEffect,
7
+ useRef,
8
+ useOptimistic,
9
+ startTransition,
10
+ } from "react";
11
+ import type { Handle } from "../../handle.js";
12
+ import type { HandleData } from "../types.js";
13
+ import { NavigationStoreContext } from "./context.js";
14
+
15
+ /**
16
+ * SSR module-level state.
17
+ * Populated by initHandleDataSync before React renders.
18
+ * Used by useState initializer during SSR.
19
+ */
20
+ let ssrHandleData: HandleData = {};
21
+ let ssrSegmentOrder: string[] = [];
22
+
23
+ /**
24
+ * Filter segment IDs to only include routes and layouts.
25
+ * Excludes parallels (contain .@) and loaders (contain D followed by digit).
26
+ */
27
+ function filterSegmentOrder(matched: string[]): string[] {
28
+ return matched.filter((id) => {
29
+ if (id.includes(".@")) return false;
30
+ if (/D\d+\./.test(id)) return false;
31
+ return true;
32
+ });
33
+ }
34
+
35
+ /**
36
+ * Collect handle data from segments and transform to final value.
37
+ */
38
+ function collectHandle<T, A>(
39
+ handle: Handle<T, A>,
40
+ data: HandleData,
41
+ segmentOrder: string[]
42
+ ): A {
43
+ const segmentData = data[handle.$$id];
44
+
45
+ if (!segmentData) {
46
+ return handle.collect([]);
47
+ }
48
+
49
+ // Build array of segment arrays in parent → child order
50
+ const segmentArrays: T[][] = [];
51
+ for (const segmentId of segmentOrder) {
52
+ const entries = segmentData[segmentId];
53
+ if (entries && entries.length > 0) {
54
+ segmentArrays.push(entries as T[]);
55
+ }
56
+ }
57
+
58
+ // Call collect once with all segment data
59
+ return handle.collect(segmentArrays);
60
+ }
61
+
62
+ /**
63
+ * Shallow equality check for selector results.
64
+ */
65
+ function shallowEqual<T>(a: T, b: T): boolean {
66
+ if (Object.is(a, b)) return true;
67
+ if (
68
+ typeof a !== "object" ||
69
+ a === null ||
70
+ typeof b !== "object" ||
71
+ b === null
72
+ ) {
73
+ return false;
74
+ }
75
+ const keysA = Object.keys(a);
76
+ const keysB = Object.keys(b);
77
+ if (keysA.length !== keysB.length) return false;
78
+ for (const key of keysA) {
79
+ if (
80
+ !Object.hasOwn(b, key) ||
81
+ !Object.is((a as any)[key], (b as any)[key])
82
+ ) {
83
+ return false;
84
+ }
85
+ }
86
+ return true;
87
+ }
88
+
89
+ /**
90
+ * Initialize handle data synchronously for SSR.
91
+ * Called before rendering to populate state for useState initializer.
92
+ *
93
+ * @param data - Handle data from RSC payload
94
+ * @param matched - Segment order for reduction
95
+ */
96
+ export function initHandleDataSync(data: HandleData, matched?: string[]): void {
97
+ ssrHandleData = data;
98
+ ssrSegmentOrder = filterSegmentOrder(matched ?? []);
99
+ }
100
+
101
+ /**
102
+ * Hook to access collected handle data.
103
+ *
104
+ * Returns the collected value from all route segments that pushed to this handle.
105
+ * Re-renders when handle data changes (navigation, actions).
106
+ *
107
+ * @param handle - The handle to read
108
+ * @param selector - Optional selector for performance (only re-render when selected value changes)
109
+ *
110
+ * @example
111
+ * ```tsx
112
+ * // Get all breadcrumbs
113
+ * const breadcrumbs = useHandle(Breadcrumbs);
114
+ *
115
+ * // With selector - only re-render when last crumb changes
116
+ * const lastCrumb = useHandle(Breadcrumbs, (data) => data.at(-1));
117
+ * ```
118
+ */
119
+ export function useHandle<T, A>(handle: Handle<T, A>): A;
120
+ export function useHandle<T, A, S>(
121
+ handle: Handle<T, A>,
122
+ selector: (data: A) => S
123
+ ): S;
124
+ export function useHandle<T, A, S>(
125
+ handle: Handle<T, A>,
126
+ selector?: (data: A) => S
127
+ ): A | S {
128
+ const ctx = useContext(NavigationStoreContext);
129
+
130
+ // Initial state from SSR module state or event controller
131
+ const [value, setValue] = useState<A | S>(() => {
132
+ // During SSR, use module-level state
133
+ if (typeof document === "undefined" || !ctx) {
134
+ const collected = collectHandle(handle, ssrHandleData, ssrSegmentOrder);
135
+ return selector ? selector(collected) : collected;
136
+ }
137
+
138
+ // On client, use event controller state
139
+ const state = ctx.eventController.getHandleState();
140
+ const collected = collectHandle(handle, state.data, state.segmentOrder);
141
+ return selector ? selector(collected) : collected;
142
+ });
143
+ const [optimisticValue, setOptimisticValue] = useOptimistic(value);
144
+
145
+ // Track previous value for shallow comparison
146
+ const prevValueRef = useRef(value);
147
+ prevValueRef.current = value;
148
+
149
+ // Memoize selector ref
150
+ const selectorRef = useRef(selector);
151
+ selectorRef.current = selector;
152
+
153
+ // Subscribe to handle data changes (client only)
154
+ useEffect(() => {
155
+ if (!ctx) return;
156
+
157
+ return ctx.eventController.subscribeToHandles(() => {
158
+ const state = ctx.eventController.getHandleState();
159
+ const isAction =
160
+ ctx.eventController.getState().inflightActions.length > 0;
161
+ const collected = collectHandle(handle, state.data, state.segmentOrder);
162
+ const nextValue = selectorRef.current
163
+ ? selectorRef.current(collected)
164
+ : collected;
165
+
166
+ if (!shallowEqual(nextValue, prevValueRef.current)) {
167
+ prevValueRef.current = nextValue;
168
+ startTransition(() => {
169
+ // Skip optimistic update during actions to prevent Suspense fallback
170
+ if (!isAction) setOptimisticValue(nextValue);
171
+ setValue(nextValue);
172
+ });
173
+ }
174
+ });
175
+ }, [handle]);
176
+
177
+ return optimisticValue;
178
+ }