@rangojs/router 0.0.0-experimental.10

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 (172) hide show
  1. package/CLAUDE.md +43 -0
  2. package/README.md +19 -0
  3. package/dist/bin/rango.js +227 -0
  4. package/dist/vite/index.js +3039 -0
  5. package/package.json +171 -0
  6. package/skills/caching/SKILL.md +191 -0
  7. package/skills/debug-manifest/SKILL.md +108 -0
  8. package/skills/document-cache/SKILL.md +180 -0
  9. package/skills/fonts/SKILL.md +165 -0
  10. package/skills/hooks/SKILL.md +442 -0
  11. package/skills/intercept/SKILL.md +190 -0
  12. package/skills/layout/SKILL.md +213 -0
  13. package/skills/links/SKILL.md +180 -0
  14. package/skills/loader/SKILL.md +246 -0
  15. package/skills/middleware/SKILL.md +202 -0
  16. package/skills/mime-routes/SKILL.md +124 -0
  17. package/skills/parallel/SKILL.md +228 -0
  18. package/skills/prerender/SKILL.md +283 -0
  19. package/skills/rango/SKILL.md +54 -0
  20. package/skills/response-routes/SKILL.md +358 -0
  21. package/skills/route/SKILL.md +173 -0
  22. package/skills/router-setup/SKILL.md +346 -0
  23. package/skills/tailwind/SKILL.md +129 -0
  24. package/skills/theme/SKILL.md +78 -0
  25. package/skills/typesafety/SKILL.md +394 -0
  26. package/src/__internal.ts +175 -0
  27. package/src/bin/rango.ts +24 -0
  28. package/src/browser/event-controller.ts +876 -0
  29. package/src/browser/index.ts +18 -0
  30. package/src/browser/link-interceptor.ts +121 -0
  31. package/src/browser/lru-cache.ts +69 -0
  32. package/src/browser/merge-segment-loaders.ts +126 -0
  33. package/src/browser/navigation-bridge.ts +913 -0
  34. package/src/browser/navigation-client.ts +165 -0
  35. package/src/browser/navigation-store.ts +823 -0
  36. package/src/browser/partial-update.ts +600 -0
  37. package/src/browser/react/Link.tsx +248 -0
  38. package/src/browser/react/NavigationProvider.tsx +346 -0
  39. package/src/browser/react/ScrollRestoration.tsx +94 -0
  40. package/src/browser/react/context.ts +53 -0
  41. package/src/browser/react/index.ts +52 -0
  42. package/src/browser/react/location-state-shared.ts +120 -0
  43. package/src/browser/react/location-state.ts +62 -0
  44. package/src/browser/react/mount-context.ts +32 -0
  45. package/src/browser/react/use-action.ts +240 -0
  46. package/src/browser/react/use-client-cache.ts +56 -0
  47. package/src/browser/react/use-handle.ts +203 -0
  48. package/src/browser/react/use-href.tsx +40 -0
  49. package/src/browser/react/use-link-status.ts +134 -0
  50. package/src/browser/react/use-mount.ts +31 -0
  51. package/src/browser/react/use-navigation.ts +140 -0
  52. package/src/browser/react/use-segments.ts +188 -0
  53. package/src/browser/request-controller.ts +164 -0
  54. package/src/browser/rsc-router.tsx +352 -0
  55. package/src/browser/scroll-restoration.ts +324 -0
  56. package/src/browser/segment-structure-assert.ts +67 -0
  57. package/src/browser/server-action-bridge.ts +762 -0
  58. package/src/browser/shallow.ts +35 -0
  59. package/src/browser/types.ts +478 -0
  60. package/src/build/generate-manifest.ts +377 -0
  61. package/src/build/generate-route-types.ts +828 -0
  62. package/src/build/index.ts +36 -0
  63. package/src/build/route-trie.ts +239 -0
  64. package/src/cache/cache-scope.ts +563 -0
  65. package/src/cache/cf/cf-cache-store.ts +428 -0
  66. package/src/cache/cf/index.ts +19 -0
  67. package/src/cache/document-cache.ts +340 -0
  68. package/src/cache/index.ts +58 -0
  69. package/src/cache/memory-segment-store.ts +150 -0
  70. package/src/cache/memory-store.ts +253 -0
  71. package/src/cache/types.ts +392 -0
  72. package/src/client.rsc.tsx +83 -0
  73. package/src/client.tsx +643 -0
  74. package/src/component-utils.ts +76 -0
  75. package/src/components/DefaultDocument.tsx +23 -0
  76. package/src/debug.ts +233 -0
  77. package/src/default-error-boundary.tsx +88 -0
  78. package/src/deps/browser.ts +8 -0
  79. package/src/deps/html-stream-client.ts +2 -0
  80. package/src/deps/html-stream-server.ts +2 -0
  81. package/src/deps/rsc.ts +10 -0
  82. package/src/deps/ssr.ts +2 -0
  83. package/src/errors.ts +295 -0
  84. package/src/handle.ts +130 -0
  85. package/src/handles/MetaTags.tsx +193 -0
  86. package/src/handles/index.ts +6 -0
  87. package/src/handles/meta.ts +247 -0
  88. package/src/host/cookie-handler.ts +159 -0
  89. package/src/host/errors.ts +97 -0
  90. package/src/host/index.ts +56 -0
  91. package/src/host/pattern-matcher.ts +214 -0
  92. package/src/host/router.ts +330 -0
  93. package/src/host/testing.ts +79 -0
  94. package/src/host/types.ts +138 -0
  95. package/src/host/utils.ts +25 -0
  96. package/src/href-client.ts +202 -0
  97. package/src/href-context.ts +33 -0
  98. package/src/index.rsc.ts +121 -0
  99. package/src/index.ts +165 -0
  100. package/src/loader.rsc.ts +207 -0
  101. package/src/loader.ts +47 -0
  102. package/src/network-error-thrower.tsx +21 -0
  103. package/src/outlet-context.ts +15 -0
  104. package/src/prerender/param-hash.ts +35 -0
  105. package/src/prerender/store.ts +40 -0
  106. package/src/prerender.ts +156 -0
  107. package/src/reverse.ts +267 -0
  108. package/src/root-error-boundary.tsx +277 -0
  109. package/src/route-content-wrapper.tsx +193 -0
  110. package/src/route-definition.ts +1431 -0
  111. package/src/route-map-builder.ts +242 -0
  112. package/src/route-types.ts +220 -0
  113. package/src/router/error-handling.ts +287 -0
  114. package/src/router/handler-context.ts +158 -0
  115. package/src/router/intercept-resolution.ts +387 -0
  116. package/src/router/loader-resolution.ts +327 -0
  117. package/src/router/manifest.ts +216 -0
  118. package/src/router/match-api.ts +621 -0
  119. package/src/router/match-context.ts +264 -0
  120. package/src/router/match-middleware/background-revalidation.ts +236 -0
  121. package/src/router/match-middleware/cache-lookup.ts +382 -0
  122. package/src/router/match-middleware/cache-store.ts +276 -0
  123. package/src/router/match-middleware/index.ts +81 -0
  124. package/src/router/match-middleware/intercept-resolution.ts +281 -0
  125. package/src/router/match-middleware/segment-resolution.ts +184 -0
  126. package/src/router/match-pipelines.ts +214 -0
  127. package/src/router/match-result.ts +213 -0
  128. package/src/router/metrics.ts +62 -0
  129. package/src/router/middleware.ts +791 -0
  130. package/src/router/pattern-matching.ts +407 -0
  131. package/src/router/revalidation.ts +190 -0
  132. package/src/router/router-context.ts +301 -0
  133. package/src/router/segment-resolution.ts +1315 -0
  134. package/src/router/trie-matching.ts +172 -0
  135. package/src/router/types.ts +163 -0
  136. package/src/router.gen.ts +6 -0
  137. package/src/router.ts +2423 -0
  138. package/src/rsc/handler.ts +1443 -0
  139. package/src/rsc/helpers.ts +64 -0
  140. package/src/rsc/index.ts +56 -0
  141. package/src/rsc/nonce.ts +18 -0
  142. package/src/rsc/types.ts +236 -0
  143. package/src/segment-system.tsx +442 -0
  144. package/src/server/context.ts +466 -0
  145. package/src/server/handle-store.ts +229 -0
  146. package/src/server/loader-registry.ts +174 -0
  147. package/src/server/request-context.ts +554 -0
  148. package/src/server/root-layout.tsx +10 -0
  149. package/src/server/tsconfig.json +14 -0
  150. package/src/server.ts +171 -0
  151. package/src/ssr/index.tsx +296 -0
  152. package/src/theme/ThemeProvider.tsx +291 -0
  153. package/src/theme/ThemeScript.tsx +61 -0
  154. package/src/theme/constants.ts +59 -0
  155. package/src/theme/index.ts +58 -0
  156. package/src/theme/theme-context.ts +70 -0
  157. package/src/theme/theme-script.ts +152 -0
  158. package/src/theme/types.ts +182 -0
  159. package/src/theme/use-theme.ts +44 -0
  160. package/src/types.ts +1757 -0
  161. package/src/urls.gen.ts +8 -0
  162. package/src/urls.ts +1282 -0
  163. package/src/use-loader.tsx +346 -0
  164. package/src/vite/expose-action-id.ts +344 -0
  165. package/src/vite/expose-handle-id.ts +209 -0
  166. package/src/vite/expose-loader-id.ts +426 -0
  167. package/src/vite/expose-location-state-id.ts +177 -0
  168. package/src/vite/expose-prerender-handler-id.ts +429 -0
  169. package/src/vite/index.ts +2068 -0
  170. package/src/vite/package-resolution.ts +125 -0
  171. package/src/vite/version.d.ts +12 -0
  172. package/src/vite/virtual-entries.ts +114 -0
@@ -0,0 +1,81 @@
1
+ /**
2
+ * Match Middleware
3
+ *
4
+ * Async generator middleware for the match pipeline.
5
+ * Each middleware transforms or enriches the segment stream.
6
+ *
7
+ * MIDDLEWARE OVERVIEW
8
+ * ===================
9
+ *
10
+ * The pipeline consists of 5 middleware layers, each with a specific role:
11
+ *
12
+ * +-------------------------------------------------------------------------+
13
+ * | MIDDLEWARE PIPELINE |
14
+ * +-------------------------------------------------------------------------+
15
+ * | |
16
+ * | [1] CACHE LOOKUP (innermost) |
17
+ * | Purpose: Check cache before resolving |
18
+ * | On hit: Yield cached segments + fresh loaders |
19
+ * | On miss: Pass through to segment resolution |
20
+ * | Side effects: Sets state.cacheHit, state.shouldRevalidate |
21
+ * | |
22
+ * | [2] SEGMENT RESOLUTION |
23
+ * | Purpose: Resolve segments when cache misses |
24
+ * | Skips if: state.cacheHit === true |
25
+ * | Produces: All route segments (layouts, routes, loaders) |
26
+ * | Two modes: Full (simple) vs Partial (with revalidation) |
27
+ * | |
28
+ * | [3] INTERCEPT RESOLUTION |
29
+ * | Purpose: Resolve intercept segments (modal slots) |
30
+ * | Triggers: When ctx.interceptResult exists |
31
+ * | Produces: Additional segments for named slots |
32
+ * | Updates: state.slots[slotName] with intercept segments |
33
+ * | |
34
+ * | [4] CACHE STORE |
35
+ * | Purpose: Store segments in cache for future requests |
36
+ * | Skips if: Cache hit, actions, or cache disabled |
37
+ * | Strategy: Direct cache if all components present |
38
+ * | Proactive cache if null components (via waitUntil) |
39
+ * | |
40
+ * | [5] BACKGROUND REVALIDATION (outermost) |
41
+ * | Purpose: SWR - serve stale, revalidate in background |
42
+ * | Triggers: When state.shouldRevalidate === true |
43
+ * | Action: Async resolution via waitUntil(), updates cache |
44
+ * | |
45
+ * +-------------------------------------------------------------------------+
46
+ *
47
+ *
48
+ * MIDDLEWARE INTERACTION PATTERNS
49
+ * ===============================
50
+ *
51
+ * Pattern 1: Producer Middleware (cache-lookup, segment-resolution)
52
+ * - Yields segments into the stream
53
+ * - Creates new data for downstream middleware
54
+ *
55
+ * Pattern 2: Transformer Middleware (intercept-resolution)
56
+ * - Passes through existing segments
57
+ * - Adds additional segments (intercepts)
58
+ *
59
+ * Pattern 3: Observer Middleware (cache-store, background-revalidation)
60
+ * - Passes through all segments unchanged
61
+ * - Triggers side effects based on state
62
+ *
63
+ *
64
+ * STATE FLAGS
65
+ * ===========
66
+ *
67
+ * The middleware communicate through MatchPipelineState:
68
+ *
69
+ * state.cacheHit - Set by cache-lookup, read by others to skip work
70
+ * state.shouldRevalidate - Set by cache-lookup, triggers bg-revalidation
71
+ * state.segments - Accumulated segments from pipeline
72
+ * state.interceptSegments - Segments for intercept slots
73
+ * state.slots - Named slot data for client
74
+ */
75
+
76
+ export { withCacheLookup } from "./cache-lookup.js";
77
+ export { withSegmentResolution } from "./segment-resolution.js";
78
+ export { withInterceptResolution } from "./intercept-resolution.js";
79
+ export { withCacheStore } from "./cache-store.js";
80
+ export { withBackgroundRevalidation } from "./background-revalidation.js";
81
+ export type { GeneratorMiddleware } from "./cache-lookup.js";
@@ -0,0 +1,281 @@
1
+ /**
2
+ * Intercept Resolution Middleware
3
+ *
4
+ * Resolves intercept (modal slot) segments for soft navigation.
5
+ * Yields intercept segments after main route segments.
6
+ *
7
+ * FLOW DIAGRAM
8
+ * ============
9
+ *
10
+ * source (from segment-resolution)
11
+ * |
12
+ * v
13
+ * +---------------------------+
14
+ * | Collect + yield source | Pass through main segments
15
+ * | segments[] |
16
+ * +---------------------------+
17
+ * |
18
+ * v
19
+ * +---------------------+
20
+ * | isFullMatch? |──yes──> return (no intercepts on doc requests)
21
+ * +---------------------+
22
+ * | no
23
+ * v
24
+ * +---------------------+
25
+ * | Has interceptResult |──no───> return
26
+ * | AND not cached? |
27
+ * +---------------------+
28
+ * | yes
29
+ * v
30
+ * +----------------------+ +----------------------------+
31
+ * | Fresh intercept? |yes>| resolveInterceptEntry() |
32
+ * | (!cacheHit or | | - middleware, loaders, UI |
33
+ * | no intercept segs) | +----------------------------+
34
+ * +----------------------+ |
35
+ * | no v
36
+ * v yield intercept segments
37
+ * +----------------------------+ |
38
+ * | Cache hit with intercept | |
39
+ * | handleCacheHitIntercept() | |
40
+ * | - Extract from cache | |
41
+ * | - Re-resolve loaders only | |
42
+ * +----------------------------+ |
43
+ * | |
44
+ * +-------------------------------+
45
+ * |
46
+ * v
47
+ * +---------------------------+
48
+ * | Update state: |
49
+ * | - interceptSegments |
50
+ * | - slots[slotName] |
51
+ * +---------------------------+
52
+ * |
53
+ * v
54
+ * next middleware
55
+ *
56
+ *
57
+ * INTERCEPT SCENARIOS
58
+ * ===================
59
+ *
60
+ * 1. Fresh intercept (no cache):
61
+ * - Full resolution of intercept entry
62
+ * - Resolves middleware, loaders, and component
63
+ * - Yields all intercept segments
64
+ *
65
+ * 2. Cache hit with intercept:
66
+ * - Extracts intercept segments from cached data
67
+ * - Re-resolves ONLY loaders for fresh data
68
+ * - Keeps cached component/layout
69
+ *
70
+ * 3. No intercept:
71
+ * - Passes through unchanged
72
+ * - No intercept segments yielded
73
+ *
74
+ *
75
+ * WHAT ARE INTERCEPTS?
76
+ * ====================
77
+ *
78
+ * Intercepts enable "soft navigation" patterns like modals:
79
+ *
80
+ * 1. User clicks a link (e.g., /photos/123)
81
+ * 2. Instead of full navigation, content renders in a modal slot
82
+ * 3. Background page remains visible and interactive
83
+ * 4. Hard navigation (direct URL) shows full page
84
+ *
85
+ * Configuration:
86
+ * intercept("@modal", "photos", <PhotoModal />, () => [...])
87
+ *
88
+ * The intercept resolves to segments that render in the named slot
89
+ * instead of replacing the main content.
90
+ *
91
+ *
92
+ * SLOT STRUCTURE
93
+ * ==============
94
+ *
95
+ * state.slots[slotName] = {
96
+ * active: true,
97
+ * segments: [...intercept segments]
98
+ * }
99
+ *
100
+ * The client uses this to:
101
+ * 1. Keep current page segments
102
+ * 2. Render intercept segments in named <Outlet name="@modal" />
103
+ */
104
+ import type { ResolvedSegment } from "../../types.js";
105
+ import type { MatchContext, MatchPipelineState } from "../match-context.js";
106
+ import { getRouterContext } from "../router-context.js";
107
+ import type { GeneratorMiddleware } from "./cache-lookup.js";
108
+
109
+ /**
110
+ * Creates intercept resolution middleware
111
+ *
112
+ * If ctx.interceptResult exists and we're not in a cache-hit-with-intercept scenario:
113
+ * - Resolves intercept segments
114
+ * - Updates state.interceptSegments
115
+ * - Updates state.slots with the intercept slot
116
+ * - Yields intercept segments after main segments
117
+ */
118
+ export function withInterceptResolution<TEnv>(
119
+ ctx: MatchContext<TEnv>,
120
+ state: MatchPipelineState
121
+ ): GeneratorMiddleware<ResolvedSegment> {
122
+ return async function* (
123
+ source: AsyncGenerator<ResolvedSegment>
124
+ ): AsyncGenerator<ResolvedSegment> {
125
+ const pipelineStart = performance.now();
126
+ const ms = ctx.metricsStore;
127
+
128
+ // First, yield all segments from the source (main segment resolution or cache)
129
+ const segments: ResolvedSegment[] = [];
130
+ for await (const segment of source) {
131
+ segments.push(segment);
132
+ yield segment;
133
+ }
134
+
135
+ // Skip intercept resolution for full match (document requests don't have intercepts)
136
+ if (ctx.isFullMatch) {
137
+ if (ms) {
138
+ ms.metrics.push({ label: "pipeline:intercept", duration: performance.now() - pipelineStart, startTime: pipelineStart - ms.requestStart });
139
+ }
140
+ return;
141
+ }
142
+
143
+ // Skip intercept resolution if:
144
+ // 1. No intercept result
145
+ // 2. Already have intercept segments (from cache hit with intercept key)
146
+ // 3. Cache hit with intercept key
147
+ const skipInterceptResolution =
148
+ !ctx.interceptResult ||
149
+ state.interceptSegments.length > 0 ||
150
+ (state.cacheHit && ctx.isIntercept);
151
+
152
+ if (skipInterceptResolution) {
153
+ // For cache hit with intercept, extract intercept segments from cached data for slots
154
+ // and re-resolve loaders for fresh data
155
+ if (ctx.interceptResult && state.cacheHit && ctx.isIntercept) {
156
+ await handleCacheHitIntercept(ctx, state, segments);
157
+ }
158
+ if (ms) {
159
+ ms.metrics.push({ label: "pipeline:intercept", duration: performance.now() - pipelineStart, startTime: pipelineStart - ms.requestStart });
160
+ }
161
+ return;
162
+ }
163
+
164
+ // Resolve intercept segments
165
+ const { resolveInterceptEntry } = getRouterContext<TEnv>();
166
+
167
+ const slotName = ctx.interceptResult!.intercept.slotName;
168
+ console.log(
169
+ `[Router.matchPartial] Found intercept for "${ctx.localRouteName}" -> slot "${slotName}"`
170
+ );
171
+
172
+ // Resolve intercept entry (middleware, loaders, handler)
173
+ const Store = ctx.Store;
174
+ const interceptSegments = await Store.run(() =>
175
+ resolveInterceptEntry(
176
+ ctx.interceptResult!.intercept,
177
+ ctx.interceptResult!.entry,
178
+ ctx.matched.params,
179
+ ctx.handlerContext,
180
+ true, // belongsToRoute
181
+ {
182
+ clientSegmentIds: ctx.clientSegmentSet,
183
+ prevParams: ctx.prevParams,
184
+ request: ctx.request,
185
+ prevUrl: ctx.prevUrl,
186
+ nextUrl: ctx.url,
187
+ routeKey: ctx.routeKey,
188
+ actionContext: ctx.actionContext,
189
+ stale: ctx.stale,
190
+ }
191
+ )
192
+ );
193
+
194
+ // Update state
195
+ state.interceptSegments = interceptSegments;
196
+ state.slots[slotName] = {
197
+ active: true,
198
+ segments: interceptSegments,
199
+ };
200
+
201
+ // Yield intercept segments
202
+ for (const segment of interceptSegments) {
203
+ yield segment;
204
+ }
205
+
206
+ if (ms) {
207
+ ms.metrics.push({ label: "pipeline:intercept", duration: performance.now() - pipelineStart, startTime: pipelineStart - ms.requestStart });
208
+ }
209
+ };
210
+ }
211
+
212
+ /**
213
+ * Handle cache hit with intercept scenario
214
+ *
215
+ * Extract intercept segments from cached data and re-resolve loaders for fresh data.
216
+ */
217
+ async function handleCacheHitIntercept<TEnv>(
218
+ ctx: MatchContext<TEnv>,
219
+ state: MatchPipelineState,
220
+ segments: ResolvedSegment[]
221
+ ): Promise<void> {
222
+ if (!ctx.interceptResult) return;
223
+
224
+ const { resolveInterceptLoadersOnly } = getRouterContext<TEnv>();
225
+
226
+ const slotName = ctx.interceptResult.intercept.slotName;
227
+
228
+ // Find intercept segments from cached segments (namespace starts with "intercept:")
229
+ const interceptSegments = segments.filter((s) =>
230
+ s.namespace?.startsWith("intercept:")
231
+ );
232
+ state.interceptSegments = interceptSegments;
233
+
234
+ // Re-resolve intercept loaders for fresh data on cache hit
235
+ // This keeps cached component/layout but fetches fresh loader data
236
+ if (resolveInterceptLoadersOnly) {
237
+ const Store = ctx.Store;
238
+ const freshLoaderResult = await Store.run(() =>
239
+ resolveInterceptLoadersOnly(
240
+ ctx.interceptResult!.intercept,
241
+ ctx.interceptResult!.entry,
242
+ ctx.matched.params,
243
+ ctx.handlerContext,
244
+ true, // belongsToRoute
245
+ {
246
+ clientSegmentIds: ctx.clientSegmentSet,
247
+ prevParams: ctx.prevParams,
248
+ request: ctx.request,
249
+ prevUrl: ctx.prevUrl,
250
+ nextUrl: ctx.url,
251
+ routeKey: ctx.routeKey,
252
+ actionContext: ctx.actionContext,
253
+ stale: ctx.stale,
254
+ }
255
+ )
256
+ );
257
+
258
+ // Update intercept segment's loaderDataPromise with fresh data
259
+ if (freshLoaderResult) {
260
+ const interceptMainSegment = interceptSegments.find(
261
+ (s) => s.type === "parallel" && s.slot
262
+ );
263
+ if (interceptMainSegment) {
264
+ interceptMainSegment.loaderDataPromise = freshLoaderResult.loaderDataPromise;
265
+ interceptMainSegment.loaderIds = freshLoaderResult.loaderIds;
266
+ console.log(
267
+ `[Router.matchPartial] Cache HIT + fresh loaders for intercept "${ctx.localRouteName}" -> slot "${slotName}"`
268
+ );
269
+ }
270
+ } else {
271
+ console.log(
272
+ `[Router.matchPartial] Cache HIT for intercept "${ctx.localRouteName}" -> slot "${slotName}" (no loader revalidation)`
273
+ );
274
+ }
275
+ }
276
+
277
+ state.slots[slotName] = {
278
+ active: true,
279
+ segments: interceptSegments,
280
+ };
281
+ }
@@ -0,0 +1,184 @@
1
+ /**
2
+ * Segment Resolution Middleware
3
+ *
4
+ * Resolves route segments when cache misses. Skips if cache hit.
5
+ *
6
+ * FLOW DIAGRAM
7
+ * ============
8
+ *
9
+ * source (from cache-lookup)
10
+ * |
11
+ * v
12
+ * +---------------------------+
13
+ * | Iterate source first! | <-- CRITICAL: Must drain source
14
+ * | yield* source | to let cache-lookup run
15
+ * +---------------------------+
16
+ * |
17
+ * v
18
+ * +---------------------+
19
+ * | state.cacheHit? |──yes──> return (cache already yielded)
20
+ * +---------------------+
21
+ * | no
22
+ * v
23
+ * +---------------------+
24
+ * | isFullMatch? |
25
+ * +---------------------+
26
+ * |
27
+ * +-----+-----+
28
+ * | |
29
+ * yes no
30
+ * | |
31
+ * v v
32
+ * resolveAll resolveAllWithRevalidation
33
+ * Segments Segments
34
+ * | |
35
+ * | | (compares with prev state)
36
+ * | | (handles null components)
37
+ * | |
38
+ * +-----------+
39
+ * |
40
+ * v
41
+ * +---------------------------+
42
+ * | Update state: |
43
+ * | - state.segments |
44
+ * | - state.matchedIds |
45
+ * +---------------------------+
46
+ * |
47
+ * v
48
+ * yield all resolved segments
49
+ * |
50
+ * v
51
+ * next middleware
52
+ *
53
+ *
54
+ * RESOLUTION MODES
55
+ * ================
56
+ *
57
+ * Full Match (document request):
58
+ * - Uses resolveAllSegments()
59
+ * - No revalidation logic (nothing to compare against)
60
+ * - Simple resolution of all route entries
61
+ *
62
+ * Partial Match (navigation):
63
+ * - Uses resolveAllSegmentsWithRevalidation()
64
+ * - Compares current vs previous params/URL
65
+ * - Sets component = null for segments client already has
66
+ * - Respects custom revalidation rules
67
+ *
68
+ *
69
+ * CRITICAL: SOURCE ITERATION
70
+ * ==========================
71
+ *
72
+ * The middleware MUST iterate the source generator before checking cacheHit:
73
+ *
74
+ * for await (const segment of source) { yield segment; }
75
+ *
76
+ * This is because:
77
+ * 1. Generator middleware are lazy (don't execute until iterated)
78
+ * 2. cache-lookup sets state.cacheHit during iteration
79
+ * 3. Without draining source first, cache-lookup never runs
80
+ *
81
+ * Incorrect pattern:
82
+ * if (!state.cacheHit) { ... } // cacheHit still false!
83
+ * yield* source; // Too late, already resolved
84
+ *
85
+ * Correct pattern:
86
+ * yield* source; // Let cache-lookup set cacheHit
87
+ * if (state.cacheHit) return; // Now we can check
88
+ */
89
+ import type { ResolvedSegment } from "../../types.js";
90
+ import type { MatchContext, MatchPipelineState } from "../match-context.js";
91
+ import { getRouterContext } from "../router-context.js";
92
+ import type { GeneratorMiddleware } from "./cache-lookup.js";
93
+
94
+ /**
95
+ * Creates segment resolution middleware
96
+ *
97
+ * Only runs on cache miss (state.cacheHit === false).
98
+ * Uses resolveAllSegmentsWithRevalidation from RouterContext to resolve segments.
99
+ */
100
+ export function withSegmentResolution<TEnv>(
101
+ ctx: MatchContext<TEnv>,
102
+ state: MatchPipelineState
103
+ ): GeneratorMiddleware<ResolvedSegment> {
104
+ return async function* (
105
+ source: AsyncGenerator<ResolvedSegment>
106
+ ): AsyncGenerator<ResolvedSegment> {
107
+ const pipelineStart = performance.now();
108
+ const ms = ctx.metricsStore;
109
+
110
+ // IMPORTANT: Always iterate source first to give cache-lookup a chance
111
+ // to run and set state.cacheHit. Without this, cache-lookup never executes!
112
+ for await (const segment of source) {
113
+ yield segment;
114
+ }
115
+
116
+ // If cache hit, segments were already yielded by cache lookup
117
+ if (state.cacheHit) {
118
+ if (ms) {
119
+ ms.metrics.push({ label: "pipeline:segment-resolve", duration: performance.now() - pipelineStart, startTime: pipelineStart - ms.requestStart });
120
+ }
121
+ return;
122
+ }
123
+
124
+ const { resolveAllSegmentsWithRevalidation, resolveAllSegments } =
125
+ getRouterContext<TEnv>();
126
+
127
+ const Store = ctx.Store;
128
+
129
+ if (ctx.isFullMatch) {
130
+ // Full match (document request) - simple resolution without revalidation
131
+ const segments = await Store.run(() =>
132
+ resolveAllSegments(
133
+ ctx.entries,
134
+ ctx.routeKey,
135
+ ctx.matched.params,
136
+ ctx.handlerContext,
137
+ ctx.loaderPromises
138
+ )
139
+ );
140
+
141
+ // Update state with resolved segments
142
+ state.segments = segments;
143
+ state.matchedIds = segments.map((s: { id: string }) => s.id);
144
+
145
+ // Yield all resolved segments
146
+ for (const segment of segments) {
147
+ yield segment;
148
+ }
149
+ } else {
150
+ // Partial match (navigation) - resolution with revalidation logic
151
+ const result = await Store.run(() =>
152
+ resolveAllSegmentsWithRevalidation(
153
+ ctx.entries,
154
+ ctx.routeKey,
155
+ ctx.matched.params,
156
+ ctx.handlerContext,
157
+ ctx.clientSegmentSet,
158
+ ctx.prevParams,
159
+ ctx.request,
160
+ ctx.prevUrl,
161
+ ctx.url,
162
+ ctx.loaderPromises,
163
+ ctx.actionContext,
164
+ ctx.interceptResult,
165
+ ctx.localRouteName,
166
+ ctx.pathname
167
+ )
168
+ );
169
+
170
+ // Update state with resolved segments
171
+ state.segments = result.segments;
172
+ state.matchedIds = result.matchedIds;
173
+
174
+ // Yield all resolved segments
175
+ for (const segment of result.segments) {
176
+ yield segment;
177
+ }
178
+ }
179
+
180
+ if (ms) {
181
+ ms.metrics.push({ label: "pipeline:segment-resolve", duration: performance.now() - pipelineStart, startTime: pipelineStart - ms.requestStart });
182
+ }
183
+ };
184
+ }