@rangojs/router 0.0.0-experimental.8a4d0430 → 0.0.0-experimental.8bcfea43

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 (174) hide show
  1. package/AGENTS.md +4 -0
  2. package/README.md +126 -38
  3. package/dist/bin/rango.js +138 -50
  4. package/dist/vite/index.js +1171 -461
  5. package/dist/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
  6. package/package.json +19 -16
  7. package/skills/breadcrumbs/SKILL.md +3 -1
  8. package/skills/cache-guide/SKILL.md +32 -0
  9. package/skills/caching/SKILL.md +45 -4
  10. package/skills/handler-use/SKILL.md +362 -0
  11. package/skills/hooks/SKILL.md +28 -20
  12. package/skills/intercept/SKILL.md +20 -0
  13. package/skills/layout/SKILL.md +22 -0
  14. package/skills/links/SKILL.md +91 -17
  15. package/skills/loader/SKILL.md +88 -45
  16. package/skills/middleware/SKILL.md +34 -3
  17. package/skills/migrate-nextjs/SKILL.md +560 -0
  18. package/skills/migrate-react-router/SKILL.md +765 -0
  19. package/skills/parallel/SKILL.md +185 -0
  20. package/skills/prerender/SKILL.md +110 -68
  21. package/skills/rango/SKILL.md +24 -22
  22. package/skills/response-routes/SKILL.md +8 -0
  23. package/skills/route/SKILL.md +55 -0
  24. package/skills/router-setup/SKILL.md +87 -2
  25. package/skills/streams-and-websockets/SKILL.md +283 -0
  26. package/skills/typesafety/SKILL.md +13 -1
  27. package/src/__internal.ts +1 -1
  28. package/src/browser/app-shell.ts +52 -0
  29. package/src/browser/app-version.ts +14 -0
  30. package/src/browser/event-controller.ts +5 -0
  31. package/src/browser/navigation-bridge.ts +90 -16
  32. package/src/browser/navigation-client.ts +167 -59
  33. package/src/browser/navigation-store.ts +68 -9
  34. package/src/browser/navigation-transaction.ts +11 -9
  35. package/src/browser/partial-update.ts +113 -17
  36. package/src/browser/prefetch/cache.ts +184 -16
  37. package/src/browser/prefetch/fetch.ts +180 -33
  38. package/src/browser/prefetch/policy.ts +6 -0
  39. package/src/browser/prefetch/queue.ts +123 -20
  40. package/src/browser/prefetch/resource-ready.ts +77 -0
  41. package/src/browser/rango-state.ts +53 -13
  42. package/src/browser/react/Link.tsx +81 -9
  43. package/src/browser/react/NavigationProvider.tsx +89 -14
  44. package/src/browser/react/context.ts +7 -2
  45. package/src/browser/react/use-handle.ts +9 -58
  46. package/src/browser/react/use-navigation.ts +22 -2
  47. package/src/browser/react/use-params.ts +11 -1
  48. package/src/browser/react/use-router.ts +29 -9
  49. package/src/browser/rsc-router.tsx +168 -65
  50. package/src/browser/scroll-restoration.ts +41 -42
  51. package/src/browser/segment-reconciler.ts +36 -9
  52. package/src/browser/server-action-bridge.ts +8 -6
  53. package/src/browser/types.ts +49 -5
  54. package/src/build/generate-manifest.ts +6 -6
  55. package/src/build/generate-route-types.ts +3 -0
  56. package/src/build/route-trie.ts +50 -24
  57. package/src/build/route-types/include-resolution.ts +8 -1
  58. package/src/build/route-types/router-processing.ts +223 -74
  59. package/src/build/route-types/scan-filter.ts +8 -1
  60. package/src/cache/cache-runtime.ts +15 -11
  61. package/src/cache/cache-scope.ts +48 -7
  62. package/src/cache/cf/cf-cache-store.ts +455 -15
  63. package/src/cache/cf/index.ts +5 -1
  64. package/src/cache/document-cache.ts +17 -7
  65. package/src/cache/index.ts +1 -0
  66. package/src/cache/taint.ts +55 -0
  67. package/src/client.tsx +84 -230
  68. package/src/context-var.ts +72 -2
  69. package/src/debug.ts +2 -2
  70. package/src/handle.ts +40 -0
  71. package/src/index.rsc.ts +6 -1
  72. package/src/index.ts +49 -6
  73. package/src/outlet-context.ts +1 -1
  74. package/src/prerender/store.ts +5 -4
  75. package/src/prerender.ts +138 -77
  76. package/src/response-utils.ts +28 -0
  77. package/src/reverse.ts +27 -2
  78. package/src/route-definition/dsl-helpers.ts +240 -40
  79. package/src/route-definition/helpers-types.ts +67 -19
  80. package/src/route-definition/index.ts +3 -0
  81. package/src/route-definition/redirect.ts +11 -3
  82. package/src/route-definition/resolve-handler-use.ts +155 -0
  83. package/src/route-map-builder.ts +7 -1
  84. package/src/route-types.ts +18 -0
  85. package/src/router/content-negotiation.ts +100 -1
  86. package/src/router/find-match.ts +4 -2
  87. package/src/router/handler-context.ts +101 -25
  88. package/src/router/intercept-resolution.ts +11 -4
  89. package/src/router/lazy-includes.ts +10 -7
  90. package/src/router/loader-resolution.ts +159 -21
  91. package/src/router/logging.ts +5 -2
  92. package/src/router/manifest.ts +31 -16
  93. package/src/router/match-api.ts +127 -192
  94. package/src/router/match-middleware/background-revalidation.ts +30 -2
  95. package/src/router/match-middleware/cache-lookup.ts +94 -17
  96. package/src/router/match-middleware/cache-store.ts +53 -10
  97. package/src/router/match-middleware/intercept-resolution.ts +9 -7
  98. package/src/router/match-middleware/segment-resolution.ts +61 -5
  99. package/src/router/match-result.ts +104 -10
  100. package/src/router/metrics.ts +6 -1
  101. package/src/router/middleware-types.ts +8 -30
  102. package/src/router/middleware.ts +36 -10
  103. package/src/router/navigation-snapshot.ts +182 -0
  104. package/src/router/pattern-matching.ts +60 -9
  105. package/src/router/prerender-match.ts +110 -10
  106. package/src/router/preview-match.ts +30 -102
  107. package/src/router/request-classification.ts +310 -0
  108. package/src/router/route-snapshot.ts +245 -0
  109. package/src/router/router-context.ts +6 -1
  110. package/src/router/router-interfaces.ts +36 -4
  111. package/src/router/router-options.ts +37 -11
  112. package/src/router/segment-resolution/fresh.ts +198 -20
  113. package/src/router/segment-resolution/helpers.ts +29 -24
  114. package/src/router/segment-resolution/loader-cache.ts +1 -0
  115. package/src/router/segment-resolution/revalidation.ts +438 -300
  116. package/src/router/segment-wrappers.ts +2 -0
  117. package/src/router/trie-matching.ts +10 -4
  118. package/src/router/types.ts +1 -0
  119. package/src/router/url-params.ts +49 -0
  120. package/src/router.ts +60 -8
  121. package/src/rsc/handler.ts +478 -374
  122. package/src/rsc/helpers.ts +69 -41
  123. package/src/rsc/loader-fetch.ts +23 -3
  124. package/src/rsc/manifest-init.ts +5 -1
  125. package/src/rsc/progressive-enhancement.ts +16 -2
  126. package/src/rsc/response-route-handler.ts +14 -1
  127. package/src/rsc/rsc-rendering.ts +19 -1
  128. package/src/rsc/server-action.ts +10 -0
  129. package/src/rsc/ssr-setup.ts +2 -2
  130. package/src/rsc/types.ts +9 -1
  131. package/src/segment-content-promise.ts +67 -0
  132. package/src/segment-loader-promise.ts +122 -0
  133. package/src/segment-system.tsx +109 -23
  134. package/src/server/context.ts +166 -17
  135. package/src/server/handle-store.ts +19 -0
  136. package/src/server/loader-registry.ts +9 -8
  137. package/src/server/request-context.ts +194 -60
  138. package/src/ssr/index.tsx +4 -0
  139. package/src/static-handler.ts +18 -6
  140. package/src/types/cache-types.ts +4 -4
  141. package/src/types/handler-context.ts +137 -65
  142. package/src/types/loader-types.ts +41 -15
  143. package/src/types/request-scope.ts +126 -0
  144. package/src/types/route-entry.ts +19 -1
  145. package/src/types/segments.ts +2 -0
  146. package/src/urls/include-helper.ts +24 -14
  147. package/src/urls/path-helper-types.ts +39 -6
  148. package/src/urls/path-helper.ts +48 -13
  149. package/src/urls/pattern-types.ts +12 -0
  150. package/src/urls/response-types.ts +18 -16
  151. package/src/use-loader.tsx +77 -5
  152. package/src/vite/debug.ts +55 -0
  153. package/src/vite/discovery/bundle-postprocess.ts +30 -33
  154. package/src/vite/discovery/discover-routers.ts +5 -1
  155. package/src/vite/discovery/prerender-collection.ts +128 -74
  156. package/src/vite/discovery/state.ts +13 -6
  157. package/src/vite/index.ts +4 -0
  158. package/src/vite/plugin-types.ts +51 -79
  159. package/src/vite/plugins/cloudflare-protocol-loader-hook.d.mts +23 -0
  160. package/src/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
  161. package/src/vite/plugins/cloudflare-protocol-stub.ts +214 -0
  162. package/src/vite/plugins/expose-action-id.ts +1 -3
  163. package/src/vite/plugins/expose-id-utils.ts +12 -0
  164. package/src/vite/plugins/expose-ids/handler-transform.ts +30 -0
  165. package/src/vite/plugins/expose-internal-ids.ts +257 -40
  166. package/src/vite/plugins/performance-tracks.ts +86 -0
  167. package/src/vite/plugins/refresh-cmd.ts +88 -26
  168. package/src/vite/plugins/version-plugin.ts +13 -1
  169. package/src/vite/rango.ts +204 -217
  170. package/src/vite/router-discovery.ts +335 -64
  171. package/src/vite/utils/banner.ts +4 -4
  172. package/src/vite/utils/package-resolution.ts +41 -1
  173. package/src/vite/utils/prerender-utils.ts +37 -5
  174. package/src/vite/utils/shared-utils.ts +3 -2
@@ -7,6 +7,7 @@
7
7
  import type { ReactNode } from "react";
8
8
  import { track } from "../server/context";
9
9
  import type { EntryData } from "../server/context";
10
+ import { contextGet } from "../context-var.js";
10
11
  import type {
11
12
  ResolvedSegment,
12
13
  HandlerContext,
@@ -19,10 +20,11 @@ import type {
19
20
  ErrorInfo,
20
21
  } from "../types";
21
22
  import type { LoaderRevalidationResult, ActionContext } from "./types";
22
- import { isHandle, type Handle } from "../handle.js";
23
- import type { HandleStore } from "../server/handle-store.js";
23
+ import { isHandle, collectHandleData, type Handle } from "../handle.js";
24
+ import { buildHandleSnapshot } from "../server/handle-store.js";
24
25
  import { getFetchableLoader } from "../server/fetchable-loader-store.js";
25
26
  import { _getRequestContext } from "../server/request-context.js";
27
+ import { isInsideLoaderScope } from "../server/context.js";
26
28
  import { debugLog } from "./logging.js";
27
29
 
28
30
  /**
@@ -241,6 +243,21 @@ function createLoaderExecutor<TEnv>(
241
243
  pendingLoaders.add(loader.$$id);
242
244
 
243
245
  const currentLoaderId = loader.$$id;
246
+ const variables = (ctx as InternalHandlerContext<any, TEnv>)._variables;
247
+
248
+ // Capture whether this loader is being started from a DSL loader scope
249
+ // (runInsideLoaderScope in fresh.ts). Handler-invoked loaders are NOT
250
+ // inside loader scope. This determines whether rendered() is allowed.
251
+ const isDslLoader = isInsideLoaderScope();
252
+
253
+ let renderedResolved = false;
254
+ let renderedPromise: Promise<void> | null = null;
255
+
256
+ // Loader functions are always fresh (never cached), so they get an
257
+ // unguarded get that bypasses non-cacheable read guards. This applies
258
+ // to ALL loaders — DSL and handler-called — because the loader
259
+ // function itself always re-executes. Also handles nested deps
260
+ // (loaderA → use(loaderB)) since all share this unguarded get.
244
261
  const loaderCtx: LoaderContext<Record<string, string | undefined>, TEnv> = {
245
262
  params: ctx.params,
246
263
  routeParams: (ctx.params ?? {}) as Record<string, string>,
@@ -249,17 +266,90 @@ function createLoaderExecutor<TEnv>(
249
266
  search: (ctx as any).search,
250
267
  pathname: ctx.pathname,
251
268
  url: ctx.url,
269
+ originalUrl: ctx.originalUrl,
252
270
  env: ctx.env,
253
- var: ctx.var,
254
- get: ctx.get,
255
- use: <TDep, TDepParams = any>(
256
- dep: LoaderDefinition<TDep, TDepParams>,
257
- ): Promise<TDep> => {
258
- return useLoader(dep, currentLoaderId);
259
- },
271
+ waitUntil: ctx.waitUntil.bind(ctx),
272
+ executionContext: ctx.executionContext,
273
+ get: ((keyOrVar: any) =>
274
+ contextGet(variables, keyOrVar)) as typeof ctx.get,
275
+ use: ((item: LoaderDefinition<any, any> | Handle<any, any>) => {
276
+ if (isHandle(item)) {
277
+ if (!renderedResolved) {
278
+ throw new Error(
279
+ `ctx.use(handle) in a loader requires "await ctx.rendered()" first. ` +
280
+ `Handle "${item.$$id}" cannot be read until the render tree has settled.`,
281
+ );
282
+ }
283
+ const reqCtx = reqCtxRef ?? _getRequestContext();
284
+ if (!reqCtx) {
285
+ throw new Error(
286
+ `ctx.use(handle) failed: request context not available.`,
287
+ );
288
+ }
289
+ const segmentOrder = reqCtx._renderBarrierSegmentOrder ?? [];
290
+ const snapshot =
291
+ reqCtx._renderBarrierHandleSnapshot ??
292
+ buildHandleSnapshot(reqCtx._handleStore, segmentOrder);
293
+ return collectHandleData(item, snapshot, segmentOrder);
294
+ }
295
+
296
+ // Loader case
297
+ return useLoader(item as LoaderDefinition<any, any>, currentLoaderId);
298
+ }) as LoaderContext["use"],
260
299
  method: "GET",
261
300
  body: undefined,
262
301
  reverse: ctx.reverse as LoaderContext["reverse"],
302
+ rendered: (): Promise<void> => {
303
+ // Guard: only DSL loaders may use rendered()
304
+ if (!isDslLoader) {
305
+ throw new Error(
306
+ `ctx.rendered() is only available in DSL loaders (registered via loader() in urls()). ` +
307
+ `Handler-invoked loaders (ctx.use(Loader) inside a handler) cannot use rendered().`,
308
+ );
309
+ }
310
+
311
+ // Guard: reject streaming trees
312
+ const reqCtx = reqCtxRef ?? _getRequestContext();
313
+ if (reqCtx?._treeHasStreaming) {
314
+ throw new Error(
315
+ `ctx.rendered() is not supported when the matched route tree uses loading(). ` +
316
+ `Streaming handlers may not have settled when rendered() resolves. ` +
317
+ `Remove loading() from the route tree or restructure to avoid rendered().`,
318
+ );
319
+ }
320
+
321
+ if (renderedPromise) return renderedPromise;
322
+
323
+ if (!reqCtx) {
324
+ throw new Error(
325
+ `ctx.rendered() failed: request context not available.`,
326
+ );
327
+ }
328
+
329
+ // Bidirectional deadlock check: if a handler already started
330
+ // awaiting this loader, calling rendered() would deadlock.
331
+ if (reqCtx._handlerLoaderDeps?.has(currentLoaderId)) {
332
+ throw new Error(
333
+ `Deadlock: loader "${currentLoaderId}" called ctx.rendered() but a handler ` +
334
+ `is already awaiting this loader via ctx.use(). The handler blocks ` +
335
+ `segment resolution, which blocks the barrier, which blocks this loader. ` +
336
+ `Move the data dependency to a loader-to-loader pattern instead.`,
337
+ );
338
+ }
339
+
340
+ // Register this loader as waiting for the barrier so that
341
+ // setupLoaderAccess can detect deadlocks when a handler
342
+ // tries to await the same loader via ctx.use().
343
+ if (!reqCtx._renderBarrierWaiters) {
344
+ reqCtx._renderBarrierWaiters = new Set();
345
+ }
346
+ reqCtx._renderBarrierWaiters.add(currentLoaderId);
347
+
348
+ renderedPromise = reqCtx._renderBarrier.then(() => {
349
+ renderedResolved = true;
350
+ });
351
+ return renderedPromise;
352
+ },
263
353
  };
264
354
 
265
355
  const doneLoader = track(`loader:${loader.$$id}`, 2);
@@ -290,15 +380,22 @@ export function setupLoaderAccess<TEnv>(
290
380
  ctx: HandlerContext<any, TEnv>,
291
381
  loaderPromises: Map<string, Promise<any>>,
292
382
  ): void {
293
- // Eagerly capture the HandleStore at setup time (before pipeline async ops).
294
- // In workerd/Cloudflare, dynamic imports and fetch() in the match pipeline
295
- // can disrupt AsyncLocalStorage, causing getRequestContext() to return
296
- // undefined when handlers later call ctx.use(handle). Capturing early
297
- // ensures the store reference survives ALS disruption.
298
- const handleStoreRef = _getRequestContext()?._handleStore;
383
+ // Eagerly capture the request context and HandleStore at setup time
384
+ // (before pipeline async ops). In workerd/Cloudflare, dynamic imports and
385
+ // fetch() in the match pipeline can disrupt AsyncLocalStorage, causing
386
+ // getRequestContext() to return undefined when handlers later call
387
+ // ctx.use(handle). Capturing early ensures references survive ALS disruption.
388
+ const reqCtxRef = _getRequestContext();
389
+ const handleStoreRef = reqCtxRef?._handleStore;
299
390
 
300
391
  const useLoader = createLoaderExecutor(ctx, loaderPromises);
301
392
 
393
+ // Track whether we're inside a handle push callback. Loaders started
394
+ // from push callbacks (e.g. push(async () => ctx.use(Loader))) do NOT
395
+ // block segment resolution, so they must not be registered as handler
396
+ // dependencies for deadlock detection.
397
+ let insideHandlePush = false;
398
+
302
399
  ctx.use = ((item: LoaderDefinition<any, any> | Handle<any, any>) => {
303
400
  if (isHandle(item)) {
304
401
  const handle = item;
@@ -318,16 +415,57 @@ export function setupLoaderAccess<TEnv>(
318
415
  ) => {
319
416
  if (!store) return;
320
417
 
321
- const valueOrPromise =
322
- typeof dataOrFn === "function"
323
- ? (dataOrFn as () => Promise<unknown>)()
324
- : dataOrFn;
418
+ if (typeof dataOrFn === "function") {
419
+ // Mark scope so ctx.use(loader) calls inside the callback
420
+ // are not registered as handler-to-loader deps.
421
+ insideHandlePush = true;
422
+ try {
423
+ const result = (dataOrFn as () => Promise<unknown>)();
424
+ store.push(handle.$$id, segmentId, result);
425
+ } finally {
426
+ insideHandlePush = false;
427
+ }
428
+ return;
429
+ }
325
430
 
326
- store.push(handle.$$id, segmentId, valueOrPromise);
431
+ store.push(handle.$$id, segmentId, dataOrFn);
327
432
  };
328
433
  }
329
434
 
330
- return useLoader(item as LoaderDefinition<any, any>, null);
435
+ // Deadlock guard and handler-to-loader dependency tracking.
436
+ // Skip when inside a DSL loader scope (resolveLoaderData also calls
437
+ // ctx.use() but that's DSL-to-DSL, not handler-to-loader) or when
438
+ // inside a handle push callback (push callbacks don't block segment
439
+ // resolution so they can't cause rendered() deadlocks).
440
+ const loader = item as LoaderDefinition<any, any>;
441
+ if (!isInsideLoaderScope() && !insideHandlePush) {
442
+ const reqCtx = reqCtxRef ?? _getRequestContext();
443
+ if (reqCtx) {
444
+ // Direction 1: handler awaits loader that already called rendered()
445
+ if (
446
+ loaderPromises.has(loader.$$id) &&
447
+ reqCtx._renderBarrierWaiters?.has(loader.$$id)
448
+ ) {
449
+ throw new Error(
450
+ `Deadlock: handler is awaiting loader "${loader.$$id}" which called ctx.rendered(). ` +
451
+ `The loader is waiting for segment resolution, but the handler blocks resolution. ` +
452
+ `Move the data dependency to a loader-to-loader pattern instead.`,
453
+ );
454
+ }
455
+ // Direction 2: track dep so rendered() can detect the deadlock
456
+ // if the loader calls it later. Skip when the barrier has already
457
+ // resolved — no deadlock is possible (rendered() resolves immediately).
458
+ // _renderBarrierSegmentOrder is undefined before resolution, string[]
459
+ // after. This also prevents false positives from handle push callbacks
460
+ // that resume after their first await (post-barrier-resolution).
461
+ if (reqCtx._renderBarrierSegmentOrder === undefined) {
462
+ if (!reqCtx._handlerLoaderDeps) reqCtx._handlerLoaderDeps = new Set();
463
+ reqCtx._handlerLoaderDeps.add(loader.$$id);
464
+ }
465
+ }
466
+ }
467
+
468
+ return useLoader(loader, null);
331
469
  }) as typeof ctx.use;
332
470
  }
333
471
 
@@ -12,7 +12,10 @@ export interface RevalidationTraceEntry {
12
12
  | "cache-hit"
13
13
  | "loader"
14
14
  | "parallel"
15
- | "orphan-layout";
15
+ | "orphan-layout"
16
+ | "route-handler"
17
+ | "layout-handler"
18
+ | "intercept-loader";
16
19
  defaultShouldRevalidate: boolean;
17
20
  finalShouldRevalidate: boolean;
18
21
  reason: string;
@@ -71,7 +74,7 @@ function getHeaderRequestId(request: Request): string | null {
71
74
  return trimmed.length > 0 ? trimmed : null;
72
75
  }
73
76
 
74
- function getOrCreateRequestId(request: Request): string {
77
+ export function getOrCreateRequestId(request: Request): string {
75
78
  const existing = requestIds.get(request);
76
79
  if (existing) return existing;
77
80
 
@@ -9,6 +9,7 @@ import { createRouteHelpers } from "../route-definition";
9
9
  import {
10
10
  getContext,
11
11
  runWithPrefixes,
12
+ getIsolatedLazyParent,
12
13
  type EntryData,
13
14
  type MetricsStore,
14
15
  } from "../server/context";
@@ -65,7 +66,9 @@ export async function loadManifest(
65
66
  const mountIndex = entry.mountIndex;
66
67
 
67
68
  // Check module-level cache (persists across requests within same isolate)
68
- const cacheKey = `${VERSION}:${mountIndex ?? ""}:${routeKey}:${isSSR ? 1 : 0}`;
69
+ // Include routerId so multi-router setups (host routing) don't share cached
70
+ // EntryData across routers with overlapping mountIndex + routeKey combinations.
71
+ const cacheKey = `${VERSION}:${entry.routerId ?? ""}:${mountIndex ?? ""}:${routeKey}:${isSSR ? 1 : 0}`;
69
72
  const cached = manifestModuleCache.get(cacheKey);
70
73
  if (cached) {
71
74
  const cacheStart = performance.now();
@@ -112,36 +115,48 @@ export async function loadManifest(
112
115
  // This ensures routes are registered under the correct layout hierarchy
113
116
  const lazyContext =
114
117
  entry.lazy && entry.lazyPatterns ? entry.lazyContext : null;
115
- const parentForContext =
116
- (lazyContext?.parent as EntryData | null) ?? Store.parent;
118
+ const parentForContext = lazyContext
119
+ ? getIsolatedLazyParent(
120
+ (lazyContext.parent as EntryData | null) ?? Store.parent,
121
+ )
122
+ : Store.parent;
117
123
 
118
124
  // For lazy entries, merge captured counters from include() so the
119
125
  // handler's entries get shortCode indices after sibling entries that
120
126
  // were created during pattern extraction. This prevents shortCode
121
127
  // collisions between lazy and non-lazy entries under the same parent
122
128
  // (e.g., ArticlesLayout and BlogLayout both under NavLayout).
123
- if (lazyContext && (lazyContext as any).counters) {
124
- const captured = (lazyContext as any).counters as Record<string, number>;
125
- for (const [key, value] of Object.entries(captured)) {
129
+ if (lazyContext?.counters) {
130
+ for (const [key, value] of Object.entries(lazyContext.counters)) {
126
131
  Store.counters[key] = Math.max(Store.counters[key] ?? 0, value);
127
132
  }
128
133
  }
129
134
 
130
135
  // Propagate cache profiles for DSL-time cache("profileName") resolution.
131
136
  // Non-lazy entries carry profiles directly; lazy entries carry them
132
- // in the captured lazyContext from include() time.
133
- const entryProfiles =
134
- entry.cacheProfiles ?? (lazyContext as any)?.cacheProfiles;
135
- if (entryProfiles) {
136
- Store.cacheProfiles = entryProfiles;
137
- }
137
+ // in the captured lazyContext from include() time. Always write
138
+ // (including clearing to undefined) so a prior lazy build's profile
139
+ // map cannot leak into a later non-lazy build on the same ALS-backed
140
+ // Store — which would otherwise let cache("name") resolve a profile
141
+ // from an unrelated entry.
142
+ Store.cacheProfiles = entry.cacheProfiles ?? lazyContext?.cacheProfiles;
138
143
 
139
144
  // Propagate rootScoped from lazyContext so that routes inside
140
145
  // nested { name: "sub" } under { name: "" } keep inherited root scope
141
- // when the manifest is rebuilt on each request.
142
- if (lazyContext && (lazyContext as any).rootScoped !== undefined) {
143
- Store.rootScoped = (lazyContext as any).rootScoped;
144
- }
146
+ // when the manifest is rebuilt on each request. Always write
147
+ // (including clearing to undefined, which makes getRootScoped()
148
+ // return its true default) so a prior lazy build's scope cannot leak
149
+ // into a later non-lazy build on the same ALS-backed Store — which
150
+ // would otherwise mis-register plain routes as non-root-scoped and
151
+ // break dot-local reverse resolution.
152
+ Store.rootScoped = lazyContext?.rootScoped;
153
+
154
+ // Propagate includeScope from lazyContext so that direct-descendant
155
+ // shortCodes of this include use the correct scoped counter namespace
156
+ // on every manifest rebuild. Always write (including clearing to
157
+ // undefined) so a prior lazy build's scope cannot leak into a later
158
+ // non-lazy build on the same ALS-backed Store.
159
+ Store.includeScope = lazyContext?.includeScope;
145
160
 
146
161
  const handlerExecStart = performance.now();
147
162
  const useItems = await getContext().runWithStore(