@rangojs/router 0.0.0-experimental.8a4d0430 → 0.0.0-experimental.8bcfea43
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 +4 -0
- package/README.md +126 -38
- package/dist/bin/rango.js +138 -50
- package/dist/vite/index.js +1171 -461
- package/dist/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
- package/package.json +19 -16
- package/skills/breadcrumbs/SKILL.md +3 -1
- package/skills/cache-guide/SKILL.md +32 -0
- package/skills/caching/SKILL.md +45 -4
- package/skills/handler-use/SKILL.md +362 -0
- package/skills/hooks/SKILL.md +28 -20
- package/skills/intercept/SKILL.md +20 -0
- package/skills/layout/SKILL.md +22 -0
- package/skills/links/SKILL.md +91 -17
- package/skills/loader/SKILL.md +88 -45
- package/skills/middleware/SKILL.md +34 -3
- package/skills/migrate-nextjs/SKILL.md +560 -0
- package/skills/migrate-react-router/SKILL.md +765 -0
- package/skills/parallel/SKILL.md +185 -0
- package/skills/prerender/SKILL.md +110 -68
- package/skills/rango/SKILL.md +24 -22
- package/skills/response-routes/SKILL.md +8 -0
- package/skills/route/SKILL.md +55 -0
- package/skills/router-setup/SKILL.md +87 -2
- package/skills/streams-and-websockets/SKILL.md +283 -0
- package/skills/typesafety/SKILL.md +13 -1
- package/src/__internal.ts +1 -1
- package/src/browser/app-shell.ts +52 -0
- package/src/browser/app-version.ts +14 -0
- package/src/browser/event-controller.ts +5 -0
- package/src/browser/navigation-bridge.ts +90 -16
- package/src/browser/navigation-client.ts +167 -59
- package/src/browser/navigation-store.ts +68 -9
- package/src/browser/navigation-transaction.ts +11 -9
- package/src/browser/partial-update.ts +113 -17
- package/src/browser/prefetch/cache.ts +184 -16
- package/src/browser/prefetch/fetch.ts +180 -33
- package/src/browser/prefetch/policy.ts +6 -0
- 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 +89 -14
- package/src/browser/react/context.ts +7 -2
- package/src/browser/react/use-handle.ts +9 -58
- package/src/browser/react/use-navigation.ts +22 -2
- package/src/browser/react/use-params.ts +11 -1
- package/src/browser/react/use-router.ts +29 -9
- package/src/browser/rsc-router.tsx +168 -65
- package/src/browser/scroll-restoration.ts +41 -42
- package/src/browser/segment-reconciler.ts +36 -9
- package/src/browser/server-action-bridge.ts +8 -6
- package/src/browser/types.ts +49 -5
- package/src/build/generate-manifest.ts +6 -6
- package/src/build/generate-route-types.ts +3 -0
- package/src/build/route-trie.ts +50 -24
- package/src/build/route-types/include-resolution.ts +8 -1
- package/src/build/route-types/router-processing.ts +223 -74
- package/src/build/route-types/scan-filter.ts +8 -1
- package/src/cache/cache-runtime.ts +15 -11
- package/src/cache/cache-scope.ts +48 -7
- package/src/cache/cf/cf-cache-store.ts +455 -15
- 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.tsx +84 -230
- package/src/context-var.ts +72 -2
- package/src/debug.ts +2 -2
- package/src/handle.ts +40 -0
- package/src/index.rsc.ts +6 -1
- package/src/index.ts +49 -6
- package/src/outlet-context.ts +1 -1
- package/src/prerender/store.ts +5 -4
- package/src/prerender.ts +138 -77
- package/src/response-utils.ts +28 -0
- package/src/reverse.ts +27 -2
- package/src/route-definition/dsl-helpers.ts +240 -40
- package/src/route-definition/helpers-types.ts +67 -19
- 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-map-builder.ts +7 -1
- package/src/route-types.ts +18 -0
- package/src/router/content-negotiation.ts +100 -1
- package/src/router/find-match.ts +4 -2
- package/src/router/handler-context.ts +101 -25
- package/src/router/intercept-resolution.ts +11 -4
- package/src/router/lazy-includes.ts +10 -7
- package/src/router/loader-resolution.ts +159 -21
- package/src/router/logging.ts +5 -2
- package/src/router/manifest.ts +31 -16
- package/src/router/match-api.ts +127 -192
- package/src/router/match-middleware/background-revalidation.ts +30 -2
- package/src/router/match-middleware/cache-lookup.ts +94 -17
- package/src/router/match-middleware/cache-store.ts +53 -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 +104 -10
- package/src/router/metrics.ts +6 -1
- package/src/router/middleware-types.ts +8 -30
- package/src/router/middleware.ts +36 -10
- package/src/router/navigation-snapshot.ts +182 -0
- package/src/router/pattern-matching.ts +60 -9
- package/src/router/prerender-match.ts +110 -10
- package/src/router/preview-match.ts +30 -102
- package/src/router/request-classification.ts +310 -0
- package/src/router/route-snapshot.ts +245 -0
- package/src/router/router-context.ts +6 -1
- package/src/router/router-interfaces.ts +36 -4
- package/src/router/router-options.ts +37 -11
- package/src/router/segment-resolution/fresh.ts +198 -20
- 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 +438 -300
- package/src/router/segment-wrappers.ts +2 -0
- package/src/router/trie-matching.ts +10 -4
- package/src/router/types.ts +1 -0
- package/src/router/url-params.ts +49 -0
- package/src/router.ts +60 -8
- package/src/rsc/handler.ts +478 -374
- package/src/rsc/helpers.ts +69 -41
- package/src/rsc/loader-fetch.ts +23 -3
- package/src/rsc/manifest-init.ts +5 -1
- package/src/rsc/progressive-enhancement.ts +16 -2
- package/src/rsc/response-route-handler.ts +14 -1
- package/src/rsc/rsc-rendering.ts +19 -1
- package/src/rsc/server-action.ts +10 -0
- package/src/rsc/ssr-setup.ts +2 -2
- package/src/rsc/types.ts +9 -1
- package/src/segment-content-promise.ts +67 -0
- package/src/segment-loader-promise.ts +122 -0
- package/src/segment-system.tsx +109 -23
- package/src/server/context.ts +166 -17
- package/src/server/handle-store.ts +19 -0
- package/src/server/loader-registry.ts +9 -8
- package/src/server/request-context.ts +194 -60
- package/src/ssr/index.tsx +4 -0
- package/src/static-handler.ts +18 -6
- package/src/types/cache-types.ts +4 -4
- package/src/types/handler-context.ts +137 -65
- 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 +2 -0
- package/src/urls/include-helper.ts +24 -14
- package/src/urls/path-helper-types.ts +39 -6
- package/src/urls/path-helper.ts +48 -13
- package/src/urls/pattern-types.ts +12 -0
- package/src/urls/response-types.ts +18 -16
- package/src/use-loader.tsx +77 -5
- package/src/vite/debug.ts +55 -0
- package/src/vite/discovery/bundle-postprocess.ts +30 -33
- package/src/vite/discovery/discover-routers.ts +5 -1
- package/src/vite/discovery/prerender-collection.ts +128 -74
- package/src/vite/discovery/state.ts +13 -6
- package/src/vite/index.ts +4 -0
- package/src/vite/plugin-types.ts +51 -79
- 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 +1 -3
- package/src/vite/plugins/expose-id-utils.ts +12 -0
- package/src/vite/plugins/expose-ids/handler-transform.ts +30 -0
- package/src/vite/plugins/expose-internal-ids.ts +257 -40
- package/src/vite/plugins/performance-tracks.ts +86 -0
- package/src/vite/plugins/refresh-cmd.ts +88 -26
- package/src/vite/plugins/version-plugin.ts +13 -1
- package/src/vite/rango.ts +204 -217
- package/src/vite/router-discovery.ts +335 -64
- package/src/vite/utils/banner.ts +4 -4
- package/src/vite/utils/package-resolution.ts +41 -1
- package/src/vite/utils/prerender-utils.ts +37 -5
- package/src/vite/utils/shared-utils.ts +3 -2
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import type { AllUseItems } from "../route-types.js";
|
|
2
|
+
import { isPrerenderHandler, isPassthroughHandler } from "../prerender.js";
|
|
3
|
+
import { isStaticHandler } from "../static-handler.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Extract the .use callback from any handler shape.
|
|
7
|
+
*
|
|
8
|
+
* Checks definition brands first (objects with __brand), then plain functions.
|
|
9
|
+
* ReactNode handlers return undefined (no .use possible).
|
|
10
|
+
*/
|
|
11
|
+
export function resolveHandlerUse(handler: unknown): (() => any[]) | undefined {
|
|
12
|
+
if (handler == null) return undefined;
|
|
13
|
+
|
|
14
|
+
// Check branded definitions first — they're objects but also have typeof "object"
|
|
15
|
+
if (isPassthroughHandler(handler)) {
|
|
16
|
+
return (handler as any).use;
|
|
17
|
+
}
|
|
18
|
+
if (isPrerenderHandler(handler)) {
|
|
19
|
+
return (handler as any).use;
|
|
20
|
+
}
|
|
21
|
+
if (isStaticHandler(handler)) {
|
|
22
|
+
return (handler as any).use;
|
|
23
|
+
}
|
|
24
|
+
// Loader definitions from createLoader() — branded objects with optional .use
|
|
25
|
+
if (typeof handler === "object" && (handler as any).__brand === "loader") {
|
|
26
|
+
return (handler as any).use;
|
|
27
|
+
}
|
|
28
|
+
// Plain handler function
|
|
29
|
+
if (typeof handler === "function") {
|
|
30
|
+
return (handler as any).use;
|
|
31
|
+
}
|
|
32
|
+
// ReactNode or other — no .use
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Allowed item types per mount site.
|
|
38
|
+
* Mirrors the RouteUseItem / ParallelUseItem / InterceptUseItem / LayoutUseItem unions
|
|
39
|
+
* from route-types.ts for runtime validation.
|
|
40
|
+
*/
|
|
41
|
+
const MOUNT_SITE_ALLOWED_TYPES: Record<string, Set<string>> = {
|
|
42
|
+
path: new Set([
|
|
43
|
+
"layout",
|
|
44
|
+
"parallel",
|
|
45
|
+
"intercept",
|
|
46
|
+
"middleware",
|
|
47
|
+
"revalidate",
|
|
48
|
+
"loader",
|
|
49
|
+
"loading",
|
|
50
|
+
"errorBoundary",
|
|
51
|
+
"notFoundBoundary",
|
|
52
|
+
"cache",
|
|
53
|
+
"transition",
|
|
54
|
+
]),
|
|
55
|
+
// Response routes (path.json, path.text, etc.) — mirrors ResponseRouteUseItem
|
|
56
|
+
response: new Set(["middleware", "cache"]),
|
|
57
|
+
route: new Set([
|
|
58
|
+
"layout",
|
|
59
|
+
"parallel",
|
|
60
|
+
"intercept",
|
|
61
|
+
"middleware",
|
|
62
|
+
"revalidate",
|
|
63
|
+
"loader",
|
|
64
|
+
"loading",
|
|
65
|
+
"errorBoundary",
|
|
66
|
+
"notFoundBoundary",
|
|
67
|
+
"cache",
|
|
68
|
+
"transition",
|
|
69
|
+
]),
|
|
70
|
+
// layout allows AllUseItems — no validation needed, but included for completeness
|
|
71
|
+
layout: new Set([
|
|
72
|
+
"layout",
|
|
73
|
+
"route",
|
|
74
|
+
"middleware",
|
|
75
|
+
"revalidate",
|
|
76
|
+
"parallel",
|
|
77
|
+
"intercept",
|
|
78
|
+
"loader",
|
|
79
|
+
"loading",
|
|
80
|
+
"errorBoundary",
|
|
81
|
+
"notFoundBoundary",
|
|
82
|
+
"cache",
|
|
83
|
+
"transition",
|
|
84
|
+
"include",
|
|
85
|
+
]),
|
|
86
|
+
parallel: new Set([
|
|
87
|
+
"revalidate",
|
|
88
|
+
"loader",
|
|
89
|
+
"loading",
|
|
90
|
+
"errorBoundary",
|
|
91
|
+
"notFoundBoundary",
|
|
92
|
+
"transition",
|
|
93
|
+
]),
|
|
94
|
+
intercept: new Set([
|
|
95
|
+
"middleware",
|
|
96
|
+
"revalidate",
|
|
97
|
+
"loader",
|
|
98
|
+
"loading",
|
|
99
|
+
"errorBoundary",
|
|
100
|
+
"notFoundBoundary",
|
|
101
|
+
"layout",
|
|
102
|
+
"route",
|
|
103
|
+
"when",
|
|
104
|
+
"transition",
|
|
105
|
+
]),
|
|
106
|
+
// LoaderUseItem — only revalidate + cache can attach to a loader entry
|
|
107
|
+
loader: new Set(["revalidate", "cache"]),
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Validate that items from handler.use() are valid for the given mount site.
|
|
112
|
+
* Throws a descriptive error if any item is not allowed.
|
|
113
|
+
*/
|
|
114
|
+
export function validateHandlerUseItems(
|
|
115
|
+
items: AllUseItems[],
|
|
116
|
+
mountSite: string,
|
|
117
|
+
): void {
|
|
118
|
+
const allowed = MOUNT_SITE_ALLOWED_TYPES[mountSite];
|
|
119
|
+
if (!allowed) return;
|
|
120
|
+
for (const item of items) {
|
|
121
|
+
if (item == null) continue;
|
|
122
|
+
if (!allowed.has((item as any).type)) {
|
|
123
|
+
throw new Error(
|
|
124
|
+
`handler.use() returned ${(item as any).type}() which is not valid inside ${mountSite}(). ` +
|
|
125
|
+
`Allowed types: ${[...allowed].join(", ")}.`,
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Create a merged use callback from handler.use and explicit use.
|
|
133
|
+
* handler.use items come first (defaults), explicit items second (overrides).
|
|
134
|
+
* Returns undefined if both are absent.
|
|
135
|
+
*/
|
|
136
|
+
export function mergeHandlerUse(
|
|
137
|
+
handlerUse: (() => any[]) | undefined,
|
|
138
|
+
explicitUse: (() => any[]) | undefined,
|
|
139
|
+
mountSite: string,
|
|
140
|
+
): (() => any[]) | undefined {
|
|
141
|
+
if (!handlerUse && !explicitUse) return undefined;
|
|
142
|
+
if (!handlerUse) return explicitUse;
|
|
143
|
+
if (!explicitUse) {
|
|
144
|
+
return () => {
|
|
145
|
+
const items = handlerUse().flat(3);
|
|
146
|
+
validateHandlerUseItems(items, mountSite);
|
|
147
|
+
return items;
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
return () => {
|
|
151
|
+
const hItems = handlerUse().flat(3);
|
|
152
|
+
validateHandlerUseItems(hItems, mountSite);
|
|
153
|
+
return [...hItems, ...explicitUse()];
|
|
154
|
+
};
|
|
155
|
+
}
|
package/src/route-map-builder.ts
CHANGED
|
@@ -199,7 +199,13 @@ export function registerRouterManifestLoader(
|
|
|
199
199
|
}
|
|
200
200
|
|
|
201
201
|
export async function ensureRouterManifest(routerId: string): Promise<void> {
|
|
202
|
-
|
|
202
|
+
// Check both manifest AND trie. The virtual module's setRouterManifest()
|
|
203
|
+
// pre-sets the manifest at startup, but the per-router trie is only
|
|
204
|
+
// available from the lazy loader. Without this, the lazy loader never
|
|
205
|
+
// runs and findMatch falls back to the global merged trie — which
|
|
206
|
+
// contains routes from ALL routers and breaks multi-router setups.
|
|
207
|
+
if (perRouterManifestMap.has(routerId) && perRouterTrieMap.has(routerId))
|
|
208
|
+
return;
|
|
203
209
|
const loader = routerManifestLoaders.get(routerId);
|
|
204
210
|
if (loader) {
|
|
205
211
|
const mod = await loader();
|
package/src/route-types.ts
CHANGED
|
@@ -176,6 +176,13 @@ export type IncludeItem = {
|
|
|
176
176
|
>;
|
|
177
177
|
/** Root scope flag for dot-local reverse resolution */
|
|
178
178
|
rootScoped?: boolean;
|
|
179
|
+
/**
|
|
180
|
+
* Positional include scope token composed from the parent scope plus this
|
|
181
|
+
* include's sibling index (`${parentScope}I${idx}`). Applied to direct-
|
|
182
|
+
* descendant shortCodes during lazy evaluation so routes inside the
|
|
183
|
+
* include cannot collide with siblings declared outside it.
|
|
184
|
+
*/
|
|
185
|
+
includeScope?: string;
|
|
179
186
|
};
|
|
180
187
|
[IncludeBrand]: void;
|
|
181
188
|
};
|
|
@@ -257,3 +264,14 @@ export type LoaderUseItem = RevalidateItem | CacheItem;
|
|
|
257
264
|
* runtime via .flat(3).
|
|
258
265
|
*/
|
|
259
266
|
export type UseItems<T> = (T | readonly T[])[];
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Union of all items that handler.use() may return.
|
|
270
|
+
* A handler doesn't know its mount site at definition time, so the type
|
|
271
|
+
* is intentionally broad — validation happens per-mount-site at runtime.
|
|
272
|
+
*/
|
|
273
|
+
export type HandlerUseItem =
|
|
274
|
+
| RouteUseItem
|
|
275
|
+
| LayoutUseItem
|
|
276
|
+
| ParallelUseItem
|
|
277
|
+
| InterceptUseItem;
|
|
@@ -2,10 +2,18 @@
|
|
|
2
2
|
* Content Negotiation Utilities
|
|
3
3
|
*
|
|
4
4
|
* Pure functions for HTTP Accept header parsing and response type matching.
|
|
5
|
-
* Used by
|
|
5
|
+
* Used by previewMatch and classifyRequest for content negotiation between
|
|
6
6
|
* RSC routes and response routes (JSON, text, image, stream, etc.).
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
+
import type { EntryData } from "../server/context.js";
|
|
10
|
+
import type { CollectedMiddleware } from "./middleware-types.js";
|
|
11
|
+
import { collectRouteMiddleware } from "./middleware.js";
|
|
12
|
+
import { loadManifest } from "./manifest.js";
|
|
13
|
+
import { traverseBack } from "./pattern-matching.js";
|
|
14
|
+
import type { RouteMatchResult } from "./pattern-matching.js";
|
|
15
|
+
import type { RouteSnapshot } from "./route-snapshot.js";
|
|
16
|
+
|
|
9
17
|
// Response type -> MIME type used for Accept header matching
|
|
10
18
|
export const RESPONSE_TYPE_MIME: Record<string, string> = {
|
|
11
19
|
json: "application/json",
|
|
@@ -114,3 +122,94 @@ export function pickNegotiateVariant(
|
|
|
114
122
|
// No match -- use first candidate as default
|
|
115
123
|
return candidates[0]!;
|
|
116
124
|
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Result of content negotiation for a route with negotiate variants.
|
|
128
|
+
*/
|
|
129
|
+
export interface NegotiationResult {
|
|
130
|
+
/** The winning response type */
|
|
131
|
+
responseType: string;
|
|
132
|
+
/** Handler function for the winning variant */
|
|
133
|
+
handler: Function;
|
|
134
|
+
/** Manifest entry for the winning variant (may differ from primary) */
|
|
135
|
+
manifestEntry: EntryData;
|
|
136
|
+
/** Route middleware for the winning variant */
|
|
137
|
+
routeMiddleware: CollectedMiddleware[];
|
|
138
|
+
/** Always true — negotiation occurred */
|
|
139
|
+
negotiated: true;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Perform content negotiation for a route with negotiate variants.
|
|
144
|
+
*
|
|
145
|
+
* Returns a NegotiationResult when a response route wins negotiation.
|
|
146
|
+
* Returns null when RSC wins or no negotiation is needed.
|
|
147
|
+
*
|
|
148
|
+
* Shared by previewMatch and classifyRequest to avoid duplicating
|
|
149
|
+
* the candidate-building and variant-loading logic.
|
|
150
|
+
*/
|
|
151
|
+
export async function negotiateRoute(
|
|
152
|
+
request: Request,
|
|
153
|
+
pathname: string,
|
|
154
|
+
snapshot: RouteSnapshot,
|
|
155
|
+
): Promise<NegotiationResult | null> {
|
|
156
|
+
const { matched, manifestEntry, routeMiddleware, responseType } = snapshot;
|
|
157
|
+
if (!matched.negotiateVariants || matched.negotiateVariants.length === 0) {
|
|
158
|
+
return null;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const acceptEntries = parseAcceptTypes(request.headers.get("accept") || "");
|
|
162
|
+
|
|
163
|
+
// Build candidate list preserving definition order.
|
|
164
|
+
const variants = matched.negotiateVariants;
|
|
165
|
+
let candidates: Array<{ routeKey: string; responseType: string }>;
|
|
166
|
+
if (responseType) {
|
|
167
|
+
candidates = [...variants, { routeKey: matched.routeKey, responseType }];
|
|
168
|
+
} else {
|
|
169
|
+
const rscCandidate = {
|
|
170
|
+
routeKey: matched.routeKey,
|
|
171
|
+
responseType: RSC_RESPONSE_TYPE,
|
|
172
|
+
};
|
|
173
|
+
candidates = matched.rscFirst
|
|
174
|
+
? [rscCandidate, ...variants]
|
|
175
|
+
: [...variants, rscCandidate];
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const variant = pickNegotiateVariant(acceptEntries, candidates);
|
|
179
|
+
|
|
180
|
+
// RSC won negotiation
|
|
181
|
+
if (variant.responseType === RSC_RESPONSE_TYPE) {
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// Primary response-type won — use existing manifest entry and middleware
|
|
186
|
+
if (responseType && variant.routeKey === matched.routeKey) {
|
|
187
|
+
return {
|
|
188
|
+
responseType,
|
|
189
|
+
handler: manifestEntry.handler as Function,
|
|
190
|
+
manifestEntry,
|
|
191
|
+
routeMiddleware,
|
|
192
|
+
negotiated: true,
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Different variant won — load its manifest entry
|
|
197
|
+
const negotiateEntry = await loadManifest(
|
|
198
|
+
matched.entry,
|
|
199
|
+
variant.routeKey,
|
|
200
|
+
pathname,
|
|
201
|
+
undefined,
|
|
202
|
+
false,
|
|
203
|
+
);
|
|
204
|
+
const variantMiddleware = collectRouteMiddleware(
|
|
205
|
+
traverseBack(negotiateEntry),
|
|
206
|
+
matched.params,
|
|
207
|
+
);
|
|
208
|
+
return {
|
|
209
|
+
responseType: variant.responseType,
|
|
210
|
+
handler: negotiateEntry.handler as Function,
|
|
211
|
+
manifestEntry: negotiateEntry,
|
|
212
|
+
routeMiddleware: variantMiddleware,
|
|
213
|
+
negotiated: true,
|
|
214
|
+
};
|
|
215
|
+
}
|
package/src/router/find-match.ts
CHANGED
|
@@ -52,8 +52,10 @@ export function createFindMatch<TEnv = any>(
|
|
|
52
52
|
: undefined;
|
|
53
53
|
|
|
54
54
|
// Phase 1: Try trie match (O(path_length))
|
|
55
|
-
//
|
|
56
|
-
|
|
55
|
+
// Only use the per-router trie. The global trie merges routes from ALL
|
|
56
|
+
// routers and must not be used — in multi-router setups (host routing)
|
|
57
|
+
// overlapping paths like "/" would match the wrong app's route.
|
|
58
|
+
const routeTrie = getRouterTrie(deps.routerId);
|
|
57
59
|
if (routeTrie) {
|
|
58
60
|
const trieStart = performance.now();
|
|
59
61
|
const trieResult = tryTrieMatch(routeTrie, pathname);
|
|
@@ -8,10 +8,18 @@ import type { HandlerContext, InternalHandlerContext } from "../types";
|
|
|
8
8
|
import { _getRequestContext } from "../server/request-context.js";
|
|
9
9
|
import { getSearchSchema, isRouteRootScoped } from "../route-map-builder.js";
|
|
10
10
|
import { parseSearchParams, serializeSearchParams } from "../search-params.js";
|
|
11
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
contextGet,
|
|
13
|
+
contextSet,
|
|
14
|
+
isNonCacheable,
|
|
15
|
+
type ContextSetOptions,
|
|
16
|
+
} from "../context-var.js";
|
|
17
|
+
import { isInsideCacheScope } from "../server/context.js";
|
|
12
18
|
import { NOCACHE_SYMBOL, assertNotInsideCacheExec } from "../cache/taint.js";
|
|
13
19
|
import { isAutoGeneratedRouteName } from "../route-name.js";
|
|
14
20
|
import { PRERENDER_PASSTHROUGH } from "../prerender.js";
|
|
21
|
+
import { encodePathSegment } from "./url-params.js";
|
|
22
|
+
import { fireAndForgetWaitUntil } from "../types/request-scope.js";
|
|
15
23
|
|
|
16
24
|
/**
|
|
17
25
|
* Strip internal _rsc* query params from a URL.
|
|
@@ -108,9 +116,9 @@ function createPrerenderPassthroughFn(
|
|
|
108
116
|
}
|
|
109
117
|
if (!isPassthroughRoute) {
|
|
110
118
|
throw new Error(
|
|
111
|
-
"ctx.passthrough() is only available on routes
|
|
112
|
-
"
|
|
113
|
-
"
|
|
119
|
+
"ctx.passthrough() is only available on routes wrapped with " +
|
|
120
|
+
"Passthrough(). Remove the passthrough() call or wrap the " +
|
|
121
|
+
"Prerender definition with Passthrough(prerenderDef, liveHandler).",
|
|
114
122
|
);
|
|
115
123
|
}
|
|
116
124
|
return PRERENDER_PASSTHROUGH;
|
|
@@ -160,17 +168,42 @@ export function createReverseFunction(
|
|
|
160
168
|
: hrefParams;
|
|
161
169
|
|
|
162
170
|
// Substitute params (strip constraint and optional syntax: :param(a|b)? -> value)
|
|
171
|
+
// Optional params (:param?) are omitted when not provided
|
|
163
172
|
if (effectiveParams) {
|
|
173
|
+
let hadOmittedOptional = false;
|
|
174
|
+
// First pass: optional params (trailing ?)
|
|
164
175
|
result = result.replace(
|
|
165
|
-
/:([a-zA-Z_][a-zA-Z0-9_]*)(\([^)]*\))
|
|
176
|
+
/:([a-zA-Z_][a-zA-Z0-9_]*)(\([^)]*\))?(\?)/g,
|
|
177
|
+
(_, key) => {
|
|
178
|
+
const value = effectiveParams[key];
|
|
179
|
+
// Empty string is treated as omitted — the trie matcher fills
|
|
180
|
+
// unmatched optional params with "" (not undefined), so reverse
|
|
181
|
+
// must collapse those segments instead of leaving empty slots.
|
|
182
|
+
if (value === undefined || value === "") {
|
|
183
|
+
hadOmittedOptional = true;
|
|
184
|
+
return "";
|
|
185
|
+
}
|
|
186
|
+
return encodePathSegment(value);
|
|
187
|
+
},
|
|
188
|
+
);
|
|
189
|
+
// Second pass: required params (no trailing ?)
|
|
190
|
+
result = result.replace(
|
|
191
|
+
/:([a-zA-Z_][a-zA-Z0-9_]*)(\([^)]*\))?(?!\?)/g,
|
|
166
192
|
(_, key) => {
|
|
167
193
|
const value = effectiveParams[key];
|
|
168
194
|
if (value === undefined) {
|
|
169
195
|
throw new Error(`Missing param "${key}" for route "${name}"`);
|
|
170
196
|
}
|
|
171
|
-
return
|
|
197
|
+
return encodePathSegment(value);
|
|
172
198
|
},
|
|
173
199
|
);
|
|
200
|
+
// Clean up slashes only when an optional param was actually omitted,
|
|
201
|
+
// so intentional trailing-slash patterns like "/blog/" are preserved.
|
|
202
|
+
if (hadOmittedOptional) {
|
|
203
|
+
const hadTrailingSlash = pattern.length > 1 && pattern.endsWith("/");
|
|
204
|
+
result = result.replace(/\/\/+/g, "/").replace(/\/+$/, "") || "/";
|
|
205
|
+
if (hadTrailingSlash && !result.endsWith("/")) result += "/";
|
|
206
|
+
}
|
|
174
207
|
}
|
|
175
208
|
|
|
176
209
|
// Append search params as query string
|
|
@@ -201,7 +234,7 @@ export function createHandlerContext<TEnv>(
|
|
|
201
234
|
// Get variables from request context - this is the unified context
|
|
202
235
|
// shared between middleware and route handlers
|
|
203
236
|
const requestContext = _getRequestContext();
|
|
204
|
-
const variables: any = requestContext?.
|
|
237
|
+
const variables: any = requestContext?._variables ?? {};
|
|
205
238
|
|
|
206
239
|
// If route has a search schema, parse URLSearchParams into typed object
|
|
207
240
|
const searchSchema = routeName ? getSearchSchema(routeName) : undefined;
|
|
@@ -213,7 +246,7 @@ export function createHandlerContext<TEnv>(
|
|
|
213
246
|
const stubResponse =
|
|
214
247
|
requestContext?.res ?? new Response(null, { status: 200 });
|
|
215
248
|
|
|
216
|
-
// Guard mutating Headers methods so they throw inside "use cache"
|
|
249
|
+
// Guard mutating Headers methods so they throw inside "use cache" or cache() scope.
|
|
217
250
|
// Uses lazy `ctx` reference (assigned below) — only the specific handler ctx
|
|
218
251
|
// is stamped by cache-runtime, not the shared request context.
|
|
219
252
|
const MUTATING_HEADERS_METHODS = new Set(["set", "append", "delete"]);
|
|
@@ -225,6 +258,13 @@ export function createHandlerContext<TEnv>(
|
|
|
225
258
|
if (MUTATING_HEADERS_METHODS.has(prop as string)) {
|
|
226
259
|
return (...args: any[]) => {
|
|
227
260
|
assertNotInsideCacheExec(ctx, "headers");
|
|
261
|
+
if (isInsideCacheScope()) {
|
|
262
|
+
throw new Error(
|
|
263
|
+
`ctx.headers.${String(prop)}() cannot be called inside a cache() boundary. ` +
|
|
264
|
+
`On cache hit the handler is skipped, so this side effect would be lost. ` +
|
|
265
|
+
`Move header mutations to a middleware or layout outside the cache() scope.`,
|
|
266
|
+
);
|
|
267
|
+
}
|
|
228
268
|
return value.apply(target, args);
|
|
229
269
|
};
|
|
230
270
|
}
|
|
@@ -237,21 +277,36 @@ export function createHandlerContext<TEnv>(
|
|
|
237
277
|
ctx = {
|
|
238
278
|
params,
|
|
239
279
|
build: false,
|
|
280
|
+
dev: false,
|
|
240
281
|
request,
|
|
241
282
|
searchParams,
|
|
242
283
|
search: searchSchema ? resolvedSearchParams : {},
|
|
243
284
|
pathname,
|
|
244
285
|
url,
|
|
245
|
-
originalUrl: new URL(request.url),
|
|
286
|
+
originalUrl: requestContext?.originalUrl ?? new URL(request.url),
|
|
246
287
|
env: bindings,
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
288
|
+
waitUntil: requestContext
|
|
289
|
+
? requestContext.waitUntil.bind(requestContext)
|
|
290
|
+
: fireAndForgetWaitUntil,
|
|
291
|
+
executionContext: requestContext?.executionContext,
|
|
292
|
+
_variables: variables,
|
|
293
|
+
get: ((keyOrVar: any) => {
|
|
294
|
+
// Read-time guard: non-cacheable var inside cache() → throw.
|
|
295
|
+
// Works for both ContextVar tokens and string keys.
|
|
296
|
+
if (isNonCacheable(variables, keyOrVar) && isInsideCacheScope()) {
|
|
297
|
+
throw new Error(
|
|
298
|
+
`ctx.get() for a non-cacheable variable cannot be called inside a cache() boundary. ` +
|
|
299
|
+
`The variable was created with { cache: false } or set with { cache: false }, ` +
|
|
300
|
+
`and its value would be stale on cache hit. Move the read outside the cached scope.`,
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
return contextGet(variables, keyOrVar);
|
|
304
|
+
}) as HandlerContext<any, TEnv>["get"],
|
|
305
|
+
set: ((keyOrVar: any, value: any, options?: ContextSetOptions) => {
|
|
253
306
|
assertNotInsideCacheExec(ctx, "set");
|
|
254
|
-
|
|
307
|
+
// Write is dumb: store value + non-cacheable metadata.
|
|
308
|
+
// Enforcement happens at read time via ctx.get().
|
|
309
|
+
contextSet(variables, keyOrVar, value, options);
|
|
255
310
|
}) as HandlerContext<any, TEnv>["set"],
|
|
256
311
|
res: stubResponse, // Stub response for setting headers
|
|
257
312
|
headers: guardedHeaders, // Guarded shorthand for res.headers
|
|
@@ -297,7 +352,7 @@ export function createHandlerContext<TEnv>(
|
|
|
297
352
|
*
|
|
298
353
|
* Returns an InternalHandlerContext where params, pathname, url, searchParams,
|
|
299
354
|
* search, reverse, and use(handle) work. Request-time properties
|
|
300
|
-
* (request, env, headers, cookies,
|
|
355
|
+
* (request, env, headers, cookies, get, set, res) throw with a clear error.
|
|
301
356
|
*/
|
|
302
357
|
export function createPrerenderContext<TEnv>(
|
|
303
358
|
params: Record<string, string>,
|
|
@@ -306,6 +361,8 @@ export function createPrerenderContext<TEnv>(
|
|
|
306
361
|
routeName?: string,
|
|
307
362
|
buildVars?: Record<string, any>,
|
|
308
363
|
isPassthroughRoute?: boolean,
|
|
364
|
+
buildEnv?: TEnv,
|
|
365
|
+
devMode?: boolean,
|
|
309
366
|
): InternalHandlerContext<any, TEnv> {
|
|
310
367
|
const syntheticUrl = new URL(`http://prerender${pathname}`);
|
|
311
368
|
const variables = buildVars ?? {};
|
|
@@ -320,6 +377,7 @@ export function createPrerenderContext<TEnv>(
|
|
|
320
377
|
return {
|
|
321
378
|
params,
|
|
322
379
|
build: true,
|
|
380
|
+
dev: devMode ?? false,
|
|
323
381
|
get request(): Request {
|
|
324
382
|
return throwUnavailable("request");
|
|
325
383
|
},
|
|
@@ -329,11 +387,19 @@ export function createPrerenderContext<TEnv>(
|
|
|
329
387
|
url: syntheticUrl,
|
|
330
388
|
originalUrl: syntheticUrl,
|
|
331
389
|
get env(): TEnv {
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
390
|
+
if (buildEnv !== undefined) return buildEnv;
|
|
391
|
+
throw new Error(
|
|
392
|
+
"ctx.env is not available during pre-rendering. " +
|
|
393
|
+
"Configure buildEnv in your rango() plugin options to enable build-time env access.",
|
|
394
|
+
);
|
|
336
395
|
},
|
|
396
|
+
// Build-time prerender has no live request. waitUntil is a true no-op
|
|
397
|
+
// (running fn() here would fire side effects during build, which is
|
|
398
|
+
// incorrect — these are meant to outlive the live response).
|
|
399
|
+
// executionContext is absent for the same reason.
|
|
400
|
+
waitUntil: () => {},
|
|
401
|
+
executionContext: undefined,
|
|
402
|
+
_variables: variables,
|
|
337
403
|
get: ((keyOrVar: any) => contextGet(variables, keyOrVar)) as any,
|
|
338
404
|
set: ((keyOrVar: any, value: any) => {
|
|
339
405
|
contextSet(variables, keyOrVar, value);
|
|
@@ -379,6 +445,8 @@ export function createPrerenderContext<TEnv>(
|
|
|
379
445
|
export function createStaticContext<TEnv>(
|
|
380
446
|
routeMap: Record<string, string>,
|
|
381
447
|
routeName?: string,
|
|
448
|
+
buildEnv?: TEnv,
|
|
449
|
+
devMode?: boolean,
|
|
382
450
|
): InternalHandlerContext<any, TEnv> {
|
|
383
451
|
const variables: Record<string, any> = {};
|
|
384
452
|
|
|
@@ -394,6 +462,7 @@ export function createStaticContext<TEnv>(
|
|
|
394
462
|
return throwUnavailable("params");
|
|
395
463
|
},
|
|
396
464
|
build: true,
|
|
465
|
+
dev: devMode ?? false,
|
|
397
466
|
get request(): Request {
|
|
398
467
|
return throwUnavailable("request");
|
|
399
468
|
},
|
|
@@ -413,11 +482,18 @@ export function createStaticContext<TEnv>(
|
|
|
413
482
|
return throwUnavailable("originalUrl");
|
|
414
483
|
},
|
|
415
484
|
get env(): TEnv {
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
485
|
+
if (buildEnv !== undefined) return buildEnv;
|
|
486
|
+
throw new Error(
|
|
487
|
+
"ctx.env is not available in Static() handlers. " +
|
|
488
|
+
"Configure buildEnv in your rango() plugin options to enable build-time env access.",
|
|
489
|
+
);
|
|
420
490
|
},
|
|
491
|
+
// Static() handlers have no live request. waitUntil is a true no-op
|
|
492
|
+
// (running fn() here would fire side effects during build, which is
|
|
493
|
+
// incorrect). executionContext is absent for the same reason.
|
|
494
|
+
waitUntil: () => {},
|
|
495
|
+
executionContext: undefined,
|
|
496
|
+
_variables: variables,
|
|
421
497
|
get: ((keyOrVar: any) => contextGet(variables, keyOrVar)) as any,
|
|
422
498
|
set: ((keyOrVar: any, value: any) => {
|
|
423
499
|
contextSet(variables, keyOrVar, value);
|
|
@@ -11,7 +11,11 @@ import type {
|
|
|
11
11
|
InterceptEntry,
|
|
12
12
|
InterceptSelectorContext,
|
|
13
13
|
} from "../server/context";
|
|
14
|
-
import type {
|
|
14
|
+
import type {
|
|
15
|
+
HandlerContext,
|
|
16
|
+
InternalHandlerContext,
|
|
17
|
+
ResolvedSegment,
|
|
18
|
+
} from "../types";
|
|
15
19
|
import { evaluateRevalidation } from "./revalidation.js";
|
|
16
20
|
import { getRequestContext } from "../server/request-context.js";
|
|
17
21
|
import { executeInterceptMiddleware } from "./middleware.js";
|
|
@@ -20,6 +24,7 @@ import { getGlobalRouteMap } from "../route-map-builder.js";
|
|
|
20
24
|
import { handleHandlerResult } from "./segment-resolution.js";
|
|
21
25
|
import type { SegmentResolutionDeps } from "./types.js";
|
|
22
26
|
import { debugLog } from "./logging.js";
|
|
27
|
+
import { runInsideLoaderScope } from "../server/context.js";
|
|
23
28
|
|
|
24
29
|
/**
|
|
25
30
|
* Check if an intercept's when conditions are satisfied.
|
|
@@ -133,7 +138,7 @@ export async function resolveInterceptEntry<TEnv>(
|
|
|
133
138
|
context.request,
|
|
134
139
|
context.env,
|
|
135
140
|
params,
|
|
136
|
-
context
|
|
141
|
+
(context as InternalHandlerContext<any, TEnv>)._variables,
|
|
137
142
|
requestCtx.res,
|
|
138
143
|
createReverseFunction(getGlobalRouteMap()),
|
|
139
144
|
);
|
|
@@ -188,6 +193,7 @@ export async function resolveInterceptEntry<TEnv>(
|
|
|
188
193
|
context,
|
|
189
194
|
actionContext,
|
|
190
195
|
stale,
|
|
196
|
+
traceSource: "intercept-loader",
|
|
191
197
|
});
|
|
192
198
|
|
|
193
199
|
if (!shouldRevalidate) {
|
|
@@ -206,7 +212,7 @@ export async function resolveInterceptEntry<TEnv>(
|
|
|
206
212
|
loaderIds.push(loader.$$id);
|
|
207
213
|
loaderPromises.push(
|
|
208
214
|
deps.wrapLoaderPromise(
|
|
209
|
-
context.use(loader),
|
|
215
|
+
runInsideLoaderScope(() => context.use(loader)),
|
|
210
216
|
parentEntry,
|
|
211
217
|
segmentId,
|
|
212
218
|
context.pathname,
|
|
@@ -355,6 +361,7 @@ export async function resolveInterceptLoadersOnly<TEnv>(
|
|
|
355
361
|
context,
|
|
356
362
|
actionContext,
|
|
357
363
|
stale,
|
|
364
|
+
traceSource: "intercept-loader",
|
|
358
365
|
});
|
|
359
366
|
|
|
360
367
|
if (!shouldRevalidate) {
|
|
@@ -372,7 +379,7 @@ export async function resolveInterceptLoadersOnly<TEnv>(
|
|
|
372
379
|
loaderIds.push(loader.$$id);
|
|
373
380
|
loaderPromises.push(
|
|
374
381
|
deps.wrapLoaderPromise(
|
|
375
|
-
context.use(loader),
|
|
382
|
+
runInsideLoaderScope(() => context.use(loader)),
|
|
376
383
|
parentEntry,
|
|
377
384
|
segmentId,
|
|
378
385
|
context.pathname,
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { registerRouteMap } from "../route-map-builder.js";
|
|
2
2
|
import { extractStaticPrefix } from "./pattern-matching.js";
|
|
3
3
|
import {
|
|
4
|
-
EntryData,
|
|
4
|
+
type EntryData,
|
|
5
5
|
RSCRouterContext,
|
|
6
6
|
runWithPrefixes,
|
|
7
|
+
getIsolatedLazyParent,
|
|
7
8
|
} from "../server/context";
|
|
8
9
|
import type { UrlPatterns } from "../urls.js";
|
|
9
10
|
import type { AllUseItems, IncludeItem } from "../route-types.js";
|
|
@@ -14,6 +15,7 @@ export interface LazyEvalDeps<TEnv = any> {
|
|
|
14
15
|
mergedRouteMap: Record<string, string>;
|
|
15
16
|
nextMountIndex: () => number;
|
|
16
17
|
getPrecomputedByPrefix: () => Map<string, Record<string, string>> | null;
|
|
18
|
+
routerId?: string;
|
|
17
19
|
}
|
|
18
20
|
|
|
19
21
|
// Detect lazy includes in handler result and create placeholder entries
|
|
@@ -123,9 +125,8 @@ export function evaluateLazyEntry<TEnv = any>(
|
|
|
123
125
|
// Merge captured counters from include() to maintain consistent
|
|
124
126
|
// shortCode indices with sibling entries from pattern extraction
|
|
125
127
|
const lazyCounters: Record<string, number> = {};
|
|
126
|
-
if (lazyContext
|
|
127
|
-
const
|
|
128
|
-
for (const [key, value] of Object.entries(captured)) {
|
|
128
|
+
if (lazyContext?.counters) {
|
|
129
|
+
for (const [key, value] of Object.entries(lazyContext.counters)) {
|
|
129
130
|
lazyCounters[key] = value;
|
|
130
131
|
}
|
|
131
132
|
}
|
|
@@ -137,10 +138,11 @@ export function evaluateLazyEntry<TEnv = any>(
|
|
|
137
138
|
patternsByPrefix,
|
|
138
139
|
trailingSlash: trailingSlashMap,
|
|
139
140
|
namespace: "lazy",
|
|
140
|
-
parent: (lazyContext?.parent as EntryData | null)
|
|
141
|
+
parent: getIsolatedLazyParent(lazyContext?.parent as EntryData | null),
|
|
141
142
|
counters: lazyCounters,
|
|
142
|
-
cacheProfiles:
|
|
143
|
-
rootScoped:
|
|
143
|
+
cacheProfiles: lazyContext?.cacheProfiles,
|
|
144
|
+
rootScoped: lazyContext?.rootScoped,
|
|
145
|
+
includeScope: lazyContext?.includeScope,
|
|
144
146
|
},
|
|
145
147
|
() => {
|
|
146
148
|
// Run the lazy patterns handler with the original context prefixes
|
|
@@ -200,6 +202,7 @@ export function evaluateLazyEntry<TEnv = any>(
|
|
|
200
202
|
trailingSlash: entry.trailingSlash,
|
|
201
203
|
handler: (lazyInclude.patterns as UrlPatterns<TEnv>).handler,
|
|
202
204
|
mountIndex: deps.nextMountIndex(),
|
|
205
|
+
routerId: deps.routerId,
|
|
203
206
|
// Lazy evaluation fields
|
|
204
207
|
lazy: true,
|
|
205
208
|
lazyPatterns: lazyInclude.patterns,
|