@rangojs/router 0.0.0-experimental.62 → 0.0.0-experimental.64
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 +61 -8
- package/dist/bin/rango.js +2 -1
- package/dist/vite/index.js +142 -62
- package/dist/vite/index.js.bak +5448 -0
- package/package.json +14 -15
- package/skills/prerender/SKILL.md +110 -68
- package/src/__internal.ts +1 -1
- package/src/build/generate-manifest.ts +3 -6
- package/src/build/route-types/scan-filter.ts +8 -1
- package/src/client.tsx +2 -56
- package/src/index.rsc.ts +3 -1
- package/src/index.ts +8 -0
- package/src/prerender/store.ts +5 -4
- package/src/prerender.ts +138 -77
- package/src/route-definition/dsl-helpers.ts +42 -19
- package/src/route-definition/helpers-types.ts +4 -1
- package/src/route-definition/index.ts +3 -0
- package/src/route-definition/resolve-handler-use.ts +149 -0
- package/src/route-types.ts +11 -0
- package/src/router/content-negotiation.ts +100 -1
- package/src/router/handler-context.ts +20 -5
- package/src/router/match-api.ts +124 -189
- package/src/router/match-middleware/cache-lookup.ts +2 -6
- package/src/router/navigation-snapshot.ts +182 -0
- package/src/router/prerender-match.ts +104 -8
- 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-interfaces.ts +11 -0
- package/src/router/segment-resolution/fresh.ts +44 -2
- package/src/router/segment-resolution/revalidation.ts +53 -5
- package/src/router.ts +13 -1
- package/src/rsc/handler.ts +456 -373
- package/src/rsc/ssr-setup.ts +1 -1
- package/src/server/context.ts +5 -1
- package/src/server/request-context.ts +7 -0
- package/src/static-handler.ts +18 -6
- package/src/types/handler-context.ts +12 -2
- package/src/types/route-entry.ts +1 -1
- package/src/urls/path-helper-types.ts +9 -2
- package/src/urls/path-helper.ts +47 -12
- package/src/urls/response-types.ts +16 -6
- package/src/use-loader.tsx +73 -4
- package/src/vite/discovery/bundle-postprocess.ts +30 -33
- package/src/vite/discovery/prerender-collection.ts +14 -1
- package/src/vite/discovery/state.ts +13 -4
- package/src/vite/index.ts +4 -0
- package/src/vite/plugin-types.ts +60 -5
- package/src/vite/rango.ts +2 -1
- package/src/vite/router-discovery.ts +153 -34
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Request Classification
|
|
3
|
+
*
|
|
4
|
+
* Replaces the implicit "preview then match again" model with a clean
|
|
5
|
+
* two-stage architecture:
|
|
6
|
+
*
|
|
7
|
+
* 1. Classification — classifyRequest() produces a RequestPlan that answers
|
|
8
|
+
* all routing questions once: target route, request mode, route middleware,
|
|
9
|
+
* response-route info, negotiation state.
|
|
10
|
+
*
|
|
11
|
+
* 2. Execution — executeRequest() dispatches on the plan to the appropriate
|
|
12
|
+
* handler (response route, loader fetch, full render, partial render,
|
|
13
|
+
* action revalidation, PE render).
|
|
14
|
+
*
|
|
15
|
+
* Builds on RouteSnapshot from route-snapshot.ts.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { RouteNotFoundError } from "../errors.js";
|
|
19
|
+
import type { EntryData } from "../server/context.js";
|
|
20
|
+
import type { CollectedMiddleware } from "./middleware-types.js";
|
|
21
|
+
import type { RouteMatchResult } from "./pattern-matching.js";
|
|
22
|
+
import { negotiateRoute } from "./content-negotiation.js";
|
|
23
|
+
import { stripInternalParams } from "./handler-context.js";
|
|
24
|
+
import { resolveRoute, type RouteSnapshot } from "./route-snapshot.js";
|
|
25
|
+
|
|
26
|
+
// ---------------------------------------------------------------------------
|
|
27
|
+
// RequestPlan — discriminated union
|
|
28
|
+
// ---------------------------------------------------------------------------
|
|
29
|
+
|
|
30
|
+
interface RedirectPlan<TEnv = any> {
|
|
31
|
+
mode: "redirect";
|
|
32
|
+
route: RouteSnapshot<TEnv>;
|
|
33
|
+
redirectUrl: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface VersionMismatchPlan<TEnv = any> {
|
|
37
|
+
mode: "version-mismatch";
|
|
38
|
+
/** May be undefined when version mismatch is detected before route resolution */
|
|
39
|
+
route?: RouteSnapshot<TEnv>;
|
|
40
|
+
reloadUrl: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
interface ResponseRoutePlan<TEnv = any> {
|
|
44
|
+
mode: "response";
|
|
45
|
+
route: RouteSnapshot<TEnv>;
|
|
46
|
+
handler: Function;
|
|
47
|
+
responseType: string;
|
|
48
|
+
negotiated: boolean;
|
|
49
|
+
manifestEntry: EntryData;
|
|
50
|
+
routeMiddleware: CollectedMiddleware[];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
interface LoaderFetchPlan<TEnv = any> {
|
|
54
|
+
mode: "loader";
|
|
55
|
+
route: RouteSnapshot<TEnv>;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
interface PeRenderPlan<TEnv = any> {
|
|
59
|
+
mode: "pe-render";
|
|
60
|
+
route: RouteSnapshot<TEnv>;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
interface ActionPlan<TEnv = any> {
|
|
64
|
+
mode: "action";
|
|
65
|
+
route: RouteSnapshot<TEnv>;
|
|
66
|
+
actionId: string;
|
|
67
|
+
negotiated: boolean;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
interface FullRenderPlan<TEnv = any> {
|
|
71
|
+
mode: "full-render";
|
|
72
|
+
route: RouteSnapshot<TEnv>;
|
|
73
|
+
negotiated: boolean;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
interface PartialRenderPlan<TEnv = any> {
|
|
77
|
+
mode: "partial-render";
|
|
78
|
+
route: RouteSnapshot<TEnv>;
|
|
79
|
+
negotiated: boolean;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* The output of request classification. A discriminated union where each
|
|
84
|
+
* variant carries exactly the fields needed for its execution path.
|
|
85
|
+
*/
|
|
86
|
+
export type RequestPlan<TEnv = any> =
|
|
87
|
+
| RedirectPlan<TEnv>
|
|
88
|
+
| VersionMismatchPlan<TEnv>
|
|
89
|
+
| ResponseRoutePlan<TEnv>
|
|
90
|
+
| LoaderFetchPlan<TEnv>
|
|
91
|
+
| PeRenderPlan<TEnv>
|
|
92
|
+
| ActionPlan<TEnv>
|
|
93
|
+
| FullRenderPlan<TEnv>
|
|
94
|
+
| PartialRenderPlan<TEnv>;
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Plans that have passed the terminal-check gate (version-mismatch handled)
|
|
98
|
+
* and are ready for execution. Always have a `route` field.
|
|
99
|
+
*/
|
|
100
|
+
export type ExecutableRequestPlan<TEnv = any> = Exclude<
|
|
101
|
+
RequestPlan<TEnv>,
|
|
102
|
+
VersionMismatchPlan<TEnv>
|
|
103
|
+
>;
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Re-export individual plan types for consumers that need to narrow.
|
|
107
|
+
*/
|
|
108
|
+
export type {
|
|
109
|
+
RedirectPlan,
|
|
110
|
+
VersionMismatchPlan,
|
|
111
|
+
ResponseRoutePlan,
|
|
112
|
+
LoaderFetchPlan,
|
|
113
|
+
PeRenderPlan,
|
|
114
|
+
ActionPlan,
|
|
115
|
+
FullRenderPlan,
|
|
116
|
+
PartialRenderPlan,
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
// ---------------------------------------------------------------------------
|
|
120
|
+
// classifyRequest — the single authoritative classification step
|
|
121
|
+
// ---------------------------------------------------------------------------
|
|
122
|
+
|
|
123
|
+
export interface ClassifyRequestDeps<TEnv = any> {
|
|
124
|
+
findMatch: (pathname: string) => RouteMatchResult<TEnv> | null;
|
|
125
|
+
routerVersion: string;
|
|
126
|
+
routerId: string;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Classify an incoming request into a RequestPlan.
|
|
131
|
+
*
|
|
132
|
+
* This is the single source of truth for request mode detection. It replaces
|
|
133
|
+
* the scattered previewMatch + isAction/isLoaderFetch/isPartial checks in
|
|
134
|
+
* handler.ts.
|
|
135
|
+
*
|
|
136
|
+
* Classification order:
|
|
137
|
+
* 1. Route resolution (findMatch + loadManifest via resolveRoute lite)
|
|
138
|
+
* 2. Redirect detection
|
|
139
|
+
* 3. Version mismatch
|
|
140
|
+
* 4. Response route + content negotiation
|
|
141
|
+
* 5. Mode detection from headers/params
|
|
142
|
+
*/
|
|
143
|
+
export async function classifyRequest<TEnv = any>(
|
|
144
|
+
request: Request,
|
|
145
|
+
url: URL,
|
|
146
|
+
deps: ClassifyRequestDeps<TEnv>,
|
|
147
|
+
): Promise<RequestPlan<TEnv>> {
|
|
148
|
+
const pathname = url.pathname;
|
|
149
|
+
const isAction =
|
|
150
|
+
request.headers.has("rsc-action") || url.searchParams.has("_rsc_action");
|
|
151
|
+
|
|
152
|
+
// Version mismatch — runs BEFORE route resolution so stale clients
|
|
153
|
+
// requesting removed routes get a reload, not a 404.
|
|
154
|
+
const clientVersion = url.searchParams.get("_rsc_v");
|
|
155
|
+
if (
|
|
156
|
+
deps.routerVersion &&
|
|
157
|
+
clientVersion &&
|
|
158
|
+
clientVersion !== deps.routerVersion
|
|
159
|
+
) {
|
|
160
|
+
// Strip internal _rsc_* params so the browser reloads to a clean URL
|
|
161
|
+
let reloadUrl = stripInternalParams(url).toString();
|
|
162
|
+
if (isAction) {
|
|
163
|
+
const referer = request.headers.get("referer");
|
|
164
|
+
if (referer) {
|
|
165
|
+
try {
|
|
166
|
+
const refererUrl = new URL(referer);
|
|
167
|
+
if (refererUrl.origin === url.origin) {
|
|
168
|
+
reloadUrl = referer;
|
|
169
|
+
}
|
|
170
|
+
} catch {
|
|
171
|
+
// Malformed referer, fall back to stripped url
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return {
|
|
177
|
+
mode: "version-mismatch",
|
|
178
|
+
reloadUrl,
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// No metricsStore — classification is a lightweight gating step.
|
|
183
|
+
// Route-matching and manifest-loading metrics belong in the match path
|
|
184
|
+
// (createMatchContextForFull/Partial) which runs the authoritative resolution.
|
|
185
|
+
const result = await resolveRoute<TEnv>(pathname, {
|
|
186
|
+
findMatch: deps.findMatch,
|
|
187
|
+
lite: true,
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
if (!result) {
|
|
191
|
+
throw new RouteNotFoundError(`No route matched for ${pathname}`, {
|
|
192
|
+
cause: { pathname, method: request.method },
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Redirect
|
|
197
|
+
if (result.type === "redirect") {
|
|
198
|
+
const snapshot: RouteSnapshot<TEnv> = {
|
|
199
|
+
matched: result as any,
|
|
200
|
+
manifestEntry: null as any,
|
|
201
|
+
entries: [],
|
|
202
|
+
routeKey: "",
|
|
203
|
+
localRouteName: "",
|
|
204
|
+
params: {},
|
|
205
|
+
routeMiddleware: [],
|
|
206
|
+
cacheScope: null,
|
|
207
|
+
isPassthrough: false,
|
|
208
|
+
};
|
|
209
|
+
return {
|
|
210
|
+
mode: "redirect",
|
|
211
|
+
route: snapshot,
|
|
212
|
+
redirectUrl: result.redirectTo + url.search,
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const snapshot = result.snapshot;
|
|
217
|
+
|
|
218
|
+
// Response route — non-RSC short-circuit (JSON, streaming, etc.)
|
|
219
|
+
const responseResult = await classifyResponseRoute(
|
|
220
|
+
request,
|
|
221
|
+
pathname,
|
|
222
|
+
snapshot,
|
|
223
|
+
);
|
|
224
|
+
if (responseResult) {
|
|
225
|
+
return responseResult;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// Mode detection from request signals
|
|
229
|
+
const actionId =
|
|
230
|
+
request.headers.get("rsc-action") || url.searchParams.get("_rsc_action");
|
|
231
|
+
const isLoaderFetch = url.searchParams.has("_rsc_loader");
|
|
232
|
+
|
|
233
|
+
const hasVariants =
|
|
234
|
+
snapshot.matched.negotiateVariants &&
|
|
235
|
+
snapshot.matched.negotiateVariants.length > 0;
|
|
236
|
+
const negotiated = !!hasVariants;
|
|
237
|
+
|
|
238
|
+
if (isAction && actionId) {
|
|
239
|
+
return { mode: "action", route: snapshot, actionId, negotiated };
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
if (isLoaderFetch) {
|
|
243
|
+
return { mode: "loader", route: snapshot };
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// PE detection: POST with form content-type, but not a server action
|
|
247
|
+
const contentType = request.headers.get("content-type") || "";
|
|
248
|
+
const isFormSubmission =
|
|
249
|
+
contentType.includes("multipart/form-data") ||
|
|
250
|
+
contentType.includes("application/x-www-form-urlencoded");
|
|
251
|
+
if (request.method === "POST" && !isAction && isFormSubmission) {
|
|
252
|
+
return { mode: "pe-render", route: snapshot };
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// App switch: client's routerId doesn't match this router
|
|
256
|
+
const clientRouterId = url.searchParams.get("_rsc_rid");
|
|
257
|
+
const isAppSwitch = !!(clientRouterId && clientRouterId !== deps.routerId);
|
|
258
|
+
const isPartial = url.searchParams.has("_rsc_partial") && !isAppSwitch;
|
|
259
|
+
|
|
260
|
+
if (isPartial) {
|
|
261
|
+
return { mode: "partial-render", route: snapshot, negotiated };
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
return { mode: "full-render", route: snapshot, negotiated };
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// ---------------------------------------------------------------------------
|
|
268
|
+
// Content negotiation for response routes
|
|
269
|
+
// ---------------------------------------------------------------------------
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Check if the route is a response route and perform content negotiation
|
|
273
|
+
* if negotiate variants exist. Returns a ResponseRoutePlan if the route
|
|
274
|
+
* is a response route, null otherwise (RSC route).
|
|
275
|
+
*/
|
|
276
|
+
async function classifyResponseRoute<TEnv>(
|
|
277
|
+
request: Request,
|
|
278
|
+
pathname: string,
|
|
279
|
+
snapshot: RouteSnapshot<TEnv>,
|
|
280
|
+
): Promise<ResponseRoutePlan<TEnv> | null> {
|
|
281
|
+
const { manifestEntry, responseType } = snapshot;
|
|
282
|
+
|
|
283
|
+
const negotiation = await negotiateRoute(request, pathname, snapshot);
|
|
284
|
+
if (negotiation) {
|
|
285
|
+
return {
|
|
286
|
+
mode: "response",
|
|
287
|
+
route: snapshot,
|
|
288
|
+
...negotiation,
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// Non-negotiated response route (no variants, or RSC won negotiation)
|
|
293
|
+
if (responseType) {
|
|
294
|
+
const handler =
|
|
295
|
+
manifestEntry.type === "route" ? manifestEntry.handler : undefined;
|
|
296
|
+
if (handler) {
|
|
297
|
+
return {
|
|
298
|
+
mode: "response",
|
|
299
|
+
route: snapshot,
|
|
300
|
+
handler,
|
|
301
|
+
responseType,
|
|
302
|
+
negotiated: false,
|
|
303
|
+
manifestEntry,
|
|
304
|
+
routeMiddleware: snapshot.routeMiddleware,
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
return null;
|
|
310
|
+
}
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Route Snapshot
|
|
3
|
+
*
|
|
4
|
+
* Pure data type representing the fully-resolved state of a single route match.
|
|
5
|
+
* Consolidates the duplicated findMatch + loadManifest + collectRouteMiddleware +
|
|
6
|
+
* cacheScope derivation that previously lived separately in preview-match.ts
|
|
7
|
+
* and match-api.ts.
|
|
8
|
+
*
|
|
9
|
+
* resolveRoute() is the factory: given a pathname and dependencies, it returns
|
|
10
|
+
* a RouteSnapshot (or redirect/null). Consumers (createMatchContextForFull,
|
|
11
|
+
* createMatchContextForPartial, previewMatch) read snapshot fields instead of
|
|
12
|
+
* re-deriving them.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import type { CacheScope } from "../cache/cache-scope.js";
|
|
16
|
+
import { createCacheScope } from "../cache/cache-scope.js";
|
|
17
|
+
import type { EntryData, MetricsStore } from "../server/context.js";
|
|
18
|
+
import { loadManifest } from "./manifest.js";
|
|
19
|
+
import { collectRouteMiddleware } from "./middleware.js";
|
|
20
|
+
import type { CollectedMiddleware } from "./middleware-types.js";
|
|
21
|
+
import { traverseBack } from "./pattern-matching.js";
|
|
22
|
+
import type { RouteMatchResult } from "./pattern-matching.js";
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Immutable snapshot of a resolved route match.
|
|
26
|
+
*
|
|
27
|
+
* Contains everything derivable from (pathname, findMatch, loadManifest)
|
|
28
|
+
* without request context, navigation state, or intercept logic.
|
|
29
|
+
*/
|
|
30
|
+
export interface RouteSnapshot<TEnv = any> {
|
|
31
|
+
/** Raw match result from the trie/pattern matcher */
|
|
32
|
+
matched: RouteMatchResult<TEnv>;
|
|
33
|
+
/** Resolved manifest entry (with loaded handler, loader, etc.) */
|
|
34
|
+
manifestEntry: EntryData;
|
|
35
|
+
/** All entries in the route chain (from traverseBack) */
|
|
36
|
+
entries: EntryData[];
|
|
37
|
+
/** Canonical route key (e.g. "blog.detail") */
|
|
38
|
+
routeKey: string;
|
|
39
|
+
/** Last segment of a dotted route key (e.g. "detail" from "blog.detail") */
|
|
40
|
+
localRouteName: string;
|
|
41
|
+
/** Extracted route params */
|
|
42
|
+
params: Record<string, string>;
|
|
43
|
+
/** Collected route-level middleware from the entry tree */
|
|
44
|
+
routeMiddleware: CollectedMiddleware[];
|
|
45
|
+
/** Merged cache scope from the entry chain */
|
|
46
|
+
cacheScope: CacheScope | null;
|
|
47
|
+
/** Whether the matched route is a passthrough route */
|
|
48
|
+
isPassthrough: boolean;
|
|
49
|
+
/** Response type for non-RSC routes (e.g. "application/json") */
|
|
50
|
+
responseType?: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export type ResolveRouteResult<TEnv = any> =
|
|
54
|
+
| { type: "match"; snapshot: RouteSnapshot<TEnv> }
|
|
55
|
+
| { type: "redirect"; redirectTo: string }
|
|
56
|
+
| null;
|
|
57
|
+
|
|
58
|
+
export interface ResolveRouteDeps<TEnv = any> {
|
|
59
|
+
findMatch: (pathname: string) => RouteMatchResult<TEnv> | null;
|
|
60
|
+
metricsStore?: MetricsStore;
|
|
61
|
+
isSSR?: boolean;
|
|
62
|
+
/**
|
|
63
|
+
* When true, skip entries array and cacheScope chain construction.
|
|
64
|
+
* Used by previewMatch which only needs matched, manifestEntry,
|
|
65
|
+
* routeMiddleware, and responseType — avoids an extra traverseBack
|
|
66
|
+
* allocation and cacheScope composition on the hot classification path.
|
|
67
|
+
*/
|
|
68
|
+
lite?: boolean;
|
|
69
|
+
/**
|
|
70
|
+
* When true, skip pushing the "route-matching" metric internally.
|
|
71
|
+
* Used by createMatchContextForPartial on the fresh path (no snapshot
|
|
72
|
+
* reuse) so it can measure current + prev + intercept-source findMatch
|
|
73
|
+
* calls under one combined "route-matching" metric. On the reuse path,
|
|
74
|
+
* the partial path emits "route-matching:nav" for the prev +
|
|
75
|
+
* intercept-source lookups only (current-route resolution was done
|
|
76
|
+
* during classification without metrics).
|
|
77
|
+
*/
|
|
78
|
+
skipRouteMatchMetric?: boolean;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Resolve a pathname into a RouteSnapshot.
|
|
83
|
+
*
|
|
84
|
+
* This is the single source of truth for route derivation. It performs:
|
|
85
|
+
* 1. findMatch(pathname)
|
|
86
|
+
* 2. Redirect check
|
|
87
|
+
* 3. loadManifest
|
|
88
|
+
* 4. Passthrough detection
|
|
89
|
+
* 5. collectRouteMiddleware
|
|
90
|
+
* 6. Cache scope chain
|
|
91
|
+
* 7. responseType + localRouteName extraction
|
|
92
|
+
*
|
|
93
|
+
* Metrics timing is preserved identically to the previous inline code.
|
|
94
|
+
*/
|
|
95
|
+
export async function resolveRoute<TEnv = any>(
|
|
96
|
+
pathname: string,
|
|
97
|
+
deps: ResolveRouteDeps<TEnv>,
|
|
98
|
+
): Promise<ResolveRouteResult<TEnv>> {
|
|
99
|
+
const {
|
|
100
|
+
metricsStore,
|
|
101
|
+
isSSR = false,
|
|
102
|
+
lite = false,
|
|
103
|
+
skipRouteMatchMetric = false,
|
|
104
|
+
} = deps;
|
|
105
|
+
|
|
106
|
+
const routeMatchStart =
|
|
107
|
+
metricsStore && !skipRouteMatchMetric ? performance.now() : 0;
|
|
108
|
+
const matched = deps.findMatch(pathname);
|
|
109
|
+
if (metricsStore && !skipRouteMatchMetric) {
|
|
110
|
+
metricsStore.metrics.push({
|
|
111
|
+
label: "route-matching",
|
|
112
|
+
duration: performance.now() - routeMatchStart,
|
|
113
|
+
startTime: routeMatchStart - metricsStore.requestStart,
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (!matched) {
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (matched.redirectTo) {
|
|
122
|
+
return { type: "redirect", redirectTo: matched.redirectTo };
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const manifestStart = metricsStore ? performance.now() : 0;
|
|
126
|
+
const manifestEntry = await loadManifest(
|
|
127
|
+
matched.entry,
|
|
128
|
+
matched.routeKey,
|
|
129
|
+
pathname,
|
|
130
|
+
metricsStore,
|
|
131
|
+
isSSR,
|
|
132
|
+
);
|
|
133
|
+
if (metricsStore) {
|
|
134
|
+
metricsStore.metrics.push({
|
|
135
|
+
label: "manifest-loading",
|
|
136
|
+
duration: performance.now() - manifestStart,
|
|
137
|
+
startTime: manifestStart - metricsStore.requestStart,
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const isPassthrough =
|
|
142
|
+
manifestEntry.type === "route" && manifestEntry.isPassthrough === true;
|
|
143
|
+
|
|
144
|
+
let entries: EntryData[];
|
|
145
|
+
let cacheScope: CacheScope | null = null;
|
|
146
|
+
if (lite) {
|
|
147
|
+
entries = [];
|
|
148
|
+
} else {
|
|
149
|
+
({ entries, cacheScope } = buildEntriesAndCacheScope(manifestEntry));
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const routeMiddleware = collectRouteMiddleware(
|
|
153
|
+
lite ? traverseBack(manifestEntry) : entries,
|
|
154
|
+
matched.params,
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
const responseType =
|
|
158
|
+
matched.responseType ||
|
|
159
|
+
(manifestEntry.type === "route" ? manifestEntry.responseType : undefined);
|
|
160
|
+
|
|
161
|
+
const localRouteName = matched.routeKey.includes(".")
|
|
162
|
+
? matched.routeKey.split(".").pop()!
|
|
163
|
+
: matched.routeKey;
|
|
164
|
+
|
|
165
|
+
return {
|
|
166
|
+
type: "match",
|
|
167
|
+
snapshot: {
|
|
168
|
+
matched,
|
|
169
|
+
manifestEntry,
|
|
170
|
+
entries,
|
|
171
|
+
routeKey: matched.routeKey,
|
|
172
|
+
localRouteName,
|
|
173
|
+
params: matched.params,
|
|
174
|
+
routeMiddleware,
|
|
175
|
+
cacheScope,
|
|
176
|
+
isPassthrough,
|
|
177
|
+
responseType,
|
|
178
|
+
},
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Fill in the entries and cacheScope fields on a lite snapshot.
|
|
184
|
+
*
|
|
185
|
+
* When classifyRequest produces a lite snapshot (entries=[], cacheScope=null),
|
|
186
|
+
* this function computes the missing fields from manifestEntry without
|
|
187
|
+
* re-running findMatch, loadManifest, or collectRouteMiddleware.
|
|
188
|
+
*
|
|
189
|
+
* If the snapshot already has entries, returns it as-is.
|
|
190
|
+
*/
|
|
191
|
+
export function ensureFullRouteSnapshot<TEnv = any>(
|
|
192
|
+
snapshot: RouteSnapshot<TEnv>,
|
|
193
|
+
): RouteSnapshot<TEnv> {
|
|
194
|
+
if (snapshot.entries.length > 0) {
|
|
195
|
+
return snapshot;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const { entries, cacheScope } = buildEntriesAndCacheScope(
|
|
199
|
+
snapshot.manifestEntry,
|
|
200
|
+
);
|
|
201
|
+
return { ...snapshot, entries, cacheScope };
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Materialize the entry chain and derive the merged cache scope.
|
|
206
|
+
* Shared by resolveRoute (non-lite) and ensureFullRouteSnapshot.
|
|
207
|
+
*/
|
|
208
|
+
function buildEntriesAndCacheScope(manifestEntry: EntryData): {
|
|
209
|
+
entries: EntryData[];
|
|
210
|
+
cacheScope: CacheScope | null;
|
|
211
|
+
} {
|
|
212
|
+
const entries = [...traverseBack(manifestEntry)];
|
|
213
|
+
let cacheScope: CacheScope | null = null;
|
|
214
|
+
for (const entry of entries) {
|
|
215
|
+
if (entry.cache) {
|
|
216
|
+
cacheScope = createCacheScope(entry.cache, cacheScope);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return { entries, cacheScope };
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Test helper: create a RouteSnapshot with sensible defaults and overrides.
|
|
224
|
+
*/
|
|
225
|
+
export function createRouteSnapshot<TEnv = any>(
|
|
226
|
+
overrides?: Partial<RouteSnapshot<TEnv>>,
|
|
227
|
+
): RouteSnapshot<TEnv> {
|
|
228
|
+
return {
|
|
229
|
+
matched: {
|
|
230
|
+
entry: {} as any,
|
|
231
|
+
routeKey: "test",
|
|
232
|
+
params: {},
|
|
233
|
+
optionalParams: new Set(),
|
|
234
|
+
} as RouteMatchResult<TEnv>,
|
|
235
|
+
manifestEntry: { type: "route", shortCode: "R0", parent: null } as any,
|
|
236
|
+
entries: [],
|
|
237
|
+
routeKey: "test",
|
|
238
|
+
localRouteName: "test",
|
|
239
|
+
params: {},
|
|
240
|
+
routeMiddleware: [],
|
|
241
|
+
cacheScope: null,
|
|
242
|
+
isPassthrough: false,
|
|
243
|
+
...overrides,
|
|
244
|
+
};
|
|
245
|
+
}
|
|
@@ -374,6 +374,8 @@ export interface RSCRouterInternal<
|
|
|
374
374
|
params: Record<string, string>,
|
|
375
375
|
buildVars?: Record<string, any>,
|
|
376
376
|
isPassthroughRoute?: boolean,
|
|
377
|
+
buildEnv?: any,
|
|
378
|
+
devMode?: boolean,
|
|
377
379
|
): Promise<{
|
|
378
380
|
segments: SerializedSegmentData[];
|
|
379
381
|
handles: Record<string, SegmentHandleData>;
|
|
@@ -392,6 +394,8 @@ export interface RSCRouterInternal<
|
|
|
392
394
|
handler: Function,
|
|
393
395
|
handlerId: string,
|
|
394
396
|
routeName?: string,
|
|
397
|
+
buildEnv?: any,
|
|
398
|
+
devMode?: boolean,
|
|
395
399
|
): Promise<{ encoded: string; handles: Record<string, unknown[]> } | null>;
|
|
396
400
|
|
|
397
401
|
/**
|
|
@@ -445,6 +449,13 @@ export interface RSCRouterInternal<
|
|
|
445
449
|
segmentType?: ErrorInfo["segmentType"],
|
|
446
450
|
): Promise<MatchResult | null>;
|
|
447
451
|
|
|
452
|
+
/**
|
|
453
|
+
* Low-level route matching function.
|
|
454
|
+
* Used by classifyRequest() for request classification without
|
|
455
|
+
* entering the full match pipeline.
|
|
456
|
+
*/
|
|
457
|
+
findMatch(pathname: string, metricsStore?: any): any;
|
|
458
|
+
|
|
448
459
|
/**
|
|
449
460
|
* Debug utility to serialize the manifest for inspection
|
|
450
461
|
* Returns a JSON-friendly representation of all routes and layouts
|
|
@@ -284,9 +284,14 @@ export async function resolveSegment<TEnv>(
|
|
|
284
284
|
entry.shortCode,
|
|
285
285
|
);
|
|
286
286
|
if (component === undefined) {
|
|
287
|
+
// For Passthrough routes at runtime, use the live handler instead of
|
|
288
|
+
// the build handler. At build time (context.build === true), always
|
|
289
|
+
// use the build handler from entry.handler.
|
|
290
|
+
const handler =
|
|
291
|
+
!context.build && entry.liveHandler ? entry.liveHandler : entry.handler;
|
|
287
292
|
const doneRouteHandler = track(`handler:${entry.id}`, 2);
|
|
288
293
|
if (entry.loading) {
|
|
289
|
-
const result = handleHandlerResult(
|
|
294
|
+
const result = handleHandlerResult(handler(context));
|
|
290
295
|
if (result instanceof Promise) {
|
|
291
296
|
result.finally(doneRouteHandler).catch(() => {});
|
|
292
297
|
const tracked = deps.trackHandler(result, {
|
|
@@ -307,7 +312,7 @@ export async function resolveSegment<TEnv>(
|
|
|
307
312
|
component = result;
|
|
308
313
|
}
|
|
309
314
|
} else {
|
|
310
|
-
component = handleHandlerResult(await
|
|
315
|
+
component = handleHandlerResult(await handler(context));
|
|
311
316
|
doneRouteHandler();
|
|
312
317
|
}
|
|
313
318
|
}
|
|
@@ -322,6 +327,7 @@ export async function resolveSegment<TEnv>(
|
|
|
322
327
|
deps,
|
|
323
328
|
options,
|
|
324
329
|
routeKey,
|
|
330
|
+
entry,
|
|
325
331
|
);
|
|
326
332
|
segments.push(...orphanSegments);
|
|
327
333
|
}
|
|
@@ -377,6 +383,9 @@ export async function resolveOrphanLayout<TEnv>(
|
|
|
377
383
|
deps: SegmentResolutionDeps<TEnv>,
|
|
378
384
|
options?: ResolveSegmentOptions,
|
|
379
385
|
routeKey?: string,
|
|
386
|
+
/** Parent route entry — its loaders are inherited by the layout so
|
|
387
|
+
* parallel slots inside this layout can access them via useLoader(). */
|
|
388
|
+
parentRouteEntry?: EntryData,
|
|
380
389
|
): Promise<ResolvedSegment[]> {
|
|
381
390
|
invariant(
|
|
382
391
|
orphan.type === "layout" || orphan.type === "cache",
|
|
@@ -392,6 +401,26 @@ export async function resolveOrphanLayout<TEnv>(
|
|
|
392
401
|
deps,
|
|
393
402
|
);
|
|
394
403
|
segments.push(...loaderSegments);
|
|
404
|
+
|
|
405
|
+
// Inherit parent route's loaders so parallel slots inside this layout
|
|
406
|
+
// can access them via useLoader(). Without this, the route's loaders
|
|
407
|
+
// are only in the route's OutletProvider (rendered as <Outlet /> content),
|
|
408
|
+
// which is a child — not a parent — of the layout's context.
|
|
409
|
+
if (
|
|
410
|
+
parentRouteEntry &&
|
|
411
|
+
parentRouteEntry.loader &&
|
|
412
|
+
parentRouteEntry.loader.length > 0 &&
|
|
413
|
+
Object.keys(orphan.parallel).length > 0
|
|
414
|
+
) {
|
|
415
|
+
const inheritedLoaders = await resolveLoaders(
|
|
416
|
+
parentRouteEntry,
|
|
417
|
+
context,
|
|
418
|
+
belongsToRoute,
|
|
419
|
+
deps,
|
|
420
|
+
orphan.shortCode,
|
|
421
|
+
);
|
|
422
|
+
segments.push(...inheritedLoaders);
|
|
423
|
+
}
|
|
395
424
|
}
|
|
396
425
|
|
|
397
426
|
// Handler-first: orphan layout handler executes before its parallels
|
|
@@ -680,6 +709,19 @@ export async function resolveLoadersOnly<TEnv>(
|
|
|
680
709
|
const childBelongsToRoute = belongsToRoute || entry.type === "route";
|
|
681
710
|
for (const layoutEntry of entry.layout) {
|
|
682
711
|
await collectEntryLoaders(layoutEntry, childBelongsToRoute);
|
|
712
|
+
// Inherit route loaders for orphan layouts with parallels
|
|
713
|
+
if (
|
|
714
|
+
entry.type === "route" &&
|
|
715
|
+
entry.loader &&
|
|
716
|
+
entry.loader.length > 0 &&
|
|
717
|
+
Object.keys(layoutEntry.parallel).length > 0
|
|
718
|
+
) {
|
|
719
|
+
await collectEntryLoaders(
|
|
720
|
+
entry,
|
|
721
|
+
childBelongsToRoute,
|
|
722
|
+
layoutEntry.shortCode,
|
|
723
|
+
);
|
|
724
|
+
}
|
|
683
725
|
}
|
|
684
726
|
}
|
|
685
727
|
|