@rangojs/router 0.0.0-experimental.b9cb8739 → 0.0.0-experimental.bd6e11bc
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 +196 -43
- package/dist/bin/rango.js +277 -99
- package/dist/testing/vitest.js +48 -0
- package/dist/vite/index.js +2779 -1064
- package/dist/vite/index.js.bak +5448 -0
- package/dist/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
- package/package.json +57 -11
- package/skills/breadcrumbs/SKILL.md +3 -1
- package/skills/bundle-analysis/SKILL.md +159 -0
- package/skills/cache-guide/SKILL.md +243 -21
- package/skills/caching/SKILL.md +155 -6
- package/skills/composability/SKILL.md +27 -2
- package/skills/document-cache/SKILL.md +78 -55
- package/skills/handler-use/SKILL.md +364 -0
- package/skills/hooks/SKILL.md +229 -20
- package/skills/host-router/SKILL.md +45 -20
- package/skills/i18n/SKILL.md +276 -0
- package/skills/intercept/SKILL.md +46 -4
- package/skills/layout/SKILL.md +28 -7
- package/skills/links/SKILL.md +249 -17
- package/skills/loader/SKILL.md +273 -53
- package/skills/middleware/SKILL.md +49 -12
- package/skills/migrate-nextjs/SKILL.md +562 -0
- package/skills/migrate-react-router/SKILL.md +769 -0
- package/skills/mime-routes/SKILL.md +27 -0
- package/skills/observability/SKILL.md +137 -0
- package/skills/parallel/SKILL.md +197 -6
- package/skills/prerender/SKILL.md +123 -100
- package/skills/rango/SKILL.md +242 -22
- package/skills/react-compiler/SKILL.md +168 -0
- package/skills/response-routes/SKILL.md +66 -9
- package/skills/route/SKILL.md +88 -4
- package/skills/router-setup/SKILL.md +90 -5
- package/skills/server-actions/SKILL.md +751 -0
- package/skills/streams-and-websockets/SKILL.md +283 -0
- package/skills/testing/SKILL.md +716 -0
- package/skills/typesafety/SKILL.md +329 -27
- package/skills/use-cache/SKILL.md +34 -5
- package/skills/view-transitions/SKILL.md +294 -0
- package/src/__augment-tests__/augment.ts +81 -0
- package/src/__augment-tests__/augmented.check.ts +117 -0
- package/src/__internal.ts +1 -1
- package/src/browser/action-coordinator.ts +53 -36
- package/src/browser/app-shell.ts +52 -0
- package/src/browser/app-version.ts +14 -0
- package/src/browser/event-controller.ts +91 -70
- package/src/browser/history-state.ts +21 -0
- package/src/browser/index.ts +3 -3
- package/src/browser/navigation-bridge.ts +102 -16
- package/src/browser/navigation-client.ts +164 -59
- package/src/browser/navigation-store.ts +75 -17
- package/src/browser/navigation-transaction.ts +21 -37
- package/src/browser/partial-update.ts +139 -38
- package/src/browser/prefetch/cache.ts +175 -15
- package/src/browser/prefetch/fetch.ts +180 -33
- package/src/browser/prefetch/queue.ts +123 -20
- package/src/browser/prefetch/resource-ready.ts +77 -0
- package/src/browser/rango-state.ts +53 -13
- package/src/browser/react/Link.tsx +81 -9
- package/src/browser/react/NavigationProvider.tsx +110 -33
- package/src/browser/react/context.ts +7 -2
- package/src/browser/react/filter-segment-order.ts +51 -7
- package/src/browser/react/index.ts +3 -0
- package/src/browser/react/location-state-shared.ts +175 -4
- package/src/browser/react/location-state.ts +39 -13
- package/src/browser/react/use-handle.ts +23 -64
- package/src/browser/react/use-navigation.ts +22 -2
- package/src/browser/react/use-params.ts +20 -8
- package/src/browser/react/use-reverse.ts +106 -0
- package/src/browser/react/use-router.ts +43 -10
- package/src/browser/react/use-segments.ts +11 -8
- package/src/browser/response-adapter.ts +25 -0
- package/src/browser/rsc-router.tsx +191 -74
- package/src/browser/scroll-restoration.ts +41 -14
- package/src/browser/segment-reconciler.ts +36 -9
- package/src/browser/segment-structure-assert.ts +2 -2
- package/src/browser/server-action-bridge.ts +31 -36
- package/src/browser/types.ts +57 -5
- package/src/build/collect-fallback-refs.ts +107 -0
- package/src/build/generate-manifest.ts +65 -40
- package/src/build/generate-route-types.ts +5 -0
- package/src/build/index.ts +2 -0
- package/src/build/route-trie.ts +52 -25
- package/src/build/route-types/codegen.ts +4 -4
- package/src/build/route-types/include-resolution.ts +9 -2
- package/src/build/route-types/per-module-writer.ts +7 -4
- package/src/build/route-types/router-processing.ts +278 -88
- package/src/build/route-types/scan-filter.ts +9 -2
- package/src/build/route-types/source-scan.ts +118 -0
- package/src/build/runtime-discovery.ts +9 -20
- package/src/cache/cache-runtime.ts +15 -11
- package/src/cache/cache-scope.ts +76 -49
- package/src/cache/cf/cf-cache-store.ts +501 -18
- package/src/cache/cf/index.ts +5 -1
- package/src/cache/document-cache.ts +17 -7
- package/src/cache/index.ts +1 -0
- package/src/cache/taint.ts +55 -0
- package/src/client.rsc.tsx +3 -0
- package/src/client.tsx +94 -238
- package/src/context-var.ts +72 -2
- package/src/debug.ts +2 -2
- package/src/decode-loader-results.ts +36 -0
- package/src/errors.ts +30 -1
- package/src/handle.ts +65 -12
- package/src/host/index.ts +2 -2
- package/src/host/router.ts +129 -57
- package/src/host/types.ts +31 -2
- package/src/host/utils.ts +1 -1
- package/src/href-client.ts +140 -20
- package/src/index.rsc.ts +12 -5
- package/src/index.ts +61 -11
- package/src/loader-store.ts +500 -0
- package/src/loader.rsc.ts +2 -5
- package/src/loader.ts +3 -10
- package/src/missing-id-error.ts +68 -0
- package/src/outlet-context.ts +1 -1
- package/src/prerender/store.ts +5 -4
- package/src/prerender.ts +141 -80
- package/src/response-utils.ts +37 -0
- package/src/reverse.ts +65 -15
- package/src/route-content-wrapper.tsx +6 -28
- package/src/route-definition/dsl-helpers.ts +435 -260
- package/src/route-definition/helper-factories.ts +29 -139
- package/src/route-definition/helpers-types.ts +110 -34
- package/src/route-definition/index.ts +3 -0
- package/src/route-definition/redirect.ts +11 -3
- package/src/route-definition/resolve-handler-use.ts +155 -0
- package/src/route-definition/use-item-types.ts +32 -0
- package/src/route-map-builder.ts +7 -1
- package/src/route-types.ts +37 -41
- package/src/router/basename.ts +14 -0
- package/src/router/content-negotiation.ts +113 -1
- package/src/router/error-handling.ts +1 -1
- package/src/router/find-match.ts +4 -2
- package/src/router/handler-context.ts +77 -38
- package/src/router/intercept-resolution.ts +15 -22
- package/src/router/lazy-includes.ts +12 -9
- package/src/router/loader-resolution.ts +174 -22
- package/src/router/logging.ts +5 -2
- package/src/router/manifest.ts +31 -16
- package/src/router/match-api.ts +128 -192
- package/src/router/match-handlers.ts +63 -20
- package/src/router/match-middleware/background-revalidation.ts +30 -2
- package/src/router/match-middleware/cache-lookup.ts +136 -106
- package/src/router/match-middleware/cache-store.ts +54 -10
- package/src/router/match-middleware/intercept-resolution.ts +9 -7
- package/src/router/match-middleware/segment-resolution.ts +61 -5
- package/src/router/match-result.ts +125 -10
- package/src/router/metrics.ts +7 -2
- package/src/router/middleware-types.ts +21 -34
- package/src/router/middleware.ts +103 -90
- package/src/router/navigation-snapshot.ts +182 -0
- package/src/router/pattern-matching.ts +101 -17
- package/src/router/prerender-match.ts +110 -10
- package/src/router/preview-match.ts +32 -102
- package/src/router/request-classification.ts +286 -0
- package/src/router/revalidation.ts +58 -2
- package/src/router/route-snapshot.ts +245 -0
- package/src/router/router-context.ts +6 -1
- package/src/router/router-interfaces.ts +77 -28
- package/src/router/router-options.ts +76 -11
- package/src/router/router-registry.ts +2 -5
- package/src/router/segment-resolution/fresh.ts +223 -24
- package/src/router/segment-resolution/helpers.ts +29 -24
- package/src/router/segment-resolution/loader-cache.ts +1 -0
- package/src/router/segment-resolution/revalidation.ts +466 -285
- package/src/router/segment-resolution/view-transition-default.ts +36 -0
- package/src/router/segment-wrappers.ts +2 -0
- package/src/router/substitute-pattern-params.ts +56 -0
- package/src/router/telemetry.ts +99 -0
- package/src/router/trie-matching.ts +18 -13
- package/src/router/types.ts +9 -0
- package/src/router/url-params.ts +49 -0
- package/src/router.ts +91 -23
- package/src/rsc/handler-context.ts +2 -2
- package/src/rsc/handler.ts +440 -381
- package/src/rsc/helpers.ts +91 -43
- package/src/rsc/index.ts +1 -1
- package/src/rsc/loader-fetch.ts +23 -3
- package/src/rsc/manifest-init.ts +5 -1
- package/src/rsc/origin-guard.ts +28 -10
- package/src/rsc/progressive-enhancement.ts +18 -2
- package/src/rsc/response-route-handler.ts +46 -53
- package/src/rsc/rsc-rendering.ts +41 -48
- package/src/rsc/runtime-warnings.ts +9 -10
- package/src/rsc/server-action.ts +25 -37
- package/src/rsc/ssr-setup.ts +18 -2
- package/src/rsc/types.ts +17 -3
- package/src/search-params.ts +4 -4
- package/src/segment-content-promise.ts +67 -0
- package/src/segment-loader-promise.ts +122 -0
- package/src/segment-system.tsx +219 -67
- package/src/serialize.ts +243 -0
- package/src/server/context.ts +277 -61
- package/src/server/cookie-store.ts +28 -4
- package/src/server/handle-store.ts +19 -0
- package/src/server/loader-registry.ts +9 -8
- package/src/server/request-context.ts +204 -60
- package/src/ssr/index.tsx +9 -1
- package/src/static-handler.ts +19 -7
- package/src/testing/cache-status.ts +166 -0
- package/src/testing/collect-handle.ts +63 -0
- package/src/testing/dispatch.ts +440 -0
- package/src/testing/dom.entry.ts +22 -0
- package/src/testing/e2e/fixture.ts +154 -0
- package/src/testing/e2e/index.ts +149 -0
- package/src/testing/e2e/matchers.ts +51 -0
- package/src/testing/e2e/page-helpers.ts +272 -0
- package/src/testing/e2e/parity.ts +306 -0
- package/src/testing/e2e/server.ts +183 -0
- package/src/testing/flight-matchers.ts +104 -0
- package/src/testing/flight-runtime.d.ts +21 -0
- package/src/testing/flight.entry.ts +22 -0
- package/src/testing/flight.ts +182 -0
- package/src/testing/generated-routes.ts +223 -0
- package/src/testing/index.ts +106 -0
- package/src/testing/internal/context.ts +255 -0
- package/src/testing/render-route.tsx +565 -0
- package/src/testing/run-loader.ts +296 -0
- package/src/testing/run-middleware.ts +179 -0
- package/src/testing/vitest-stubs/cloudflare-email.ts +9 -0
- package/src/testing/vitest-stubs/cloudflare-workers.ts +21 -0
- package/src/testing/vitest-stubs/plugin-rsc.ts +16 -0
- package/src/testing/vitest-stubs/version.ts +5 -0
- package/src/testing/vitest.ts +183 -0
- package/src/types/cache-types.ts +4 -4
- package/src/types/global-namespace.ts +39 -26
- package/src/types/handler-context.ts +194 -72
- package/src/types/index.ts +1 -0
- package/src/types/loader-types.ts +41 -15
- package/src/types/request-scope.ts +126 -0
- package/src/types/route-entry.ts +19 -1
- package/src/types/segments.ts +37 -1
- package/src/urls/include-helper.ts +34 -67
- package/src/urls/index.ts +0 -3
- package/src/urls/path-helper-types.ts +50 -9
- package/src/urls/path-helper.ts +63 -63
- package/src/urls/pattern-types.ts +48 -19
- package/src/urls/response-types.ts +25 -22
- package/src/urls/type-extraction.ts +26 -116
- package/src/urls/urls-function.ts +1 -5
- package/src/use-loader.tsx +487 -44
- package/src/vite/debug.ts +185 -0
- package/src/vite/discovery/bundle-postprocess.ts +34 -37
- package/src/vite/discovery/discover-routers.ts +105 -51
- package/src/vite/discovery/discovery-errors.ts +194 -0
- package/src/vite/discovery/gate-state.ts +171 -0
- package/src/vite/discovery/prerender-collection.ts +188 -93
- package/src/vite/discovery/route-types-writer.ts +40 -84
- package/src/vite/discovery/self-gen-tracking.ts +27 -1
- package/src/vite/discovery/state.ts +46 -6
- package/src/vite/discovery/virtual-module-codegen.ts +13 -23
- package/src/vite/index.ts +6 -0
- package/src/vite/plugin-types.ts +111 -72
- package/src/vite/plugins/cjs-to-esm.ts +8 -7
- package/src/vite/plugins/client-ref-dedup.ts +16 -0
- package/src/vite/plugins/client-ref-hashing.ts +28 -5
- package/src/vite/plugins/cloudflare-protocol-loader-hook.d.mts +23 -0
- package/src/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
- package/src/vite/plugins/cloudflare-protocol-stub.ts +214 -0
- package/src/vite/plugins/expose-action-id.ts +55 -33
- package/src/vite/plugins/expose-id-utils.ts +24 -8
- package/src/vite/plugins/expose-ids/export-analysis.ts +100 -20
- package/src/vite/plugins/expose-ids/handler-transform.ts +12 -35
- package/src/vite/plugins/expose-ids/loader-transform.ts +3 -5
- package/src/vite/plugins/expose-ids/router-transform.ts +20 -3
- package/src/vite/plugins/expose-internal-ids.ts +544 -317
- package/src/vite/plugins/performance-tracks.ts +92 -0
- package/src/vite/plugins/refresh-cmd.ts +88 -26
- package/src/vite/plugins/use-cache-transform.ts +65 -50
- package/src/vite/plugins/version-injector.ts +39 -23
- package/src/vite/plugins/version-plugin.ts +72 -3
- package/src/vite/plugins/virtual-entries.ts +2 -2
- package/src/vite/rango.ts +265 -226
- package/src/vite/router-discovery.ts +920 -137
- package/src/vite/utils/ast-handler-extract.ts +15 -15
- package/src/vite/utils/banner.ts +4 -4
- package/src/vite/utils/bundle-analysis.ts +4 -2
- package/src/vite/utils/client-chunks.ts +190 -0
- package/src/vite/utils/forward-user-plugins.ts +193 -0
- package/src/vite/utils/manifest-utils.ts +21 -5
- package/src/vite/utils/package-resolution.ts +41 -1
- package/src/vite/utils/prerender-utils.ts +38 -5
- package/src/vite/utils/shared-utils.ts +109 -27
- package/src/browser/action-response-classifier.ts +0 -99
|
@@ -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
|
*
|
|
@@ -104,7 +143,6 @@ export function withSegmentResolution<TEnv>(
|
|
|
104
143
|
return async function* (
|
|
105
144
|
source: AsyncGenerator<ResolvedSegment>,
|
|
106
145
|
): AsyncGenerator<ResolvedSegment> {
|
|
107
|
-
const pipelineStart = performance.now();
|
|
108
146
|
const ms = ctx.metricsStore;
|
|
109
147
|
|
|
110
148
|
// IMPORTANT: Always iterate source first to give cache-lookup a chance
|
|
@@ -113,18 +151,27 @@ export function withSegmentResolution<TEnv>(
|
|
|
113
151
|
yield segment;
|
|
114
152
|
}
|
|
115
153
|
|
|
154
|
+
// Measure own work only (after source iteration completes)
|
|
155
|
+
const ownStart = performance.now();
|
|
156
|
+
|
|
116
157
|
// If cache hit, segments were already yielded by cache lookup
|
|
158
|
+
// (render barrier is resolved on the cache-hit path)
|
|
117
159
|
if (state.cacheHit) {
|
|
118
160
|
if (ms) {
|
|
119
161
|
ms.metrics.push({
|
|
120
162
|
label: "pipeline:segment-resolve",
|
|
121
|
-
duration: performance.now() -
|
|
122
|
-
startTime:
|
|
163
|
+
duration: performance.now() - ownStart,
|
|
164
|
+
startTime: ownStart - ms.requestStart,
|
|
123
165
|
});
|
|
124
166
|
}
|
|
125
167
|
return;
|
|
126
168
|
}
|
|
127
169
|
|
|
170
|
+
const reqCtx = _getRequestContext();
|
|
171
|
+
if (reqCtx && reqCtx._treeHasStreaming === undefined) {
|
|
172
|
+
reqCtx._treeHasStreaming = treeHasStreaming(ctx.entries);
|
|
173
|
+
}
|
|
174
|
+
|
|
128
175
|
const { resolveAllSegmentsWithRevalidation, resolveAllSegments } =
|
|
129
176
|
getRouterContext<TEnv>();
|
|
130
177
|
|
|
@@ -146,6 +193,10 @@ export function withSegmentResolution<TEnv>(
|
|
|
146
193
|
state.segments = segments;
|
|
147
194
|
state.matchedIds = segments.map((s: { id: string }) => s.id);
|
|
148
195
|
|
|
196
|
+
if (reqCtx) {
|
|
197
|
+
reqCtx._resolveRenderBarrier(segments);
|
|
198
|
+
}
|
|
199
|
+
|
|
149
200
|
// Yield all resolved segments
|
|
150
201
|
for (const segment of segments) {
|
|
151
202
|
yield segment;
|
|
@@ -168,6 +219,7 @@ export function withSegmentResolution<TEnv>(
|
|
|
168
219
|
ctx.interceptResult,
|
|
169
220
|
ctx.localRouteName,
|
|
170
221
|
ctx.pathname,
|
|
222
|
+
ctx.stale,
|
|
171
223
|
),
|
|
172
224
|
);
|
|
173
225
|
|
|
@@ -175,6 +227,10 @@ export function withSegmentResolution<TEnv>(
|
|
|
175
227
|
state.segments = result.segments;
|
|
176
228
|
state.matchedIds = result.matchedIds;
|
|
177
229
|
|
|
230
|
+
if (reqCtx) {
|
|
231
|
+
reqCtx._resolveRenderBarrier(result.segments);
|
|
232
|
+
}
|
|
233
|
+
|
|
178
234
|
// Yield all resolved segments
|
|
179
235
|
for (const segment of result.segments) {
|
|
180
236
|
yield segment;
|
|
@@ -184,8 +240,8 @@ export function withSegmentResolution<TEnv>(
|
|
|
184
240
|
if (ms) {
|
|
185
241
|
ms.metrics.push({
|
|
186
242
|
label: "pipeline:segment-resolve",
|
|
187
|
-
duration: performance.now() -
|
|
188
|
-
startTime:
|
|
243
|
+
duration: performance.now() - ownStart,
|
|
244
|
+
startTime: ownStart - ms.requestStart,
|
|
189
245
|
});
|
|
190
246
|
}
|
|
191
247
|
};
|
|
@@ -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
|
|
@@ -109,6 +110,7 @@
|
|
|
109
110
|
import type { MatchResult, ResolvedSegment } from "../types.js";
|
|
110
111
|
import type { MatchContext, MatchPipelineState } from "./match-context.js";
|
|
111
112
|
import { debugLog } from "./logging.js";
|
|
113
|
+
import { appendMetric } from "./metrics.js";
|
|
112
114
|
|
|
113
115
|
/**
|
|
114
116
|
* Collect all segments from an async generator
|
|
@@ -123,6 +125,76 @@ export async function collectSegments(
|
|
|
123
125
|
return segments;
|
|
124
126
|
}
|
|
125
127
|
|
|
128
|
+
/**
|
|
129
|
+
* Deduplicate inherited loader segments by loaderId.
|
|
130
|
+
*
|
|
131
|
+
* When a route has loaders and a child layout has parallel slots, the same
|
|
132
|
+
* loader is resolved twice: once for the route and once inherited into the
|
|
133
|
+
* layout (tagged with `_inherited`). The inherited copy is only needed when
|
|
134
|
+
* the route uses `loading()` — in that case, the loader data is inside a
|
|
135
|
+
* LoaderBoundary/Suspense that parallel slots can't reach through. Without
|
|
136
|
+
* loading(), useLoader() traverses parent contexts and finds the data.
|
|
137
|
+
*/
|
|
138
|
+
function deduplicateLoaderSegments(
|
|
139
|
+
segments: ResolvedSegment[],
|
|
140
|
+
logPrefix: string,
|
|
141
|
+
): { segments: ResolvedSegment[]; removedIds: Set<string> } {
|
|
142
|
+
// Single pass: original (non-inherited) loaderIds, all loaderIds grouped by
|
|
143
|
+
// namespace, and namespaces of segments that declare loading().
|
|
144
|
+
const originalLoaders = new Set<string>();
|
|
145
|
+
const loaderIdsByNamespace = new Map<string, string[]>();
|
|
146
|
+
const namespacesWithLoading = new Set<string>();
|
|
147
|
+
for (const s of segments) {
|
|
148
|
+
if (s.type === "loader" && s.loaderId) {
|
|
149
|
+
if (!s._inherited) originalLoaders.add(s.loaderId);
|
|
150
|
+
const ids = loaderIdsByNamespace.get(s.namespace);
|
|
151
|
+
if (ids) ids.push(s.loaderId);
|
|
152
|
+
else loaderIdsByNamespace.set(s.namespace, [s.loaderId]);
|
|
153
|
+
} else if (
|
|
154
|
+
s.type !== "loader" &&
|
|
155
|
+
s.loading !== undefined &&
|
|
156
|
+
s.loading !== false
|
|
157
|
+
) {
|
|
158
|
+
namespacesWithLoading.add(s.namespace);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// An inherited loader is needed when it shares a namespace with a
|
|
163
|
+
// loading-bearing segment (its data sits behind that LoaderBoundary).
|
|
164
|
+
const loadersWithLoading = new Set<string>();
|
|
165
|
+
for (const ns of namespacesWithLoading) {
|
|
166
|
+
for (const id of loaderIdsByNamespace.get(ns) ?? []) {
|
|
167
|
+
loadersWithLoading.add(id);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const result: ResolvedSegment[] = [];
|
|
172
|
+
const removedIds = new Set<string>();
|
|
173
|
+
|
|
174
|
+
for (const s of segments) {
|
|
175
|
+
if (
|
|
176
|
+
s.type === "loader" &&
|
|
177
|
+
s.loaderId &&
|
|
178
|
+
s._inherited &&
|
|
179
|
+
originalLoaders.has(s.loaderId) &&
|
|
180
|
+
!loadersWithLoading.has(s.loaderId)
|
|
181
|
+
) {
|
|
182
|
+
removedIds.add(s.id);
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
result.push(s);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (removedIds.size > 0) {
|
|
189
|
+
debugLog(
|
|
190
|
+
logPrefix,
|
|
191
|
+
`deduped ${removedIds.size} inherited loader segment(s)`,
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return { segments: result, removedIds };
|
|
196
|
+
}
|
|
197
|
+
|
|
126
198
|
/**
|
|
127
199
|
* Build the final MatchResult from collected segments and context
|
|
128
200
|
*/
|
|
@@ -167,13 +239,23 @@ export function buildMatchResult<TEnv>(
|
|
|
167
239
|
// Deduplicate allIds (defense-in-depth for partial match path)
|
|
168
240
|
allIds = [...new Set(allIds)];
|
|
169
241
|
|
|
170
|
-
// Filter out segments
|
|
171
|
-
//
|
|
242
|
+
// Filter out null-component segments only when the client already has
|
|
243
|
+
// them cached (revalidation skip). If the client doesn't have the segment,
|
|
244
|
+
// it must be included even with null component — it's structurally required
|
|
245
|
+
// as a parent node for child layouts/parallels to reconcile against.
|
|
246
|
+
// Loader segments are always included as they carry data.
|
|
247
|
+
const clientIdSet = new Set(ctx.clientSegmentIds);
|
|
172
248
|
segmentsToRender = allSegments.filter(
|
|
173
|
-
(s) =>
|
|
249
|
+
(s) =>
|
|
250
|
+
s.component !== null || s.type === "loader" || !clientIdSet.has(s.id),
|
|
174
251
|
);
|
|
175
252
|
}
|
|
176
253
|
|
|
254
|
+
const { segments: dedupedSegments, removedIds } = deduplicateLoaderSegments(
|
|
255
|
+
segmentsToRender,
|
|
256
|
+
logPrefix,
|
|
257
|
+
);
|
|
258
|
+
|
|
177
259
|
debugLog(logPrefix, "all segments", {
|
|
178
260
|
segments: allSegments.map((s) => ({
|
|
179
261
|
id: s.id,
|
|
@@ -182,13 +264,37 @@ export function buildMatchResult<TEnv>(
|
|
|
182
264
|
})),
|
|
183
265
|
});
|
|
184
266
|
debugLog(logPrefix, "segments to render", {
|
|
185
|
-
segmentIds:
|
|
267
|
+
segmentIds: dedupedSegments.map((s) => s.id),
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
// Remove deduped loader IDs from matched so the client doesn't treat
|
|
271
|
+
// them as missing segments and trigger a fallback refetch.
|
|
272
|
+
const matchedIds =
|
|
273
|
+
removedIds.size > 0 ? allIds.filter((id) => !removedIds.has(id)) : allIds;
|
|
274
|
+
|
|
275
|
+
// resolvedIds: every segment whose handler actually ran this request.
|
|
276
|
+
// For full-match every segment is fresh; for partial-match we filter by
|
|
277
|
+
// the internal `_handlerRan` flag set in revalidation.ts. Drives the
|
|
278
|
+
// client's handle-bucket cleanup — a slot that re-resolved and pushed
|
|
279
|
+
// nothing must have its previous handle data cleared, but `diff` won't
|
|
280
|
+
// carry it because the segment payload skips null-component cached
|
|
281
|
+
// segments to save bytes.
|
|
282
|
+
const resolvedIds = ctx.isFullMatch
|
|
283
|
+
? allSegments.map((s) => s.id)
|
|
284
|
+
: allSegments.filter((s) => s._handlerRan).map((s) => s.id);
|
|
285
|
+
|
|
286
|
+
// Strip internal-only fields from the segments going on the wire.
|
|
287
|
+
const cleanedSegments = dedupedSegments.map((s) => {
|
|
288
|
+
if (s._handlerRan === undefined) return s;
|
|
289
|
+
const { _handlerRan: _drop, ...rest } = s;
|
|
290
|
+
return rest as ResolvedSegment;
|
|
186
291
|
});
|
|
187
292
|
|
|
188
293
|
return {
|
|
189
|
-
segments:
|
|
190
|
-
matched:
|
|
191
|
-
diff:
|
|
294
|
+
segments: cleanedSegments,
|
|
295
|
+
matched: matchedIds,
|
|
296
|
+
diff: cleanedSegments.map((s) => s.id),
|
|
297
|
+
resolvedIds,
|
|
192
298
|
params: ctx.matched.params,
|
|
193
299
|
routeName: ctx.routeKey,
|
|
194
300
|
slots: Object.keys(state.slots).length > 0 ? state.slots : undefined,
|
|
@@ -210,10 +316,19 @@ export async function collectMatchResult<TEnv>(
|
|
|
210
316
|
): Promise<MatchResult> {
|
|
211
317
|
const allSegments = await collectSegments(pipeline);
|
|
212
318
|
|
|
319
|
+
const buildStart = performance.now();
|
|
320
|
+
|
|
213
321
|
// Update state with collected segments if not already set
|
|
214
322
|
if (state.segments.length === 0) {
|
|
215
323
|
state.segments = allSegments;
|
|
216
324
|
}
|
|
217
325
|
|
|
218
|
-
|
|
326
|
+
const result = buildMatchResult(allSegments, ctx, state);
|
|
327
|
+
appendMetric(
|
|
328
|
+
ctx.metricsStore,
|
|
329
|
+
"collect-result",
|
|
330
|
+
buildStart,
|
|
331
|
+
performance.now() - buildStart,
|
|
332
|
+
);
|
|
333
|
+
return result;
|
|
219
334
|
}
|
package/src/router/metrics.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Router Metrics Utilities
|
|
3
3
|
*
|
|
4
|
-
* Performance metrics collection and reporting for
|
|
4
|
+
* Performance metrics collection and reporting for Rango.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import type { MetricsStore, PerformanceMetric } from "../server/context";
|
|
@@ -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 {
|
|
@@ -14,6 +14,7 @@ import type {
|
|
|
14
14
|
import type { ScopedReverseFunction } from "../reverse.js";
|
|
15
15
|
import type { Theme } from "../theme/types.js";
|
|
16
16
|
import type { LocationStateEntry } from "../browser/react/location-state-shared.js";
|
|
17
|
+
import type { RequestScope } from "../types/request-scope.js";
|
|
17
18
|
|
|
18
19
|
/**
|
|
19
20
|
* Get variable function type
|
|
@@ -27,8 +28,12 @@ type GetVariableFn = {
|
|
|
27
28
|
* Set variable function type
|
|
28
29
|
*/
|
|
29
30
|
type SetVariableFn = {
|
|
30
|
-
<T>(contextVar: ContextVar<T>, value: T): void;
|
|
31
|
-
<K extends keyof DefaultVars>(
|
|
31
|
+
<T>(contextVar: ContextVar<T>, value: T, options?: { cache?: boolean }): void;
|
|
32
|
+
<K extends keyof DefaultVars>(
|
|
33
|
+
key: K,
|
|
34
|
+
value: DefaultVars[K],
|
|
35
|
+
options?: { cache?: boolean },
|
|
36
|
+
): void;
|
|
32
37
|
};
|
|
33
38
|
|
|
34
39
|
/**
|
|
@@ -48,33 +53,15 @@ export interface CookieOptions {
|
|
|
48
53
|
* Context passed to middleware
|
|
49
54
|
*
|
|
50
55
|
* @template TEnv - Environment type (bindings, variables) - defaults to any for internal flexibility
|
|
51
|
-
* @template TParams - URL params type (typed for route middleware,
|
|
56
|
+
* @template TParams - URL params type (typed for route middleware,
|
|
57
|
+
* `Record<string, string | undefined>` for global middleware — absent
|
|
58
|
+
* optional segments are omitted from the params record at runtime, so
|
|
59
|
+
* the index signature must include `undefined`)
|
|
52
60
|
*/
|
|
53
61
|
export interface MiddlewareContext<
|
|
54
62
|
TEnv = any,
|
|
55
|
-
TParams = Record<string, string>,
|
|
56
|
-
> {
|
|
57
|
-
/** Original request */
|
|
58
|
-
request: Request;
|
|
59
|
-
|
|
60
|
-
/** Parsed URL (with internal `_rsc*` params stripped) */
|
|
61
|
-
url: URL;
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* The original request URL with all parameters intact, including
|
|
65
|
-
* internal `_rsc*` transport params.
|
|
66
|
-
*/
|
|
67
|
-
originalUrl: URL;
|
|
68
|
-
|
|
69
|
-
/** URL pathname */
|
|
70
|
-
pathname: string;
|
|
71
|
-
|
|
72
|
-
/** URL search params */
|
|
73
|
-
searchParams: URLSearchParams;
|
|
74
|
-
|
|
75
|
-
/** Platform bindings (Cloudflare, etc.) */
|
|
76
|
-
env: TEnv;
|
|
77
|
-
|
|
63
|
+
TParams = Record<string, string | undefined>,
|
|
64
|
+
> extends RequestScope<TEnv> {
|
|
78
65
|
/** URL params extracted from route/middleware pattern */
|
|
79
66
|
params: TParams;
|
|
80
67
|
|
|
@@ -91,12 +78,6 @@ export interface MiddlewareContext<
|
|
|
91
78
|
/** Set a context variable (shared with route handlers) */
|
|
92
79
|
set: SetVariableFn;
|
|
93
80
|
|
|
94
|
-
/**
|
|
95
|
-
* Middleware-injected variables.
|
|
96
|
-
* Same shared dictionary as `ctx.get()`/`ctx.set()`.
|
|
97
|
-
*/
|
|
98
|
-
var: DefaultVars;
|
|
99
|
-
|
|
100
81
|
/**
|
|
101
82
|
* Set a response header - can be called before or after `next()`.
|
|
102
83
|
*
|
|
@@ -159,7 +140,7 @@ export interface MiddlewareContext<
|
|
|
159
140
|
* @template TEnv - Environment type - defaults to any for internal flexibility
|
|
160
141
|
* @template TParams - URL params type (typed for route middleware)
|
|
161
142
|
*
|
|
162
|
-
* When using middleware with global augmentation (
|
|
143
|
+
* When using middleware with global augmentation (Rango.Env), explicitly
|
|
163
144
|
* annotate your middleware functions, or the types will be inferred from context:
|
|
164
145
|
*
|
|
165
146
|
* @example
|
|
@@ -171,7 +152,10 @@ export interface MiddlewareContext<
|
|
|
171
152
|
* router.use((ctx, next) => {...}) // ctx is typed from router's TEnv
|
|
172
153
|
* ```
|
|
173
154
|
*/
|
|
174
|
-
export type MiddlewareFn<
|
|
155
|
+
export type MiddlewareFn<
|
|
156
|
+
TEnv = any,
|
|
157
|
+
TParams = Record<string, string | undefined>,
|
|
158
|
+
> = (
|
|
175
159
|
ctx: MiddlewareContext<TEnv, TParams>,
|
|
176
160
|
next: () => Promise<Response>,
|
|
177
161
|
) => Response | void | Promise<Response | void>;
|
|
@@ -218,5 +202,8 @@ export interface MiddlewareCollectableEntry {
|
|
|
218
202
|
*/
|
|
219
203
|
export interface CollectedMiddleware {
|
|
220
204
|
handler: MiddlewareFn<any, any>;
|
|
205
|
+
// Internal shape only. The user-facing `MiddlewareContext.params` is
|
|
206
|
+
// typed `Record<string, string | undefined>` to reflect that absent
|
|
207
|
+
// optional segments are omitted from the params record at runtime.
|
|
221
208
|
params: Record<string, string>;
|
|
222
209
|
}
|