@rangojs/router 0.0.0-experimental.19 → 0.0.0-experimental.1fa245e2

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 (160) hide show
  1. package/{CLAUDE.md → AGENTS.md} +4 -0
  2. package/README.md +122 -30
  3. package/dist/bin/rango.js +245 -63
  4. package/dist/vite/index.js +859 -418
  5. package/package.json +3 -3
  6. package/skills/breadcrumbs/SKILL.md +250 -0
  7. package/skills/cache-guide/SKILL.md +32 -0
  8. package/skills/caching/SKILL.md +49 -8
  9. package/skills/document-cache/SKILL.md +2 -2
  10. package/skills/hooks/SKILL.md +33 -31
  11. package/skills/host-router/SKILL.md +218 -0
  12. package/skills/links/SKILL.md +3 -1
  13. package/skills/loader/SKILL.md +72 -22
  14. package/skills/middleware/SKILL.md +2 -0
  15. package/skills/parallel/SKILL.md +126 -0
  16. package/skills/prerender/SKILL.md +112 -70
  17. package/skills/rango/SKILL.md +0 -1
  18. package/skills/route/SKILL.md +34 -4
  19. package/skills/router-setup/SKILL.md +95 -5
  20. package/skills/typesafety/SKILL.md +35 -23
  21. package/src/__internal.ts +92 -0
  22. package/src/bin/rango.ts +18 -0
  23. package/src/browser/app-version.ts +14 -0
  24. package/src/browser/event-controller.ts +5 -0
  25. package/src/browser/link-interceptor.ts +4 -0
  26. package/src/browser/navigation-bridge.ts +114 -18
  27. package/src/browser/navigation-client.ts +126 -44
  28. package/src/browser/navigation-store.ts +43 -8
  29. package/src/browser/navigation-transaction.ts +11 -9
  30. package/src/browser/partial-update.ts +80 -15
  31. package/src/browser/prefetch/cache.ts +166 -27
  32. package/src/browser/prefetch/fetch.ts +52 -39
  33. package/src/browser/prefetch/policy.ts +6 -0
  34. package/src/browser/prefetch/queue.ts +92 -20
  35. package/src/browser/prefetch/resource-ready.ts +77 -0
  36. package/src/browser/react/Link.tsx +70 -14
  37. package/src/browser/react/NavigationProvider.tsx +40 -4
  38. package/src/browser/react/context.ts +7 -2
  39. package/src/browser/react/use-handle.ts +9 -58
  40. package/src/browser/react/use-router.ts +21 -8
  41. package/src/browser/rsc-router.tsx +143 -59
  42. package/src/browser/scroll-restoration.ts +41 -42
  43. package/src/browser/segment-reconciler.ts +6 -1
  44. package/src/browser/server-action-bridge.ts +454 -436
  45. package/src/browser/types.ts +60 -5
  46. package/src/build/generate-manifest.ts +6 -6
  47. package/src/build/generate-route-types.ts +5 -0
  48. package/src/build/route-trie.ts +19 -3
  49. package/src/build/route-types/include-resolution.ts +8 -1
  50. package/src/build/route-types/router-processing.ts +346 -87
  51. package/src/build/route-types/scan-filter.ts +8 -1
  52. package/src/cache/cache-runtime.ts +15 -11
  53. package/src/cache/cache-scope.ts +48 -7
  54. package/src/cache/cf/cf-cache-store.ts +453 -11
  55. package/src/cache/cf/index.ts +5 -1
  56. package/src/cache/document-cache.ts +17 -7
  57. package/src/cache/index.ts +1 -0
  58. package/src/cache/taint.ts +55 -0
  59. package/src/client.rsc.tsx +2 -1
  60. package/src/client.tsx +3 -102
  61. package/src/context-var.ts +72 -2
  62. package/src/debug.ts +2 -2
  63. package/src/handle.ts +40 -0
  64. package/src/handles/breadcrumbs.ts +66 -0
  65. package/src/handles/index.ts +1 -0
  66. package/src/host/index.ts +0 -3
  67. package/src/index.rsc.ts +8 -37
  68. package/src/index.ts +40 -66
  69. package/src/prerender/store.ts +57 -15
  70. package/src/prerender.ts +138 -77
  71. package/src/reverse.ts +22 -1
  72. package/src/route-definition/dsl-helpers.ts +73 -25
  73. package/src/route-definition/helpers-types.ts +10 -6
  74. package/src/route-definition/index.ts +3 -3
  75. package/src/route-definition/redirect.ts +11 -3
  76. package/src/route-definition/resolve-handler-use.ts +149 -0
  77. package/src/route-map-builder.ts +7 -1
  78. package/src/route-types.ts +11 -0
  79. package/src/router/content-negotiation.ts +100 -1
  80. package/src/router/find-match.ts +4 -2
  81. package/src/router/handler-context.ts +108 -25
  82. package/src/router/intercept-resolution.ts +11 -4
  83. package/src/router/lazy-includes.ts +4 -1
  84. package/src/router/loader-resolution.ts +123 -11
  85. package/src/router/logging.ts +5 -2
  86. package/src/router/manifest.ts +9 -3
  87. package/src/router/match-api.ts +125 -190
  88. package/src/router/match-middleware/background-revalidation.ts +30 -2
  89. package/src/router/match-middleware/cache-lookup.ts +88 -16
  90. package/src/router/match-middleware/cache-store.ts +53 -10
  91. package/src/router/match-middleware/intercept-resolution.ts +9 -7
  92. package/src/router/match-middleware/segment-resolution.ts +61 -5
  93. package/src/router/match-result.ts +22 -15
  94. package/src/router/metrics.ts +238 -13
  95. package/src/router/middleware-types.ts +53 -12
  96. package/src/router/middleware.ts +172 -85
  97. package/src/router/navigation-snapshot.ts +182 -0
  98. package/src/router/pattern-matching.ts +20 -5
  99. package/src/router/prerender-match.ts +114 -10
  100. package/src/router/preview-match.ts +30 -102
  101. package/src/router/request-classification.ts +310 -0
  102. package/src/router/revalidation.ts +27 -7
  103. package/src/router/route-snapshot.ts +245 -0
  104. package/src/router/router-context.ts +6 -1
  105. package/src/router/router-interfaces.ts +50 -5
  106. package/src/router/router-options.ts +50 -19
  107. package/src/router/segment-resolution/fresh.ts +200 -19
  108. package/src/router/segment-resolution/helpers.ts +30 -25
  109. package/src/router/segment-resolution/loader-cache.ts +1 -0
  110. package/src/router/segment-resolution/revalidation.ts +429 -301
  111. package/src/router/segment-wrappers.ts +2 -0
  112. package/src/router/trie-matching.ts +20 -2
  113. package/src/router/types.ts +1 -0
  114. package/src/router.ts +88 -15
  115. package/src/rsc/handler.ts +546 -359
  116. package/src/rsc/index.ts +0 -20
  117. package/src/rsc/manifest-init.ts +5 -1
  118. package/src/rsc/progressive-enhancement.ts +25 -8
  119. package/src/rsc/rsc-rendering.ts +35 -43
  120. package/src/rsc/server-action.ts +16 -10
  121. package/src/rsc/ssr-setup.ts +128 -0
  122. package/src/rsc/types.ts +10 -1
  123. package/src/search-params.ts +16 -13
  124. package/src/segment-system.tsx +140 -4
  125. package/src/server/context.ts +148 -16
  126. package/src/server/loader-registry.ts +9 -8
  127. package/src/server/request-context.ts +182 -34
  128. package/src/server.ts +6 -0
  129. package/src/ssr/index.tsx +4 -0
  130. package/src/static-handler.ts +18 -6
  131. package/src/theme/index.ts +4 -13
  132. package/src/types/cache-types.ts +4 -4
  133. package/src/types/handler-context.ts +149 -49
  134. package/src/types/loader-types.ts +36 -9
  135. package/src/types/route-config.ts +17 -8
  136. package/src/types/route-entry.ts +8 -1
  137. package/src/types/segments.ts +2 -5
  138. package/src/urls/path-helper-types.ts +9 -2
  139. package/src/urls/path-helper.ts +48 -13
  140. package/src/urls/pattern-types.ts +12 -0
  141. package/src/urls/response-types.ts +16 -6
  142. package/src/use-loader.tsx +73 -4
  143. package/src/vite/discovery/bundle-postprocess.ts +61 -89
  144. package/src/vite/discovery/discover-routers.ts +23 -5
  145. package/src/vite/discovery/prerender-collection.ts +48 -15
  146. package/src/vite/discovery/state.ts +17 -13
  147. package/src/vite/index.ts +8 -3
  148. package/src/vite/plugin-types.ts +51 -79
  149. package/src/vite/plugins/client-ref-dedup.ts +115 -0
  150. package/src/vite/plugins/expose-action-id.ts +1 -3
  151. package/src/vite/plugins/performance-tracks.ts +88 -0
  152. package/src/vite/plugins/refresh-cmd.ts +127 -0
  153. package/src/vite/plugins/version-plugin.ts +13 -1
  154. package/src/vite/rango.ts +174 -211
  155. package/src/vite/router-discovery.ts +169 -42
  156. package/src/vite/utils/banner.ts +3 -3
  157. package/src/vite/utils/prerender-utils.ts +78 -0
  158. package/src/vite/utils/shared-utils.ts +3 -2
  159. package/skills/testing/SKILL.md +0 -226
  160. package/src/route-definition/route-function.ts +0 -119
@@ -29,6 +29,7 @@ export { MemorySegmentCacheStore } from "./memory-segment-store.js";
29
29
  export {
30
30
  CFCacheStore,
31
31
  type CFCacheStoreOptions,
32
+ type KVNamespace,
32
33
  CACHE_STALE_AT_HEADER,
33
34
  CACHE_STATUS_HEADER,
34
35
  } from "./cf/index.js";
@@ -81,6 +81,61 @@ export function assertNotInsideCacheExec(
81
81
  }
82
82
  }
83
83
 
84
+ /**
85
+ * Symbol stamped on ctx when resolving handlers inside a cache() DSL boundary.
86
+ * Separate from INSIDE_CACHE_EXEC ("use cache") because cache() allows
87
+ * ctx.set() (children are also cached) but blocks response-level side effects
88
+ * (headers, cookies, status) which are lost on cache hit.
89
+ */
90
+ export const INSIDE_CACHE_SCOPE: unique symbol = Symbol.for(
91
+ "rango:inside-cache-scope",
92
+ ) as any;
93
+
94
+ /**
95
+ * Mark ctx as inside a cache() scope. Must be paired with unstampCacheScope.
96
+ */
97
+ export function stampCacheScope(obj: object): void {
98
+ const current = (obj as any)[INSIDE_CACHE_SCOPE] ?? 0;
99
+ (obj as any)[INSIDE_CACHE_SCOPE] = current + 1;
100
+ }
101
+
102
+ /**
103
+ * Remove cache() scope mark.
104
+ */
105
+ export function unstampCacheScope(obj: object): void {
106
+ const current = (obj as any)[INSIDE_CACHE_SCOPE] ?? 0;
107
+ if (current <= 1) {
108
+ delete (obj as any)[INSIDE_CACHE_SCOPE];
109
+ } else {
110
+ (obj as any)[INSIDE_CACHE_SCOPE] = current - 1;
111
+ }
112
+ }
113
+
114
+ /**
115
+ * Throw if ctx is inside a cache() DSL boundary.
116
+ * Call from response-level side effects (header, setCookie, setStatus, etc.)
117
+ * which are lost on cache hit because the handler body is skipped.
118
+ * ctx.set() is allowed inside cache() — children are also cached and can
119
+ * read the value.
120
+ */
121
+ export function assertNotInsideCacheScope(
122
+ ctx: unknown,
123
+ methodName: string,
124
+ ): void {
125
+ if (
126
+ ctx !== null &&
127
+ ctx !== undefined &&
128
+ typeof ctx === "object" &&
129
+ (INSIDE_CACHE_SCOPE as symbol) in (ctx as Record<symbol, unknown>)
130
+ ) {
131
+ throw new Error(
132
+ `ctx.${methodName}() cannot be called inside a cache() boundary. ` +
133
+ `On cache hit the handler is skipped, so this side effect would be lost. ` +
134
+ `Move ctx.${methodName}() to a middleware or layout outside the cache() scope.`,
135
+ );
136
+ }
137
+ }
138
+
84
139
  /**
85
140
  * Brand symbol for functions wrapped by registerCachedFunction().
86
141
  * Used at runtime to detect when a "use cache" function is misused
@@ -17,7 +17,6 @@ export {
17
17
  OutletProvider,
18
18
  useOutlet,
19
19
  useLoader,
20
- useLoaderData,
21
20
  ErrorBoundary,
22
21
  type ErrorBoundaryProps,
23
22
  } from "./client.js";
@@ -64,6 +63,8 @@ export { Meta } from "./handles/meta.js";
64
63
  // MetaTags is a "use client" component that can be imported from RSC
65
64
  export { MetaTags } from "./handles/MetaTags.js";
66
65
  export type { MetaDescriptor, MetaDescriptorBase } from "./router/types.js";
66
+ // Breadcrumbs handle works in RSC context
67
+ export { Breadcrumbs, type BreadcrumbItem } from "./handles/breadcrumbs.js";
67
68
 
68
69
  // Location state - createLocationState works in RSC (just creates definition)
69
70
  // useLocationState is NOT exported here as it uses client hooks
package/src/client.tsx CHANGED
@@ -13,7 +13,6 @@ import {
13
13
  type ClientErrorBoundaryFallbackProps,
14
14
  type ErrorInfo,
15
15
  type LoaderDefinition,
16
- type LoaderFn,
17
16
  type ResolvedSegment,
18
17
  } from "./types";
19
18
  import {
@@ -313,103 +312,6 @@ export {
313
312
  type UseLoaderOptions,
314
313
  } from "./use-loader.js";
315
314
 
316
- /**
317
- * Hook to access all loader data in the current context
318
- *
319
- * Returns a record of all loader data available in the current outlet context
320
- * and all parent contexts. Useful for debugging or when you need access to
321
- * multiple loaders.
322
- *
323
- * @returns Record of loader name to data, or empty object if no loaders
324
- *
325
- * @example
326
- * ```tsx
327
- * "use client";
328
- * import { useLoaderData } from "rsc-router/client";
329
- *
330
- * export function DebugPanel() {
331
- * const loaderData = useLoaderData();
332
- * return <pre>{JSON.stringify(loaderData, null, 2)}</pre>;
333
- * }
334
- * ```
335
- */
336
- export function useLoaderData(): Record<string, any> {
337
- const context = useContext(OutletContext);
338
-
339
- // Collect all loader data from the context chain
340
- // Child loaders override parent loaders with the same name
341
- const result: Record<string, any> = {};
342
- const stack: OutletContextValue[] = [];
343
-
344
- // Build stack from current to root
345
- let current: OutletContextValue | null | undefined = context;
346
- while (current) {
347
- stack.push(current);
348
- current = current.parent;
349
- }
350
-
351
- // Apply from root to current (so children override parents)
352
- for (let i = stack.length - 1; i >= 0; i--) {
353
- const ctx = stack[i];
354
- if (ctx.loaderData) {
355
- Object.assign(result, ctx.loaderData);
356
- }
357
- }
358
-
359
- return result;
360
- }
361
-
362
- /**
363
- * Client-safe createLoader factory
364
- *
365
- * Creates a loader definition that can be used with useLoader().
366
- * This is the client-side version that only stores the $$id - the function
367
- * is ignored since loaders only execute on the server.
368
- *
369
- * The $$id is injected by the exposeLoaderId Vite plugin. In most cases,
370
- * you should import the loader directly from the server file rather than
371
- * creating a reference manually.
372
- *
373
- * @param fn - Loader function (ignored on client, kept for API compatibility)
374
- * @param _fetchable - Optional fetchable flag (ignored on client)
375
- * @param __injectedId - $$id injected by Vite plugin
376
- *
377
- * @example
378
- * ```tsx
379
- * "use client";
380
- * import { useLoader } from "rsc-router/client";
381
- * import { CartLoader } from "../loaders/cart"; // Import from server file
382
- *
383
- * export function CartIcon() {
384
- * const cart = useLoader(CartLoader);
385
- * return <span>Cart ({cart?.items.length ?? 0})</span>;
386
- * }
387
- * ```
388
- */
389
- // Overload 1: With function only (not fetchable)
390
- export function createLoader<T>(
391
- fn: LoaderFn<T, Record<string, string | undefined>, any>,
392
- ): LoaderDefinition<Awaited<T>, Record<string, string | undefined>>;
393
-
394
- // Overload 2: With function and fetchable flag
395
- export function createLoader<T>(
396
- fn: LoaderFn<T, Record<string, string | undefined>, any>,
397
- fetchable: true,
398
- ): LoaderDefinition<Awaited<T>, Record<string, string | undefined>>;
399
-
400
- // Implementation - function is ignored at runtime on client
401
- // The $$id is injected by Vite plugin as hidden third parameter
402
- export function createLoader(
403
- _fn: LoaderFn<any, Record<string, string | undefined>, any>,
404
- _fetchable?: true,
405
- __injectedId?: string,
406
- ): LoaderDefinition<any, Record<string, string | undefined>> {
407
- return {
408
- __brand: "loader",
409
- $$id: __injectedId || "",
410
- };
411
- }
412
-
413
315
  /**
414
316
  * Props for the ErrorBoundary component
415
317
  */
@@ -580,16 +482,15 @@ export {
580
482
  type ScrollRestorationProps,
581
483
  } from "./browser/react/ScrollRestoration.js";
582
484
 
583
- // Handle API - for accumulating data across route segments
584
- export { createHandle, isHandle, type Handle } from "./handle.js";
585
-
586
- // Handle data hook
485
+ // Handle data hook (client-side only createHandle/isHandle are server APIs from the root export)
486
+ export { type Handle } from "./handle.js";
587
487
  export { useHandle } from "./browser/react/use-handle.js";
588
488
 
589
489
  // Built-in handles
590
490
  export { Meta } from "./handles/meta.js";
591
491
  export { MetaTags } from "./handles/MetaTags.js";
592
492
  export type { MetaDescriptor, MetaDescriptorBase } from "./router/types.js";
493
+ export { Breadcrumbs, type BreadcrumbItem } from "./handles/breadcrumbs.js";
593
494
 
594
495
  // Location state - type-safe navigation state
595
496
  export {
@@ -12,6 +12,9 @@
12
12
  * interface PaginationData { current: number; total: number }
13
13
  * export const Pagination = createVar<PaginationData>();
14
14
  *
15
+ * // Non-cacheable var — throws if set/get inside cache() or "use cache"
16
+ * export const User = createVar<UserData>({ cache: false });
17
+ *
15
18
  * // handler
16
19
  * ctx.set(Pagination, { current: 1, total: 4 });
17
20
  *
@@ -23,18 +26,36 @@
23
26
  export interface ContextVar<T> {
24
27
  readonly __brand: "context-var";
25
28
  readonly key: symbol;
29
+ /** When false, the var is non-cacheable — throws inside cache() / "use cache" */
30
+ readonly cache: boolean;
26
31
  /** Phantom field to carry the type parameter. Never set at runtime. */
27
32
  readonly __type?: T;
28
33
  }
29
34
 
35
+ export interface ContextVarOptions {
36
+ /**
37
+ * When false, marks this variable as non-cacheable.
38
+ * Setting or getting this var inside a cache() boundary or "use cache"
39
+ * function will throw. Use for inherently request-specific data (user
40
+ * sessions, auth tokens, etc.) that must never be baked into cached segments.
41
+ *
42
+ * @default true
43
+ */
44
+ cache?: boolean;
45
+ }
46
+
30
47
  /**
31
48
  * Create a typed context variable token.
32
49
  *
33
50
  * The returned object is used with ctx.set(token, value) and ctx.get(token)
34
51
  * for compile-time-checked data flow between handlers, layouts, and middleware.
35
52
  */
36
- export function createVar<T>(): ContextVar<T> {
37
- return { __brand: "context-var" as const, key: Symbol() };
53
+ export function createVar<T>(options?: ContextVarOptions): ContextVar<T> {
54
+ return {
55
+ __brand: "context-var" as const,
56
+ key: Symbol(),
57
+ cache: options?.cache !== false,
58
+ };
38
59
  }
39
60
 
40
61
  /**
@@ -49,6 +70,36 @@ export function isContextVar(value: unknown): value is ContextVar<unknown> {
49
70
  );
50
71
  }
51
72
 
73
+ /**
74
+ * Symbol used as a Set stored on the variables object to track
75
+ * which keys hold non-cacheable values (from write-level { cache: false }).
76
+ */
77
+ const NON_CACHEABLE_KEYS: unique symbol = Symbol.for(
78
+ "rango:non-cacheable-keys",
79
+ ) as any;
80
+
81
+ function getNonCacheableKeys(variables: any): Set<string | symbol> {
82
+ if (!variables[NON_CACHEABLE_KEYS]) {
83
+ variables[NON_CACHEABLE_KEYS] = new Set();
84
+ }
85
+ return variables[NON_CACHEABLE_KEYS];
86
+ }
87
+
88
+ /**
89
+ * Check if a variable value is non-cacheable (either var-level or write-level).
90
+ */
91
+ export function isNonCacheable(
92
+ variables: any,
93
+ keyOrVar: string | ContextVar<any>,
94
+ ): boolean {
95
+ if (typeof keyOrVar !== "string" && !keyOrVar.cache) {
96
+ return true; // var-level policy
97
+ }
98
+ const key = typeof keyOrVar === "string" ? keyOrVar : keyOrVar.key;
99
+ const set = variables[NON_CACHEABLE_KEYS] as Set<string | symbol> | undefined;
100
+ return set?.has(key) ?? false; // write-level policy
101
+ }
102
+
52
103
  /**
53
104
  * Read a variable from the variables store.
54
105
  * Accepts either a string key (legacy) or a ContextVar token (typed).
@@ -64,6 +115,17 @@ export function contextGet(
64
115
  /** Keys that must never be used as string variable names */
65
116
  const FORBIDDEN_KEYS = new Set(["__proto__", "constructor", "prototype"]);
66
117
 
118
+ export interface ContextSetOptions {
119
+ /**
120
+ * When false, marks this specific write as non-cacheable.
121
+ * "Least cacheable wins" — if either the var definition or this option
122
+ * says cache: false, the value is non-cacheable.
123
+ *
124
+ * @default true (inherits from createVar)
125
+ */
126
+ cache?: boolean;
127
+ }
128
+
67
129
  /**
68
130
  * Write a variable to the variables store.
69
131
  * Accepts either a string key (legacy) or a ContextVar token (typed).
@@ -72,6 +134,7 @@ export function contextSet(
72
134
  variables: any,
73
135
  keyOrVar: string | ContextVar<any>,
74
136
  value: any,
137
+ options?: ContextSetOptions,
75
138
  ): void {
76
139
  if (typeof keyOrVar === "string") {
77
140
  if (FORBIDDEN_KEYS.has(keyOrVar)) {
@@ -80,7 +143,14 @@ export function contextSet(
80
143
  );
81
144
  }
82
145
  variables[keyOrVar] = value;
146
+ if (options?.cache === false) {
147
+ getNonCacheableKeys(variables).add(keyOrVar);
148
+ }
83
149
  } else {
84
150
  variables[keyOrVar.key] = value;
151
+ // Track write-level non-cacheable (var-level is checked via keyOrVar.cache)
152
+ if (options?.cache === false) {
153
+ getNonCacheableKeys(variables).add(keyOrVar.key);
154
+ }
85
155
  }
86
156
  }
package/src/debug.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  * Debug utilities for manifest inspection and comparison
3
3
  */
4
4
 
5
- import type { EntryData } from "./server/context";
5
+ import { getParallelSlotCount, type EntryData } from "./server/context";
6
6
 
7
7
  /**
8
8
  * Serialized entry for debug output
@@ -64,7 +64,7 @@ export function serializeManifest(
64
64
  hasLoader: entry.loader?.length > 0,
65
65
  hasMiddleware: entry.middleware?.length > 0,
66
66
  hasErrorBoundary: entry.errorBoundary?.length > 0,
67
- parallelCount: entry.parallel?.length ?? 0,
67
+ parallelCount: getParallelSlotCount(entry.parallel),
68
68
  interceptCount: entry.intercept?.length ?? 0,
69
69
  };
70
70
 
package/src/handle.ts CHANGED
@@ -133,3 +133,43 @@ export function isHandle(value: unknown): value is Handle<unknown, unknown> {
133
133
  (value as { __brand: unknown }).__brand === "handle"
134
134
  );
135
135
  }
136
+
137
+ /**
138
+ * Collect handle data from a HandleData map, applying the handle's collect
139
+ * function over segments in order. Shared between server-side rendered()
140
+ * reads and client-side useHandle().
141
+ *
142
+ * @param handle - The handle to collect data for
143
+ * @param data - Full handle data map (handleName -> segmentId -> entries[])
144
+ * @param segmentOrder - Segment IDs in parent -> child resolution order
145
+ */
146
+ export function collectHandleData<TData, TAccumulated>(
147
+ handle: Handle<TData, TAccumulated>,
148
+ data: Record<string, Record<string, unknown[]>>,
149
+ segmentOrder: string[],
150
+ ): TAccumulated {
151
+ const collectFn = getCollectFn(handle.$$id);
152
+ if (!collectFn && process.env.NODE_ENV !== "production") {
153
+ console.warn(
154
+ `[rsc-router] Handle "${handle.$$id}" has no registered collect function. ` +
155
+ `Falling back to flat array. Ensure the handle module is imported so ` +
156
+ `createHandle() runs and registers the collect function.`,
157
+ );
158
+ }
159
+ const collect = (collectFn ??
160
+ (defaultCollect as unknown as (segments: unknown[][]) => unknown)) as (
161
+ segments: TData[][],
162
+ ) => TAccumulated;
163
+
164
+ const segmentData = data[handle.$$id];
165
+ if (!segmentData) return collect([]);
166
+
167
+ const segmentArrays: TData[][] = [];
168
+ for (const segmentId of segmentOrder) {
169
+ const entries = segmentData[segmentId];
170
+ if (entries && entries.length > 0) {
171
+ segmentArrays.push(entries as TData[]);
172
+ }
173
+ }
174
+ return collect(segmentArrays);
175
+ }
@@ -0,0 +1,66 @@
1
+ /**
2
+ * Built-in Breadcrumbs handle for accumulating breadcrumb items across route segments.
3
+ *
4
+ * Each layout/route pushes breadcrumb items via `ctx.use(Breadcrumbs)`.
5
+ * Items are collected in parent-to-child order with automatic deduplication
6
+ * by `href` (last item for each href wins).
7
+ *
8
+ * @example
9
+ * ```tsx
10
+ * // In route handler
11
+ * route("/blog/:slug", (ctx) => {
12
+ * const breadcrumb = ctx.use(Breadcrumbs);
13
+ * breadcrumb({ label: "Blog", href: "/blog" });
14
+ * breadcrumb({ label: post.title, href: `/blog/${ctx.params.slug}` });
15
+ * });
16
+ *
17
+ * // In client component (consume with useHandle)
18
+ * const crumbs = useHandle(Breadcrumbs);
19
+ * crumbs.map((c) => <a href={c.href}>{c.label}</a>);
20
+ * ```
21
+ */
22
+
23
+ import type { ReactNode } from "react";
24
+ import { createHandle, type Handle } from "../handle.js";
25
+
26
+ /**
27
+ * A single breadcrumb item.
28
+ *
29
+ * @property label - Display text for the breadcrumb
30
+ * @property href - URL the breadcrumb links to
31
+ * @property content - Optional extra content (sync or async) rendered alongside the label
32
+ */
33
+ export interface BreadcrumbItem {
34
+ label: string;
35
+ href: string;
36
+ content?: ReactNode | Promise<ReactNode>;
37
+ }
38
+
39
+ /**
40
+ * Collect function for Breadcrumbs handle.
41
+ * Flattens segments in parent-to-child order with deduplication by href
42
+ * (last item for each href wins).
43
+ */
44
+ function collectBreadcrumbs(segments: BreadcrumbItem[][]): BreadcrumbItem[] {
45
+ const all = segments.flat();
46
+ const seen = new Map<string, number>();
47
+
48
+ for (let i = 0; i < all.length; i++) {
49
+ seen.set(all[i].href, i);
50
+ }
51
+
52
+ // Return items in order, keeping only the last occurrence per href
53
+ return all.filter((item, index) => seen.get(item.href) === index);
54
+ }
55
+
56
+ /**
57
+ * Built-in handle for accumulating breadcrumb navigation items.
58
+ *
59
+ * Use `ctx.use(Breadcrumbs)` in route handlers to push breadcrumb items.
60
+ * Use `useHandle(Breadcrumbs)` in client components to consume them.
61
+ */
62
+ export const Breadcrumbs: Handle<BreadcrumbItem, BreadcrumbItem[]> =
63
+ createHandle<BreadcrumbItem, BreadcrumbItem[]>(
64
+ collectBreadcrumbs,
65
+ "__rsc_router_breadcrumbs__",
66
+ );
@@ -4,3 +4,4 @@
4
4
 
5
5
  export { Meta } from "./meta.ts";
6
6
  export { MetaTags } from "./MetaTags.tsx";
7
+ export { Breadcrumbs, type BreadcrumbItem } from "./breadcrumbs.ts";
package/src/host/index.ts CHANGED
@@ -25,9 +25,6 @@
25
25
  // Core router
26
26
  export { createHostRouter } from "./router.js";
27
27
 
28
- // Host router registry for build-time discovery
29
- export { HostRouterRegistry, type HostRouterRegistryEntry } from "./router.js";
30
-
31
28
  // Utilities
32
29
  export { defineHosts } from "./utils.js";
33
30
 
package/src/index.rsc.ts CHANGED
@@ -11,8 +11,6 @@
11
11
 
12
12
  // Re-export all universal exports from index.ts
13
13
  export {
14
- // Universal rendering utilities
15
- renderSegments,
16
14
  // Error classes
17
15
  RouteNotFoundError,
18
16
  DataNotFoundError,
@@ -21,9 +19,6 @@ export {
21
19
  HandlerError,
22
20
  BuildError,
23
21
  InvalidHandlerError,
24
- NetworkError,
25
- isNetworkError,
26
- sanitizeError,
27
22
  RouterError,
28
23
  Skip,
29
24
  isSkip,
@@ -40,7 +35,6 @@ export type {
40
35
  TrailingSlashMode,
41
36
  // Handler types
42
37
  Handler,
43
- ScopedRouteMap,
44
38
  HandlerContext,
45
39
  ExtractParams,
46
40
  GenericParams,
@@ -106,6 +100,7 @@ export type {
106
100
  LayoutUseItem,
107
101
  AllUseItems,
108
102
  UseItems,
103
+ HandlerUseItem,
109
104
  } from "./route-types.js";
110
105
 
111
106
  // Handle API
@@ -120,9 +115,9 @@ export { nonce } from "./rsc/nonce.js";
120
115
  // Pre-render handler API
121
116
  export {
122
117
  Prerender,
123
- isPrerenderHandler,
118
+ Passthrough,
124
119
  type PrerenderHandlerDefinition,
125
- type PrerenderPassthroughContext,
120
+ type PassthroughHandlerDefinition,
126
121
  type PrerenderOptions,
127
122
  type BuildContext,
128
123
  type StaticBuildContext,
@@ -130,16 +125,11 @@ export {
130
125
  } from "./prerender.js";
131
126
 
132
127
  // Static handler API
133
- export {
134
- Static,
135
- isStaticHandler,
136
- type StaticHandlerDefinition,
137
- } from "./static-handler.js";
128
+ export { Static, type StaticHandlerDefinition } from "./static-handler.js";
138
129
 
139
130
  // Django-style URL patterns (RSC/server context)
140
131
  export {
141
132
  urls,
142
- RESPONSE_TYPE,
143
133
  type PathHelpers,
144
134
  type PathOptions,
145
135
  type UrlPatterns,
@@ -171,6 +161,7 @@ export type { HandlerCacheConfig } from "./rsc/types.js";
171
161
 
172
162
  // Built-in handles (server-side)
173
163
  export { Meta } from "./handles/meta.js";
164
+ export { Breadcrumbs, type BreadcrumbItem } from "./handles/breadcrumbs.js";
174
165
 
175
166
  // Request context (for accessing request data in server actions/components).
176
167
  // Re-exported with a narrowed return type so that public consumers only see
@@ -179,9 +170,10 @@ export { Meta } from "./handles/meta.js";
179
170
  import { getRequestContext as _getRequestContextInternal } from "./server/request-context.js";
180
171
  export type { PublicRequestContext as RequestContext } from "./server/request-context.js";
181
172
  import type { PublicRequestContext } from "./server/request-context.js";
173
+ import type { DefaultEnv } from "./types/global-namespace.js";
182
174
 
183
175
  export const getRequestContext: <
184
- TEnv = unknown,
176
+ TEnv = DefaultEnv,
185
177
  >() => PublicRequestContext<TEnv> = _getRequestContextInternal;
186
178
 
187
179
  // Request-scoped shorthands
@@ -205,8 +197,6 @@ export type {
205
197
  ReverseFunction,
206
198
  ExtractLocalRoutes,
207
199
  ParamsFor,
208
- SanitizePrefix,
209
- MergeRoutes,
210
200
  } from "./reverse.js";
211
201
  export { scopedReverse, createReverse } from "./reverse.js";
212
202
 
@@ -219,12 +209,6 @@ export type {
219
209
  RouteParams,
220
210
  } from "./search-params.js";
221
211
 
222
- // Debug utilities for route matching (development only)
223
- export {
224
- enableMatchDebug,
225
- getMatchDebugStats,
226
- } from "./router/pattern-matching.js";
227
-
228
212
  // Location state (universal)
229
213
  export {
230
214
  createLocationState,
@@ -240,20 +224,7 @@ export type { PathResponse } from "./href-client.js";
240
224
  export { createConsoleSink } from "./router/telemetry.js";
241
225
  export { createOTelSink } from "./router/telemetry-otel.js";
242
226
  export type { OTelTracer, OTelSpan } from "./router/telemetry-otel.js";
243
- export type {
244
- TelemetrySink,
245
- TelemetryEvent,
246
- RequestStartEvent,
247
- RequestEndEvent,
248
- RequestErrorEvent,
249
- RequestTimeoutEvent,
250
- LoaderStartEvent,
251
- LoaderEndEvent,
252
- LoaderErrorEvent,
253
- HandlerErrorEvent,
254
- CacheDecisionEvent,
255
- RevalidationDecisionEvent,
256
- } from "./router/telemetry.js";
227
+ export type { TelemetrySink, TelemetryEvent } from "./router/telemetry.js";
257
228
 
258
229
  // Timeout types and error class
259
230
  export { RouterTimeoutError } from "./router/timeout.js";