@rangojs/router 0.15.1 → 0.17.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.
Files changed (249) hide show
  1. package/README.md +254 -89
  2. package/dist/testing/vitest.js +252 -0
  3. package/dist/types/browser/navigation-transaction.d.ts +6 -0
  4. package/dist/types/browser/partial-update.d.ts +11 -0
  5. package/dist/types/browser/react/ScrollRestoration.d.ts +6 -6
  6. package/dist/types/browser/rsc-router.d.ts +1 -1
  7. package/dist/types/browser/scroll-restoration.d.ts +7 -0
  8. package/dist/types/cache/cache-exec-scope.d.ts +18 -1
  9. package/dist/types/cache/cache-scope.d.ts +34 -5
  10. package/dist/types/cache/handle-capture.d.ts +41 -3
  11. package/dist/types/cache/handle-snapshot.d.ts +53 -13
  12. package/dist/types/cache/segment-codec.d.ts +10 -3
  13. package/dist/types/cache/taint.d.ts +1 -1
  14. package/dist/types/cache/types.d.ts +15 -3
  15. package/dist/types/client.d.ts +4 -4
  16. package/dist/types/client.rsc.d.ts +4 -5
  17. package/dist/types/components/DefaultDocument.d.ts +5 -2
  18. package/dist/types/context-var.d.ts +6 -5
  19. package/dist/types/escape-script.d.ts +1 -1
  20. package/dist/types/handles/MetaTags.d.ts +5 -4
  21. package/dist/types/handles/Scripts.d.ts +8 -7
  22. package/dist/types/handles/meta.d.ts +2 -2
  23. package/dist/types/handles/script.d.ts +17 -16
  24. package/dist/types/href-client.d.ts +1 -1
  25. package/dist/types/html.d.ts +12 -0
  26. package/dist/types/index.d.ts +1 -1
  27. package/dist/types/index.rsc.d.ts +3 -2
  28. package/dist/types/loader-store.d.ts +52 -0
  29. package/dist/types/reverse.d.ts +36 -4
  30. package/dist/types/route-definition/dsl-helpers.d.ts +3 -3
  31. package/dist/types/route-definition/helpers-types.d.ts +36 -10
  32. package/dist/types/route-definition/resolve-handler-use.d.ts +23 -1
  33. package/dist/types/route-types.d.ts +3 -1
  34. package/dist/types/router/debug-manifest.d.ts +3 -2
  35. package/dist/types/router/error-handling.d.ts +20 -0
  36. package/dist/types/router/handler-context.d.ts +6 -0
  37. package/dist/types/router/match-middleware/background-revalidation.d.ts +46 -5
  38. package/dist/types/router/middleware-types.d.ts +3 -2
  39. package/dist/types/router/router-context.d.ts +3 -1
  40. package/dist/types/router/router-interfaces.d.ts +24 -2
  41. package/dist/types/router/router-options.d.ts +8 -5
  42. package/dist/types/router/segment-resolution/fresh.d.ts +10 -0
  43. package/dist/types/router/segment-resolution/loader-cache.d.ts +37 -1
  44. package/dist/types/router/segment-resolution/loader-mask.d.ts +9 -19
  45. package/dist/types/router/segment-resolution/loader-snapshot.d.ts +3 -3
  46. package/dist/types/router/segment-resolution/mask-nested.d.ts +7 -7
  47. package/dist/types/router/segment-resolution/revalidation.d.ts +6 -2
  48. package/dist/types/router/segment-wrappers.d.ts +1 -1
  49. package/dist/types/rsc/response-route-handler.d.ts +1 -1
  50. package/dist/types/rsc/rsc-rendering.d.ts +1 -2
  51. package/dist/types/rsc/shell-capture-constants.d.ts +8 -0
  52. package/dist/types/rsc/shell-capture.d.ts +5 -4
  53. package/dist/types/rsc/ssr-setup.d.ts +6 -6
  54. package/dist/types/rsc/types.d.ts +10 -0
  55. package/dist/types/server/context.d.ts +33 -3
  56. package/dist/types/server/handle-store.d.ts +26 -2
  57. package/dist/types/server/request-context.d.ts +24 -21
  58. package/dist/types/ssr/index.d.ts +15 -0
  59. package/dist/types/static-handler.d.ts +4 -1
  60. package/dist/types/testing/generated-routes.d.ts +2 -2
  61. package/dist/types/testing/internal/context.d.ts +1 -1
  62. package/dist/types/testing/render-route.d.ts +32 -6
  63. package/dist/types/testing/vitest-stubs/plugin-rsc.d.ts +28 -5
  64. package/dist/types/testing/vitest.d.ts +35 -1
  65. package/dist/types/theme/ThemeScript.d.ts +3 -3
  66. package/dist/types/thenable-status.d.ts +9 -0
  67. package/dist/types/types/cache-types.d.ts +14 -10
  68. package/dist/types/types/handler-context.d.ts +14 -0
  69. package/dist/types/types/loader-types.d.ts +8 -6
  70. package/dist/types/urls/response-types.d.ts +3 -12
  71. package/dist/types/use-loader.d.ts +12 -3
  72. package/dist/types/vite/plugins/use-cache-transform.d.ts +33 -2
  73. package/dist/types/vite/rango.d.ts +1 -1
  74. package/dist/vite/index.js +884 -768
  75. package/package.json +4 -4
  76. package/skills/api-client/SKILL.md +22 -10
  77. package/skills/breadcrumbs/SKILL.md +65 -22
  78. package/skills/bundle-analysis/SKILL.md +22 -22
  79. package/skills/cache-guide/SKILL.md +207 -97
  80. package/skills/caching/SKILL.md +295 -93
  81. package/skills/catalog.json +16 -16
  82. package/skills/client-urls/SKILL.md +53 -31
  83. package/skills/cloudflare/SKILL.md +72 -20
  84. package/skills/cloudflare/references/d1-and-local-dev.md +15 -2
  85. package/skills/cloudflare/references/streaming-and-deploy.md +10 -3
  86. package/skills/cloudflare/references/webhooks-and-crypto.md +9 -2
  87. package/skills/comparison/SKILL.md +5 -0
  88. package/skills/comparison/references/framework-comparison.md +39 -33
  89. package/skills/composability/SKILL.md +38 -29
  90. package/skills/css/SKILL.md +10 -13
  91. package/skills/debug-manifest/SKILL.md +74 -31
  92. package/skills/defer-hydration/SKILL.md +2 -1
  93. package/skills/deployment-caching/SKILL.md +6 -1
  94. package/skills/document-cache/SKILL.md +92 -28
  95. package/skills/fonts/SKILL.md +16 -8
  96. package/skills/handler-use/SKILL.md +53 -17
  97. package/skills/hooks/SKILL.md +28 -20
  98. package/skills/hooks/data.md +43 -1
  99. package/skills/hooks/handle-and-actions.md +31 -16
  100. package/skills/hooks/navigation.md +39 -10
  101. package/skills/hooks/outlets.md +13 -6
  102. package/skills/hooks/state.md +37 -15
  103. package/skills/hooks/urls.md +7 -5
  104. package/skills/host-router/SKILL.md +64 -12
  105. package/skills/i18n/SKILL.md +60 -34
  106. package/skills/intercept/SKILL.md +161 -26
  107. package/skills/layout/SKILL.md +106 -50
  108. package/skills/links/SKILL.md +51 -19
  109. package/skills/loader/SKILL.md +242 -106
  110. package/skills/middleware/SKILL.md +119 -23
  111. package/skills/migrate-nextjs/SKILL.md +100 -45
  112. package/skills/migrate-nextjs/backend-host-swap.md +5 -2
  113. package/skills/migrate-react-router/SKILL.md +49 -38
  114. package/skills/migrate-react-router/component-migration.md +25 -6
  115. package/skills/migrate-react-router/data-and-actions.md +18 -5
  116. package/skills/migrate-react-router/route-mapping.md +12 -6
  117. package/skills/mime-routes/SKILL.md +12 -7
  118. package/skills/observability/SKILL.md +28 -14
  119. package/skills/parallel/SKILL.md +75 -39
  120. package/skills/ppr/SKILL.md +345 -150
  121. package/skills/prerender/SKILL.md +189 -100
  122. package/skills/rango/SKILL.md +65 -34
  123. package/skills/react-compiler/SKILL.md +6 -5
  124. package/skills/response-routes/SKILL.md +133 -25
  125. package/skills/route/SKILL.md +165 -60
  126. package/skills/router-setup/SKILL.md +418 -240
  127. package/skills/scripts/SKILL.md +31 -20
  128. package/skills/server-actions/SKILL.md +220 -59
  129. package/skills/shell-manifest/SKILL.md +12 -7
  130. package/skills/streams-and-websockets/SKILL.md +32 -14
  131. package/skills/tailwind/SKILL.md +37 -33
  132. package/skills/testing/SKILL.md +68 -32
  133. package/skills/testing/cache-prerender.md +55 -29
  134. package/skills/testing/client-components.md +59 -6
  135. package/skills/testing/e2e-parity.md +12 -2
  136. package/skills/testing/flight.md +19 -11
  137. package/skills/testing/handles.md +8 -5
  138. package/skills/testing/loader.md +8 -5
  139. package/skills/testing/middleware.md +8 -3
  140. package/skills/testing/predicates.md +120 -0
  141. package/skills/testing/render-handler.md +70 -22
  142. package/skills/testing/response-routes.md +11 -4
  143. package/skills/testing/reverse-and-types.md +20 -14
  144. package/skills/testing/server-actions.md +12 -3
  145. package/skills/testing/server-tree.md +9 -0
  146. package/skills/testing/setup.md +9 -5
  147. package/skills/theme/SKILL.md +106 -29
  148. package/skills/typesafety/SKILL.md +20 -9
  149. package/skills/typesafety/env-and-bindings.md +126 -47
  150. package/skills/typesafety/generated-files-and-cli.md +26 -14
  151. package/skills/typesafety/params-and-search.md +44 -19
  152. package/skills/typesafety/route-types.md +50 -14
  153. package/skills/use-cache/SKILL.md +200 -82
  154. package/skills/vercel/SKILL.md +21 -11
  155. package/skills/view-transitions/SKILL.md +41 -20
  156. package/src/__augment-tests__/augmented.check.ts +26 -1
  157. package/src/browser/index.ts +2 -2
  158. package/src/browser/navigation-bridge.ts +3 -4
  159. package/src/browser/navigation-transaction.ts +22 -2
  160. package/src/browser/partial-update.ts +75 -80
  161. package/src/browser/react/ScrollRestoration.tsx +6 -6
  162. package/src/browser/rsc-router.tsx +1 -1
  163. package/src/browser/scroll-restoration.ts +53 -6
  164. package/src/cache/cache-exec-scope.ts +37 -4
  165. package/src/cache/cache-key-utils.ts +1 -6
  166. package/src/cache/cache-runtime.ts +230 -61
  167. package/src/cache/cache-scope.ts +76 -12
  168. package/src/cache/document-cache.ts +15 -0
  169. package/src/cache/handle-capture.ts +90 -10
  170. package/src/cache/handle-snapshot.ts +132 -28
  171. package/src/cache/segment-codec.ts +26 -7
  172. package/src/cache/tag-invalidation.ts +4 -3
  173. package/src/cache/taint.ts +1 -1
  174. package/src/cache/types.ts +16 -3
  175. package/src/client.rsc.tsx +10 -9
  176. package/src/client.tsx +5 -4
  177. package/src/components/DefaultDocument.tsx +7 -2
  178. package/src/context-var.ts +6 -5
  179. package/src/escape-script.ts +1 -1
  180. package/src/handles/MetaTags.tsx +13 -11
  181. package/src/handles/Scripts.tsx +11 -10
  182. package/src/handles/meta.ts +2 -2
  183. package/src/handles/script.ts +17 -16
  184. package/src/href-client.ts +1 -1
  185. package/src/html.ts +12 -0
  186. package/src/index.rsc.ts +5 -0
  187. package/src/index.ts +1 -0
  188. package/src/loader-store.ts +106 -0
  189. package/src/prerender/build-shell-capture.ts +8 -11
  190. package/src/reverse.ts +44 -4
  191. package/src/route-definition/dsl-helpers.ts +134 -42
  192. package/src/route-definition/helpers-types.ts +36 -10
  193. package/src/route-definition/resolve-handler-use.ts +121 -6
  194. package/src/route-types.ts +2 -3
  195. package/src/router/debug-manifest.ts +10 -2
  196. package/src/router/error-handling.ts +51 -15
  197. package/src/router/handler-context.ts +34 -25
  198. package/src/router/intercept-resolution.ts +125 -39
  199. package/src/router/loader-resolution.ts +56 -11
  200. package/src/router/match-api.ts +12 -17
  201. package/src/router/match-middleware/background-revalidation.ts +161 -96
  202. package/src/router/match-middleware/cache-lookup.ts +93 -10
  203. package/src/router/match-middleware/cache-store.ts +17 -90
  204. package/src/router/middleware-types.ts +3 -5
  205. package/src/router/middleware.ts +52 -9
  206. package/src/router/prerender-match.ts +53 -19
  207. package/src/router/route-snapshot.ts +11 -1
  208. package/src/router/router-context.ts +2 -0
  209. package/src/router/router-interfaces.ts +25 -2
  210. package/src/router/router-options.ts +8 -5
  211. package/src/router/segment-resolution/fresh.ts +44 -12
  212. package/src/router/segment-resolution/loader-cache.ts +205 -45
  213. package/src/router/segment-resolution/loader-mask.ts +12 -22
  214. package/src/router/segment-resolution/loader-snapshot.ts +6 -6
  215. package/src/router/segment-resolution/mask-nested.ts +7 -7
  216. package/src/router/segment-resolution/revalidation.ts +34 -6
  217. package/src/router/segment-wrappers.ts +4 -0
  218. package/src/rsc/render-pipeline.ts +3 -0
  219. package/src/rsc/response-route-handler.ts +11 -3
  220. package/src/rsc/rsc-rendering.ts +8 -45
  221. package/src/rsc/shell-capture-constants.ts +13 -0
  222. package/src/rsc/shell-capture.ts +111 -90
  223. package/src/rsc/ssr-setup.ts +14 -8
  224. package/src/rsc/types.ts +11 -0
  225. package/src/server/context.ts +88 -11
  226. package/src/server/cookie-store.ts +5 -4
  227. package/src/server/handle-store.ts +232 -7
  228. package/src/server/request-context.ts +35 -30
  229. package/src/ssr/index.tsx +40 -7
  230. package/src/ssr-suspension-warning.ts +2 -13
  231. package/src/static-handler.ts +4 -1
  232. package/src/testing/flight-runtime.d.ts +21 -0
  233. package/src/testing/generated-routes.ts +2 -2
  234. package/src/testing/internal/context.ts +1 -1
  235. package/src/testing/internal/flight-client-globals.ts +7 -5
  236. package/src/testing/render-route.tsx +85 -24
  237. package/src/testing/run-loader.ts +5 -1
  238. package/src/testing/vitest-stubs/plugin-rsc.ts +123 -20
  239. package/src/testing/vitest.ts +62 -1
  240. package/src/theme/ThemeProvider.tsx +14 -12
  241. package/src/theme/ThemeScript.tsx +3 -3
  242. package/src/thenable-status.ts +16 -0
  243. package/src/types/cache-types.ts +14 -10
  244. package/src/types/handler-context.ts +14 -0
  245. package/src/types/loader-types.ts +8 -6
  246. package/src/urls/response-types.ts +3 -23
  247. package/src/use-loader.tsx +24 -4
  248. package/src/vite/plugins/use-cache-transform.ts +118 -74
  249. package/src/vite/rango.ts +7 -3
@@ -23,9 +23,10 @@ import type { RequestScope } from "./request-scope.js";
23
23
  * return await db.cart.get(user.id);
24
24
  * });
25
25
  *
26
- * // With typed params:
27
- * const ProductLoader = createLoader<Product, { slug: string }>(async (ctx) => {
28
- * const { slug } = ctx.params; // slug is typed as string
26
+ * // createLoader's one type parameter is the data type; params are
27
+ * // Record<string, string | undefined>:
28
+ * const ProductLoader = createLoader<Product>(async (ctx) => {
29
+ * const { slug } = ctx.params; // string | undefined
29
30
  * return await db.products.findBySlug(slug);
30
31
  * });
31
32
  * ```
@@ -213,9 +214,10 @@ export type LoadOptions = {
213
214
  * return await db.cart.get(ctx.get("user").id);
214
215
  * });
215
216
  *
216
- * // With typed params:
217
- * export const ProductLoader = createLoader<Product, { slug: string }>(async (ctx) => {
218
- * const { slug } = ctx.params; // slug is typed as string
217
+ * // createLoader's one type parameter is the data type; params are
218
+ * // Record<string, string | undefined>:
219
+ * export const ProductLoader = createLoader<Product>(async (ctx) => {
220
+ * const { slug } = ctx.params; // string | undefined
219
221
  * return await db.products.findBySlug(slug);
220
222
  * });
221
223
  *
@@ -1,18 +1,8 @@
1
1
  import type { ContextVar } from "../context-var.js";
2
- import type { ReverseFunction } from "../reverse.js";
2
+ import type { GlobalReverseFunction } from "../reverse.js";
3
3
  import type { DefaultReverseRouteMap, DefaultVars } from "../types/global-namespace.js";
4
4
  import type { UseItems, ResponseRouteUseItem } from "../route-types.js";
5
5
  import type { RequestScope } from "../types/request-scope.js";
6
- /**
7
- * Reverse function for response handler contexts.
8
- * Global names get autocomplete and param validation from the generated route map.
9
- * Local `.name` calls are accepted but not validated (scope unknown at type level).
10
- */
11
- type ResponseReverseFunction = [DefaultReverseRouteMap] extends [
12
- Record<string, string>
13
- ] ? (name: string, params?: Record<string, string>, search?: Record<string, unknown>) => string : ReverseFunction<DefaultReverseRouteMap> & {
14
- (name: `.${string}`, params?: Record<string, string>, search?: Record<string, unknown>): string;
15
- };
16
6
  /**
17
7
  * Symbol marking a route as a response route (non-RSC).
18
8
  * Stored on PathOptions and UrlPatterns to signal the trie to short-circuit.
@@ -56,7 +46,8 @@ export interface ResponseHandlerContext<TParams = Record<string, string>, TEnv =
56
46
  params: TParams;
57
47
  /** @internal Phantom property for params type invariance. Prevents mounting handlers on wrong routes. */
58
48
  readonly _paramCheck?: (params: TParams) => TParams;
59
- reverse: ResponseReverseFunction;
49
+ /** Global route names only; see {@link GlobalReverseFunction}. */
50
+ reverse: GlobalReverseFunction<DefaultReverseRouteMap>;
60
51
  /** Read a variable set by middleware via ctx.set(key, value) or ctx.set(ContextVar, value). */
61
52
  get: {
62
53
  <T>(contextVar: ContextVar<T>): T | undefined;
@@ -2,6 +2,14 @@ import type { LoaderDefinition, LoadOptions } from "./types.js";
2
2
  export type LoadFunction<T> = (options?: LoadOptions) => Promise<T>;
3
3
  export interface UseLoaderResult<T> {
4
4
  data: T;
5
+ /**
6
+ * True while this loader family (`loader.$$id`) is in flight: a `load()` /
7
+ * group refresh, or a held navigation still streaming a re-run of this
8
+ * loader. Family-keyed, not segment-keyed: a layout `useLoader` of the same
9
+ * `createLoader` the child route is re-running also reports true, even if
10
+ * the layout copy is not replaced. Not used for the initial streamed read
11
+ * (that suspends).
12
+ */
5
13
  isLoading: boolean;
6
14
  error: Error | null;
7
15
  load: LoadFunction<T>;
@@ -9,6 +17,7 @@ export interface UseLoaderResult<T> {
9
17
  }
10
18
  export interface UseFetchLoaderResult<T> {
11
19
  data: T | undefined;
20
+ /** Same family-keyed contract as {@link UseLoaderResult.isLoading}. */
12
21
  isLoading: boolean;
13
22
  error: Error | null;
14
23
  load: LoadFunction<T>;
@@ -36,7 +45,7 @@ export interface UseLoaderOptions {
36
45
  * @example Basic usage - accessing route loader data
37
46
  * ```tsx
38
47
  * "use client";
39
- * import { useLoader } from "rsc-router/client";
48
+ * import { useLoader } from "@rangojs/router/client";
40
49
  * import { CartLoader } from "../loaders/cart";
41
50
  *
42
51
  * // In route definition: loader(CartLoader)
@@ -67,7 +76,7 @@ export declare function useLoader<T>(loader: LoaderDefinition<T>, options?: UseL
67
76
  * @example On-demand fetching
68
77
  * ```tsx
69
78
  * "use client";
70
- * import { useFetchLoader } from "rsc-router/client";
79
+ * import { useFetchLoader } from "@rangojs/router/client";
71
80
  * import { SearchLoader } from "../loaders/search";
72
81
  *
73
82
  * export function SearchResults() {
@@ -121,7 +130,7 @@ export declare function useFetchLoader<T>(loader: LoaderDefinition<T>, options?:
121
130
  * @example
122
131
  * ```tsx
123
132
  * "use client";
124
- * import { useLoader, useRefreshLoaders } from "rsc-router/client";
133
+ * import { useLoader, useRefreshLoaders } from "@rangojs/router/client";
125
134
  *
126
135
  * function Profile() {
127
136
  * const { data } = useLoader(ProfileLoader, { key: userId, refreshGroup: "account" });
@@ -15,6 +15,37 @@
15
15
  * - transformWrapExport() for file-level wrapping
16
16
  * - transformHoistInlineDirective() for function-level hoisting
17
17
  */
18
- import type { Plugin } from "vite";
18
+ import type { Plugin, Rollup } from "vite";
19
+ import type { Counter } from "../debug.js";
19
20
  export declare const USE_CACHE_DIRECTIVE_RE: RegExp;
20
- export declare function useCacheTransform(): Plugin;
21
+ /**
22
+ * @param counter `rango:transform` timing counter, created by `rango()`
23
+ * (`createCounter` in ../debug.js); omitted when debugging is off.
24
+ */
25
+ export declare function useCacheTransform(counter?: Counter): Plugin;
26
+ /** Options for {@link transformUseCache}. */
27
+ export interface UseCacheTransformOptions {
28
+ /** Root the ids are relative to (Vite's `config.root`). */
29
+ root: string;
30
+ /** Build ids hash the path (`hashId`); dev ids are `<path>#<export>`. */
31
+ isBuild: boolean;
32
+ /** Sink for the near-miss directive warning. */
33
+ warn: (message: string) => void;
34
+ /**
35
+ * Called when `@vitejs/plugin-rsc/transforms` fails to load; the module is
36
+ * then left unwrapped. The rango plugin omits it (skip silently).
37
+ */
38
+ onTransformsError?: (error: unknown) => void;
39
+ }
40
+ /** Whether a module may carry a `"use cache"` directive the transform wraps. */
41
+ export declare function isUseCacheCandidate(code: string, id: string): boolean;
42
+ /**
43
+ * Wrap a module's `"use cache"` functions with `registerCachedFunction`. The
44
+ * rango plugin runs it in the rsc environment; `rangoUseCacheTransform()`
45
+ * (testing/vitest.ts) runs it in a Vitest project, whose environment is `ssr`.
46
+ * Call it only for an {@link isUseCacheCandidate} module.
47
+ */
48
+ export declare function transformUseCache(code: string, id: string, options: UseCacheTransformOptions): Promise<{
49
+ code: string;
50
+ map: Rollup.ExistingRawSourceMap;
51
+ } | undefined>;
@@ -21,7 +21,7 @@ export { hasUseClientDirective };
21
21
  * plugins: [
22
22
  * react(),
23
23
  * rango({ preset: 'cloudflare' }),
24
- * cloudflare({ viteEnvironment: { name: 'rsc' } }),
24
+ * cloudflare({ viteEnvironment: { name: 'rsc', childEnvironments: ['ssr'] } }),
25
25
  * ],
26
26
  * });
27
27
  * ```