@rangojs/router 0.0.0-experimental.122 → 0.0.0-experimental.125

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 (260) hide show
  1. package/dist/bin/rango.js +10 -6
  2. package/dist/testing/vitest.js +82 -0
  3. package/dist/vite/index.js +55 -48
  4. package/package.json +61 -21
  5. package/skills/caching/SKILL.md +2 -1
  6. package/skills/hooks/SKILL.md +40 -29
  7. package/skills/host-router/SKILL.md +16 -2
  8. package/skills/intercept/SKILL.md +4 -2
  9. package/skills/layout/SKILL.md +11 -6
  10. package/skills/loader/SKILL.md +6 -2
  11. package/skills/middleware/SKILL.md +4 -2
  12. package/skills/migrate-nextjs/SKILL.md +3 -1
  13. package/skills/parallel/SKILL.md +9 -4
  14. package/skills/rango/SKILL.md +12 -0
  15. package/skills/route/SKILL.md +10 -2
  16. package/skills/testing/SKILL.md +129 -0
  17. package/skills/testing/bindings.md +89 -0
  18. package/skills/testing/cache-prerender.md +98 -0
  19. package/skills/testing/client-components.md +122 -0
  20. package/skills/testing/e2e-parity.md +125 -0
  21. package/skills/testing/flight.md +89 -0
  22. package/skills/testing/handles.md +129 -0
  23. package/skills/testing/loader.md +128 -0
  24. package/skills/testing/middleware.md +99 -0
  25. package/skills/testing/render-handler.md +118 -0
  26. package/skills/testing/response-routes.md +95 -0
  27. package/skills/testing/reverse-and-types.md +84 -0
  28. package/skills/testing/server-actions.md +107 -0
  29. package/skills/testing/server-tree.md +128 -0
  30. package/skills/testing/setup.md +120 -0
  31. package/src/__internal.ts +0 -65
  32. package/src/browser/action-coordinator.ts +1 -1
  33. package/src/browser/action-fence.ts +47 -0
  34. package/src/browser/cookie-name.ts +140 -0
  35. package/src/browser/event-controller.ts +1 -83
  36. package/src/browser/invalidate-client-cache.ts +52 -0
  37. package/src/browser/navigation-bridge.ts +14 -1
  38. package/src/browser/navigation-client.ts +14 -1
  39. package/src/browser/navigation-store-handle.ts +38 -0
  40. package/src/browser/navigation-store.ts +26 -51
  41. package/src/browser/navigation-transaction.ts +0 -32
  42. package/src/browser/partial-update.ts +1 -83
  43. package/src/browser/prefetch/cache.ts +6 -45
  44. package/src/browser/prefetch/fetch.ts +7 -0
  45. package/src/browser/prefetch/queue.ts +6 -3
  46. package/src/browser/rango-state.ts +157 -99
  47. package/src/browser/react/Link.tsx +0 -2
  48. package/src/browser/react/NavigationProvider.tsx +2 -1
  49. package/src/browser/react/ScrollRestoration.tsx +10 -6
  50. package/src/browser/react/filter-segment-order.ts +0 -2
  51. package/src/browser/react/index.ts +0 -51
  52. package/src/browser/react/location-state-shared.ts +0 -13
  53. package/src/browser/react/location-state.ts +0 -1
  54. package/src/browser/react/use-action.ts +6 -15
  55. package/src/browser/react/use-handle.ts +0 -5
  56. package/src/browser/react/use-link-status.ts +0 -4
  57. package/src/browser/react/use-navigation.ts +0 -3
  58. package/src/browser/react/use-params.ts +0 -2
  59. package/src/browser/react/use-search-params.ts +0 -5
  60. package/src/browser/react/use-segments.ts +0 -13
  61. package/src/browser/rsc-router.tsx +12 -4
  62. package/src/browser/server-action-bridge.ts +77 -15
  63. package/src/browser/types.ts +7 -2
  64. package/src/browser/validate-redirect-origin.ts +4 -5
  65. package/src/build/route-trie.ts +3 -0
  66. package/src/build/route-types/param-extraction.ts +6 -3
  67. package/src/build/route-types/router-processing.ts +0 -8
  68. package/src/cache/cache-policy.ts +0 -54
  69. package/src/cache/cache-runtime.ts +27 -24
  70. package/src/cache/cache-scope.ts +0 -27
  71. package/src/cache/cache-tag.ts +0 -37
  72. package/src/cache/cf/cf-cache-store.ts +94 -46
  73. package/src/cache/cf/index.ts +0 -24
  74. package/src/cache/document-cache.ts +11 -36
  75. package/src/cache/handle-snapshot.ts +0 -40
  76. package/src/cache/index.ts +0 -27
  77. package/src/cache/memory-segment-store.ts +2 -48
  78. package/src/cache/profile-registry.ts +7 -3
  79. package/src/cache/read-through-swr.ts +41 -11
  80. package/src/cache/segment-codec.ts +0 -16
  81. package/src/cache/types.ts +0 -98
  82. package/src/client.rsc.tsx +1 -22
  83. package/src/client.tsx +14 -38
  84. package/src/component-utils.ts +19 -0
  85. package/src/deps/ssr.ts +0 -1
  86. package/src/handle.ts +28 -18
  87. package/src/handles/MetaTags.tsx +0 -14
  88. package/src/handles/meta.ts +0 -39
  89. package/src/host/cookie-handler.ts +0 -36
  90. package/src/host/errors.ts +0 -24
  91. package/src/host/index.ts +6 -0
  92. package/src/host/pattern-matcher.ts +7 -50
  93. package/src/host/router.ts +1 -65
  94. package/src/host/testing.ts +40 -27
  95. package/src/host/types.ts +6 -2
  96. package/src/href-client.ts +0 -4
  97. package/src/index.rsc.ts +42 -3
  98. package/src/index.ts +31 -1
  99. package/src/internal-debug.ts +2 -4
  100. package/src/loader.rsc.ts +19 -9
  101. package/src/loader.ts +12 -4
  102. package/src/network-error-thrower.tsx +1 -6
  103. package/src/outlet-provider.tsx +1 -5
  104. package/src/prerender/param-hash.ts +10 -11
  105. package/src/prerender/store.ts +23 -30
  106. package/src/prerender.ts +58 -3
  107. package/src/root-error-boundary.tsx +1 -19
  108. package/src/route-content-wrapper.tsx +1 -44
  109. package/src/route-definition/dsl-helpers.ts +7 -19
  110. package/src/route-definition/helpers-types.ts +3 -3
  111. package/src/route-definition/redirect.ts +11 -1
  112. package/src/route-map-builder.ts +0 -16
  113. package/src/router/basename.ts +14 -0
  114. package/src/router/content-negotiation.ts +0 -13
  115. package/src/router/error-handling.ts +12 -16
  116. package/src/router/find-match.ts +4 -30
  117. package/src/router/intercept-resolution.ts +10 -1
  118. package/src/router/lazy-includes.ts +1 -57
  119. package/src/router/loader-resolution.ts +3 -2
  120. package/src/router/logging.ts +0 -6
  121. package/src/router/manifest.ts +1 -25
  122. package/src/router/match-api.ts +0 -20
  123. package/src/router/match-context.ts +0 -22
  124. package/src/router/match-handlers.ts +57 -58
  125. package/src/router/match-middleware/background-revalidation.ts +0 -7
  126. package/src/router/match-middleware/cache-lookup.ts +1 -54
  127. package/src/router/match-middleware/cache-store.ts +0 -31
  128. package/src/router/match-middleware/intercept-resolution.ts +0 -22
  129. package/src/router/match-middleware/segment-resolution.ts +0 -21
  130. package/src/router/match-pipelines.ts +1 -42
  131. package/src/router/match-result.ts +1 -52
  132. package/src/router/metrics.ts +0 -34
  133. package/src/router/middleware-cookies.ts +0 -13
  134. package/src/router/middleware-types.ts +0 -115
  135. package/src/router/middleware.ts +7 -30
  136. package/src/router/navigation-snapshot.ts +0 -51
  137. package/src/router/params-util.ts +23 -0
  138. package/src/router/pattern-matching.ts +1 -33
  139. package/src/router/prerender-match.ts +33 -45
  140. package/src/router/request-classification.ts +1 -38
  141. package/src/router/revalidation.ts +5 -58
  142. package/src/router/router-context.ts +0 -26
  143. package/src/router/router-interfaces.ts +7 -0
  144. package/src/router/router-options.ts +30 -0
  145. package/src/router/segment-resolution/fresh.ts +25 -57
  146. package/src/router/segment-resolution/helpers.ts +34 -0
  147. package/src/router/segment-resolution/loader-cache.ts +10 -13
  148. package/src/router/segment-resolution/revalidation.ts +5 -42
  149. package/src/router/segment-resolution/streamed-handler-telemetry.ts +52 -0
  150. package/src/router/segment-resolution.ts +4 -1
  151. package/src/router/state-cookie-name.ts +33 -0
  152. package/src/router/telemetry-otel.ts +0 -20
  153. package/src/router/telemetry.ts +96 -19
  154. package/src/router/timeout.ts +0 -20
  155. package/src/router/trie-matching.ts +63 -40
  156. package/src/router/types.ts +1 -63
  157. package/src/router/url-params.ts +0 -5
  158. package/src/router.ts +40 -9
  159. package/src/rsc/handler.ts +14 -2
  160. package/src/rsc/helpers.ts +34 -0
  161. package/src/rsc/origin-guard.ts +0 -12
  162. package/src/rsc/progressive-enhancement.ts +4 -1
  163. package/src/rsc/rsc-rendering.ts +4 -7
  164. package/src/rsc/runtime-warnings.ts +14 -0
  165. package/src/rsc/server-action.ts +30 -28
  166. package/src/rsc/types.ts +2 -1
  167. package/src/runtime-env.ts +18 -0
  168. package/src/search-params.ts +0 -16
  169. package/src/segment-loader-promise.ts +14 -2
  170. package/src/segment-system.tsx +79 -88
  171. package/src/server/cookie-store.ts +52 -1
  172. package/src/server/handle-store.ts +7 -24
  173. package/src/server/loader-registry.ts +5 -24
  174. package/src/server/request-context.ts +74 -77
  175. package/src/ssr/index.tsx +14 -14
  176. package/src/static-handler.ts +10 -13
  177. package/src/testing/cache-status.ts +119 -0
  178. package/src/testing/collect-handle.ts +40 -0
  179. package/src/testing/dispatch.ts +581 -0
  180. package/src/testing/dom.entry.ts +22 -0
  181. package/src/testing/e2e/fixture.ts +188 -0
  182. package/src/testing/e2e/index.ts +127 -0
  183. package/src/testing/e2e/matchers.ts +35 -0
  184. package/src/testing/e2e/page-helpers.ts +272 -0
  185. package/src/testing/e2e/parity.ts +387 -0
  186. package/src/testing/e2e/server.ts +195 -0
  187. package/src/testing/flight-matchers.ts +97 -0
  188. package/src/testing/flight-normalize.ts +11 -0
  189. package/src/testing/flight-runtime.d.ts +57 -0
  190. package/src/testing/flight-tree.ts +682 -0
  191. package/src/testing/flight.entry.ts +52 -0
  192. package/src/testing/flight.ts +186 -0
  193. package/src/testing/generated-routes.ts +183 -0
  194. package/src/testing/index.ts +98 -0
  195. package/src/testing/internal/context.ts +348 -0
  196. package/src/testing/internal/flight-client-globals.ts +30 -0
  197. package/src/testing/internal/seed-vars.ts +54 -0
  198. package/src/testing/render-handler.ts +311 -0
  199. package/src/testing/render-route.tsx +504 -0
  200. package/src/testing/run-loader.ts +378 -0
  201. package/src/testing/run-middleware.ts +205 -0
  202. package/src/testing/vitest-stubs/cloudflare-email.ts +9 -0
  203. package/src/testing/vitest-stubs/cloudflare-workers.ts +21 -0
  204. package/src/testing/vitest-stubs/plugin-rsc.ts +16 -0
  205. package/src/testing/vitest-stubs/version.ts +5 -0
  206. package/src/testing/vitest.ts +305 -0
  207. package/src/theme/ThemeProvider.tsx +0 -52
  208. package/src/theme/ThemeScript.tsx +0 -6
  209. package/src/theme/constants.ts +0 -12
  210. package/src/theme/index.ts +0 -7
  211. package/src/theme/theme-context.ts +1 -5
  212. package/src/theme/theme-script.ts +0 -14
  213. package/src/theme/use-theme.ts +0 -3
  214. package/src/types/boundaries.ts +0 -35
  215. package/src/types/error-types.ts +25 -89
  216. package/src/types/global-namespace.ts +15 -15
  217. package/src/types/handler-context.ts +16 -13
  218. package/src/types/index.ts +0 -10
  219. package/src/types/request-scope.ts +0 -19
  220. package/src/types/route-config.ts +6 -50
  221. package/src/types/route-entry.ts +0 -6
  222. package/src/types/segments.ts +0 -13
  223. package/src/urls/include-helper.ts +0 -4
  224. package/src/urls/index.ts +0 -6
  225. package/src/urls/path-helper-types.ts +2 -2
  226. package/src/urls/path-helper.ts +0 -54
  227. package/src/urls/urls-function.ts +0 -13
  228. package/src/use-loader.tsx +0 -186
  229. package/src/vite/discovery/bundle-postprocess.ts +2 -1
  230. package/src/vite/discovery/discover-routers.ts +6 -7
  231. package/src/vite/discovery/virtual-module-codegen.ts +1 -11
  232. package/src/vite/plugin-types.ts +3 -1
  233. package/src/vite/plugins/cjs-to-esm.ts +0 -11
  234. package/src/vite/plugins/client-ref-dedup.ts +0 -11
  235. package/src/vite/plugins/client-ref-hashing.ts +0 -10
  236. package/src/vite/plugins/cloudflare-protocol-stub.ts +0 -20
  237. package/src/vite/plugins/expose-action-id.ts +2 -73
  238. package/src/vite/plugins/expose-id-utils.ts +0 -55
  239. package/src/vite/plugins/expose-ids/export-analysis.ts +0 -38
  240. package/src/vite/plugins/expose-ids/handler-transform.ts +0 -15
  241. package/src/vite/plugins/expose-ids/loader-transform.ts +0 -15
  242. package/src/vite/plugins/expose-ids/router-transform.ts +0 -13
  243. package/src/vite/plugins/expose-internal-ids.ts +10 -0
  244. package/src/vite/plugins/performance-tracks.ts +0 -3
  245. package/src/vite/plugins/use-cache-transform.ts +0 -36
  246. package/src/vite/plugins/version-injector.ts +0 -20
  247. package/src/vite/plugins/version-plugin.ts +1 -49
  248. package/src/vite/plugins/virtual-entries.ts +0 -15
  249. package/src/vite/rango.ts +1 -108
  250. package/src/vite/router-discovery.ts +2 -1
  251. package/src/vite/utils/ast-handler-extract.ts +0 -16
  252. package/src/vite/utils/bundle-analysis.ts +6 -13
  253. package/src/vite/utils/client-chunks.ts +0 -6
  254. package/src/vite/utils/forward-user-plugins.ts +0 -22
  255. package/src/vite/utils/manifest-utils.ts +0 -4
  256. package/src/vite/utils/package-resolution.ts +1 -73
  257. package/src/vite/utils/prerender-utils.ts +0 -35
  258. package/src/vite/utils/shared-utils.ts +3 -35
  259. package/src/browser/react/use-client-cache.ts +0 -58
  260. package/src/browser/shallow.ts +0 -40
@@ -0,0 +1,311 @@
1
+ /// <reference path="./flight-runtime.d.ts" />
2
+ /**
3
+ * renderHandler — run a REAL route handler and assert what it renders.
4
+ *
5
+ * A Rango route handler is a pure function `(ctx) => RSC` (what you pass to
6
+ * `path("/p/:slug", ProductPage)`), NOT a component. To test one faithfully you
7
+ * must give it the HandlerContext the router builds at runtime, so `ctx.params`,
8
+ * `ctx.use(Loader)`, `ctx.use(Meta)` / `ctx.use(Breadcrumbs)` (handles),
9
+ * `ctx.reverse`, `ctx.get`/`ctx.header`/`cookies()` all work. renderHandler does
10
+ * exactly that, then serializes the handler's returned RSC and deserializes it
11
+ * to an inspectable tree (same serialize/deserialize core as renderServerTree).
12
+ *
13
+ * Loaders are SEEDED (no real loader execution) the same way `runLoader` seeds
14
+ * them — pass `loaders: [[ProductLoader, data]]`. Handle pushes
15
+ * (`ctx.use(Meta)({...})`) are captured on `result.handles`. The handler's
16
+ * cookie/header/flash effects and a thrown/returned redirect are surfaced too
17
+ * (like `runInRequestContext`). If the handler returns/throws a `Response`
18
+ * (a response route / `throw redirect()`), there is no RSC `tree`.
19
+ *
20
+ * Must run under the `react-server` export condition (the rsc Vitest project).
21
+ * Wire `rangoUseClientTransform()` so `"use client"` islands in the handler's RSC
22
+ * auto-register (or pass `clientComponents`).
23
+ */
24
+ import type { ReactNode } from "react";
25
+ import {
26
+ createRequestContext,
27
+ runWithRequestContext,
28
+ setRequestContextParams,
29
+ type RequestContext,
30
+ } from "../server/request-context.js";
31
+ import { createHandlerContext } from "../router/handler-context.js";
32
+ import { resolveLocationStateEntries } from "../browser/react/location-state-shared.js";
33
+ import { isHandle, type Handle } from "../handle.js";
34
+ import type { HandlerContext } from "../types/handler-context.js";
35
+ import type { LoaderDefinition } from "../types.js";
36
+ import {
37
+ seedVariables,
38
+ resolveSeededStateCookieName,
39
+ type VarsInit,
40
+ type StateCookieSeed,
41
+ } from "./internal/seed-vars.js";
42
+
43
+ export type { StateCookieSeed } from "./internal/seed-vars.js";
44
+ import { assertNoLegacyUrlOption, serializeNodeToFlight } from "./flight.js";
45
+ import {
46
+ deserializeFlight,
47
+ makeClientManifest,
48
+ registerClientComponents,
49
+ } from "./flight-tree.js";
50
+
51
+ const DEFAULT_URL = "http://localhost/";
52
+
53
+ /** A route handler under test: the `(ctx) => RSC | Response` function you pass to `path(...)`. */
54
+ export type TestableHandler<TEnv = any> = (
55
+ ctx: HandlerContext<any, TEnv>,
56
+ ) => ReactNode | Promise<ReactNode> | Response | Promise<Response>;
57
+
58
+ /** Options for {@link renderHandler}. */
59
+ export interface RenderHandlerOptions<TEnv = any> {
60
+ /** Route params surfaced as `ctx.params`. */
61
+ params?: Record<string, string>;
62
+ /** Environment bindings surfaced as `ctx.env`. */
63
+ env?: TEnv;
64
+ /** Backing Request (string or Request); defaults to a localhost GET. */
65
+ request?: Request | string;
66
+ /** Request headers (e.g. Cookie) the handler reads via `cookies()`. */
67
+ headers?: HeadersInit;
68
+ /** Variables a prior middleware set, read via `ctx.get(...)`. Object or tuples. */
69
+ vars?: VarsInit;
70
+ /** Matched route name (drives `ctx.routeName` and scoped reverse). */
71
+ routeName?: string;
72
+ /** Route name -> pattern map enabling `ctx.reverse()`. */
73
+ routeMap?: Record<string, string>;
74
+ /**
75
+ * Seed the data `ctx.use(SomeLoader)` returns — NO real loader runs (same model
76
+ * as `runLoader`'s `loaders`). Matched by loader reference, so a real
77
+ * `createLoader()` handle resolves regardless of its build-injected `$$id`.
78
+ */
79
+ loaders?: ReadonlyArray<readonly [LoaderDefinition<any, any>, unknown]>;
80
+ /**
81
+ * `"use client"` components in the handler's RSC, so they serialize as real
82
+ * boundaries when `rangoUseClientTransform()` is not wired. Keyed by name; see
83
+ * renderServerTree's `clientComponents`.
84
+ */
85
+ clientComponents?: Record<string, unknown>;
86
+ /**
87
+ * Customize the rango state cookie a handler that calls
88
+ * `invalidateClientCache()` rotates. The name is ALWAYS seeded (default
89
+ * `rango-state_router_0`) so the rotation `Set-Cookie` is emitted like
90
+ * production rather than no-opping; override `prefix`/`routerId` to match your
91
+ * `createRouter({ stateCookiePrefix, id })`, or `version` (the build
92
+ * identifier prefixed to the rotated `{version}:{timestamp}` value, default
93
+ * `"0"`). Assert via `result.response.headers.getSetCookie()` against
94
+ * `result.stateCookieName`.
95
+ */
96
+ stateCookie?: StateCookieSeed;
97
+ }
98
+
99
+ /** Result of {@link renderHandler}. */
100
+ export interface RenderHandlerResult {
101
+ /**
102
+ * The deserialized RSC the handler returned, as an inspectable React element
103
+ * tree — `undefined` when the handler returned or threw a `Response`. Use
104
+ * `findClientBoundaries` (from testing/flight) to locate client islands.
105
+ */
106
+ tree: unknown;
107
+ /** The raw Flight wire string; `undefined` when the handler produced a `Response`. */
108
+ flight: string | undefined;
109
+ /** The value the handler THREW (a `redirect()`/`notFound()` Response), captured not re-thrown. */
110
+ thrown: unknown;
111
+ /** The merged Response (status + headers + Set-Cookie); a thrown/returned redirect merged with accumulated effects. */
112
+ response: Response;
113
+ /** Effective cookie view after the handler ran, as `{ name: value }`. */
114
+ cookies: Record<string, string>;
115
+ /** Response headers as `{ name: value }` (excludes set-cookie; includes a redirect Location). */
116
+ headers: Record<string, string>;
117
+ /**
118
+ * The resolved rango state cookie name this run seeded (default
119
+ * `rango-state_router_0`, or composed from `opts.stateCookie`). Assert the
120
+ * `invalidateClientCache()` rotation against it without recomputing:
121
+ * `response.headers.getSetCookie().some((c) => c.startsWith(stateCookieName + "="))`.
122
+ */
123
+ stateCookieName: string;
124
+ /** Location state the handler set (`ctx.setLocationState`/`redirect({ state })`), as `{ key: value }`. */
125
+ locationState: Record<string, unknown>;
126
+ /** What the handler pushed via `ctx.use(Handle)(...)` (e.g. Meta, Breadcrumbs), keyed by handle. */
127
+ handles: Map<Handle<any, any>, unknown[]>;
128
+ }
129
+
130
+ /**
131
+ * A renderHandler MISCONFIGURATION (e.g. an unseeded loader) — distinct from a
132
+ * value the handler intentionally threw (a redirect). Setup errors REJECT;
133
+ * handler throws are captured on `result.thrown`.
134
+ */
135
+ class RenderHandlerSetupError extends Error {}
136
+
137
+ function headersToObject(headers: Headers): Record<string, string> {
138
+ const out: Record<string, string> = {};
139
+ headers.forEach((value, name) => {
140
+ if (name.toLowerCase() !== "set-cookie") out[name] = value;
141
+ });
142
+ return out;
143
+ }
144
+
145
+ function isServerOnlyStubError(error: unknown): boolean {
146
+ return (
147
+ error instanceof Error &&
148
+ error.message.includes("is only available from") &&
149
+ error.message.includes("react-server")
150
+ );
151
+ }
152
+
153
+ function toRequest(
154
+ request: Request | string | undefined,
155
+ headers?: HeadersInit,
156
+ ): Request {
157
+ if (request instanceof Request) return request;
158
+ if (typeof request === "string") {
159
+ return new Request(new URL(request, DEFAULT_URL), { headers });
160
+ }
161
+ return new Request(DEFAULT_URL, { headers });
162
+ }
163
+
164
+ function buildResponse(reqCtx: RequestContext<any>, source: unknown): Response {
165
+ const stub = reqCtx.res;
166
+ if (source instanceof Response) {
167
+ const merged = new Headers(source.headers);
168
+ for (const cookie of stub.headers.getSetCookie()) {
169
+ merged.append("set-cookie", cookie);
170
+ }
171
+ stub.headers.forEach((value, name) => {
172
+ if (name.toLowerCase() === "set-cookie") return;
173
+ if (!merged.has(name)) merged.set(name, value);
174
+ });
175
+ return new Response(source.body, {
176
+ status: source.status,
177
+ headers: merged,
178
+ });
179
+ }
180
+ return new Response(null, { status: stub.status, headers: stub.headers });
181
+ }
182
+
183
+ /**
184
+ * Run a route handler with a seeded HandlerContext and return its rendered RSC
185
+ * (deserialized tree) plus the effects it produced. See the module header.
186
+ *
187
+ * @example
188
+ * ```ts
189
+ * // ProductPage is the real handler: (ctx) => <main>{ctx.params.slug}...</main>
190
+ * const { tree, handles } = await renderHandler(ProductPage, {
191
+ * params: { slug: "wine" },
192
+ * loaders: [[ProductLoader, { name: "Wine", price: 9 }]],
193
+ * vars: [[Tenant, tenant]],
194
+ * routeMap: { product: "/p/:slug" },
195
+ * });
196
+ * ```
197
+ */
198
+ export async function renderHandler<TEnv = any>(
199
+ handler: TestableHandler<TEnv>,
200
+ opts: RenderHandlerOptions<TEnv> = {},
201
+ ): Promise<RenderHandlerResult> {
202
+ assertNoLegacyUrlOption(opts, "renderHandler");
203
+ if (opts.clientComponents) registerClientComponents(opts.clientComponents);
204
+ const request = toRequest(opts.request, opts.headers);
205
+ const url = new URL(request.url);
206
+ const stateCookieName = resolveSeededStateCookieName(opts.stateCookie);
207
+ const reqCtx = createRequestContext<TEnv>({
208
+ env: (opts.env ?? {}) as TEnv,
209
+ request,
210
+ url,
211
+ variables: seedVariables({}, opts.vars),
212
+ stateCookieName,
213
+ version: opts.stateCookie?.version,
214
+ });
215
+
216
+ const loaderSeeds = new Map<unknown, unknown>(opts.loaders ?? []);
217
+ const handlePushes = new Map<Handle<any, any>, unknown[]>();
218
+
219
+ let out: ReactNode | Response | undefined;
220
+ let flight: string | undefined;
221
+ let thrown: unknown;
222
+ let didThrow = false;
223
+
224
+ await runWithRequestContext(reqCtx as RequestContext<TEnv>, async () => {
225
+ setRequestContextParams(opts.params ?? {}, opts.routeName);
226
+ const hctx = createHandlerContext<TEnv>(
227
+ opts.params ?? {},
228
+ reqCtx.request,
229
+ reqCtx.searchParams,
230
+ reqCtx.pathname,
231
+ reqCtx.url,
232
+ reqCtx.env,
233
+ opts.routeMap ?? {},
234
+ opts.routeName,
235
+ );
236
+ (hctx as { use: unknown }).use = (item: unknown) => {
237
+ if (isHandle(item)) {
238
+ const handle = item as Handle<any, any>;
239
+ return (dataOrFn: unknown) => {
240
+ const value =
241
+ typeof dataOrFn === "function"
242
+ ? (dataOrFn as () => unknown)()
243
+ : dataOrFn;
244
+ const pushed = handlePushes.get(handle) ?? [];
245
+ pushed.push(value);
246
+ handlePushes.set(handle, pushed);
247
+ };
248
+ }
249
+ if (loaderSeeds.has(item)) return loaderSeeds.get(item);
250
+ throw new RenderHandlerSetupError(
251
+ `renderHandler: ctx.use(loader) was not seeded. Pass ` +
252
+ `{ loaders: [[YourLoader, data]] } for each loader the handler reads.`,
253
+ );
254
+ };
255
+ (hctx as { _currentSegmentId?: string })._currentSegmentId = "test.segment";
256
+
257
+ try {
258
+ out = await handler(hctx as HandlerContext<any, TEnv>);
259
+ if (out !== undefined && !(out instanceof Response)) {
260
+ flight = await serializeNodeToFlight(
261
+ out as ReactNode,
262
+ makeClientManifest(),
263
+ url.pathname,
264
+ );
265
+ }
266
+ } catch (error) {
267
+ if (error instanceof RenderHandlerSetupError) throw error;
268
+ if (isServerOnlyStubError(error)) {
269
+ throw new RenderHandlerSetupError(
270
+ `renderHandler: the handler called a server-only API (getRequestContext/cookies/...) ` +
271
+ `but "@rangojs/router" resolved to the out-of-react-server stub. Add ` +
272
+ `rangoTestAliases({ preset }) to your vitest.rsc.config.ts \`resolve.alias\` so the ` +
273
+ `bare specifier maps to index.rsc.ts (the real react-server implementations). ` +
274
+ `Original: ${(error as Error).message}`,
275
+ );
276
+ }
277
+ didThrow = true;
278
+ thrown = error;
279
+ }
280
+ });
281
+
282
+ const cookies = { ...reqCtx.cookies() };
283
+ const responseSource = didThrow
284
+ ? thrown
285
+ : out instanceof Response
286
+ ? out
287
+ : undefined;
288
+ const response = buildResponse(reqCtx as RequestContext<any>, responseSource);
289
+ const headers = headersToObject(response.headers);
290
+ const locationState = resolveLocationStateEntries(
291
+ (
292
+ reqCtx as {
293
+ _locationState?: Parameters<typeof resolveLocationStateEntries>[0];
294
+ }
295
+ )._locationState ?? [],
296
+ );
297
+ const tree =
298
+ flight !== undefined ? await deserializeFlight(flight) : undefined;
299
+
300
+ return {
301
+ tree,
302
+ flight,
303
+ thrown,
304
+ response,
305
+ cookies,
306
+ headers,
307
+ stateCookieName,
308
+ locationState,
309
+ handles: handlePushes,
310
+ };
311
+ }