@rangojs/router 0.0.0-experimental.fa8a383a → 0.0.0-experimental.fad716ff
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.
- package/README.md +76 -18
- package/dist/bin/rango.js +130 -47
- package/dist/vite/index.js +526 -168
- package/dist/vite/index.js.bak +5448 -0
- package/package.json +2 -2
- package/skills/cache-guide/SKILL.md +32 -0
- package/skills/caching/SKILL.md +8 -0
- package/skills/links/SKILL.md +3 -1
- package/skills/loader/SKILL.md +53 -43
- package/skills/middleware/SKILL.md +2 -0
- package/skills/parallel/SKILL.md +67 -0
- package/skills/prerender/SKILL.md +110 -68
- package/skills/route/SKILL.md +31 -0
- package/skills/router-setup/SKILL.md +87 -2
- package/skills/typesafety/SKILL.md +10 -0
- package/src/__internal.ts +1 -1
- package/src/browser/app-version.ts +14 -0
- package/src/browser/navigation-bridge.ts +16 -3
- package/src/browser/navigation-client.ts +64 -40
- package/src/browser/navigation-store.ts +43 -8
- package/src/browser/partial-update.ts +37 -4
- package/src/browser/prefetch/fetch.ts +8 -2
- package/src/browser/prefetch/queue.ts +61 -29
- package/src/browser/prefetch/resource-ready.ts +77 -0
- package/src/browser/react/Link.tsx +44 -8
- package/src/browser/react/NavigationProvider.tsx +13 -4
- package/src/browser/react/context.ts +7 -2
- package/src/browser/react/use-handle.ts +9 -58
- package/src/browser/react/use-router.ts +21 -8
- package/src/browser/rsc-router.tsx +26 -3
- package/src/browser/scroll-restoration.ts +10 -8
- package/src/browser/server-action-bridge.ts +8 -6
- package/src/browser/types.ts +27 -5
- package/src/build/generate-manifest.ts +6 -6
- package/src/build/generate-route-types.ts +3 -0
- package/src/build/route-types/include-resolution.ts +8 -1
- package/src/build/route-types/router-processing.ts +211 -72
- package/src/build/route-types/scan-filter.ts +8 -1
- package/src/cache/cache-runtime.ts +15 -11
- package/src/cache/cache-scope.ts +46 -5
- package/src/cache/taint.ts +55 -0
- package/src/client.tsx +2 -56
- package/src/context-var.ts +72 -2
- package/src/handle.ts +40 -0
- package/src/index.rsc.ts +3 -1
- package/src/index.ts +12 -0
- package/src/prerender/store.ts +5 -4
- package/src/prerender.ts +138 -77
- package/src/reverse.ts +22 -1
- package/src/route-definition/dsl-helpers.ts +42 -19
- package/src/route-definition/helpers-types.ts +10 -6
- package/src/route-definition/index.ts +3 -0
- package/src/route-definition/redirect.ts +9 -1
- package/src/route-definition/resolve-handler-use.ts +149 -0
- package/src/route-types.ts +11 -0
- package/src/router/content-negotiation.ts +100 -1
- package/src/router/handler-context.ts +79 -23
- package/src/router/intercept-resolution.ts +9 -4
- package/src/router/loader-resolution.ts +156 -21
- package/src/router/match-api.ts +124 -189
- package/src/router/match-middleware/background-revalidation.ts +12 -1
- package/src/router/match-middleware/cache-lookup.ts +72 -13
- package/src/router/match-middleware/cache-store.ts +21 -4
- package/src/router/match-middleware/segment-resolution.ts +53 -0
- package/src/router/match-result.ts +11 -5
- package/src/router/metrics.ts +6 -1
- package/src/router/middleware-types.ts +6 -8
- package/src/router/middleware.ts +2 -5
- package/src/router/navigation-snapshot.ts +182 -0
- package/src/router/prerender-match.ts +110 -10
- package/src/router/preview-match.ts +30 -102
- package/src/router/request-classification.ts +310 -0
- package/src/router/route-snapshot.ts +245 -0
- package/src/router/router-context.ts +1 -0
- package/src/router/router-interfaces.ts +36 -4
- package/src/router/router-options.ts +37 -11
- package/src/router/segment-resolution/fresh.ts +101 -18
- package/src/router/segment-resolution/helpers.ts +29 -24
- package/src/router/segment-resolution/revalidation.ts +122 -26
- package/src/router/types.ts +1 -0
- package/src/router.ts +54 -5
- package/src/rsc/handler.ts +464 -377
- package/src/rsc/loader-fetch.ts +23 -3
- package/src/rsc/manifest-init.ts +5 -1
- package/src/rsc/progressive-enhancement.ts +14 -2
- package/src/rsc/rsc-rendering.ts +10 -1
- package/src/rsc/server-action.ts +8 -0
- package/src/rsc/ssr-setup.ts +2 -2
- package/src/rsc/types.ts +9 -1
- package/src/server/context.ts +50 -1
- package/src/server/handle-store.ts +19 -0
- package/src/server/loader-registry.ts +9 -8
- package/src/server/request-context.ts +175 -15
- package/src/ssr/index.tsx +3 -0
- package/src/static-handler.ts +18 -6
- package/src/types/cache-types.ts +4 -4
- package/src/types/handler-context.ts +137 -33
- package/src/types/loader-types.ts +36 -9
- package/src/types/route-entry.ts +1 -1
- package/src/urls/path-helper-types.ts +9 -2
- package/src/urls/path-helper.ts +47 -12
- package/src/urls/pattern-types.ts +12 -0
- package/src/urls/response-types.ts +16 -6
- package/src/use-loader.tsx +73 -4
- package/src/vite/discovery/bundle-postprocess.ts +30 -33
- package/src/vite/discovery/discover-routers.ts +5 -1
- package/src/vite/discovery/prerender-collection.ts +14 -1
- package/src/vite/discovery/state.ts +13 -4
- package/src/vite/index.ts +4 -0
- package/src/vite/plugin-types.ts +60 -5
- package/src/vite/plugins/expose-ids/handler-transform.ts +30 -0
- package/src/vite/plugins/expose-internal-ids.ts +118 -39
- package/src/vite/plugins/performance-tracks.ts +88 -0
- package/src/vite/plugins/refresh-cmd.ts +88 -26
- package/src/vite/rango.ts +19 -2
- package/src/vite/router-discovery.ts +178 -37
- package/src/vite/utils/prerender-utils.ts +18 -0
- package/src/vite/utils/shared-utils.ts +3 -2
|
@@ -70,9 +70,11 @@
|
|
|
70
70
|
* - No segments yielded from this middleware
|
|
71
71
|
*
|
|
72
72
|
* Loaders:
|
|
73
|
-
* - NEVER cached
|
|
73
|
+
* - NEVER cached in the segment cache
|
|
74
74
|
* - Always resolved fresh on every request
|
|
75
75
|
* - Ensures data freshness even with cached UI components
|
|
76
|
+
* - Segment cache staleness does NOT propagate to loader revalidation;
|
|
77
|
+
* loaders use their own revalidation rules (actionId, user-defined)
|
|
76
78
|
*
|
|
77
79
|
*
|
|
78
80
|
* REVALIDATION RULES
|
|
@@ -94,6 +96,7 @@ import type { MatchContext, MatchPipelineState } from "../match-context.js";
|
|
|
94
96
|
import { getRouterContext } from "../router-context.js";
|
|
95
97
|
import { resolveSink, safeEmit } from "../telemetry.js";
|
|
96
98
|
import { pushRevalidationTraceEntry, isTraceActive } from "../logging.js";
|
|
99
|
+
import { treeHasStreaming } from "./segment-resolution.js";
|
|
97
100
|
import type { PrerenderStore, PrerenderEntry } from "../../prerender/store.js";
|
|
98
101
|
import type { HandleStore } from "../../server/handle-store.js";
|
|
99
102
|
import {
|
|
@@ -191,6 +194,16 @@ async function* yieldFromStore<TEnv>(
|
|
|
191
194
|
state.cachedSegments = segments;
|
|
192
195
|
state.cachedMatchedIds = segments.map((s) => s.id);
|
|
193
196
|
|
|
197
|
+
// Set streaming flag (once) and resolve render barrier.
|
|
198
|
+
const reqCtx = handleStoreRef ? undefined : _lazyGetRequestContext?.();
|
|
199
|
+
const barrierReqCtx = reqCtx ?? _getRequestContext();
|
|
200
|
+
if (barrierReqCtx) {
|
|
201
|
+
if (barrierReqCtx._treeHasStreaming === undefined) {
|
|
202
|
+
barrierReqCtx._treeHasStreaming = treeHasStreaming(ctx.entries);
|
|
203
|
+
}
|
|
204
|
+
barrierReqCtx._resolveRenderBarrier(segments);
|
|
205
|
+
}
|
|
206
|
+
|
|
194
207
|
// For partial navigation, nullify components the client already has
|
|
195
208
|
// so parent layouts stay live (client keeps its existing versions).
|
|
196
209
|
// When params changed (e.g., different guide slug), the segments have
|
|
@@ -261,7 +274,7 @@ async function* yieldFromStore<TEnv>(
|
|
|
261
274
|
depth: 1,
|
|
262
275
|
});
|
|
263
276
|
ms.metrics.push({
|
|
264
|
-
label: "pipeline:cache-
|
|
277
|
+
label: "pipeline:cache-hit",
|
|
265
278
|
duration: loaderEnd - pipelineStart,
|
|
266
279
|
startTime: pipelineStart - ms.requestStart,
|
|
267
280
|
});
|
|
@@ -314,14 +327,15 @@ export function withCacheLookup<TEnv>(
|
|
|
314
327
|
|
|
315
328
|
// Prerender lookup: check build-time cached data before runtime cache.
|
|
316
329
|
// Prerender data is available regardless of runtime cache configuration.
|
|
317
|
-
|
|
330
|
+
// Skip for HMR requests — the dev prerender endpoint reads from a stale
|
|
331
|
+
// RouterRegistry snapshot; rendering fresh ensures edits are visible.
|
|
332
|
+
const isHmr = !!ctx.request.headers.get("X-RSC-HMR");
|
|
333
|
+
if (!ctx.isAction && !isHmr && ctx.matched.pr) {
|
|
318
334
|
await ensurePrerenderDeps();
|
|
319
335
|
if (prerenderStoreInstance) {
|
|
320
336
|
const paramHash = _hashParams!(ctx.matched.params);
|
|
321
337
|
const isPassthroughPrerenderRoute = ctx.entries.some(
|
|
322
|
-
(entry) =>
|
|
323
|
-
entry.type === "route" &&
|
|
324
|
-
entry.prerenderDef?.options?.passthrough === true,
|
|
338
|
+
(entry) => entry.type === "route" && entry.isPassthrough === true,
|
|
325
339
|
);
|
|
326
340
|
|
|
327
341
|
if (ctx.isIntercept) {
|
|
@@ -391,9 +405,7 @@ export function withCacheLookup<TEnv>(
|
|
|
391
405
|
if (prerenderStoreInstance) {
|
|
392
406
|
const paramHash = _hashParams!(ctx.matched.params);
|
|
393
407
|
const isPassthroughPrerenderRoute = ctx.entries.some(
|
|
394
|
-
(entry) =>
|
|
395
|
-
entry.type === "route" &&
|
|
396
|
-
entry.prerenderDef?.options?.passthrough === true,
|
|
408
|
+
(entry) => entry.type === "route" && entry.isPassthrough === true,
|
|
397
409
|
);
|
|
398
410
|
|
|
399
411
|
if (ctx.isIntercept) {
|
|
@@ -446,7 +458,7 @@ export function withCacheLookup<TEnv>(
|
|
|
446
458
|
yield* source;
|
|
447
459
|
if (ms) {
|
|
448
460
|
ms.metrics.push({
|
|
449
|
-
label: "pipeline:cache-
|
|
461
|
+
label: "pipeline:cache-miss",
|
|
450
462
|
duration: performance.now() - pipelineStart,
|
|
451
463
|
startTime: pipelineStart - ms.requestStart,
|
|
452
464
|
});
|
|
@@ -466,7 +478,7 @@ export function withCacheLookup<TEnv>(
|
|
|
466
478
|
yield* source;
|
|
467
479
|
if (ms) {
|
|
468
480
|
ms.metrics.push({
|
|
469
|
-
label: "pipeline:cache-
|
|
481
|
+
label: "pipeline:cache-miss",
|
|
470
482
|
duration: performance.now() - pipelineStart,
|
|
471
483
|
startTime: pipelineStart - ms.requestStart,
|
|
472
484
|
});
|
|
@@ -518,7 +530,41 @@ export function withCacheLookup<TEnv>(
|
|
|
518
530
|
|
|
519
531
|
// Look up revalidation rules for this segment
|
|
520
532
|
const entryInfo = entryRevalidateMap?.get(segment.id);
|
|
533
|
+
|
|
534
|
+
// Even without explicit revalidation rules, route segments and their
|
|
535
|
+
// children must re-render when params or search params change — the
|
|
536
|
+
// handler reads ctx.params/ctx.searchParams so different values produce
|
|
537
|
+
// different content. Matches evaluateRevalidation's default logic.
|
|
538
|
+
const searchChanged = ctx.prevUrl.search !== ctx.url.search;
|
|
539
|
+
const routeParamsChanged = !paramsEqual(
|
|
540
|
+
ctx.matched.params,
|
|
541
|
+
ctx.prevParams,
|
|
542
|
+
);
|
|
543
|
+
const shouldDefaultRevalidate =
|
|
544
|
+
(searchChanged || routeParamsChanged) &&
|
|
545
|
+
(segment.type === "route" ||
|
|
546
|
+
(segment.belongsToRoute &&
|
|
547
|
+
(segment.type === "layout" || segment.type === "parallel")));
|
|
548
|
+
|
|
521
549
|
if (!entryInfo || entryInfo.revalidate.length === 0) {
|
|
550
|
+
if (shouldDefaultRevalidate) {
|
|
551
|
+
// Params or search params changed — must re-render even without custom rules
|
|
552
|
+
if (isTraceActive()) {
|
|
553
|
+
pushRevalidationTraceEntry({
|
|
554
|
+
segmentId: segment.id,
|
|
555
|
+
segmentType: segment.type,
|
|
556
|
+
belongsToRoute: segment.belongsToRoute ?? false,
|
|
557
|
+
source: "cache-hit",
|
|
558
|
+
defaultShouldRevalidate: true,
|
|
559
|
+
finalShouldRevalidate: true,
|
|
560
|
+
reason: routeParamsChanged
|
|
561
|
+
? "cached-params-changed"
|
|
562
|
+
: "cached-search-changed",
|
|
563
|
+
});
|
|
564
|
+
}
|
|
565
|
+
yield segment;
|
|
566
|
+
continue;
|
|
567
|
+
}
|
|
522
568
|
// No revalidation rules, use default behavior (skip if client has)
|
|
523
569
|
if (isTraceActive()) {
|
|
524
570
|
pushRevalidationTraceEntry({
|
|
@@ -579,6 +625,15 @@ export function withCacheLookup<TEnv>(
|
|
|
579
625
|
yield segment;
|
|
580
626
|
}
|
|
581
627
|
|
|
628
|
+
// Set streaming flag (once) and resolve render barrier.
|
|
629
|
+
const barrierReqCtx = _getRequestContext();
|
|
630
|
+
if (barrierReqCtx) {
|
|
631
|
+
if (barrierReqCtx._treeHasStreaming === undefined) {
|
|
632
|
+
barrierReqCtx._treeHasStreaming = treeHasStreaming(ctx.entries);
|
|
633
|
+
}
|
|
634
|
+
barrierReqCtx._resolveRenderBarrier(cacheResult.segments);
|
|
635
|
+
}
|
|
636
|
+
|
|
582
637
|
// Resolve loaders fresh (loaders are NOT cached by default)
|
|
583
638
|
// This ensures fresh data even on cache hit
|
|
584
639
|
const Store = ctx.Store;
|
|
@@ -615,7 +670,11 @@ export function withCacheLookup<TEnv>(
|
|
|
615
670
|
ctx.url,
|
|
616
671
|
ctx.routeKey,
|
|
617
672
|
ctx.actionContext,
|
|
618
|
-
|
|
673
|
+
// Loaders are never cached in the segment cache, so segment
|
|
674
|
+
// staleness (cacheResult.shouldRevalidate) must not propagate.
|
|
675
|
+
// But browser-sent staleness (ctx.stale) — indicating an action
|
|
676
|
+
// happened in this or another tab — must still reach loaders.
|
|
677
|
+
ctx.stale || undefined,
|
|
619
678
|
),
|
|
620
679
|
);
|
|
621
680
|
|
|
@@ -642,7 +701,7 @@ export function withCacheLookup<TEnv>(
|
|
|
642
701
|
depth: 1,
|
|
643
702
|
});
|
|
644
703
|
ms.metrics.push({
|
|
645
|
-
label: "pipeline:cache-
|
|
704
|
+
label: "pipeline:cache-hit",
|
|
646
705
|
duration: loaderEnd - pipelineStart,
|
|
647
706
|
startTime: pipelineStart - ms.requestStart,
|
|
648
707
|
});
|
|
@@ -165,10 +165,14 @@ export function withCacheStore<TEnv>(
|
|
|
165
165
|
// Combine main segments with intercept segments
|
|
166
166
|
const allSegmentsToCache = [...allSegments, ...state.interceptSegments];
|
|
167
167
|
|
|
168
|
-
// Check if any non-loader segments have null components
|
|
169
|
-
//
|
|
168
|
+
// Check if any non-loader segments have null components from revalidation
|
|
169
|
+
// skip (client already had them). Segments where the handler intentionally
|
|
170
|
+
// returned null are not revalidation skips — re-rendering them will still
|
|
171
|
+
// produce null, so proactive caching would be wasted work.
|
|
172
|
+
const clientIdSet = new Set(ctx.clientSegmentIds);
|
|
170
173
|
const hasNullComponents = allSegmentsToCache.some(
|
|
171
|
-
(s) =>
|
|
174
|
+
(s) =>
|
|
175
|
+
s.component === null && s.type !== "loader" && clientIdSet.has(s.id),
|
|
172
176
|
);
|
|
173
177
|
|
|
174
178
|
const requestCtx = getRequestContext();
|
|
@@ -195,6 +199,10 @@ export function withCacheStore<TEnv>(
|
|
|
195
199
|
// Proactive caching: render all segments fresh in background
|
|
196
200
|
// This ensures cache has complete components for future requests
|
|
197
201
|
requestCtx.waitUntil(async () => {
|
|
202
|
+
// Prevent background metrics from polluting foreground timeline.
|
|
203
|
+
const savedMetrics = ctx.Store.metrics;
|
|
204
|
+
ctx.Store.metrics = undefined;
|
|
205
|
+
|
|
198
206
|
const start = performance.now();
|
|
199
207
|
debugLog("cacheStore", "proactive caching started", {
|
|
200
208
|
pathname: ctx.pathname,
|
|
@@ -225,7 +233,9 @@ export function withCacheStore<TEnv>(
|
|
|
225
233
|
// Use normal loader access so handle data is captured
|
|
226
234
|
setupLoaderAccess(proactiveHandlerContext, proactiveLoaderPromises);
|
|
227
235
|
|
|
228
|
-
// Re-resolve ALL segments without revalidation
|
|
236
|
+
// Re-resolve ALL segments without revalidation.
|
|
237
|
+
// Skip DSL loaders — they are never cached (cacheRoute filters them)
|
|
238
|
+
// and are always resolved fresh on each request.
|
|
229
239
|
const Store = ctx.Store;
|
|
230
240
|
const freshSegments = await Store.run(() =>
|
|
231
241
|
resolveAllSegments(
|
|
@@ -234,6 +244,7 @@ export function withCacheStore<TEnv>(
|
|
|
234
244
|
ctx.matched.params,
|
|
235
245
|
proactiveHandlerContext,
|
|
236
246
|
proactiveLoaderPromises,
|
|
247
|
+
{ skipLoaders: true },
|
|
237
248
|
),
|
|
238
249
|
);
|
|
239
250
|
|
|
@@ -285,11 +296,17 @@ export function withCacheStore<TEnv>(
|
|
|
285
296
|
});
|
|
286
297
|
} finally {
|
|
287
298
|
requestCtx._handleStore = originalHandleStore;
|
|
299
|
+
ctx.Store.metrics = savedMetrics;
|
|
288
300
|
}
|
|
289
301
|
});
|
|
290
302
|
} else {
|
|
291
303
|
// All segments have components - cache directly
|
|
292
304
|
// Schedule caching in waitUntil since cacheRoute is now async (key resolution)
|
|
305
|
+
if (INTERNAL_RANGO_DEBUG) {
|
|
306
|
+
console.log(
|
|
307
|
+
`[RSC CacheStore][req:${reqId}] Direct cache path: scheduling cacheRoute for ${ctx.pathname} (${allSegmentsToCache.length} segments, hasNullComponents=${hasNullComponents})`,
|
|
308
|
+
);
|
|
309
|
+
}
|
|
293
310
|
requestCtx.waitUntil(async () => {
|
|
294
311
|
const start = performance.now();
|
|
295
312
|
await cacheScope.cacheRoute(
|
|
@@ -87,10 +87,49 @@
|
|
|
87
87
|
* if (state.cacheHit) return; // Now we can check
|
|
88
88
|
*/
|
|
89
89
|
import type { ResolvedSegment } from "../../types.js";
|
|
90
|
+
import type { EntryData } from "../../server/context.js";
|
|
91
|
+
import { _getRequestContext } from "../../server/request-context.js";
|
|
90
92
|
import type { MatchContext, MatchPipelineState } from "../match-context.js";
|
|
91
93
|
import { getRouterContext } from "../router-context.js";
|
|
92
94
|
import type { GeneratorMiddleware } from "./cache-lookup.js";
|
|
93
95
|
|
|
96
|
+
/**
|
|
97
|
+
* Check whether any entry in the tree uses loading() (streaming).
|
|
98
|
+
* Matches the router's streaming semantics in fresh.ts: streaming is
|
|
99
|
+
* enabled when `loading` is defined AND not `false`. `loading: false`
|
|
100
|
+
* explicitly disables streaming; `undefined` means no loading at all.
|
|
101
|
+
*/
|
|
102
|
+
export function treeHasStreaming(entries: EntryData[]): boolean {
|
|
103
|
+
for (const entry of entries) {
|
|
104
|
+
if (
|
|
105
|
+
"loading" in entry &&
|
|
106
|
+
entry.loading !== undefined &&
|
|
107
|
+
entry.loading !== false
|
|
108
|
+
)
|
|
109
|
+
return true;
|
|
110
|
+
if (entry.layout) {
|
|
111
|
+
if (treeHasStreaming(entry.layout)) return true;
|
|
112
|
+
}
|
|
113
|
+
if (entry.parallel) {
|
|
114
|
+
for (const key in entry.parallel) {
|
|
115
|
+
const parallelEntry = entry.parallel[key as `@${string}`];
|
|
116
|
+
if (parallelEntry) {
|
|
117
|
+
if (
|
|
118
|
+
"loading" in parallelEntry &&
|
|
119
|
+
parallelEntry.loading !== undefined &&
|
|
120
|
+
parallelEntry.loading !== false
|
|
121
|
+
)
|
|
122
|
+
return true;
|
|
123
|
+
if (parallelEntry.layout) {
|
|
124
|
+
if (treeHasStreaming(parallelEntry.layout)) return true;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
|
|
94
133
|
/**
|
|
95
134
|
* Creates segment resolution middleware
|
|
96
135
|
*
|
|
@@ -116,6 +155,7 @@ export function withSegmentResolution<TEnv>(
|
|
|
116
155
|
const ownStart = performance.now();
|
|
117
156
|
|
|
118
157
|
// If cache hit, segments were already yielded by cache lookup
|
|
158
|
+
// (render barrier is resolved on the cache-hit path)
|
|
119
159
|
if (state.cacheHit) {
|
|
120
160
|
if (ms) {
|
|
121
161
|
ms.metrics.push({
|
|
@@ -127,6 +167,11 @@ export function withSegmentResolution<TEnv>(
|
|
|
127
167
|
return;
|
|
128
168
|
}
|
|
129
169
|
|
|
170
|
+
const reqCtx = _getRequestContext();
|
|
171
|
+
if (reqCtx && reqCtx._treeHasStreaming === undefined) {
|
|
172
|
+
reqCtx._treeHasStreaming = treeHasStreaming(ctx.entries);
|
|
173
|
+
}
|
|
174
|
+
|
|
130
175
|
const { resolveAllSegmentsWithRevalidation, resolveAllSegments } =
|
|
131
176
|
getRouterContext<TEnv>();
|
|
132
177
|
|
|
@@ -148,6 +193,10 @@ export function withSegmentResolution<TEnv>(
|
|
|
148
193
|
state.segments = segments;
|
|
149
194
|
state.matchedIds = segments.map((s: { id: string }) => s.id);
|
|
150
195
|
|
|
196
|
+
if (reqCtx) {
|
|
197
|
+
reqCtx._resolveRenderBarrier(segments);
|
|
198
|
+
}
|
|
199
|
+
|
|
151
200
|
// Yield all resolved segments
|
|
152
201
|
for (const segment of segments) {
|
|
153
202
|
yield segment;
|
|
@@ -178,6 +227,10 @@ export function withSegmentResolution<TEnv>(
|
|
|
178
227
|
state.segments = result.segments;
|
|
179
228
|
state.matchedIds = result.matchedIds;
|
|
180
229
|
|
|
230
|
+
if (reqCtx) {
|
|
231
|
+
reqCtx._resolveRenderBarrier(result.segments);
|
|
232
|
+
}
|
|
233
|
+
|
|
181
234
|
// Yield all resolved segments
|
|
182
235
|
for (const segment of result.segments) {
|
|
183
236
|
yield segment;
|
|
@@ -67,10 +67,11 @@
|
|
|
67
67
|
* Keep if:
|
|
68
68
|
* - component !== null (needs rendering)
|
|
69
69
|
* - type === "loader" (carries data even with null component)
|
|
70
|
+
* - client doesn't have the segment (structurally required parent node)
|
|
70
71
|
*
|
|
71
72
|
* Skip if:
|
|
72
|
-
* - component === null AND type !== "loader"
|
|
73
|
-
* - (
|
|
73
|
+
* - component === null AND type !== "loader" AND client has it cached
|
|
74
|
+
* - (Revalidation skip — client already has this segment's UI)
|
|
74
75
|
*
|
|
75
76
|
*
|
|
76
77
|
* INTERCEPT HANDLING
|
|
@@ -168,10 +169,15 @@ export function buildMatchResult<TEnv>(
|
|
|
168
169
|
// Deduplicate allIds (defense-in-depth for partial match path)
|
|
169
170
|
allIds = [...new Set(allIds)];
|
|
170
171
|
|
|
171
|
-
// Filter out segments
|
|
172
|
-
//
|
|
172
|
+
// Filter out null-component segments only when the client already has
|
|
173
|
+
// them cached (revalidation skip). If the client doesn't have the segment,
|
|
174
|
+
// it must be included even with null component — it's structurally required
|
|
175
|
+
// as a parent node for child layouts/parallels to reconcile against.
|
|
176
|
+
// Loader segments are always included as they carry data.
|
|
177
|
+
const clientIdSet = new Set(ctx.clientSegmentIds);
|
|
173
178
|
segmentsToRender = allSegments.filter(
|
|
174
|
-
(s) =>
|
|
179
|
+
(s) =>
|
|
180
|
+
s.component !== null || s.type === "loader" || !clientIdSet.has(s.id),
|
|
175
181
|
);
|
|
176
182
|
}
|
|
177
183
|
|
package/src/router/metrics.ts
CHANGED
|
@@ -15,7 +15,12 @@ function formatMs(value: number): string {
|
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
function sortMetrics(metrics: PerformanceMetric[]): PerformanceMetric[] {
|
|
18
|
-
return [...metrics].sort((a, b) =>
|
|
18
|
+
return [...metrics].sort((a, b) => {
|
|
19
|
+
// handler:total always goes last (it wraps everything)
|
|
20
|
+
if (a.label === "handler:total") return 1;
|
|
21
|
+
if (b.label === "handler:total") return -1;
|
|
22
|
+
return a.startTime - b.startTime;
|
|
23
|
+
});
|
|
19
24
|
}
|
|
20
25
|
|
|
21
26
|
interface Span {
|
|
@@ -27,8 +27,12 @@ type GetVariableFn = {
|
|
|
27
27
|
* Set variable function type
|
|
28
28
|
*/
|
|
29
29
|
type SetVariableFn = {
|
|
30
|
-
<T>(contextVar: ContextVar<T>, value: T): void;
|
|
31
|
-
<K extends keyof DefaultVars>(
|
|
30
|
+
<T>(contextVar: ContextVar<T>, value: T, options?: { cache?: boolean }): void;
|
|
31
|
+
<K extends keyof DefaultVars>(
|
|
32
|
+
key: K,
|
|
33
|
+
value: DefaultVars[K],
|
|
34
|
+
options?: { cache?: boolean },
|
|
35
|
+
): void;
|
|
32
36
|
};
|
|
33
37
|
|
|
34
38
|
/**
|
|
@@ -91,12 +95,6 @@ export interface MiddlewareContext<
|
|
|
91
95
|
/** Set a context variable (shared with route handlers) */
|
|
92
96
|
set: SetVariableFn;
|
|
93
97
|
|
|
94
|
-
/**
|
|
95
|
-
* Middleware-injected variables.
|
|
96
|
-
* Same shared dictionary as `ctx.get()`/`ctx.set()`.
|
|
97
|
-
*/
|
|
98
|
-
var: DefaultVars;
|
|
99
|
-
|
|
100
98
|
/**
|
|
101
99
|
* Set a response header - can be called before or after `next()`.
|
|
102
100
|
*
|
package/src/router/middleware.ts
CHANGED
|
@@ -204,12 +204,9 @@ export function createMiddlewareContext<TEnv>(
|
|
|
204
204
|
get: ((keyOrVar: any) =>
|
|
205
205
|
contextGet(variables, keyOrVar)) as MiddlewareContext<TEnv>["get"],
|
|
206
206
|
|
|
207
|
-
set: ((keyOrVar: any, value: unknown) => {
|
|
208
|
-
contextSet(variables, keyOrVar, value);
|
|
207
|
+
set: ((keyOrVar: any, value: unknown, options?: any) => {
|
|
208
|
+
contextSet(variables, keyOrVar, value, options);
|
|
209
209
|
}) as MiddlewareContext<TEnv>["set"],
|
|
210
|
-
|
|
211
|
-
var: variables as MiddlewareContext<TEnv>["var"],
|
|
212
|
-
|
|
213
210
|
header(name: string, value: string): void {
|
|
214
211
|
// Before next(): delegate to shared RequestContext stub
|
|
215
212
|
if (isPreNext()) {
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Navigation Snapshot
|
|
3
|
+
*
|
|
4
|
+
* Pure data type representing the navigation-specific state for partial requests.
|
|
5
|
+
* Consolidates the header parsing, previous-route matching, intercept-context
|
|
6
|
+
* detection, and segment ID filtering that previously lived inline in
|
|
7
|
+
* createMatchContextForPartial (match-api.ts).
|
|
8
|
+
*
|
|
9
|
+
* resolveNavigation() is the factory: given a request + URL + current route key,
|
|
10
|
+
* it returns a NavigationSnapshot (or null if no previous URL).
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import type { RouteMatchResult } from "./pattern-matching.js";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Snapshot of navigation state for a partial (navigation/action) request.
|
|
17
|
+
*
|
|
18
|
+
* Contains the "where are we coming from?" data: previous route, intercept
|
|
19
|
+
* source, client segment state, and derived flags.
|
|
20
|
+
*/
|
|
21
|
+
export interface NavigationSnapshot {
|
|
22
|
+
/** Previous page URL (from X-RSC-Router-Client-Path or Referer) */
|
|
23
|
+
prevUrl: URL;
|
|
24
|
+
/** Params from the previous route match */
|
|
25
|
+
prevParams: Record<string, string>;
|
|
26
|
+
/** Previous route match result (null if prev URL doesn't match any route) */
|
|
27
|
+
prevMatch: RouteMatchResult | null;
|
|
28
|
+
|
|
29
|
+
/** URL used as intercept context source */
|
|
30
|
+
interceptContextUrl: URL;
|
|
31
|
+
/** Route match for the intercept context URL */
|
|
32
|
+
interceptContextMatch: RouteMatchResult | null;
|
|
33
|
+
|
|
34
|
+
/** Raw segment IDs the client currently has */
|
|
35
|
+
clientSegmentIds: string[];
|
|
36
|
+
/** Set version for O(1) lookup */
|
|
37
|
+
clientSegmentSet: Set<string>;
|
|
38
|
+
/** Segment IDs filtered to remove parallel (.@) and loader (D\d+.) entries */
|
|
39
|
+
filteredSegmentIds: string[];
|
|
40
|
+
|
|
41
|
+
/** Whether client considers its cache stale */
|
|
42
|
+
stale: boolean;
|
|
43
|
+
|
|
44
|
+
/** Whether the intercept context route is the same as the current route */
|
|
45
|
+
isSameRouteNavigation: boolean;
|
|
46
|
+
|
|
47
|
+
/** Effective "from" URL (intercept source URL when present, else prevUrl) */
|
|
48
|
+
effectiveFromUrl: URL;
|
|
49
|
+
/** Effective "from" match (intercept source match when present, else prevMatch) */
|
|
50
|
+
effectiveFromMatch: RouteMatchResult | null;
|
|
51
|
+
|
|
52
|
+
/** Whether an intercept source header was present */
|
|
53
|
+
hasInterceptSource: boolean;
|
|
54
|
+
|
|
55
|
+
/** Whether an HMR request header was present */
|
|
56
|
+
isHmr: boolean;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface ResolveNavigationDeps {
|
|
60
|
+
findMatch: (pathname: string) => RouteMatchResult | null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Resolve navigation state from a partial request.
|
|
65
|
+
*
|
|
66
|
+
* Returns null if no previous URL is available (required for partial navigation).
|
|
67
|
+
*
|
|
68
|
+
* @param request - The incoming HTTP request
|
|
69
|
+
* @param url - Parsed URL of the request
|
|
70
|
+
* @param currentRouteKey - Route key of the current (target) route match
|
|
71
|
+
* @param deps - Dependencies (findMatch)
|
|
72
|
+
*/
|
|
73
|
+
export function resolveNavigation(
|
|
74
|
+
request: Request,
|
|
75
|
+
url: URL,
|
|
76
|
+
currentRouteKey: string,
|
|
77
|
+
deps: ResolveNavigationDeps,
|
|
78
|
+
): NavigationSnapshot | null {
|
|
79
|
+
// Parse client state from RSC request params/headers
|
|
80
|
+
const clientSegmentIds =
|
|
81
|
+
url.searchParams.get("_rsc_segments")?.split(",").filter(Boolean) || [];
|
|
82
|
+
const stale = url.searchParams.get("_rsc_stale") === "true";
|
|
83
|
+
const previousUrl =
|
|
84
|
+
request.headers.get("X-RSC-Router-Client-Path") ||
|
|
85
|
+
request.headers.get("Referer");
|
|
86
|
+
const interceptSourceUrl = request.headers.get(
|
|
87
|
+
"X-RSC-Router-Intercept-Source",
|
|
88
|
+
);
|
|
89
|
+
const isHmr = !!request.headers.get("X-RSC-HMR");
|
|
90
|
+
|
|
91
|
+
if (!previousUrl) {
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Parse previous URL
|
|
96
|
+
let prevUrl: URL;
|
|
97
|
+
try {
|
|
98
|
+
prevUrl = new URL(previousUrl, url.origin);
|
|
99
|
+
} catch {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Parse intercept context URL
|
|
104
|
+
let interceptContextUrl: URL;
|
|
105
|
+
try {
|
|
106
|
+
interceptContextUrl = interceptSourceUrl
|
|
107
|
+
? new URL(interceptSourceUrl, url.origin)
|
|
108
|
+
: prevUrl;
|
|
109
|
+
} catch {
|
|
110
|
+
interceptContextUrl = prevUrl;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Match previous and intercept context routes
|
|
114
|
+
const prevMatch = deps.findMatch(prevUrl.pathname);
|
|
115
|
+
const prevParams = prevMatch?.params || {};
|
|
116
|
+
const interceptContextMatch = interceptSourceUrl
|
|
117
|
+
? deps.findMatch(interceptContextUrl.pathname)
|
|
118
|
+
: prevMatch;
|
|
119
|
+
|
|
120
|
+
// Derived state
|
|
121
|
+
const isSameRouteNavigation = !!(
|
|
122
|
+
interceptContextMatch && interceptContextMatch.routeKey === currentRouteKey
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
const hasInterceptSource = !!interceptSourceUrl;
|
|
126
|
+
const effectiveFromUrl = hasInterceptSource ? interceptContextUrl : prevUrl;
|
|
127
|
+
const effectiveFromMatch = hasInterceptSource
|
|
128
|
+
? interceptContextMatch
|
|
129
|
+
: prevMatch;
|
|
130
|
+
|
|
131
|
+
// Filter segment IDs: remove parallel (.@) and loader (D\d+.) entries
|
|
132
|
+
const filteredSegmentIds = clientSegmentIds.filter((id) => {
|
|
133
|
+
if (id.includes(".@")) return false;
|
|
134
|
+
if (/D\d+\./.test(id)) return false;
|
|
135
|
+
return true;
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
const clientSegmentSet = new Set(clientSegmentIds);
|
|
139
|
+
|
|
140
|
+
return {
|
|
141
|
+
prevUrl,
|
|
142
|
+
prevParams,
|
|
143
|
+
prevMatch,
|
|
144
|
+
interceptContextUrl,
|
|
145
|
+
interceptContextMatch,
|
|
146
|
+
clientSegmentIds,
|
|
147
|
+
clientSegmentSet,
|
|
148
|
+
filteredSegmentIds,
|
|
149
|
+
stale,
|
|
150
|
+
isSameRouteNavigation,
|
|
151
|
+
effectiveFromUrl,
|
|
152
|
+
effectiveFromMatch,
|
|
153
|
+
hasInterceptSource,
|
|
154
|
+
isHmr,
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Test helper: create a NavigationSnapshot with sensible defaults and overrides.
|
|
160
|
+
*/
|
|
161
|
+
export function createNavigationSnapshot(
|
|
162
|
+
overrides?: Partial<NavigationSnapshot>,
|
|
163
|
+
): NavigationSnapshot {
|
|
164
|
+
const defaultUrl = new URL("http://localhost/");
|
|
165
|
+
return {
|
|
166
|
+
prevUrl: defaultUrl,
|
|
167
|
+
prevParams: {},
|
|
168
|
+
prevMatch: null,
|
|
169
|
+
interceptContextUrl: defaultUrl,
|
|
170
|
+
interceptContextMatch: null,
|
|
171
|
+
clientSegmentIds: [],
|
|
172
|
+
clientSegmentSet: new Set(),
|
|
173
|
+
filteredSegmentIds: [],
|
|
174
|
+
stale: false,
|
|
175
|
+
isSameRouteNavigation: false,
|
|
176
|
+
effectiveFromUrl: defaultUrl,
|
|
177
|
+
effectiveFromMatch: null,
|
|
178
|
+
hasInterceptSource: false,
|
|
179
|
+
isHmr: false,
|
|
180
|
+
...overrides,
|
|
181
|
+
};
|
|
182
|
+
}
|