@rangojs/router 0.0.0-experimental.7 → 0.0.0-experimental.71
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/AGENTS.md +9 -0
- package/README.md +942 -4
- package/dist/bin/rango.js +1689 -0
- package/dist/vite/index.js +4951 -930
- package/package.json +70 -60
- package/skills/breadcrumbs/SKILL.md +250 -0
- package/skills/cache-guide/SKILL.md +294 -0
- package/skills/caching/SKILL.md +93 -23
- package/skills/composability/SKILL.md +172 -0
- package/skills/debug-manifest/SKILL.md +12 -8
- package/skills/document-cache/SKILL.md +18 -16
- package/skills/fonts/SKILL.md +167 -0
- package/skills/hooks/SKILL.md +334 -72
- package/skills/host-router/SKILL.md +218 -0
- package/skills/intercept/SKILL.md +131 -8
- package/skills/layout/SKILL.md +100 -3
- package/skills/links/SKILL.md +92 -31
- package/skills/loader/SKILL.md +404 -44
- package/skills/middleware/SKILL.md +173 -34
- package/skills/mime-routes/SKILL.md +128 -0
- package/skills/parallel/SKILL.md +204 -1
- package/skills/prerender/SKILL.md +685 -0
- package/skills/rango/SKILL.md +85 -16
- package/skills/response-routes/SKILL.md +411 -0
- package/skills/route/SKILL.md +257 -14
- package/skills/router-setup/SKILL.md +210 -32
- package/skills/tailwind/SKILL.md +129 -0
- package/skills/theme/SKILL.md +9 -8
- package/skills/typesafety/SKILL.md +328 -89
- package/skills/use-cache/SKILL.md +324 -0
- package/src/__internal.ts +102 -4
- package/src/bin/rango.ts +321 -0
- package/src/browser/action-coordinator.ts +97 -0
- package/src/browser/action-response-classifier.ts +99 -0
- package/src/browser/app-version.ts +14 -0
- package/src/browser/event-controller.ts +92 -64
- package/src/browser/history-state.ts +80 -0
- package/src/browser/intercept-utils.ts +52 -0
- package/src/browser/link-interceptor.ts +24 -4
- package/src/browser/logging.ts +55 -0
- package/src/browser/merge-segment-loaders.ts +20 -12
- package/src/browser/navigation-bridge.ts +296 -558
- package/src/browser/navigation-client.ts +179 -69
- package/src/browser/navigation-store.ts +73 -55
- package/src/browser/navigation-transaction.ts +297 -0
- package/src/browser/network-error-handler.ts +61 -0
- package/src/browser/partial-update.ts +328 -313
- package/src/browser/prefetch/cache.ts +206 -0
- package/src/browser/prefetch/fetch.ts +150 -0
- package/src/browser/prefetch/observer.ts +65 -0
- package/src/browser/prefetch/policy.ts +48 -0
- package/src/browser/prefetch/queue.ts +160 -0
- package/src/browser/prefetch/resource-ready.ts +77 -0
- package/src/browser/rango-state.ts +112 -0
- package/src/browser/react/Link.tsx +230 -74
- package/src/browser/react/NavigationProvider.tsx +87 -11
- package/src/browser/react/context.ts +11 -0
- package/src/browser/react/filter-segment-order.ts +11 -0
- package/src/browser/react/index.ts +12 -12
- package/src/browser/react/location-state-shared.ts +95 -53
- package/src/browser/react/location-state.ts +60 -15
- package/src/browser/react/mount-context.ts +6 -1
- package/src/browser/react/nonce-context.ts +23 -0
- package/src/browser/react/shallow-equal.ts +27 -0
- package/src/browser/react/use-action.ts +29 -51
- package/src/browser/react/use-client-cache.ts +5 -3
- package/src/browser/react/use-handle.ts +30 -126
- package/src/browser/react/use-href.tsx +2 -2
- package/src/browser/react/use-link-status.ts +6 -5
- package/src/browser/react/use-navigation.ts +22 -63
- package/src/browser/react/use-params.ts +65 -0
- package/src/browser/react/use-pathname.ts +47 -0
- package/src/browser/react/use-router.ts +76 -0
- package/src/browser/react/use-search-params.ts +56 -0
- package/src/browser/react/use-segments.ts +80 -97
- package/src/browser/response-adapter.ts +73 -0
- package/src/browser/rsc-router.tsx +214 -58
- package/src/browser/scroll-restoration.ts +127 -52
- package/src/browser/segment-reconciler.ts +221 -0
- package/src/browser/segment-structure-assert.ts +16 -0
- package/src/browser/server-action-bridge.ts +510 -603
- package/src/browser/shallow.ts +6 -1
- package/src/browser/types.ts +141 -48
- package/src/browser/validate-redirect-origin.ts +29 -0
- package/src/build/generate-manifest.ts +235 -24
- package/src/build/generate-route-types.ts +39 -0
- package/src/build/index.ts +13 -0
- package/src/build/route-trie.ts +265 -0
- package/src/build/route-types/ast-helpers.ts +25 -0
- package/src/build/route-types/ast-route-extraction.ts +98 -0
- package/src/build/route-types/codegen.ts +102 -0
- package/src/build/route-types/include-resolution.ts +418 -0
- package/src/build/route-types/param-extraction.ts +48 -0
- package/src/build/route-types/per-module-writer.ts +128 -0
- package/src/build/route-types/router-processing.ts +618 -0
- package/src/build/route-types/scan-filter.ts +85 -0
- package/src/build/runtime-discovery.ts +231 -0
- package/src/cache/background-task.ts +34 -0
- package/src/cache/cache-key-utils.ts +44 -0
- package/src/cache/cache-policy.ts +125 -0
- package/src/cache/cache-runtime.ts +342 -0
- package/src/cache/cache-scope.ts +167 -309
- package/src/cache/cf/cf-cache-store.ts +571 -17
- package/src/cache/cf/index.ts +13 -3
- package/src/cache/document-cache.ts +116 -77
- package/src/cache/handle-capture.ts +81 -0
- package/src/cache/handle-snapshot.ts +41 -0
- package/src/cache/index.ts +1 -15
- package/src/cache/memory-segment-store.ts +191 -13
- package/src/cache/profile-registry.ts +73 -0
- package/src/cache/read-through-swr.ts +134 -0
- package/src/cache/segment-codec.ts +256 -0
- package/src/cache/taint.ts +153 -0
- package/src/cache/types.ts +72 -122
- package/src/client.rsc.tsx +3 -1
- package/src/client.tsx +105 -179
- package/src/component-utils.ts +4 -4
- package/src/components/DefaultDocument.tsx +5 -1
- package/src/context-var.ts +156 -0
- package/src/debug.ts +19 -9
- package/src/errors.ts +108 -2
- package/src/handle.ts +55 -29
- package/src/handles/MetaTags.tsx +73 -20
- package/src/handles/breadcrumbs.ts +66 -0
- package/src/handles/index.ts +1 -0
- package/src/handles/meta.ts +30 -13
- package/src/host/cookie-handler.ts +21 -15
- package/src/host/errors.ts +8 -8
- package/src/host/index.ts +4 -7
- package/src/host/pattern-matcher.ts +27 -27
- package/src/host/router.ts +61 -39
- package/src/host/testing.ts +8 -8
- package/src/host/types.ts +15 -7
- package/src/host/utils.ts +1 -1
- package/src/href-client.ts +119 -29
- package/src/index.rsc.ts +155 -19
- package/src/index.ts +223 -30
- package/src/internal-debug.ts +11 -0
- package/src/loader.rsc.ts +26 -157
- package/src/loader.ts +27 -10
- package/src/network-error-thrower.tsx +3 -1
- package/src/outlet-provider.tsx +45 -0
- package/src/prerender/param-hash.ts +37 -0
- package/src/prerender/store.ts +186 -0
- package/src/prerender.ts +524 -0
- package/src/reverse.ts +351 -0
- package/src/root-error-boundary.tsx +41 -29
- package/src/route-content-wrapper.tsx +7 -4
- package/src/route-definition/dsl-helpers.ts +982 -0
- package/src/route-definition/helper-factories.ts +200 -0
- package/src/route-definition/helpers-types.ts +434 -0
- package/src/route-definition/index.ts +55 -0
- package/src/route-definition/redirect.ts +101 -0
- package/src/route-definition/resolve-handler-use.ts +149 -0
- package/src/route-definition.ts +1 -1428
- package/src/route-map-builder.ts +217 -123
- package/src/route-name.ts +53 -0
- package/src/route-types.ts +70 -8
- package/src/router/content-negotiation.ts +215 -0
- package/src/router/debug-manifest.ts +72 -0
- package/src/router/error-handling.ts +9 -9
- package/src/router/find-match.ts +160 -0
- package/src/router/handler-context.ts +435 -86
- package/src/router/intercept-resolution.ts +402 -0
- package/src/router/lazy-includes.ts +237 -0
- package/src/router/loader-resolution.ts +356 -128
- package/src/router/logging.ts +251 -0
- package/src/router/manifest.ts +154 -35
- package/src/router/match-api.ts +555 -0
- package/src/router/match-context.ts +5 -3
- package/src/router/match-handlers.ts +440 -0
- package/src/router/match-middleware/background-revalidation.ts +108 -93
- package/src/router/match-middleware/cache-lookup.ts +459 -10
- package/src/router/match-middleware/cache-store.ts +98 -26
- package/src/router/match-middleware/intercept-resolution.ts +57 -17
- package/src/router/match-middleware/segment-resolution.ts +80 -6
- package/src/router/match-pipelines.ts +10 -45
- package/src/router/match-result.ts +135 -35
- package/src/router/metrics.ts +240 -15
- package/src/router/middleware-cookies.ts +55 -0
- package/src/router/middleware-types.ts +220 -0
- package/src/router/middleware.ts +324 -369
- package/src/router/navigation-snapshot.ts +182 -0
- package/src/router/pattern-matching.ts +211 -43
- package/src/router/prerender-match.ts +502 -0
- package/src/router/preview-match.ts +98 -0
- package/src/router/request-classification.ts +310 -0
- package/src/router/revalidation.ts +137 -38
- package/src/router/route-snapshot.ts +245 -0
- package/src/router/router-context.ts +41 -21
- package/src/router/router-interfaces.ts +484 -0
- package/src/router/router-options.ts +618 -0
- package/src/router/router-registry.ts +24 -0
- package/src/router/segment-resolution/fresh.ts +748 -0
- package/src/router/segment-resolution/helpers.ts +268 -0
- package/src/router/segment-resolution/loader-cache.ts +199 -0
- package/src/router/segment-resolution/revalidation.ts +1379 -0
- package/src/router/segment-resolution/static-store.ts +67 -0
- package/src/router/segment-resolution.ts +21 -0
- package/src/router/segment-wrappers.ts +291 -0
- package/src/router/telemetry-otel.ts +299 -0
- package/src/router/telemetry.ts +300 -0
- package/src/router/timeout.ts +148 -0
- package/src/router/trie-matching.ts +239 -0
- package/src/router/types.ts +78 -3
- package/src/router.ts +740 -4252
- package/src/rsc/handler-context.ts +45 -0
- package/src/rsc/handler.ts +907 -797
- package/src/rsc/helpers.ts +140 -6
- package/src/rsc/index.ts +0 -20
- package/src/rsc/loader-fetch.ts +229 -0
- package/src/rsc/manifest-init.ts +90 -0
- package/src/rsc/nonce.ts +14 -0
- package/src/rsc/origin-guard.ts +141 -0
- package/src/rsc/progressive-enhancement.ts +391 -0
- package/src/rsc/response-error.ts +37 -0
- package/src/rsc/response-route-handler.ts +347 -0
- package/src/rsc/rsc-rendering.ts +246 -0
- package/src/rsc/runtime-warnings.ts +42 -0
- package/src/rsc/server-action.ts +356 -0
- package/src/rsc/ssr-setup.ts +128 -0
- package/src/rsc/types.ts +46 -11
- package/src/search-params.ts +230 -0
- package/src/segment-system.tsx +165 -17
- package/src/server/context.ts +315 -58
- package/src/server/cookie-store.ts +190 -0
- package/src/server/fetchable-loader-store.ts +37 -0
- package/src/server/handle-store.ts +113 -15
- package/src/server/loader-registry.ts +24 -64
- package/src/server/request-context.ts +607 -81
- package/src/server.ts +35 -130
- package/src/ssr/index.tsx +103 -30
- package/src/static-handler.ts +126 -0
- package/src/theme/ThemeProvider.tsx +21 -15
- package/src/theme/ThemeScript.tsx +5 -5
- package/src/theme/constants.ts +5 -2
- package/src/theme/index.ts +4 -14
- package/src/theme/theme-context.ts +4 -30
- package/src/theme/theme-script.ts +21 -18
- package/src/types/boundaries.ts +158 -0
- package/src/types/cache-types.ts +198 -0
- package/src/types/error-types.ts +192 -0
- package/src/types/global-namespace.ts +100 -0
- package/src/types/handler-context.ts +791 -0
- package/src/types/index.ts +88 -0
- package/src/types/loader-types.ts +210 -0
- package/src/types/route-config.ts +170 -0
- package/src/types/route-entry.ts +109 -0
- package/src/types/segments.ts +151 -0
- package/src/types.ts +1 -1623
- package/src/urls/include-helper.ts +197 -0
- package/src/urls/index.ts +53 -0
- package/src/urls/path-helper-types.ts +346 -0
- package/src/urls/path-helper.ts +364 -0
- package/src/urls/pattern-types.ts +107 -0
- package/src/urls/response-types.ts +116 -0
- package/src/urls/type-extraction.ts +372 -0
- package/src/urls/urls-function.ts +98 -0
- package/src/urls.ts +1 -802
- package/src/use-loader.tsx +161 -81
- package/src/vite/discovery/bundle-postprocess.ts +181 -0
- package/src/vite/discovery/discover-routers.ts +348 -0
- package/src/vite/discovery/prerender-collection.ts +439 -0
- package/src/vite/discovery/route-types-writer.ts +258 -0
- package/src/vite/discovery/self-gen-tracking.ts +47 -0
- package/src/vite/discovery/state.ts +117 -0
- package/src/vite/discovery/virtual-module-codegen.ts +203 -0
- package/src/vite/index.ts +15 -1129
- package/src/vite/plugin-types.ts +103 -0
- package/src/vite/plugins/cjs-to-esm.ts +93 -0
- package/src/vite/plugins/client-ref-dedup.ts +115 -0
- package/src/vite/plugins/client-ref-hashing.ts +105 -0
- package/src/vite/{expose-action-id.ts → plugins/expose-action-id.ts} +72 -53
- package/src/vite/plugins/expose-id-utils.ts +299 -0
- package/src/vite/plugins/expose-ids/export-analysis.ts +296 -0
- package/src/vite/plugins/expose-ids/handler-transform.ts +209 -0
- package/src/vite/plugins/expose-ids/loader-transform.ts +74 -0
- package/src/vite/plugins/expose-ids/router-transform.ts +110 -0
- package/src/vite/plugins/expose-ids/types.ts +45 -0
- package/src/vite/plugins/expose-internal-ids.ts +786 -0
- package/src/vite/plugins/performance-tracks.ts +88 -0
- package/src/vite/plugins/refresh-cmd.ts +127 -0
- package/src/vite/plugins/use-cache-transform.ts +323 -0
- package/src/vite/plugins/version-injector.ts +83 -0
- package/src/vite/plugins/version-plugin.ts +266 -0
- package/src/vite/{virtual-entries.ts → plugins/virtual-entries.ts} +23 -14
- package/src/vite/plugins/virtual-stub-plugin.ts +29 -0
- package/src/vite/rango.ts +462 -0
- package/src/vite/router-discovery.ts +918 -0
- package/src/vite/utils/ast-handler-extract.ts +517 -0
- package/src/vite/utils/banner.ts +36 -0
- package/src/vite/utils/bundle-analysis.ts +137 -0
- package/src/vite/utils/manifest-utils.ts +70 -0
- package/src/vite/{package-resolution.ts → utils/package-resolution.ts} +25 -29
- package/src/vite/utils/prerender-utils.ts +207 -0
- package/src/vite/utils/shared-utils.ts +170 -0
- package/CLAUDE.md +0 -43
- package/src/browser/lru-cache.ts +0 -69
- package/src/browser/request-controller.ts +0 -164
- package/src/cache/memory-store.ts +0 -253
- package/src/href-context.ts +0 -33
- package/src/href.ts +0 -255
- package/src/server/route-manifest-cache.ts +0 -173
- package/src/vite/expose-handle-id.ts +0 -209
- package/src/vite/expose-loader-id.ts +0 -426
- package/src/vite/expose-location-state-id.ts +0 -177
- /package/src/vite/{version.d.ts → plugins/version.d.ts} +0 -0
|
@@ -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
|
|
@@ -92,12 +94,198 @@
|
|
|
92
94
|
import type { ResolvedSegment } from "../../types.js";
|
|
93
95
|
import type { MatchContext, MatchPipelineState } from "../match-context.js";
|
|
94
96
|
import { getRouterContext } from "../router-context.js";
|
|
97
|
+
import { resolveSink, safeEmit } from "../telemetry.js";
|
|
98
|
+
import { pushRevalidationTraceEntry, isTraceActive } from "../logging.js";
|
|
99
|
+
import { treeHasStreaming } from "./segment-resolution.js";
|
|
100
|
+
import type { PrerenderStore, PrerenderEntry } from "../../prerender/store.js";
|
|
101
|
+
import type { HandleStore } from "../../server/handle-store.js";
|
|
102
|
+
import {
|
|
103
|
+
getRequestContext,
|
|
104
|
+
_getRequestContext,
|
|
105
|
+
} from "../../server/request-context.js";
|
|
106
|
+
|
|
107
|
+
// Lazily initialized prerender store singleton and dynamically imported deps.
|
|
108
|
+
// Dynamic imports prevent pulling in @vitejs/plugin-rsc/rsc virtual module at
|
|
109
|
+
// top-level, which breaks vitest (only URLs with file:, data:, node: schemes).
|
|
110
|
+
let prerenderStoreInstance: PrerenderStore | null | undefined;
|
|
111
|
+
let _deserializeSegments:
|
|
112
|
+
| typeof import("../../cache/segment-codec.js").deserializeSegments
|
|
113
|
+
| undefined;
|
|
114
|
+
let _restoreHandles:
|
|
115
|
+
| typeof import("../../cache/handle-snapshot.js").restoreHandles
|
|
116
|
+
| undefined;
|
|
117
|
+
let _hashParams:
|
|
118
|
+
| typeof import("../../prerender/param-hash.js").hashParams
|
|
119
|
+
| undefined;
|
|
120
|
+
let _lazyGetRequestContext:
|
|
121
|
+
| typeof import("../../server/request-context.js").getRequestContext
|
|
122
|
+
| undefined;
|
|
123
|
+
|
|
124
|
+
function paramsEqual(
|
|
125
|
+
a: Record<string, string>,
|
|
126
|
+
b: Record<string, string>,
|
|
127
|
+
): boolean {
|
|
128
|
+
if (a === b) return true;
|
|
129
|
+
|
|
130
|
+
const keysA = Object.keys(a);
|
|
131
|
+
if (keysA.length !== Object.keys(b).length) return false;
|
|
132
|
+
|
|
133
|
+
for (const key of keysA) {
|
|
134
|
+
if (a[key] !== b[key]) return false;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return true;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
async function ensurePrerenderDeps() {
|
|
141
|
+
if (!_deserializeSegments) {
|
|
142
|
+
const [codec, snapshot, paramHash, reqCtx, store] = await Promise.all([
|
|
143
|
+
import("../../cache/segment-codec.js"),
|
|
144
|
+
import("../../cache/handle-snapshot.js"),
|
|
145
|
+
import("../../prerender/param-hash.js"),
|
|
146
|
+
import("../../server/request-context.js"),
|
|
147
|
+
import("../../prerender/store.js"),
|
|
148
|
+
]);
|
|
149
|
+
_deserializeSegments = codec.deserializeSegments;
|
|
150
|
+
_restoreHandles = snapshot.restoreHandles;
|
|
151
|
+
_hashParams = paramHash.hashParams;
|
|
152
|
+
_lazyGetRequestContext = reqCtx.getRequestContext;
|
|
153
|
+
if (prerenderStoreInstance === undefined) {
|
|
154
|
+
prerenderStoreInstance = store.createPrerenderStore();
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Shared yield logic for prerender and static handler store entries.
|
|
161
|
+
* Deserializes segments, replays handle data, yields segments with partial
|
|
162
|
+
* navigation nullification, and resolves fresh loaders.
|
|
163
|
+
*/
|
|
164
|
+
async function* yieldFromStore<TEnv>(
|
|
165
|
+
entry: PrerenderEntry,
|
|
166
|
+
ctx: MatchContext<TEnv>,
|
|
167
|
+
state: MatchPipelineState,
|
|
168
|
+
pipelineStart: number,
|
|
169
|
+
handleStoreRef?: HandleStore,
|
|
170
|
+
): AsyncGenerator<ResolvedSegment> {
|
|
171
|
+
const { resolveLoadersOnlyWithRevalidation, resolveLoadersOnly } =
|
|
172
|
+
getRouterContext<TEnv>();
|
|
173
|
+
|
|
174
|
+
if (
|
|
175
|
+
!_deserializeSegments ||
|
|
176
|
+
!_restoreHandles ||
|
|
177
|
+
!_hashParams ||
|
|
178
|
+
!_lazyGetRequestContext
|
|
179
|
+
) {
|
|
180
|
+
throw new Error("yieldFromStore called before ensurePrerenderDeps");
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const segments = await _deserializeSegments(entry.segments);
|
|
184
|
+
|
|
185
|
+
// Replay handle data (same as runtime cache hit path).
|
|
186
|
+
// Prefer the eagerly-captured handleStoreRef to avoid ALS disruption in workerd.
|
|
187
|
+
const handleStore = handleStoreRef ?? _lazyGetRequestContext()?._handleStore;
|
|
188
|
+
if (handleStore) {
|
|
189
|
+
_restoreHandles(entry.handles, handleStore);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
state.cacheHit = true;
|
|
193
|
+
state.cacheSource = "prerender";
|
|
194
|
+
state.cachedSegments = segments;
|
|
195
|
+
state.cachedMatchedIds = segments.map((s) => s.id);
|
|
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
|
+
|
|
207
|
+
// For partial navigation, nullify components the client already has
|
|
208
|
+
// so parent layouts stay live (client keeps its existing versions).
|
|
209
|
+
// When params changed (e.g., different guide slug), the segments have
|
|
210
|
+
// different content, so we must NOT nullify.
|
|
211
|
+
const paramsChanged =
|
|
212
|
+
!ctx.isFullMatch && !paramsEqual(ctx.matched.params, ctx.prevParams);
|
|
213
|
+
for (const segment of segments) {
|
|
214
|
+
if (
|
|
215
|
+
!ctx.isFullMatch &&
|
|
216
|
+
!paramsChanged &&
|
|
217
|
+
ctx.clientSegmentSet.has(segment.id)
|
|
218
|
+
) {
|
|
219
|
+
segment.component = null;
|
|
220
|
+
segment.loading = undefined;
|
|
221
|
+
}
|
|
222
|
+
yield segment;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// Resolve loaders fresh (loaders are never pre-rendered/cached)
|
|
226
|
+
const ms = ctx.metricsStore;
|
|
227
|
+
const loaderStart = performance.now();
|
|
228
|
+
|
|
229
|
+
if (ctx.isFullMatch) {
|
|
230
|
+
if (resolveLoadersOnly) {
|
|
231
|
+
const loaderSegments = await ctx.Store.run(() =>
|
|
232
|
+
resolveLoadersOnly(ctx.entries, ctx.handlerContext),
|
|
233
|
+
);
|
|
234
|
+
state.matchedIds = state.cachedMatchedIds!;
|
|
235
|
+
for (const segment of loaderSegments) {
|
|
236
|
+
yield segment;
|
|
237
|
+
}
|
|
238
|
+
} else {
|
|
239
|
+
state.matchedIds = state.cachedMatchedIds!;
|
|
240
|
+
}
|
|
241
|
+
} else {
|
|
242
|
+
if (resolveLoadersOnlyWithRevalidation) {
|
|
243
|
+
const loaderResult = await ctx.Store.run(() =>
|
|
244
|
+
resolveLoadersOnlyWithRevalidation(
|
|
245
|
+
ctx.entries,
|
|
246
|
+
ctx.handlerContext,
|
|
247
|
+
ctx.clientSegmentSet,
|
|
248
|
+
ctx.prevParams,
|
|
249
|
+
ctx.request,
|
|
250
|
+
ctx.prevUrl,
|
|
251
|
+
ctx.url,
|
|
252
|
+
ctx.routeKey,
|
|
253
|
+
ctx.actionContext,
|
|
254
|
+
),
|
|
255
|
+
);
|
|
256
|
+
state.matchedIds = [
|
|
257
|
+
...state.cachedMatchedIds!,
|
|
258
|
+
...loaderResult.matchedIds,
|
|
259
|
+
];
|
|
260
|
+
for (const segment of loaderResult.segments) {
|
|
261
|
+
yield segment;
|
|
262
|
+
}
|
|
263
|
+
} else {
|
|
264
|
+
state.matchedIds = state.cachedMatchedIds!;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
if (ms) {
|
|
269
|
+
const loaderEnd = performance.now();
|
|
270
|
+
ms.metrics.push({
|
|
271
|
+
label: "pipeline:loader-resolve",
|
|
272
|
+
duration: loaderEnd - loaderStart,
|
|
273
|
+
startTime: loaderStart - ms.requestStart,
|
|
274
|
+
depth: 1,
|
|
275
|
+
});
|
|
276
|
+
ms.metrics.push({
|
|
277
|
+
label: "pipeline:cache-hit",
|
|
278
|
+
duration: loaderEnd - pipelineStart,
|
|
279
|
+
startTime: pipelineStart - ms.requestStart,
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
}
|
|
95
283
|
|
|
96
284
|
/**
|
|
97
285
|
* Async generator middleware type
|
|
98
286
|
*/
|
|
99
287
|
export type GeneratorMiddleware<T> = (
|
|
100
|
-
source: AsyncGenerator<T
|
|
288
|
+
source: AsyncGenerator<T>,
|
|
101
289
|
) => AsyncGenerator<T>;
|
|
102
290
|
|
|
103
291
|
/**
|
|
@@ -115,11 +303,21 @@ export type GeneratorMiddleware<T> = (
|
|
|
115
303
|
*/
|
|
116
304
|
export function withCacheLookup<TEnv>(
|
|
117
305
|
ctx: MatchContext<TEnv>,
|
|
118
|
-
state: MatchPipelineState
|
|
306
|
+
state: MatchPipelineState,
|
|
119
307
|
): GeneratorMiddleware<ResolvedSegment> {
|
|
120
308
|
return async function* (
|
|
121
|
-
source: AsyncGenerator<ResolvedSegment
|
|
309
|
+
source: AsyncGenerator<ResolvedSegment>,
|
|
122
310
|
): AsyncGenerator<ResolvedSegment> {
|
|
311
|
+
const pipelineStart = performance.now();
|
|
312
|
+
const ms = ctx.metricsStore;
|
|
313
|
+
|
|
314
|
+
// Eagerly capture the HandleStore before any async operations.
|
|
315
|
+
// In workerd/Cloudflare, dynamic imports and fetch() inside the pipeline
|
|
316
|
+
// can disrupt AsyncLocalStorage, causing getRequestContext() to return
|
|
317
|
+
// undefined afterward. Capturing the reference early ensures handle replay
|
|
318
|
+
// and handler handle-push work regardless of ALS state.
|
|
319
|
+
const handleStoreRef = _getRequestContext()?._handleStore;
|
|
320
|
+
|
|
123
321
|
const {
|
|
124
322
|
evaluateRevalidation,
|
|
125
323
|
buildEntryRevalidateMap,
|
|
@@ -127,10 +325,144 @@ export function withCacheLookup<TEnv>(
|
|
|
127
325
|
resolveLoadersOnly,
|
|
128
326
|
} = getRouterContext<TEnv>();
|
|
129
327
|
|
|
328
|
+
// Prerender lookup: check build-time cached data before runtime cache.
|
|
329
|
+
// Prerender data is available regardless of runtime cache configuration.
|
|
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) {
|
|
334
|
+
await ensurePrerenderDeps();
|
|
335
|
+
if (prerenderStoreInstance) {
|
|
336
|
+
const paramHash = _hashParams!(ctx.matched.params);
|
|
337
|
+
const isPassthroughPrerenderRoute = ctx.entries.some(
|
|
338
|
+
(entry) => entry.type === "route" && entry.isPassthrough === true,
|
|
339
|
+
);
|
|
340
|
+
|
|
341
|
+
if (ctx.isIntercept) {
|
|
342
|
+
// Intercept navigation: try intercept-specific prerender entry
|
|
343
|
+
const entry = await prerenderStoreInstance.get(
|
|
344
|
+
ctx.matched.routeKey,
|
|
345
|
+
paramHash + "/i",
|
|
346
|
+
{
|
|
347
|
+
pathname: ctx.pathname,
|
|
348
|
+
isPassthroughRoute: isPassthroughPrerenderRoute,
|
|
349
|
+
},
|
|
350
|
+
);
|
|
351
|
+
if (entry) {
|
|
352
|
+
yield* yieldFromStore(
|
|
353
|
+
entry,
|
|
354
|
+
ctx,
|
|
355
|
+
state,
|
|
356
|
+
pipelineStart,
|
|
357
|
+
handleStoreRef,
|
|
358
|
+
);
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
// No intercept prerender -- fall through to normal pipeline
|
|
362
|
+
// (skip non-intercept prerender to let intercept-resolution run)
|
|
363
|
+
} else {
|
|
364
|
+
// Normal navigation: existing behavior
|
|
365
|
+
const entry = await prerenderStoreInstance.get(
|
|
366
|
+
ctx.matched.routeKey,
|
|
367
|
+
paramHash,
|
|
368
|
+
{
|
|
369
|
+
pathname: ctx.pathname,
|
|
370
|
+
isPassthroughRoute: isPassthroughPrerenderRoute,
|
|
371
|
+
},
|
|
372
|
+
);
|
|
373
|
+
if (entry) {
|
|
374
|
+
yield* yieldFromStore(
|
|
375
|
+
entry,
|
|
376
|
+
ctx,
|
|
377
|
+
state,
|
|
378
|
+
pipelineStart,
|
|
379
|
+
handleStoreRef,
|
|
380
|
+
);
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
// Dev-mode static handler interception for non-Node.js runtimes.
|
|
388
|
+
// __PRERENDER_DEV_URL is set by the Vite plugin when the RSC environment
|
|
389
|
+
// lacks a Node.js module runner (e.g. workerd, Deno workers). In those
|
|
390
|
+
// runtimes, handlers that depend on Node APIs like node:fs can't run
|
|
391
|
+
// in-process. We redirect them to the /__rsc_prerender endpoint which
|
|
392
|
+
// resolves segments in a Node.js temp server, same as prerender routes.
|
|
393
|
+
// In Node.js dev mode this variable is undefined -- handlers run
|
|
394
|
+
// in-process where Node APIs work, so no interception is needed.
|
|
395
|
+
if (!ctx.isAction && !ctx.matched.pr && globalThis.__PRERENDER_DEV_URL) {
|
|
396
|
+
const hasStatic = ctx.entries.some(
|
|
397
|
+
(e) =>
|
|
398
|
+
(e.type === "layout" ||
|
|
399
|
+
e.type === "route" ||
|
|
400
|
+
e.type === "parallel") &&
|
|
401
|
+
e.isStaticPrerender,
|
|
402
|
+
);
|
|
403
|
+
if (hasStatic) {
|
|
404
|
+
await ensurePrerenderDeps();
|
|
405
|
+
if (prerenderStoreInstance) {
|
|
406
|
+
const paramHash = _hashParams!(ctx.matched.params);
|
|
407
|
+
const isPassthroughPrerenderRoute = ctx.entries.some(
|
|
408
|
+
(entry) => entry.type === "route" && entry.isPassthrough === true,
|
|
409
|
+
);
|
|
410
|
+
|
|
411
|
+
if (ctx.isIntercept) {
|
|
412
|
+
const entry = await prerenderStoreInstance.get(
|
|
413
|
+
ctx.matched.routeKey,
|
|
414
|
+
paramHash + "/i",
|
|
415
|
+
{
|
|
416
|
+
pathname: ctx.pathname,
|
|
417
|
+
isPassthroughRoute: isPassthroughPrerenderRoute,
|
|
418
|
+
},
|
|
419
|
+
);
|
|
420
|
+
if (entry) {
|
|
421
|
+
yield* yieldFromStore(
|
|
422
|
+
entry,
|
|
423
|
+
ctx,
|
|
424
|
+
state,
|
|
425
|
+
pipelineStart,
|
|
426
|
+
handleStoreRef,
|
|
427
|
+
);
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
// No intercept prerender -- fall through to normal pipeline
|
|
431
|
+
} else {
|
|
432
|
+
const entry = await prerenderStoreInstance.get(
|
|
433
|
+
ctx.matched.routeKey,
|
|
434
|
+
paramHash,
|
|
435
|
+
{
|
|
436
|
+
pathname: ctx.pathname,
|
|
437
|
+
isPassthroughRoute: isPassthroughPrerenderRoute,
|
|
438
|
+
},
|
|
439
|
+
);
|
|
440
|
+
if (entry) {
|
|
441
|
+
yield* yieldFromStore(
|
|
442
|
+
entry,
|
|
443
|
+
ctx,
|
|
444
|
+
state,
|
|
445
|
+
pipelineStart,
|
|
446
|
+
handleStoreRef,
|
|
447
|
+
);
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
|
|
130
455
|
// Skip cache during actions
|
|
131
456
|
if (ctx.isAction || !ctx.cacheScope?.enabled) {
|
|
132
457
|
// Cache miss - pass through to segment resolution
|
|
133
458
|
yield* source;
|
|
459
|
+
if (ms) {
|
|
460
|
+
ms.metrics.push({
|
|
461
|
+
label: "pipeline:cache-miss",
|
|
462
|
+
duration: performance.now() - pipelineStart,
|
|
463
|
+
startTime: pipelineStart - ms.requestStart,
|
|
464
|
+
});
|
|
465
|
+
}
|
|
134
466
|
return;
|
|
135
467
|
}
|
|
136
468
|
|
|
@@ -138,27 +470,54 @@ export function withCacheLookup<TEnv>(
|
|
|
138
470
|
const cacheResult = await ctx.cacheScope.lookupRoute(
|
|
139
471
|
ctx.pathname,
|
|
140
472
|
ctx.matched.params,
|
|
141
|
-
ctx.isIntercept
|
|
473
|
+
ctx.isIntercept,
|
|
142
474
|
);
|
|
143
475
|
|
|
144
476
|
if (!cacheResult) {
|
|
145
477
|
// Cache miss - pass through to segment resolution
|
|
146
478
|
yield* source;
|
|
479
|
+
if (ms) {
|
|
480
|
+
ms.metrics.push({
|
|
481
|
+
label: "pipeline:cache-miss",
|
|
482
|
+
duration: performance.now() - pipelineStart,
|
|
483
|
+
startTime: pipelineStart - ms.requestStart,
|
|
484
|
+
});
|
|
485
|
+
}
|
|
147
486
|
return;
|
|
148
487
|
}
|
|
149
488
|
|
|
150
489
|
// Cache HIT
|
|
151
490
|
state.cacheHit = true;
|
|
491
|
+
state.cacheSource = "runtime";
|
|
152
492
|
state.shouldRevalidate = cacheResult.shouldRevalidate;
|
|
153
493
|
state.cachedSegments = cacheResult.segments;
|
|
154
494
|
state.cachedMatchedIds = cacheResult.segments.map((s) => s.id);
|
|
155
495
|
|
|
156
|
-
// Apply revalidation to cached segments
|
|
157
|
-
|
|
496
|
+
// Apply revalidation to cached segments.
|
|
497
|
+
// For full matches or empty client segment sets, this map is unnecessary:
|
|
498
|
+
// we never run segment-level revalidation and can stream segments directly.
|
|
499
|
+
const canCheckSegmentRevalidation =
|
|
500
|
+
!ctx.isFullMatch &&
|
|
501
|
+
ctx.clientSegmentSet.size > 0 &&
|
|
502
|
+
!!buildEntryRevalidateMap;
|
|
503
|
+
const entryRevalidateMap = canCheckSegmentRevalidation
|
|
504
|
+
? buildEntryRevalidateMap(ctx.entries)
|
|
505
|
+
: undefined;
|
|
158
506
|
|
|
159
507
|
for (const segment of cacheResult.segments) {
|
|
160
508
|
// Skip segments client doesn't have - they need their component
|
|
161
509
|
if (!ctx.clientSegmentSet.has(segment.id)) {
|
|
510
|
+
if (isTraceActive()) {
|
|
511
|
+
pushRevalidationTraceEntry({
|
|
512
|
+
segmentId: segment.id,
|
|
513
|
+
segmentType: segment.type,
|
|
514
|
+
belongsToRoute: segment.belongsToRoute ?? false,
|
|
515
|
+
source: "cache-hit",
|
|
516
|
+
defaultShouldRevalidate: true,
|
|
517
|
+
finalShouldRevalidate: true,
|
|
518
|
+
reason: "new-segment",
|
|
519
|
+
});
|
|
520
|
+
}
|
|
162
521
|
yield segment;
|
|
163
522
|
continue;
|
|
164
523
|
}
|
|
@@ -171,8 +530,53 @@ export function withCacheLookup<TEnv>(
|
|
|
171
530
|
|
|
172
531
|
// Look up revalidation rules for this segment
|
|
173
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
|
+
|
|
174
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
|
+
}
|
|
175
568
|
// No revalidation rules, use default behavior (skip if client has)
|
|
569
|
+
if (isTraceActive()) {
|
|
570
|
+
pushRevalidationTraceEntry({
|
|
571
|
+
segmentId: segment.id,
|
|
572
|
+
segmentType: segment.type,
|
|
573
|
+
belongsToRoute: segment.belongsToRoute ?? false,
|
|
574
|
+
source: "cache-hit",
|
|
575
|
+
defaultShouldRevalidate: false,
|
|
576
|
+
finalShouldRevalidate: false,
|
|
577
|
+
reason: "cached-no-rules",
|
|
578
|
+
});
|
|
579
|
+
}
|
|
176
580
|
segment.component = null;
|
|
177
581
|
segment.loading = undefined;
|
|
178
582
|
yield segment;
|
|
@@ -194,8 +598,24 @@ export function withCacheLookup<TEnv>(
|
|
|
194
598
|
routeKey: ctx.routeKey,
|
|
195
599
|
context: ctx.handlerContext,
|
|
196
600
|
actionContext: ctx.actionContext,
|
|
601
|
+
stale: cacheResult.shouldRevalidate || undefined,
|
|
602
|
+
traceSource: "cache-hit",
|
|
197
603
|
});
|
|
198
604
|
|
|
605
|
+
const routerCtx = getRouterContext<TEnv>();
|
|
606
|
+
if (routerCtx.telemetry) {
|
|
607
|
+
const tSink = resolveSink(routerCtx.telemetry);
|
|
608
|
+
safeEmit(tSink, {
|
|
609
|
+
type: "revalidation.decision",
|
|
610
|
+
timestamp: performance.now(),
|
|
611
|
+
requestId: routerCtx.requestId,
|
|
612
|
+
segmentId: segment.id,
|
|
613
|
+
pathname: ctx.pathname,
|
|
614
|
+
routeKey: ctx.routeKey,
|
|
615
|
+
shouldRevalidate,
|
|
616
|
+
});
|
|
617
|
+
}
|
|
618
|
+
|
|
199
619
|
if (!shouldRevalidate) {
|
|
200
620
|
// Client has it, no revalidation needed
|
|
201
621
|
segment.component = null;
|
|
@@ -205,15 +625,25 @@ export function withCacheLookup<TEnv>(
|
|
|
205
625
|
yield segment;
|
|
206
626
|
}
|
|
207
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
|
+
|
|
208
637
|
// Resolve loaders fresh (loaders are NOT cached by default)
|
|
209
638
|
// This ensures fresh data even on cache hit
|
|
210
639
|
const Store = ctx.Store;
|
|
640
|
+
const loaderStart = performance.now();
|
|
211
641
|
|
|
212
642
|
if (ctx.isFullMatch) {
|
|
213
643
|
// Full match (document request) - simple loader resolution without revalidation
|
|
214
644
|
if (resolveLoadersOnly) {
|
|
215
645
|
const loaderSegments = await Store.run(() =>
|
|
216
|
-
resolveLoadersOnly(ctx.entries, ctx.handlerContext)
|
|
646
|
+
resolveLoadersOnly(ctx.entries, ctx.handlerContext),
|
|
217
647
|
);
|
|
218
648
|
|
|
219
649
|
// Update state - full match doesn't track matchedIds separately
|
|
@@ -239,8 +669,13 @@ export function withCacheLookup<TEnv>(
|
|
|
239
669
|
ctx.prevUrl,
|
|
240
670
|
ctx.url,
|
|
241
671
|
ctx.routeKey,
|
|
242
|
-
ctx.actionContext
|
|
243
|
-
|
|
672
|
+
ctx.actionContext,
|
|
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,
|
|
678
|
+
),
|
|
244
679
|
);
|
|
245
680
|
|
|
246
681
|
// Update state with fresh loader matchedIds
|
|
@@ -257,5 +692,19 @@ export function withCacheLookup<TEnv>(
|
|
|
257
692
|
state.matchedIds = state.cachedMatchedIds!;
|
|
258
693
|
}
|
|
259
694
|
}
|
|
695
|
+
if (ms) {
|
|
696
|
+
const loaderEnd = performance.now();
|
|
697
|
+
ms.metrics.push({
|
|
698
|
+
label: "pipeline:loader-resolve",
|
|
699
|
+
duration: loaderEnd - loaderStart,
|
|
700
|
+
startTime: loaderStart - ms.requestStart,
|
|
701
|
+
depth: 1,
|
|
702
|
+
});
|
|
703
|
+
ms.metrics.push({
|
|
704
|
+
label: "pipeline:cache-hit",
|
|
705
|
+
duration: loaderEnd - pipelineStart,
|
|
706
|
+
startTime: pipelineStart - ms.requestStart,
|
|
707
|
+
});
|
|
708
|
+
}
|
|
260
709
|
};
|
|
261
710
|
}
|