@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
package/src/router/middleware.ts
CHANGED
|
@@ -10,6 +10,8 @@
|
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
12
|
import { contextGet, contextSet } from "../context-var.js";
|
|
13
|
+
import { safeDecodeURIComponent } from "./url-params.js";
|
|
14
|
+
import { fireAndForgetWaitUntil } from "../types/request-scope.js";
|
|
13
15
|
import type {
|
|
14
16
|
CollectedMiddleware,
|
|
15
17
|
MiddlewareCollectableEntry,
|
|
@@ -21,6 +23,8 @@ import type {
|
|
|
21
23
|
import { _getRequestContext } from "../server/request-context.js";
|
|
22
24
|
import { isAutoGeneratedRouteName } from "../route-name.js";
|
|
23
25
|
import { appendMetric, createMetricsStore } from "./metrics.js";
|
|
26
|
+
import { stripInternalParams } from "./handler-context.js";
|
|
27
|
+
import { isWebSocketUpgradeResponse } from "../response-utils.js";
|
|
24
28
|
|
|
25
29
|
// Re-export types and cookie utilities for backward compatibility
|
|
26
30
|
export type {
|
|
@@ -111,7 +115,12 @@ function escapeRegex(str: string): string {
|
|
|
111
115
|
}
|
|
112
116
|
|
|
113
117
|
/**
|
|
114
|
-
* Extract params from a pathname using a pattern's regex and param names
|
|
118
|
+
* Extract params from a pathname using a pattern's regex and param names.
|
|
119
|
+
*
|
|
120
|
+
* Values are URL-decoded so apps see the raw string (e.g. "ivo@example.com")
|
|
121
|
+
* instead of the percent-encoded form ("ivo%40example.com"). This matches the
|
|
122
|
+
* contract assumed by ctx.reverse (which re-encodes) and aligns with
|
|
123
|
+
* Express/React Router/Fastify/Koa.
|
|
115
124
|
*/
|
|
116
125
|
export function extractParams(
|
|
117
126
|
pathname: string,
|
|
@@ -123,7 +132,7 @@ export function extractParams(
|
|
|
123
132
|
|
|
124
133
|
const params: Record<string, string> = {};
|
|
125
134
|
for (let i = 0; i < paramNames.length; i++) {
|
|
126
|
-
params[paramNames[i]] = match[i + 1] || "";
|
|
135
|
+
params[paramNames[i]] = safeDecodeURIComponent(match[i + 1] || "");
|
|
127
136
|
}
|
|
128
137
|
return params;
|
|
129
138
|
}
|
|
@@ -147,7 +156,7 @@ export function createMiddlewareContext<TEnv>(
|
|
|
147
156
|
search?: Record<string, unknown>,
|
|
148
157
|
) => string,
|
|
149
158
|
): MiddlewareContext<TEnv> {
|
|
150
|
-
const url = new URL(request.url);
|
|
159
|
+
const url = stripInternalParams(new URL(request.url));
|
|
151
160
|
|
|
152
161
|
// Track the initial response to detect pre/post-next() phase.
|
|
153
162
|
// Before next(): responseHolder.response === initialResponse (the stub).
|
|
@@ -178,14 +187,22 @@ export function createMiddlewareContext<TEnv>(
|
|
|
178
187
|
return responseHolder.response;
|
|
179
188
|
};
|
|
180
189
|
|
|
190
|
+
// Capture reqCtx once: the request-scoped platform fields
|
|
191
|
+
// (originalUrl, executionContext, waitUntil) are immutable per request,
|
|
192
|
+
// so snapshotting beats re-reading ALS on every access. The lazy getters
|
|
193
|
+
// below (routeName, theme, setTheme) stay lazy because those can change
|
|
194
|
+
// during `await next()`.
|
|
195
|
+
const reqCtx = _getRequestContext();
|
|
181
196
|
return {
|
|
182
197
|
request,
|
|
183
198
|
url,
|
|
184
|
-
originalUrl: new URL(request.url),
|
|
199
|
+
originalUrl: reqCtx?.originalUrl ?? new URL(request.url),
|
|
185
200
|
pathname: url.pathname,
|
|
186
201
|
searchParams: url.searchParams,
|
|
187
202
|
env: env as MiddlewareContext<TEnv>["env"],
|
|
188
203
|
params,
|
|
204
|
+
executionContext: reqCtx?.executionContext,
|
|
205
|
+
waitUntil: reqCtx ? reqCtx.waitUntil.bind(reqCtx) : fireAndForgetWaitUntil,
|
|
189
206
|
// Getter: re-derives from request context on each access so that global
|
|
190
207
|
// middleware sees the matched route name after await next().
|
|
191
208
|
get routeName(): MiddlewareContext<TEnv>["routeName"] {
|
|
@@ -203,12 +220,9 @@ export function createMiddlewareContext<TEnv>(
|
|
|
203
220
|
get: ((keyOrVar: any) =>
|
|
204
221
|
contextGet(variables, keyOrVar)) as MiddlewareContext<TEnv>["get"],
|
|
205
222
|
|
|
206
|
-
set: ((keyOrVar: any, value: unknown) => {
|
|
207
|
-
contextSet(variables, keyOrVar, value);
|
|
223
|
+
set: ((keyOrVar: any, value: unknown, options?: any) => {
|
|
224
|
+
contextSet(variables, keyOrVar, value, options);
|
|
208
225
|
}) as MiddlewareContext<TEnv>["set"],
|
|
209
|
-
|
|
210
|
-
var: variables as MiddlewareContext<TEnv>["var"],
|
|
211
|
-
|
|
212
226
|
header(name: string, value: string): void {
|
|
213
227
|
// Before next(): delegate to shared RequestContext stub
|
|
214
228
|
if (isPreNext()) {
|
|
@@ -293,6 +307,46 @@ export function matchMiddleware<TEnv>(
|
|
|
293
307
|
return matches;
|
|
294
308
|
}
|
|
295
309
|
|
|
310
|
+
// Set-Cookie is appended; for other headers stubOverridesNonCookie=true
|
|
311
|
+
// overwrites (chain ran to completion), false fills only missing slots (an
|
|
312
|
+
// explicit short-circuit Response's own headers win).
|
|
313
|
+
function mergeStubHeaders(
|
|
314
|
+
target: Headers,
|
|
315
|
+
stub: Headers,
|
|
316
|
+
stubOverridesNonCookie: boolean,
|
|
317
|
+
): void {
|
|
318
|
+
stub.forEach((value, name) => {
|
|
319
|
+
if (name.toLowerCase() === "set-cookie") {
|
|
320
|
+
target.append(name, value);
|
|
321
|
+
} else if (stubOverridesNonCookie || !target.has(name)) {
|
|
322
|
+
target.set(name, value);
|
|
323
|
+
}
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
// Set-Cookie is deduped so a nested inner executeMiddleware that already merged
|
|
328
|
+
// the same reqCtx cookies does not duplicate them; other headers fill if missing.
|
|
329
|
+
function mergeReqCtxStub(
|
|
330
|
+
target: Headers,
|
|
331
|
+
reqCtx: ReturnType<typeof _getRequestContext>,
|
|
332
|
+
): void {
|
|
333
|
+
if (!reqCtx) return;
|
|
334
|
+
const stubCookies = reqCtx.res.headers.getSetCookie();
|
|
335
|
+
if (stubCookies.length > 0) {
|
|
336
|
+
const existing = new Set(target.getSetCookie());
|
|
337
|
+
for (const cookie of stubCookies) {
|
|
338
|
+
if (!existing.has(cookie)) {
|
|
339
|
+
target.append("set-cookie", cookie);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
reqCtx.res.headers.forEach((value, name) => {
|
|
344
|
+
if (name !== "set-cookie" && !target.has(name)) {
|
|
345
|
+
target.set(name, value);
|
|
346
|
+
}
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
|
|
296
350
|
/**
|
|
297
351
|
* Execute middleware chain
|
|
298
352
|
*
|
|
@@ -331,35 +385,13 @@ export async function executeMiddleware<TEnv>(
|
|
|
331
385
|
// End of chain - call actual RSC handler
|
|
332
386
|
const response = await finalHandler();
|
|
333
387
|
|
|
334
|
-
// Merge headers set on stub into the real response.
|
|
335
|
-
// Use append for Set-Cookie to preserve multiple cookies.
|
|
336
388
|
const mergedHeaders = new Headers(response.headers);
|
|
337
|
-
stubResponse.headers
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
});
|
|
344
|
-
// Also merge shared RequestContext stub (cookies written via cookies().set()).
|
|
345
|
-
// Dedup Set-Cookie: an inner executeMiddleware (route-level middleware)
|
|
346
|
-
// may have already merged the same reqCtx cookies into the response.
|
|
347
|
-
const reqCtx = _getRequestContext();
|
|
348
|
-
if (reqCtx) {
|
|
349
|
-
const stubCookies = reqCtx.res.headers.getSetCookie();
|
|
350
|
-
if (stubCookies.length > 0) {
|
|
351
|
-
const existing = new Set(mergedHeaders.getSetCookie());
|
|
352
|
-
for (const cookie of stubCookies) {
|
|
353
|
-
if (!existing.has(cookie)) {
|
|
354
|
-
mergedHeaders.append("set-cookie", cookie);
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
reqCtx.res.headers.forEach((value, name) => {
|
|
359
|
-
if (name !== "set-cookie" && !mergedHeaders.has(name)) {
|
|
360
|
-
mergedHeaders.set(name, value);
|
|
361
|
-
}
|
|
362
|
-
});
|
|
389
|
+
mergeStubHeaders(mergedHeaders, stubResponse.headers, true);
|
|
390
|
+
mergeReqCtxStub(mergedHeaders, _getRequestContext());
|
|
391
|
+
|
|
392
|
+
if (isWebSocketUpgradeResponse(response)) {
|
|
393
|
+
responseHolder.response = response;
|
|
394
|
+
return response;
|
|
363
395
|
}
|
|
364
396
|
|
|
365
397
|
// Clone response with merged headers (mutable for post-next() modifications)
|
|
@@ -428,8 +460,16 @@ export async function executeMiddleware<TEnv>(
|
|
|
428
460
|
try {
|
|
429
461
|
result = await entry.handler(ctx, wrappedNext);
|
|
430
462
|
} catch (error) {
|
|
431
|
-
|
|
432
|
-
|
|
463
|
+
// Thrown Response is short-circuit control flow, not an error.
|
|
464
|
+
// Fall through to the `if (result instanceof Response)` branch below
|
|
465
|
+
// so stub headers and request-context cookies merge as they do for
|
|
466
|
+
// an explicit `return new Response(...)`. Real errors propagate.
|
|
467
|
+
if (error instanceof Response) {
|
|
468
|
+
result = error;
|
|
469
|
+
} else {
|
|
470
|
+
finishMiddleware();
|
|
471
|
+
throw error;
|
|
472
|
+
}
|
|
433
473
|
}
|
|
434
474
|
finishMiddleware();
|
|
435
475
|
|
|
@@ -453,34 +493,13 @@ export async function executeMiddleware<TEnv>(
|
|
|
453
493
|
// RequestContext stub headers (from ctx.setCookie) into the
|
|
454
494
|
// returned Response so they are not lost.
|
|
455
495
|
if (result instanceof Response) {
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
mergedHeaders.append(name, value);
|
|
460
|
-
} else if (!mergedHeaders.has(name)) {
|
|
461
|
-
mergedHeaders.set(name, value);
|
|
462
|
-
}
|
|
463
|
-
});
|
|
464
|
-
// Also merge shared RequestContext stub (cookies written via setCookie).
|
|
465
|
-
// Dedup Set-Cookie: an inner executeMiddleware (route-level middleware)
|
|
466
|
-
// may have already merged the same reqCtx cookies into the response.
|
|
467
|
-
const reqCtx = _getRequestContext();
|
|
468
|
-
if (reqCtx) {
|
|
469
|
-
const stubCookies = reqCtx.res.headers.getSetCookie();
|
|
470
|
-
if (stubCookies.length > 0) {
|
|
471
|
-
const existing = new Set(mergedHeaders.getSetCookie());
|
|
472
|
-
for (const cookie of stubCookies) {
|
|
473
|
-
if (!existing.has(cookie)) {
|
|
474
|
-
mergedHeaders.append("set-cookie", cookie);
|
|
475
|
-
}
|
|
476
|
-
}
|
|
477
|
-
}
|
|
478
|
-
reqCtx.res.headers.forEach((value, name) => {
|
|
479
|
-
if (name !== "set-cookie" && !mergedHeaders.has(name)) {
|
|
480
|
-
mergedHeaders.set(name, value);
|
|
481
|
-
}
|
|
482
|
-
});
|
|
496
|
+
if (isWebSocketUpgradeResponse(result)) {
|
|
497
|
+
responseHolder.response = result;
|
|
498
|
+
return result;
|
|
483
499
|
}
|
|
500
|
+
const mergedHeaders = new Headers(result.headers);
|
|
501
|
+
mergeStubHeaders(mergedHeaders, stubResponse.headers, false);
|
|
502
|
+
mergeReqCtxStub(mergedHeaders, _getRequestContext());
|
|
484
503
|
const merged = new Response(result.body, {
|
|
485
504
|
status: result.status,
|
|
486
505
|
statusText: result.statusText,
|
|
@@ -529,23 +548,12 @@ export async function executeMiddleware<TEnv>(
|
|
|
529
548
|
// last merge point (e.g. cookies().set() called after await next()).
|
|
530
549
|
// The reqCtx stub may have already been partially merged during finalHandler
|
|
531
550
|
// or early-return paths; only append *new* Set-Cookie entries to avoid dupes.
|
|
551
|
+
//
|
|
552
|
+
// Skip for upgrade responses: upgrade headers are semantically immutable and
|
|
553
|
+
// set-cookie on an upgrade is not meaningful.
|
|
532
554
|
const reqCtx = _getRequestContext();
|
|
533
|
-
if (reqCtx) {
|
|
534
|
-
|
|
535
|
-
if (stubCookies.length > 0) {
|
|
536
|
-
const existingCookies = new Set(finalResponse.headers.getSetCookie());
|
|
537
|
-
for (const cookie of stubCookies) {
|
|
538
|
-
if (!existingCookies.has(cookie)) {
|
|
539
|
-
finalResponse.headers.append("set-cookie", cookie);
|
|
540
|
-
}
|
|
541
|
-
}
|
|
542
|
-
}
|
|
543
|
-
// Fill in non-cookie headers that aren't already on the response
|
|
544
|
-
reqCtx.res.headers.forEach((value, name) => {
|
|
545
|
-
if (name !== "set-cookie" && !finalResponse.headers.has(name)) {
|
|
546
|
-
finalResponse.headers.set(name, value);
|
|
547
|
-
}
|
|
548
|
-
});
|
|
555
|
+
if (reqCtx && !isWebSocketUpgradeResponse(finalResponse)) {
|
|
556
|
+
mergeReqCtxStub(finalResponse.headers, reqCtx);
|
|
549
557
|
}
|
|
550
558
|
|
|
551
559
|
return finalResponse;
|
|
@@ -615,7 +623,18 @@ export async function executeInterceptMiddleware<TEnv>(
|
|
|
615
623
|
return next();
|
|
616
624
|
};
|
|
617
625
|
|
|
618
|
-
|
|
626
|
+
let result: Response | void;
|
|
627
|
+
try {
|
|
628
|
+
result = await middleware(ctx, guardedNext);
|
|
629
|
+
} catch (error) {
|
|
630
|
+
// Thrown Response is short-circuit control flow, parity with the
|
|
631
|
+
// explicit-return path below. Real errors propagate.
|
|
632
|
+
if (error instanceof Response) {
|
|
633
|
+
result = error;
|
|
634
|
+
} else {
|
|
635
|
+
throw error;
|
|
636
|
+
}
|
|
637
|
+
}
|
|
619
638
|
|
|
620
639
|
if (result instanceof Response) {
|
|
621
640
|
earlyResponse = result;
|
|
@@ -643,13 +662,7 @@ export async function executeInterceptMiddleware<TEnv>(
|
|
|
643
662
|
// Only fill in missing headers — the returned Response's explicit
|
|
644
663
|
// headers take precedence, matching executeMiddleware behavior.
|
|
645
664
|
const mergedHeaders = new Headers(response.headers);
|
|
646
|
-
stubResponse.headers
|
|
647
|
-
if (name.toLowerCase() === "set-cookie") {
|
|
648
|
-
mergedHeaders.append(name, value);
|
|
649
|
-
} else if (!mergedHeaders.has(name)) {
|
|
650
|
-
mergedHeaders.set(name, value);
|
|
651
|
-
}
|
|
652
|
-
});
|
|
665
|
+
mergeStubHeaders(mergedHeaders, stubResponse.headers, false);
|
|
653
666
|
return new Response(response.body, {
|
|
654
667
|
status: response.status,
|
|
655
668
|
statusText: response.statusText,
|
|
@@ -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
|
+
}
|
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
import type { RouteEntry, TrailingSlashMode } from "../types";
|
|
8
8
|
import type { EntryData } from "../server/context";
|
|
9
9
|
import { debugLog, isRouterDebugEnabled } from "./logging.js";
|
|
10
|
+
import { safeDecodeURIComponent } from "./url-params.js";
|
|
10
11
|
|
|
11
12
|
/**
|
|
12
13
|
* Parsed segment info
|
|
@@ -82,6 +83,13 @@ export interface CompiledPattern {
|
|
|
82
83
|
paramNames: string[];
|
|
83
84
|
optionalParams: Set<string>;
|
|
84
85
|
hasTrailingSlash: boolean;
|
|
86
|
+
/**
|
|
87
|
+
* Param-name → allowed values for constrained params (e.g. `:lang(en|gb)`).
|
|
88
|
+
* Validated against the **decoded** param value after regex extraction so
|
|
89
|
+
* a URL like `/en%20GB` still matches `:lang(en GB)` — matching the trie
|
|
90
|
+
* path's behavior (trie-matching.ts:validateAndBuild).
|
|
91
|
+
*/
|
|
92
|
+
constraints?: Record<string, string[]>;
|
|
85
93
|
}
|
|
86
94
|
|
|
87
95
|
// Module-level cache for compiled patterns. Route patterns are a finite set
|
|
@@ -142,6 +150,7 @@ export function compilePattern(pattern: string): CompiledPattern {
|
|
|
142
150
|
const segments = parsePattern(normalizedPattern);
|
|
143
151
|
const paramNames: string[] = [];
|
|
144
152
|
const optionalParams = new Set<string>();
|
|
153
|
+
let constraints: Record<string, string[]> | undefined;
|
|
145
154
|
|
|
146
155
|
let regexPattern = "";
|
|
147
156
|
|
|
@@ -152,11 +161,14 @@ export function compilePattern(pattern: string): CompiledPattern {
|
|
|
152
161
|
} else if (segment.type === "param") {
|
|
153
162
|
paramNames.push(segment.value);
|
|
154
163
|
const suffixPattern = segment.suffix ? escapeRegex(segment.suffix) : "";
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
164
|
+
// Constrained params capture anything here; the allowed values are
|
|
165
|
+
// checked post-decode in findMatch so URL-encoded constraint values
|
|
166
|
+
// (e.g. `:lang(en GB)` via `/en%20GB`) still match.
|
|
167
|
+
const valuePattern = segment.suffix ? "([^/]+?)" : "([^/]+)";
|
|
168
|
+
|
|
169
|
+
if (segment.constraint) {
|
|
170
|
+
(constraints ??= {})[segment.value] = segment.constraint;
|
|
171
|
+
}
|
|
160
172
|
|
|
161
173
|
if (segment.optional) {
|
|
162
174
|
optionalParams.add(segment.value);
|
|
@@ -176,6 +188,20 @@ export function compilePattern(pattern: string): CompiledPattern {
|
|
|
176
188
|
regexPattern = "/";
|
|
177
189
|
}
|
|
178
190
|
|
|
191
|
+
// Patterns of only optional segments (e.g. `/:locale?`, `/:a?/:b?`) need
|
|
192
|
+
// an explicit `/` alternative so a bare `/` matches the absent form. The
|
|
193
|
+
// optional template `(?:/X)?` matches `/X` or empty string, but pathnames
|
|
194
|
+
// are never empty. Arises from `include("/:locale?", routes)` + inner
|
|
195
|
+
// `path("/")`. Skip when an explicit trailing slash already anchors the
|
|
196
|
+
// match.
|
|
197
|
+
const hasOnlyOptionalSegments =
|
|
198
|
+
!hasTrailingSlash &&
|
|
199
|
+
segments.length > 0 &&
|
|
200
|
+
segments.every((segment) => segment.type === "param" && segment.optional);
|
|
201
|
+
if (hasOnlyOptionalSegments) {
|
|
202
|
+
regexPattern = `(?:/|${regexPattern})`;
|
|
203
|
+
}
|
|
204
|
+
|
|
179
205
|
// Add trailing slash to regex if pattern has one
|
|
180
206
|
if (hasTrailingSlash) {
|
|
181
207
|
regexPattern += "/";
|
|
@@ -186,9 +212,35 @@ export function compilePattern(pattern: string): CompiledPattern {
|
|
|
186
212
|
paramNames,
|
|
187
213
|
optionalParams,
|
|
188
214
|
hasTrailingSlash,
|
|
215
|
+
...(constraints ? { constraints } : {}),
|
|
189
216
|
};
|
|
190
217
|
}
|
|
191
218
|
|
|
219
|
+
/**
|
|
220
|
+
* Validate decoded params against a compiled pattern's constraints.
|
|
221
|
+
* Returns false if any constrained param has a non-empty value not in the
|
|
222
|
+
* allowed list. Absent optionals (key missing or `undefined`) are allowed;
|
|
223
|
+
* `""` is also tolerated as "absent" so user-provided params or fixtures
|
|
224
|
+
* that pass empty strings explicitly behave the same way.
|
|
225
|
+
*/
|
|
226
|
+
function satisfiesConstraints(
|
|
227
|
+
params: Record<string, string>,
|
|
228
|
+
constraints: Record<string, string[]> | undefined,
|
|
229
|
+
): boolean {
|
|
230
|
+
if (!constraints) return true;
|
|
231
|
+
for (const name in constraints) {
|
|
232
|
+
const value = params[name];
|
|
233
|
+
if (
|
|
234
|
+
value !== undefined &&
|
|
235
|
+
value !== "" &&
|
|
236
|
+
!constraints[name].includes(value)
|
|
237
|
+
) {
|
|
238
|
+
return false;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return true;
|
|
242
|
+
}
|
|
243
|
+
|
|
192
244
|
/**
|
|
193
245
|
* Escape special regex characters in a string
|
|
194
246
|
*/
|
|
@@ -196,6 +248,27 @@ function escapeRegex(str: string): string {
|
|
|
196
248
|
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
197
249
|
}
|
|
198
250
|
|
|
251
|
+
/**
|
|
252
|
+
* Build the named-params record from a regex match. Optional segments that
|
|
253
|
+
* didn't capture leave the corresponding group `undefined`; we skip those
|
|
254
|
+
* keys so `ctx.params.<name>` reads as `undefined` rather than `""`. This
|
|
255
|
+
* keeps the runtime aligned with the `ExtractParams` type and matches the
|
|
256
|
+
* trie matcher's contract (see `trie-matching.ts:validateAndBuild`).
|
|
257
|
+
*/
|
|
258
|
+
function buildParamsFromMatch(
|
|
259
|
+
match: RegExpExecArray,
|
|
260
|
+
paramNames: string[],
|
|
261
|
+
): Record<string, string> {
|
|
262
|
+
const params: Record<string, string> = {};
|
|
263
|
+
paramNames.forEach((name, index) => {
|
|
264
|
+
const captured = match[index + 1];
|
|
265
|
+
if (captured !== undefined) {
|
|
266
|
+
params[name] = safeDecodeURIComponent(captured);
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
return params;
|
|
270
|
+
}
|
|
271
|
+
|
|
199
272
|
/**
|
|
200
273
|
* Extract the static prefix from a route pattern.
|
|
201
274
|
* Returns everything before the first param/wildcard.
|
|
@@ -247,8 +320,10 @@ export function extractStaticPrefix(pattern: string): string {
|
|
|
247
320
|
/**
|
|
248
321
|
* Match a pathname against registered routes
|
|
249
322
|
*
|
|
250
|
-
* Note: Optional params that are absent in the path
|
|
251
|
-
*
|
|
323
|
+
* Note: Optional params that are absent in the path are omitted from the
|
|
324
|
+
* returned `params` (read as `undefined`), matching the trie matcher and
|
|
325
|
+
* the `ExtractParams<"/:locale?/...">` type. Use the pattern definition or
|
|
326
|
+
* `optionalParams` to determine which keys are optional.
|
|
252
327
|
*
|
|
253
328
|
* Trailing slash handling (priority order):
|
|
254
329
|
* 1. Per-route `trailingSlash` config from route()
|
|
@@ -392,8 +467,13 @@ export function findMatch<TEnv>(
|
|
|
392
467
|
fullPattern = entry.prefix + pattern;
|
|
393
468
|
}
|
|
394
469
|
|
|
395
|
-
const {
|
|
396
|
-
|
|
470
|
+
const {
|
|
471
|
+
regex,
|
|
472
|
+
paramNames,
|
|
473
|
+
optionalParams,
|
|
474
|
+
hasTrailingSlash,
|
|
475
|
+
constraints,
|
|
476
|
+
} = getCompiledPattern(fullPattern);
|
|
397
477
|
|
|
398
478
|
// Get trailing slash mode for this route (per-route config or pattern-based)
|
|
399
479
|
const trailingSlashMode: TrailingSlashMode | undefined =
|
|
@@ -410,10 +490,13 @@ export function findMatch<TEnv>(
|
|
|
410
490
|
// Try exact match first
|
|
411
491
|
const match = regex.exec(pathname);
|
|
412
492
|
if (match) {
|
|
413
|
-
const params
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
493
|
+
const params = buildParamsFromMatch(match, paramNames);
|
|
494
|
+
|
|
495
|
+
// Validate constraints against decoded values; a failure falls
|
|
496
|
+
// through to the next route so other patterns can still match.
|
|
497
|
+
if (!satisfiesConstraints(params, constraints)) {
|
|
498
|
+
continue;
|
|
499
|
+
}
|
|
417
500
|
|
|
418
501
|
if (effectiveDebug) {
|
|
419
502
|
debugLog("findMatch", "matched route", {
|
|
@@ -465,10 +548,11 @@ export function findMatch<TEnv>(
|
|
|
465
548
|
// Try alternate pathname (opposite trailing slash)
|
|
466
549
|
const altMatch = regex.exec(alternatePathname);
|
|
467
550
|
if (altMatch) {
|
|
468
|
-
const params
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
551
|
+
const params = buildParamsFromMatch(altMatch, paramNames);
|
|
552
|
+
|
|
553
|
+
if (!satisfiesConstraints(params, constraints)) {
|
|
554
|
+
continue;
|
|
555
|
+
}
|
|
472
556
|
|
|
473
557
|
// Determine redirect behavior based on mode
|
|
474
558
|
if (trailingSlashMode === "ignore") {
|