@rangojs/router 0.0.0-experimental.77 → 0.0.0-experimental.77ed8945

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 (239) hide show
  1. package/README.md +120 -25
  2. package/dist/bin/rango.js +147 -57
  3. package/dist/vite/index.js +2103 -861
  4. package/dist/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
  5. package/package.json +13 -8
  6. package/skills/api-client/SKILL.md +211 -0
  7. package/skills/breadcrumbs/SKILL.md +3 -1
  8. package/skills/bundle-analysis/SKILL.md +159 -0
  9. package/skills/cache-guide/SKILL.md +220 -30
  10. package/skills/caching/SKILL.md +116 -8
  11. package/skills/composability/SKILL.md +27 -2
  12. package/skills/css/SKILL.md +76 -0
  13. package/skills/document-cache/SKILL.md +78 -55
  14. package/skills/handler-use/SKILL.md +3 -1
  15. package/skills/hooks/SKILL.md +229 -20
  16. package/skills/host-router/SKILL.md +66 -20
  17. package/skills/i18n/SKILL.md +276 -0
  18. package/skills/intercept/SKILL.md +26 -4
  19. package/skills/layout/SKILL.md +6 -7
  20. package/skills/links/SKILL.md +247 -17
  21. package/skills/loader/SKILL.md +219 -9
  22. package/skills/middleware/SKILL.md +47 -12
  23. package/skills/migrate-nextjs/SKILL.md +562 -0
  24. package/skills/migrate-react-router/SKILL.md +769 -0
  25. package/skills/mime-routes/SKILL.md +27 -0
  26. package/skills/observability/SKILL.md +137 -0
  27. package/skills/parallel/SKILL.md +12 -6
  28. package/skills/prerender/SKILL.md +14 -33
  29. package/skills/rango/SKILL.md +238 -22
  30. package/skills/react-compiler/SKILL.md +168 -0
  31. package/skills/response-routes/SKILL.md +122 -47
  32. package/skills/route/SKILL.md +33 -4
  33. package/skills/router-setup/SKILL.md +3 -3
  34. package/skills/server-actions/SKILL.md +751 -0
  35. package/skills/streams-and-websockets/SKILL.md +283 -0
  36. package/skills/tailwind/SKILL.md +27 -3
  37. package/skills/typesafety/SKILL.md +319 -27
  38. package/skills/use-cache/SKILL.md +34 -5
  39. package/skills/view-transitions/SKILL.md +294 -0
  40. package/src/__augment-tests__/augment.ts +81 -0
  41. package/src/__augment-tests__/augmented.check.ts +116 -0
  42. package/src/browser/action-coordinator.ts +53 -36
  43. package/src/browser/app-shell.ts +39 -0
  44. package/src/browser/event-controller.ts +86 -70
  45. package/src/browser/history-state.ts +21 -0
  46. package/src/browser/index.ts +3 -3
  47. package/src/browser/navigation-bridge.ts +29 -9
  48. package/src/browser/navigation-client.ts +99 -77
  49. package/src/browser/navigation-store.ts +7 -8
  50. package/src/browser/navigation-transaction.ts +10 -28
  51. package/src/browser/partial-update.ts +60 -40
  52. package/src/browser/prefetch/cache.ts +196 -49
  53. package/src/browser/prefetch/fetch.ts +203 -59
  54. package/src/browser/prefetch/queue.ts +36 -5
  55. package/src/browser/rango-state.ts +37 -13
  56. package/src/browser/react/Link.tsx +18 -13
  57. package/src/browser/react/NavigationProvider.tsx +75 -31
  58. package/src/browser/react/filter-segment-order.ts +51 -7
  59. package/src/browser/react/index.ts +3 -0
  60. package/src/browser/react/location-state-shared.ts +175 -4
  61. package/src/browser/react/location-state.ts +39 -13
  62. package/src/browser/react/use-handle.ts +17 -9
  63. package/src/browser/react/use-navigation.ts +22 -2
  64. package/src/browser/react/use-params.ts +20 -8
  65. package/src/browser/react/use-reverse.ts +106 -0
  66. package/src/browser/react/use-router.ts +23 -2
  67. package/src/browser/react/use-segments.ts +11 -8
  68. package/src/browser/response-adapter.ts +52 -1
  69. package/src/browser/rsc-router.tsx +71 -22
  70. package/src/browser/scroll-restoration.ts +22 -14
  71. package/src/browser/segment-reconciler.ts +10 -14
  72. package/src/browser/segment-structure-assert.ts +2 -2
  73. package/src/browser/server-action-bridge.ts +44 -30
  74. package/src/browser/types.ts +12 -2
  75. package/src/build/collect-fallback-refs.ts +107 -0
  76. package/src/build/generate-manifest.ts +60 -35
  77. package/src/build/generate-route-types.ts +2 -0
  78. package/src/build/index.ts +8 -1
  79. package/src/build/prefix-tree-utils.ts +123 -0
  80. package/src/build/route-trie.ts +45 -1
  81. package/src/build/route-types/codegen.ts +4 -4
  82. package/src/build/route-types/include-resolution.ts +1 -1
  83. package/src/build/route-types/per-module-writer.ts +7 -4
  84. package/src/build/route-types/router-processing.ts +55 -14
  85. package/src/build/route-types/scan-filter.ts +1 -1
  86. package/src/build/route-types/source-scan.ts +118 -0
  87. package/src/build/runtime-discovery.ts +9 -20
  88. package/src/cache/cache-runtime.ts +17 -5
  89. package/src/cache/cache-scope.ts +51 -49
  90. package/src/cache/cf/cf-cache-store.ts +502 -32
  91. package/src/cache/cf/index.ts +3 -0
  92. package/src/cache/handle-snapshot.ts +103 -0
  93. package/src/cache/index.ts +3 -0
  94. package/src/cache/memory-segment-store.ts +3 -2
  95. package/src/cache/types.ts +10 -6
  96. package/src/client.rsc.tsx +3 -0
  97. package/src/client.tsx +96 -205
  98. package/src/context-var.ts +5 -5
  99. package/src/decode-loader-results.ts +36 -0
  100. package/src/errors.ts +30 -4
  101. package/src/handle.ts +4 -6
  102. package/src/host/index.ts +2 -2
  103. package/src/host/router.ts +129 -57
  104. package/src/host/types.ts +31 -2
  105. package/src/host/utils.ts +1 -1
  106. package/src/href-client.ts +140 -21
  107. package/src/index.rsc.ts +10 -6
  108. package/src/index.ts +17 -8
  109. package/src/loader-store.ts +500 -0
  110. package/src/loader.rsc.ts +2 -5
  111. package/src/loader.ts +3 -10
  112. package/src/missing-id-error.ts +68 -0
  113. package/src/outlet-context.ts +1 -1
  114. package/src/prerender/store.ts +9 -7
  115. package/src/prerender.ts +4 -4
  116. package/src/response-utils.ts +37 -0
  117. package/src/reverse.ts +65 -39
  118. package/src/route-content-wrapper.tsx +6 -28
  119. package/src/route-definition/dsl-helpers.ts +253 -265
  120. package/src/route-definition/helper-factories.ts +29 -139
  121. package/src/route-definition/helpers-types.ts +43 -15
  122. package/src/route-definition/resolve-handler-use.ts +6 -0
  123. package/src/route-definition/use-item-types.ts +32 -0
  124. package/src/route-types.ts +26 -41
  125. package/src/router/content-negotiation.ts +15 -2
  126. package/src/router/error-handling.ts +1 -1
  127. package/src/router/find-match.ts +54 -6
  128. package/src/router/handler-context.ts +21 -41
  129. package/src/router/intercept-resolution.ts +4 -18
  130. package/src/router/lazy-includes.ts +41 -22
  131. package/src/router/loader-resolution.ts +82 -36
  132. package/src/router/manifest.ts +41 -19
  133. package/src/router/match-api.ts +4 -3
  134. package/src/router/match-handlers.ts +1 -0
  135. package/src/router/match-middleware/cache-lookup.ts +57 -95
  136. package/src/router/match-middleware/cache-store.ts +3 -2
  137. package/src/router/match-result.ts +53 -32
  138. package/src/router/metrics.ts +1 -1
  139. package/src/router/middleware-types.ts +15 -26
  140. package/src/router/middleware.ts +99 -84
  141. package/src/router/pattern-matching.ts +116 -19
  142. package/src/router/prerender-match.ts +40 -15
  143. package/src/router/preview-match.ts +3 -1
  144. package/src/router/request-classification.ts +40 -37
  145. package/src/router/revalidation.ts +58 -2
  146. package/src/router/router-interfaces.ts +51 -35
  147. package/src/router/router-options.ts +25 -1
  148. package/src/router/router-registry.ts +2 -5
  149. package/src/router/segment-resolution/fresh.ts +27 -6
  150. package/src/router/segment-resolution/revalidation.ts +147 -106
  151. package/src/router/segment-resolution/static-store.ts +19 -5
  152. package/src/router/segment-resolution/view-transition-default.ts +36 -0
  153. package/src/router/substitute-pattern-params.ts +56 -0
  154. package/src/router/trie-matching.ts +40 -16
  155. package/src/router/types.ts +8 -0
  156. package/src/router/url-params.ts +49 -0
  157. package/src/router.ts +37 -25
  158. package/src/rsc/handler-context.ts +2 -2
  159. package/src/rsc/handler.ts +58 -77
  160. package/src/rsc/helpers.ts +72 -43
  161. package/src/rsc/index.ts +1 -1
  162. package/src/rsc/manifest-init.ts +28 -41
  163. package/src/rsc/origin-guard.ts +30 -10
  164. package/src/rsc/progressive-enhancement.ts +4 -0
  165. package/src/rsc/response-error.ts +79 -12
  166. package/src/rsc/response-route-handler.ts +76 -61
  167. package/src/rsc/rsc-rendering.ts +45 -51
  168. package/src/rsc/runtime-warnings.ts +9 -10
  169. package/src/rsc/server-action.ts +33 -39
  170. package/src/rsc/ssr-setup.ts +16 -0
  171. package/src/rsc/types.ts +8 -2
  172. package/src/search-params.ts +4 -4
  173. package/src/segment-content-promise.ts +67 -0
  174. package/src/segment-loader-promise.ts +122 -0
  175. package/src/segment-system.tsx +132 -116
  176. package/src/serialize.ts +243 -0
  177. package/src/server/context.ts +175 -53
  178. package/src/server/cookie-store.ts +28 -4
  179. package/src/server/request-context.ts +57 -51
  180. package/src/ssr/index.tsx +5 -1
  181. package/src/static-handler.ts +1 -1
  182. package/src/types/global-namespace.ts +39 -26
  183. package/src/types/handler-context.ts +68 -50
  184. package/src/types/index.ts +1 -0
  185. package/src/types/loader-types.ts +11 -9
  186. package/src/types/request-scope.ts +126 -0
  187. package/src/types/route-entry.ts +11 -0
  188. package/src/types/segments.ts +35 -2
  189. package/src/urls/include-helper.ts +34 -67
  190. package/src/urls/index.ts +1 -5
  191. package/src/urls/path-helper-types.ts +17 -3
  192. package/src/urls/path-helper.ts +17 -52
  193. package/src/urls/pattern-types.ts +36 -19
  194. package/src/urls/response-types.ts +22 -29
  195. package/src/urls/type-extraction.ts +58 -139
  196. package/src/urls/urls-function.ts +1 -5
  197. package/src/use-loader.tsx +413 -42
  198. package/src/vite/debug.ts +185 -0
  199. package/src/vite/discovery/bundle-postprocess.ts +6 -6
  200. package/src/vite/discovery/discover-routers.ts +106 -75
  201. package/src/vite/discovery/discovery-errors.ts +194 -0
  202. package/src/vite/discovery/gate-state.ts +171 -0
  203. package/src/vite/discovery/prerender-collection.ts +72 -31
  204. package/src/vite/discovery/route-types-writer.ts +40 -84
  205. package/src/vite/discovery/self-gen-tracking.ts +27 -1
  206. package/src/vite/discovery/state.ts +33 -0
  207. package/src/vite/discovery/virtual-module-codegen.ts +13 -23
  208. package/src/vite/index.ts +2 -0
  209. package/src/vite/plugin-types.ts +67 -0
  210. package/src/vite/plugins/cjs-to-esm.ts +8 -7
  211. package/src/vite/plugins/client-ref-dedup.ts +16 -0
  212. package/src/vite/plugins/client-ref-hashing.ts +28 -5
  213. package/src/vite/plugins/cloudflare-protocol-loader-hook.d.mts +23 -0
  214. package/src/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
  215. package/src/vite/plugins/cloudflare-protocol-stub.ts +214 -0
  216. package/src/vite/plugins/expose-action-id.ts +54 -30
  217. package/src/vite/plugins/expose-id-utils.ts +12 -8
  218. package/src/vite/plugins/expose-ids/export-analysis.ts +100 -20
  219. package/src/vite/plugins/expose-ids/handler-transform.ts +8 -61
  220. package/src/vite/plugins/expose-ids/loader-transform.ts +3 -5
  221. package/src/vite/plugins/expose-ids/router-transform.ts +20 -3
  222. package/src/vite/plugins/expose-internal-ids.ts +496 -486
  223. package/src/vite/plugins/performance-tracks.ts +29 -25
  224. package/src/vite/plugins/use-cache-transform.ts +65 -50
  225. package/src/vite/plugins/version-injector.ts +39 -23
  226. package/src/vite/plugins/version-plugin.ts +59 -2
  227. package/src/vite/plugins/virtual-entries.ts +2 -2
  228. package/src/vite/rango.ts +116 -29
  229. package/src/vite/router-discovery.ts +753 -104
  230. package/src/vite/utils/ast-handler-extract.ts +15 -15
  231. package/src/vite/utils/banner.ts +1 -1
  232. package/src/vite/utils/bundle-analysis.ts +4 -2
  233. package/src/vite/utils/client-chunks.ts +190 -0
  234. package/src/vite/utils/forward-user-plugins.ts +193 -0
  235. package/src/vite/utils/manifest-utils.ts +8 -59
  236. package/src/vite/utils/package-resolution.ts +41 -1
  237. package/src/vite/utils/prerender-utils.ts +5 -4
  238. package/src/vite/utils/shared-utils.ts +107 -26
  239. package/src/browser/action-response-classifier.ts +0 -99
@@ -0,0 +1,194 @@
1
+ /**
2
+ * Router discovery error aggregation.
3
+ *
4
+ * During host-router discovery the lazy mounts registered by a host router are
5
+ * invoked to trigger each sub-app's createRouter() registration. Some mount
6
+ * failures are expected in the temporary discovery server context (a sub-app may
7
+ * reference runtime-only bindings), so each is invoked defensively and its error
8
+ * is collected rather than thrown.
9
+ *
10
+ * Previously these errors were discarded with an empty `catch {}`. When a real
11
+ * failure - typically a sub-app whose router module fails to import - left the
12
+ * registry empty, discovery reported the misleading "No routers found" message
13
+ * with no trace of the underlying cause. The collected errors are now surfaced
14
+ * via the `DiscoveryError` thrown at the end of discovery (issue #499).
15
+ *
16
+ * Which entries to invoke is taken from the consumer's declared intent, not
17
+ * inferred from the function's shape. A host route is registered either with
18
+ * `.map((request) => Response)` (an inline request handler, `kind: "handler"`)
19
+ * or `.lazy(() => import("./sub-app"))` (a lazy mount, `kind: "lazy"`). Only
20
+ * `kind === "lazy"` entries are invoked here; inline handlers are never invoked
21
+ * during discovery (they need a Request and register no routers). Because a lazy
22
+ * entry is known to be a module loader, ANY failure it produces - a synchronous
23
+ * throw or a rejected promise - is a genuine discovery failure and is collected.
24
+ */
25
+
26
+ /** An error caught (and previously swallowed) while resolving host routers. */
27
+ export interface CaughtDiscoveryError {
28
+ /** Human-readable description of where the error was caught. */
29
+ context: string;
30
+ /** The caught value (an Error or otherwise). */
31
+ error: unknown;
32
+ }
33
+
34
+ /**
35
+ * Minimal shape of a host registry entry needed for mount resolution.
36
+ * Mirrors the runtime HostRouterRegistry value without coupling to its type.
37
+ */
38
+ interface HostRegistryRoute {
39
+ handler?: unknown;
40
+ kind?: string;
41
+ }
42
+ interface HostRegistryEntry {
43
+ routes: HostRegistryRoute[];
44
+ fallback?: HostRegistryRoute | null;
45
+ }
46
+
47
+ /** Indent every non-empty line of `text` by `pad`. */
48
+ function indent(text: string, pad: string): string {
49
+ return text
50
+ .split("\n")
51
+ .map((line) => (line.length > 0 ? pad + line : line))
52
+ .join("\n");
53
+ }
54
+
55
+ /**
56
+ * Invoke a single lazy mount to trigger its sub-app import (and createRouter()
57
+ * registration), collecting any failure under `context`. The entry is known to
58
+ * be a loader (`kind === "lazy"`), so both a synchronous throw and a rejected
59
+ * promise are genuine failures - no shape heuristics are needed.
60
+ */
61
+ async function invokeLazyMount(
62
+ loader: () => unknown,
63
+ context: string,
64
+ errors: CaughtDiscoveryError[],
65
+ ): Promise<void> {
66
+ try {
67
+ await loader();
68
+ } catch (error) {
69
+ errors.push({ context, error });
70
+ }
71
+ }
72
+
73
+ /** Whether a registry route is a `.lazy()` mount with an invokable loader. */
74
+ function isLazyMount(
75
+ route: HostRegistryRoute | null | undefined,
76
+ ): route is { handler: () => unknown; kind: "lazy" } {
77
+ return (
78
+ !!route && route.kind === "lazy" && typeof route.handler === "function"
79
+ );
80
+ }
81
+
82
+ /**
83
+ * Invoke every lazy mount in the host registry to trigger sub-app
84
+ * createRouter() registration, collecting (not throwing) any failures.
85
+ *
86
+ * Only `.lazy()` entries are invoked; `.map()` inline request handlers are
87
+ * skipped (they need a Request and register no routers). Failures are returned
88
+ * rather than thrown because some mounts legitimately fail in the temporary
89
+ * discovery server context; the caller decides whether the failures matter,
90
+ * which is only when discovery finds no routers at all.
91
+ */
92
+ export async function resolveHostRouterHandlers(
93
+ hostRegistry: Map<string, HostRegistryEntry>,
94
+ ): Promise<CaughtDiscoveryError[]> {
95
+ const errors: CaughtDiscoveryError[] = [];
96
+
97
+ for (const [hostId, entry] of hostRegistry) {
98
+ for (const route of entry.routes) {
99
+ if (isLazyMount(route)) {
100
+ await invokeLazyMount(
101
+ route.handler,
102
+ `host "${hostId}" route handler`,
103
+ errors,
104
+ );
105
+ }
106
+ }
107
+ if (isLazyMount(entry.fallback)) {
108
+ await invokeLazyMount(
109
+ entry.fallback.handler,
110
+ `host "${hostId}" fallback handler`,
111
+ errors,
112
+ );
113
+ }
114
+ }
115
+
116
+ return errors;
117
+ }
118
+
119
+ /**
120
+ * Build the terminal "No routers found" message, appending any errors caught
121
+ * during host-router discovery so the real cause is visible.
122
+ *
123
+ * The aggregated errors are inlined into the message (in addition to being
124
+ * attached via `cause` on `DiscoveryError`) so they survive every caller: the
125
+ * dev/HMR paths log `err.message`, and the build path re-throws using
126
+ * `err.stack`, which begins with the message. None of those callers traverse
127
+ * `cause`, so the message must carry the detail. Each error includes its stack
128
+ * when available.
129
+ */
130
+ export function formatNoRoutersError(
131
+ entryPath: string | undefined,
132
+ errors: CaughtDiscoveryError[],
133
+ ): string {
134
+ const base = `[rango] No routers found in registry after importing ${entryPath}`;
135
+ if (errors.length === 0) {
136
+ return base;
137
+ }
138
+
139
+ const formatted = errors
140
+ .map(({ context, error }) => {
141
+ const err = error instanceof Error ? error : new Error(String(error));
142
+ const detail = err.stack ?? err.message;
143
+ return ` - while resolving ${context}:\n${indent(detail, " ")}`;
144
+ })
145
+ .join("\n");
146
+
147
+ return (
148
+ `${base}\n\n` +
149
+ `${errors.length} error(s) were caught during host-router discovery and ` +
150
+ `likely explain why no routers were registered:\n${formatted}`
151
+ );
152
+ }
153
+
154
+ /**
155
+ * Reduce the caught errors to an `ErrorOptions.cause`: a single failure becomes
156
+ * the direct cause; multiple failures are wrapped in an `AggregateError` so
157
+ * each underlying error remains reachable. No errors -> no cause.
158
+ */
159
+ function toCause(errors: CaughtDiscoveryError[]): unknown {
160
+ if (errors.length === 0) return undefined;
161
+ if (errors.length === 1) return errors[0].error;
162
+ return new AggregateError(
163
+ errors.map((e) => e.error),
164
+ "Multiple host-router handlers failed during discovery",
165
+ );
166
+ }
167
+
168
+ /**
169
+ * Thrown when router discovery completes without finding any routers.
170
+ *
171
+ * Carries the entry path and the individual failures caught while resolving
172
+ * host-router lazy handlers. The formatted detail is embedded in `message` (for
173
+ * callers that log `err.message`/`err.stack`) and the underlying error(s) are
174
+ * also attached via `cause` (a single failure directly, multiple wrapped in an
175
+ * `AggregateError`) for cause-aware tooling such as the Vite error overlay.
176
+ */
177
+ export class DiscoveryError extends Error {
178
+ /** The entry file that was imported before discovery gave up. */
179
+ readonly entryPath: string | undefined;
180
+ /** Individual failures caught while resolving host-router handlers. */
181
+ readonly caught: CaughtDiscoveryError[];
182
+
183
+ constructor(entryPath: string | undefined, caught: CaughtDiscoveryError[]) {
184
+ super(formatNoRoutersError(entryPath, caught));
185
+ const cause = toCause(caught);
186
+ if (cause !== undefined) {
187
+ this.cause = cause;
188
+ }
189
+ this.name = "DiscoveryError";
190
+ this.entryPath = entryPath;
191
+ this.caught = caught;
192
+ Object.setPrototypeOf(this, DiscoveryError.prototype);
193
+ }
194
+ }
@@ -0,0 +1,171 @@
1
+ import type { Debugger } from "../debug.js";
2
+
3
+ /**
4
+ * Manifest-readiness gate + rediscovery scheduler.
5
+ *
6
+ * Owns the four pieces of state that cooperate to keep
7
+ * `s.discoveryDone` (the promise the manifest virtual module's `load()`
8
+ * hook awaits) consistent across HMR fan-out:
9
+ *
10
+ * - **gatePending**: a Promise has been issued and not yet resolved.
11
+ * Workerd's manifest virtual module load() is blocked on it.
12
+ * - **inProgress**: a refresh's work callback is currently executing.
13
+ * - **queued**: a refresh was attempted while one was already in
14
+ * flight; the active run consumes this in its `finally` and
15
+ * recurses.
16
+ * - **pendingEvents**: a route-file event has been received (gate
17
+ * already reset) but the corresponding refresh's work hasn't started
18
+ * yet — i.e. the debounce hasn't fired. Set in `noteRouteEvent`,
19
+ * cleared at the start of each refresh cycle. Refresh's finally MUST
20
+ * hold the gate if this is true even when `queued` is false,
21
+ * otherwise an event whose debounce fires AFTER the active refresh
22
+ * completes (the "tail-race" window) would observe a resolved gate.
23
+ *
24
+ * The HMR-event flow (cloudflare-stress repro):
25
+ *
26
+ * t=0 Touch 1 → noteRouteEvent → pendingEvents=true, beginGate
27
+ * (gate1 pending)
28
+ * → debounce 100ms
29
+ * t=100 runRefreshCycle(work) → clear pendingEvents, work starts
30
+ * t=750 Touch 2 → noteRouteEvent → pendingEvents=true (no-op gate)
31
+ * → debounce fires at t=850
32
+ * t=800 refresh A's finally → queued=false, pendingEvents=true
33
+ * → HOLD gate (don't resolve)
34
+ * t=850 runRefreshCycle (debounce) → clear pendingEvents, work starts
35
+ * t=1500 refresh B's finally → queued=false, pendingEvents=false
36
+ * → resolveGate (gate1 resolves)
37
+ *
38
+ * @internal Exported only for unit tests.
39
+ */
40
+ export interface DiscoveryGate {
41
+ /**
42
+ * Reset the gate to a fresh pending Promise via `s.discoveryDone`.
43
+ * No-op when a gate is already pending — file watchers can fire
44
+ * multiple events for one save, and replacing the resolver would
45
+ * orphan the original promise (workerd's manifest load() would hang).
46
+ */
47
+ beginGate(): void;
48
+ /**
49
+ * Resolve the current pending gate. No-op when no gate is pending.
50
+ * Called at the tail of the last refresh cycle in a burst.
51
+ */
52
+ resolveGate(): void;
53
+ /**
54
+ * Record that a route-file event has arrived. Sets `pendingEvents`
55
+ * and begins the gate. Idempotent for both flags.
56
+ */
57
+ noteRouteEvent(): void;
58
+ /**
59
+ * Run one refresh cycle, managing queue + pending state around it.
60
+ * If a cycle is already in flight, sets `queued=true` and returns.
61
+ * Otherwise clears `pendingEvents`, runs `work`, and in `finally`:
62
+ *
63
+ * - queued → recurse, gate stays pending
64
+ * - pendingEvents → hold gate (next debounced cycle resolves)
65
+ * - neither → resolveGate
66
+ */
67
+ runRefreshCycle(work: () => Promise<void>): Promise<void>;
68
+ /** Snapshot of internal state. Test-only. */
69
+ readonly state: () => Readonly<{
70
+ gatePending: boolean;
71
+ inProgress: boolean;
72
+ queued: boolean;
73
+ pendingEvents: boolean;
74
+ }>;
75
+ }
76
+
77
+ /** State container the gate writes `discoveryDone` into. */
78
+ export interface GateOwner {
79
+ discoveryDone: Promise<void> | null | undefined;
80
+ }
81
+
82
+ export function createDiscoveryGate(
83
+ s: GateOwner,
84
+ debug?: Debugger,
85
+ ): DiscoveryGate {
86
+ let gatePending = false;
87
+ let gateResolver: () => void = () => {};
88
+ let inProgress = false;
89
+ let queued = false;
90
+ let pendingEvents = false;
91
+
92
+ const beginGate = (): void => {
93
+ if (gatePending) return;
94
+ s.discoveryDone = new Promise<void>((resolve) => {
95
+ gateResolver = resolve;
96
+ });
97
+ gatePending = true;
98
+ };
99
+
100
+ const resolveGate = (): void => {
101
+ if (!gatePending) return;
102
+ // Defer resolution while a refresh cycle is in flight or queued, or
103
+ // while an unprocessed route-file event is pending its debounce.
104
+ // Without this guard, cold-start's `discover().then(resolveGate)`
105
+ // could fire while an HMR-triggered runRefreshCycle is mid-flight,
106
+ // prematurely unblocking workerd's manifest load() against the
107
+ // stale cold-start gen. The active cycle's `finally` calls
108
+ // resolveGate again at the tail and finishes the resolution then.
109
+ if (inProgress || queued || pendingEvents) {
110
+ debug?.(
111
+ "hmr: resolveGate deferred — work in flight (inProgress=%s queued=%s pendingEvents=%s)",
112
+ inProgress,
113
+ queued,
114
+ pendingEvents,
115
+ );
116
+ return;
117
+ }
118
+ gatePending = false;
119
+ debug?.("hmr: discoveryDone resolved");
120
+ gateResolver();
121
+ };
122
+
123
+ const noteRouteEvent = (): void => {
124
+ pendingEvents = true;
125
+ beginGate();
126
+ };
127
+
128
+ const runRefreshCycle = async (work: () => Promise<void>): Promise<void> => {
129
+ if (inProgress) {
130
+ queued = true;
131
+ debug?.("hmr: rediscovery in flight — queued for a follow-up cycle");
132
+ return;
133
+ }
134
+ // Snapshot the current pendingEvents into "we're about to process";
135
+ // events arriving from now on re-set it.
136
+ pendingEvents = false;
137
+ inProgress = true;
138
+ try {
139
+ await work();
140
+ } finally {
141
+ inProgress = false;
142
+ if (queued) {
143
+ queued = false;
144
+ debug?.("hmr: consuming queued rediscovery");
145
+ runRefreshCycle(work).catch((err: unknown) => {
146
+ debug?.(
147
+ "hmr: queued cycle rejected — releasing gate (%s)",
148
+ err instanceof Error ? err.message : String(err),
149
+ );
150
+ // Belt-and-suspenders: even if the queued cycle's own try/catch
151
+ // missed something, ensure workerd doesn't hang.
152
+ resolveGate();
153
+ });
154
+ } else if (pendingEvents) {
155
+ debug?.(
156
+ "hmr: holding gate for pending events (debounce not yet fired)",
157
+ );
158
+ } else {
159
+ resolveGate();
160
+ }
161
+ }
162
+ };
163
+
164
+ return {
165
+ beginGate,
166
+ resolveGate,
167
+ noteRouteEvent,
168
+ runRefreshCycle,
169
+ state: () => ({ gatePending, inProgress, queued, pendingEvents }),
170
+ };
171
+ }
@@ -16,6 +16,9 @@ import {
16
16
  stageBuildAssetModule,
17
17
  } from "../utils/prerender-utils.js";
18
18
  import type { DiscoveryState } from "./state.js";
19
+ import { createRangoDebugger, NS } from "../debug.js";
20
+
21
+ const debug = createRangoDebugger(NS.prerender);
19
22
 
20
23
  /**
21
24
  * Expand prerender routes into concrete URLs and render them via the
@@ -30,6 +33,12 @@ export async function expandPrerenderRoutes(
30
33
  ): Promise<void> {
31
34
  if (!state.opts?.enableBuildPrerender || !state.isBuildMode) return;
32
35
 
36
+ const overallStart = debug ? performance.now() : 0;
37
+ debug?.(
38
+ "expandPrerenderRoutes: start (%d router manifest(s))",
39
+ allManifests.length,
40
+ );
41
+
33
42
  type PrerenderEntry = {
34
43
  urlPath: string;
35
44
  routeName: string;
@@ -72,7 +81,7 @@ export async function expandPrerenderRoutes(
72
81
  ? setInterval(() => {
73
82
  const elapsed = ((performance.now() - paramsStart) / 1000).toFixed(1);
74
83
  console.log(
75
- `[rsc-router] Resolving prerender params... ${resolvedRoutes}/${totalDynamic} routes (${elapsed}s)`,
84
+ `[rango] Resolving prerender params... ${resolvedRoutes}/${totalDynamic} routes (${elapsed}s)`,
76
85
  );
77
86
  }, 5000)
78
87
  : undefined;
@@ -99,6 +108,7 @@ export async function expandPrerenderRoutes(
99
108
  } else {
100
109
  // Dynamic route: call getParams() to enumerate param combinations
101
110
  if (def?.getParams) {
111
+ const getParamsStart = debug ? performance.now() : 0;
102
112
  try {
103
113
  const buildVars: Record<string, any> = {};
104
114
  const buildEnv = state.resolvedBuildEnv;
@@ -112,12 +122,18 @@ export async function expandPrerenderRoutes(
112
122
  get env() {
113
123
  if (buildEnv !== undefined) return buildEnv;
114
124
  throw new Error(
115
- "[rsc-router] ctx.env is not available during build-time getParams(). " +
125
+ "[rango] ctx.env is not available during build-time getParams(). " +
116
126
  "Configure buildEnv in your rango() plugin options to enable build-time env access.",
117
127
  );
118
128
  },
119
129
  };
120
130
  const paramsList = await def.getParams(getParamsCtx);
131
+ debug?.(
132
+ "getParams %s -> %d params (%sms)",
133
+ routeName,
134
+ paramsList.length,
135
+ (performance.now() - getParamsStart).toFixed(1),
136
+ );
121
137
  const concurrency = def.options?.concurrency ?? 1;
122
138
  const hasBuildVars =
123
139
  Object.keys(buildVars).length > 0 ||
@@ -154,7 +170,7 @@ export async function expandPrerenderRoutes(
154
170
  // Skip in getParams() skips the entire route
155
171
  if (err.name === "Skip") {
156
172
  console.log(
157
- `[rsc-router] SKIP route "${routeName}" - ${err.message}`,
173
+ `[rango] SKIP route "${routeName}" - ${err.message}`,
158
174
  );
159
175
  notifyOnError(
160
176
  registry,
@@ -168,14 +184,14 @@ export async function expandPrerenderRoutes(
168
184
  }
169
185
  // Regular error: fail the build
170
186
  console.error(
171
- `[rsc-router] Failed to get params for prerender route "${routeName}": ${err.message}`,
187
+ `[rango] Failed to get params for prerender route "${routeName}": ${err.message}`,
172
188
  );
173
189
  notifyOnError(registry, err, "prerender", routeName);
174
190
  throw err;
175
191
  }
176
192
  } else {
177
193
  console.warn(
178
- `[rsc-router] Dynamic prerender route "${routeName}" has no getParams(), skipping`,
194
+ `[rango] Dynamic prerender route "${routeName}" has no getParams(), skipping`,
179
195
  );
180
196
  }
181
197
  }
@@ -186,19 +202,30 @@ export async function expandPrerenderRoutes(
186
202
  clearInterval(progressInterval);
187
203
  const elapsed = ((performance.now() - paramsStart) / 1000).toFixed(1);
188
204
  console.log(
189
- `[rsc-router] Resolved prerender params: ${resolvedRoutes}/${totalDynamic} routes (${elapsed}s)`,
205
+ `[rango] Resolved prerender params: ${resolvedRoutes}/${totalDynamic} routes (${elapsed}s)`,
190
206
  );
191
207
  }
192
208
  }
193
209
 
194
- if (entries.length === 0) return;
210
+ if (entries.length === 0) {
211
+ debug?.(
212
+ "no prerender entries (done in %sms)",
213
+ (performance.now() - overallStart).toFixed(1),
214
+ );
215
+ return;
216
+ }
195
217
 
196
218
  // Determine the max concurrency for the log header
197
219
  const maxConcurrency = Math.max(...entries.map((e) => e.concurrency));
198
220
  const concurrencyNote =
199
221
  maxConcurrency > 1 ? ` (concurrency: ${maxConcurrency})` : "";
200
222
  console.log(
201
- `[rsc-router] Pre-rendering ${entries.length} URL(s)${concurrencyNote}...`,
223
+ `[rango] Pre-rendering ${entries.length} URL(s)${concurrencyNote}...`,
224
+ );
225
+ debug?.(
226
+ "prerender loop: %d entries, max concurrency %d",
227
+ entries.length,
228
+ maxConcurrency,
202
229
  );
203
230
 
204
231
  const { hashParams } = await rscEnv.runner.import("@rangojs/router/build");
@@ -234,7 +261,7 @@ export async function expandPrerenderRoutes(
234
261
  if (result.passthrough) {
235
262
  const elapsed = (performance.now() - startUrl).toFixed(0);
236
263
  console.log(
237
- `[rsc-router] PASS ${entry.urlPath.padEnd(40)} (${elapsed}ms) - live fallback`,
264
+ `[rango] PASS ${entry.urlPath.padEnd(40)} (${elapsed}ms) - live fallback`,
238
265
  );
239
266
  doneCount++;
240
267
  break;
@@ -255,10 +282,9 @@ export async function expandPrerenderRoutes(
255
282
  const interceptKey = `${result.routeName}/${paramHash}/i`;
256
283
  const interceptValue = JSON.stringify({
257
284
  segments: [...result.segments, ...result.interceptSegments],
258
- handles: {
259
- ...result.handles,
260
- ...(result.interceptHandles || {}),
261
- },
285
+ // interceptHandles is the pre-encoded MERGED (main + intercept)
286
+ // handle string (the producer merged before encoding).
287
+ handles: result.interceptHandles ?? "",
262
288
  });
263
289
  manifestEntries[interceptKey] = stageBuildAssetModule(
264
290
  state.projectRoot,
@@ -268,7 +294,7 @@ export async function expandPrerenderRoutes(
268
294
  }
269
295
  const elapsed = (performance.now() - startUrl).toFixed(0);
270
296
  console.log(
271
- `[rsc-router] OK ${entry.urlPath.padEnd(40)} (${elapsed}ms)`,
297
+ `[rango] OK ${entry.urlPath.padEnd(40)} (${elapsed}ms)`,
272
298
  );
273
299
  doneCount++;
274
300
  break;
@@ -276,7 +302,7 @@ export async function expandPrerenderRoutes(
276
302
  if (err.name === "Skip") {
277
303
  const elapsed = (performance.now() - startUrl).toFixed(0);
278
304
  console.log(
279
- `[rsc-router] SKIP ${entry.urlPath.padEnd(40)} (${elapsed}ms) - ${err.message}`,
305
+ `[rango] SKIP ${entry.urlPath.padEnd(40)} (${elapsed}ms) - ${err.message}`,
280
306
  );
281
307
  skipCount++;
282
308
  notifyOnError(
@@ -292,7 +318,7 @@ export async function expandPrerenderRoutes(
292
318
  // Regular error: log, notify, and fail the build
293
319
  const elapsed = (performance.now() - startUrl).toFixed(0);
294
320
  console.error(
295
- `[rsc-router] FAIL ${entry.urlPath.padEnd(40)} (${elapsed}ms) - ${err.message}`,
321
+ `[rango] FAIL ${entry.urlPath.padEnd(40)} (${elapsed}ms) - ${err.message}`,
296
322
  );
297
323
  notifyOnError(
298
324
  registry,
@@ -315,7 +341,14 @@ export async function expandPrerenderRoutes(
315
341
  const parts = [`${doneCount} done`];
316
342
  if (skipCount > 0) parts.push(`${skipCount} skipped`);
317
343
  console.log(
318
- `[rsc-router] Pre-render complete: ${parts.join(", ")} (${totalElapsed}ms total)`,
344
+ `[rango] Pre-render complete: ${parts.join(", ")} (${totalElapsed}ms total)`,
345
+ );
346
+ debug?.(
347
+ "expandPrerenderRoutes done: %d done, %d skipped, %sms (overall %sms)",
348
+ doneCount,
349
+ skipCount,
350
+ totalElapsed,
351
+ (performance.now() - overallStart).toFixed(1),
319
352
  );
320
353
  }
321
354
 
@@ -337,6 +370,12 @@ export async function renderStaticHandlers(
337
370
  )
338
371
  return;
339
372
 
373
+ const overallStart = debug ? performance.now() : 0;
374
+ debug?.(
375
+ "renderStaticHandlers: start (%d static module(s))",
376
+ state.resolvedStaticModules.size,
377
+ );
378
+
340
379
  const manifestEntries: Record<string, string> = {};
341
380
  let staticDone = 0;
342
381
  let staticSkip = 0;
@@ -347,9 +386,7 @@ export async function renderStaticHandlers(
347
386
  totalStaticCount += exportNames.length;
348
387
  }
349
388
  const startStatic = performance.now();
350
- console.log(
351
- `[rsc-router] Rendering ${totalStaticCount} static handler(s)...`,
352
- );
389
+ console.log(`[rango] Rendering ${totalStaticCount} static handler(s)...`);
353
390
 
354
391
  for (const [moduleId, exportNames] of state.resolvedStaticModules) {
355
392
  let mod: any;
@@ -357,7 +394,7 @@ export async function renderStaticHandlers(
357
394
  mod = await rscEnv!.runner.import(moduleId);
358
395
  } catch (err: any) {
359
396
  console.error(
360
- `[rsc-router] Failed to import static module ${moduleId}: ${err.message}`,
397
+ `[rango] Failed to import static module ${moduleId}: ${err.message}`,
361
398
  );
362
399
  notifyOnError(registry, err, "static");
363
400
  throw err;
@@ -382,7 +419,8 @@ export async function renderStaticHandlers(
382
419
  !state.isBuildMode,
383
420
  );
384
421
  if (result) {
385
- const hasHandles = Object.keys(result.handles).length > 0;
422
+ // result.handles is the pre-encoded handle string ("" when none).
423
+ const hasHandles = result.handles !== "";
386
424
  const exportValue = hasHandles
387
425
  ? JSON.stringify(result)
388
426
  : JSON.stringify(result.encoded);
@@ -392,9 +430,7 @@ export async function renderStaticHandlers(
392
430
  exportValue,
393
431
  );
394
432
  const elapsed = (performance.now() - startHandler).toFixed(0);
395
- console.log(
396
- `[rsc-router] OK ${name.padEnd(40)} (${elapsed}ms)`,
397
- );
433
+ console.log(`[rango] OK ${name.padEnd(40)} (${elapsed}ms)`);
398
434
  staticDone++;
399
435
  handled = true;
400
436
  break;
@@ -403,7 +439,7 @@ export async function renderStaticHandlers(
403
439
  if (err.name === "Skip") {
404
440
  const elapsed = (performance.now() - startHandler).toFixed(0);
405
441
  console.log(
406
- `[rsc-router] SKIP ${name.padEnd(40)} (${elapsed}ms) - ${err.message}`,
442
+ `[rango] SKIP ${name.padEnd(40)} (${elapsed}ms) - ${err.message}`,
407
443
  );
408
444
  staticSkip++;
409
445
  notifyOnError(registry, err, "static", undefined, undefined, true);
@@ -413,16 +449,14 @@ export async function renderStaticHandlers(
413
449
  // Regular error: log, notify, and fail the build
414
450
  const elapsed = (performance.now() - startHandler).toFixed(0);
415
451
  console.error(
416
- `[rsc-router] FAIL ${name.padEnd(40)} (${elapsed}ms) - ${err.message}`,
452
+ `[rango] FAIL ${name.padEnd(40)} (${elapsed}ms) - ${err.message}`,
417
453
  );
418
454
  notifyOnError(registry, err, "static");
419
455
  throw err;
420
456
  }
421
457
  }
422
458
  if (!handled) {
423
- console.warn(
424
- `[rsc-router] No router could render static handler "${name}"`,
425
- );
459
+ console.warn(`[rango] No router could render static handler "${name}"`);
426
460
  }
427
461
  }
428
462
  }
@@ -434,6 +468,13 @@ export async function renderStaticHandlers(
434
468
  const staticParts = [`${staticDone} done`];
435
469
  if (staticSkip > 0) staticParts.push(`${staticSkip} skipped`);
436
470
  console.log(
437
- `[rsc-router] Static render complete: ${staticParts.join(", ")} (${totalStaticElapsed}ms total)`,
471
+ `[rango] Static render complete: ${staticParts.join(", ")} (${totalStaticElapsed}ms total)`,
472
+ );
473
+ debug?.(
474
+ "renderStaticHandlers done: %d done, %d skipped, %sms (overall %sms)",
475
+ staticDone,
476
+ staticSkip,
477
+ totalStaticElapsed,
478
+ (performance.now() - overallStart).toFixed(1),
438
479
  );
439
480
  }