vinext 0.0.46 → 0.0.47
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 +7 -5
- package/dist/build/prerender.d.ts +2 -1
- package/dist/build/prerender.js +70 -14
- package/dist/build/prerender.js.map +1 -1
- package/dist/build/report.d.ts +1 -1
- package/dist/build/route-classification-injector.d.ts +35 -0
- package/dist/build/route-classification-injector.js +61 -0
- package/dist/build/route-classification-injector.js.map +1 -0
- package/dist/build/route-classification-manifest.d.ts +1 -1
- package/dist/build/static-export.d.ts +1 -1
- package/dist/cli-args.d.ts +31 -0
- package/dist/cli-args.js +104 -0
- package/dist/cli-args.js.map +1 -0
- package/dist/cli.js +2 -19
- package/dist/cli.js.map +1 -1
- package/dist/cloudflare/kv-cache-handler.js +29 -9
- package/dist/cloudflare/kv-cache-handler.js.map +1 -1
- package/dist/config/next-config.d.ts +4 -2
- package/dist/config/next-config.js +3 -0
- package/dist/config/next-config.js.map +1 -1
- package/dist/entries/app-rsc-entry.d.ts +4 -3
- package/dist/entries/app-rsc-entry.js +373 -854
- package/dist/entries/app-rsc-entry.js.map +1 -1
- package/dist/entries/app-rsc-manifest.d.ts +1 -1
- package/dist/entries/app-rsc-manifest.js +2 -0
- package/dist/entries/app-rsc-manifest.js.map +1 -1
- package/dist/entries/pages-server-entry.js +5 -2
- package/dist/entries/pages-server-entry.js.map +1 -1
- package/dist/index.js +28 -51
- package/dist/index.js.map +1 -1
- package/dist/plugins/fonts.js +54 -32
- package/dist/plugins/fonts.js.map +1 -1
- package/dist/plugins/rsc-client-shim-excludes.js +1 -0
- package/dist/plugins/rsc-client-shim-excludes.js.map +1 -1
- package/dist/routing/app-route-graph.d.ts +109 -0
- package/dist/routing/app-route-graph.js +819 -0
- package/dist/routing/app-route-graph.js.map +1 -0
- package/dist/routing/app-router.d.ts +2 -88
- package/dist/routing/app-router.js +6 -694
- package/dist/routing/app-router.js.map +1 -1
- package/dist/server/app-browser-entry.js +86 -252
- package/dist/server/app-browser-entry.js.map +1 -1
- package/dist/server/app-browser-error.d.ts +3 -4
- package/dist/server/app-browser-error.js +8 -4
- package/dist/server/app-browser-error.js.map +1 -1
- package/dist/server/app-browser-navigation-controller.d.ts +73 -0
- package/dist/server/app-browser-navigation-controller.js +282 -0
- package/dist/server/app-browser-navigation-controller.js.map +1 -0
- package/dist/server/app-browser-state.d.ts +1 -1
- package/dist/server/app-elements.js +1 -5
- package/dist/server/app-elements.js.map +1 -1
- package/dist/server/app-fallback-renderer.d.ts +57 -0
- package/dist/server/app-fallback-renderer.js +79 -0
- package/dist/server/app-fallback-renderer.js.map +1 -0
- package/dist/server/app-hook-warning-suppression.d.ts +7 -0
- package/dist/server/app-hook-warning-suppression.js +12 -0
- package/dist/server/app-hook-warning-suppression.js.map +1 -0
- package/dist/server/app-mounted-slots-header.d.ts +17 -0
- package/dist/server/app-mounted-slots-header.js +21 -0
- package/dist/server/app-mounted-slots-header.js.map +1 -0
- package/dist/server/app-page-boundary-render.d.ts +2 -2
- package/dist/server/app-page-boundary-render.js.map +1 -1
- package/dist/server/app-page-cache.d.ts +18 -4
- package/dist/server/app-page-cache.js +53 -10
- package/dist/server/app-page-cache.js.map +1 -1
- package/dist/server/app-page-dispatch.d.ts +7 -4
- package/dist/server/app-page-dispatch.js +24 -8
- package/dist/server/app-page-dispatch.js.map +1 -1
- package/dist/server/app-page-element-builder.d.ts +61 -0
- package/dist/server/app-page-element-builder.js +139 -0
- package/dist/server/app-page-element-builder.js.map +1 -0
- package/dist/server/app-page-params.d.ts +2 -1
- package/dist/server/app-page-params.js +3 -3
- package/dist/server/app-page-params.js.map +1 -1
- package/dist/server/app-page-render.d.ts +5 -1
- package/dist/server/app-page-render.js +80 -27
- package/dist/server/app-page-render.js.map +1 -1
- package/dist/server/app-page-request.d.ts +19 -4
- package/dist/server/app-page-request.js +51 -6
- package/dist/server/app-page-request.js.map +1 -1
- package/dist/server/app-page-response.d.ts +1 -0
- package/dist/server/app-page-response.js +3 -7
- package/dist/server/app-page-response.js.map +1 -1
- package/dist/server/app-page-route-wiring.d.ts +15 -2
- package/dist/server/app-page-route-wiring.js.map +1 -1
- package/dist/server/app-post-middleware-context.d.ts +16 -0
- package/dist/server/app-post-middleware-context.js +28 -0
- package/dist/server/app-post-middleware-context.js.map +1 -0
- package/dist/server/app-request-context.d.ts +22 -0
- package/dist/server/app-request-context.js +30 -0
- package/dist/server/app-request-context.js.map +1 -0
- package/dist/server/app-route-handler-cache.d.ts +1 -0
- package/dist/server/app-route-handler-cache.js +5 -1
- package/dist/server/app-route-handler-cache.js.map +1 -1
- package/dist/server/app-route-handler-dispatch.d.ts +1 -0
- package/dist/server/app-route-handler-dispatch.js +2 -0
- package/dist/server/app-route-handler-dispatch.js.map +1 -1
- package/dist/server/app-route-handler-execution.d.ts +2 -1
- package/dist/server/app-route-handler-execution.js +2 -2
- package/dist/server/app-route-handler-execution.js.map +1 -1
- package/dist/server/app-route-handler-response.d.ts +4 -2
- package/dist/server/app-route-handler-response.js +8 -7
- package/dist/server/app-route-handler-response.js.map +1 -1
- package/dist/server/app-rsc-error-handler.d.ts +21 -0
- package/dist/server/app-rsc-error-handler.js +30 -0
- package/dist/server/app-rsc-error-handler.js.map +1 -0
- package/dist/server/app-rsc-handler.d.ts +117 -0
- package/dist/server/app-rsc-handler.js +260 -0
- package/dist/server/app-rsc-handler.js.map +1 -0
- package/dist/server/app-rsc-request-normalization.d.ts +40 -0
- package/dist/server/app-rsc-request-normalization.js +63 -0
- package/dist/server/app-rsc-request-normalization.js.map +1 -0
- package/dist/server/app-rsc-response-finalizer.d.ts +30 -0
- package/dist/server/app-rsc-response-finalizer.js +38 -0
- package/dist/server/app-rsc-response-finalizer.js.map +1 -0
- package/dist/server/app-segment-config.d.ts +33 -0
- package/dist/server/app-segment-config.js +86 -0
- package/dist/server/app-segment-config.js.map +1 -0
- package/dist/server/app-server-action-execution.d.ts +2 -0
- package/dist/server/app-server-action-execution.js +2 -0
- package/dist/server/app-server-action-execution.js.map +1 -1
- package/dist/server/cache-control.d.ts +24 -0
- package/dist/server/cache-control.js +33 -0
- package/dist/server/cache-control.js.map +1 -0
- package/dist/server/dev-error-overlay-store.d.ts +23 -0
- package/dist/server/dev-error-overlay-store.js +67 -0
- package/dist/server/dev-error-overlay-store.js.map +1 -0
- package/dist/server/dev-error-overlay.d.ts +15 -0
- package/dist/server/dev-error-overlay.js +548 -0
- package/dist/server/dev-error-overlay.js.map +1 -0
- package/dist/server/instrumentation-runtime.d.ts +44 -0
- package/dist/server/instrumentation-runtime.js +29 -0
- package/dist/server/instrumentation-runtime.js.map +1 -0
- package/dist/server/isr-cache.d.ts +2 -7
- package/dist/server/isr-cache.js +7 -10
- package/dist/server/isr-cache.js.map +1 -1
- package/dist/server/pages-page-data.d.ts +2 -1
- package/dist/server/pages-page-data.js +6 -5
- package/dist/server/pages-page-data.js.map +1 -1
- package/dist/server/pages-page-response.d.ts +2 -1
- package/dist/server/pages-page-response.js +3 -2
- package/dist/server/pages-page-response.js.map +1 -1
- package/dist/server/rsc-stream-hints.d.ts +3 -1
- package/dist/server/rsc-stream-hints.js +4 -1
- package/dist/server/rsc-stream-hints.js.map +1 -1
- package/dist/server/seed-cache.js +19 -8
- package/dist/server/seed-cache.js.map +1 -1
- package/dist/shims/cache-runtime.js +28 -11
- package/dist/shims/cache-runtime.js.map +1 -1
- package/dist/shims/cache.d.ts +15 -3
- package/dist/shims/cache.js +42 -15
- package/dist/shims/cache.js.map +1 -1
- package/dist/shims/error-boundary.d.ts +17 -1
- package/dist/shims/error-boundary.js +31 -1
- package/dist/shims/error-boundary.js.map +1 -1
- package/dist/shims/fetch-cache.d.ts +4 -1
- package/dist/shims/fetch-cache.js +55 -13
- package/dist/shims/fetch-cache.js.map +1 -1
- package/dist/shims/image.js +93 -5
- package/dist/shims/image.js.map +1 -1
- package/dist/shims/request-state-types.d.ts +1 -1
- package/dist/shims/unified-request-context.d.ts +1 -1
- package/dist/shims/unified-request-context.js +1 -0
- package/dist/shims/unified-request-context.js.map +1 -1
- package/dist/shims/use-merged-ref.d.ts +7 -0
- package/dist/shims/use-merged-ref.js +40 -0
- package/dist/shims/use-merged-ref.js.map +1 -0
- package/dist/utils/cache-control-metadata.d.ts +6 -0
- package/dist/utils/cache-control-metadata.js +16 -0
- package/dist/utils/cache-control-metadata.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,819 @@
|
|
|
1
|
+
import { compareRoutes, decodeRouteSegment } from "./utils.js";
|
|
2
|
+
import { scanWithExtensions } from "./file-matcher.js";
|
|
3
|
+
import { validateRoutePatterns } from "./route-validation.js";
|
|
4
|
+
import fs from "node:fs";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
//#region src/routing/app-route-graph.ts
|
|
7
|
+
/**
|
|
8
|
+
* App Router route graph construction.
|
|
9
|
+
*
|
|
10
|
+
* Scans app/ directories and materializes route metadata before the request-time
|
|
11
|
+
* matcher consumes it. Keep request matching and cache ownership in app-router.ts.
|
|
12
|
+
*/
|
|
13
|
+
async function buildAppRouteGraph(appDir, matcher) {
|
|
14
|
+
const routes = [];
|
|
15
|
+
const excludeDir = (name) => name.startsWith("@") || name.startsWith("_");
|
|
16
|
+
for await (const file of scanWithExtensions("**/page", appDir, matcher.extensions, excludeDir)) {
|
|
17
|
+
const route = fileToAppRoute(file, appDir, "page", matcher);
|
|
18
|
+
if (route) routes.push(route);
|
|
19
|
+
}
|
|
20
|
+
for await (const file of scanWithExtensions("**/route", appDir, matcher.extensions, excludeDir)) {
|
|
21
|
+
const route = fileToAppRoute(file, appDir, "route", matcher);
|
|
22
|
+
if (route) routes.push(route);
|
|
23
|
+
}
|
|
24
|
+
const routePatterns = new Set(routes.map((route) => route.pattern));
|
|
25
|
+
for await (const file of scanWithExtensions("**/layout", appDir, matcher.extensions, excludeDir)) {
|
|
26
|
+
const dir = path.dirname(file);
|
|
27
|
+
const routeDir = dir === "." ? appDir : path.join(appDir, dir);
|
|
28
|
+
if (!hasParallelSlotDirectory(routeDir)) continue;
|
|
29
|
+
if (discoverParallelSlots(routeDir, appDir, matcher).length === 0) continue;
|
|
30
|
+
const route = directoryToAppRoute(dir, appDir, matcher, null, null);
|
|
31
|
+
if (!route || routePatterns.has(route.pattern)) continue;
|
|
32
|
+
routes.push(route);
|
|
33
|
+
routePatterns.add(route.pattern);
|
|
34
|
+
}
|
|
35
|
+
const slotSubRoutes = discoverSlotSubRoutes(routes, matcher);
|
|
36
|
+
routes.push(...slotSubRoutes);
|
|
37
|
+
validatePageRouteConflicts(routes, appDir);
|
|
38
|
+
validateRoutePatterns(routes.map((route) => route.pattern));
|
|
39
|
+
validateRoutePatterns([...new Set(routes.flatMap((route) => route.parallelSlots.flatMap((slot) => slot.interceptingRoutes.map((intercept) => intercept.targetPattern))))]);
|
|
40
|
+
routes.sort(compareRoutes);
|
|
41
|
+
return { routes };
|
|
42
|
+
}
|
|
43
|
+
function hasParallelSlotDirectory(dir) {
|
|
44
|
+
try {
|
|
45
|
+
return fs.readdirSync(dir, { withFileTypes: true }).some((entry) => entry.isDirectory() && entry.name.startsWith("@"));
|
|
46
|
+
} catch {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
function validatePageRouteConflicts(routes, appDir) {
|
|
51
|
+
const byPattern = /* @__PURE__ */ new Map();
|
|
52
|
+
for (const route of routes) {
|
|
53
|
+
const entry = byPattern.get(route.pattern);
|
|
54
|
+
if (!entry) {
|
|
55
|
+
byPattern.set(route.pattern, {
|
|
56
|
+
pagePath: route.pagePath,
|
|
57
|
+
routePath: route.routePath
|
|
58
|
+
});
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if (!entry.pagePath && route.pagePath) entry.pagePath = route.pagePath;
|
|
62
|
+
if (!entry.routePath && route.routePath) entry.routePath = route.routePath;
|
|
63
|
+
}
|
|
64
|
+
for (const [pattern, entry] of byPattern) {
|
|
65
|
+
if (!entry.pagePath || !entry.routePath) continue;
|
|
66
|
+
throw new Error(`Conflicting route and page at ${pattern}: route at ${formatAppFilePath(entry.routePath, appDir)} and page at ${formatAppFilePath(entry.pagePath, appDir)}`);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
function formatAppFilePath(filePath, appDir) {
|
|
70
|
+
const relativePath = path.relative(appDir, filePath).replace(/\\/g, "/");
|
|
71
|
+
const parsedPath = path.parse(relativePath);
|
|
72
|
+
const withoutExtension = path.join(parsedPath.dir, parsedPath.name).replace(/\\/g, "/");
|
|
73
|
+
return withoutExtension.startsWith("/") ? withoutExtension : `/${withoutExtension}`;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Discover sub-routes created by nested pages within parallel slots.
|
|
77
|
+
*
|
|
78
|
+
* In Next.js, pages nested inside @slot directories create additional URL routes.
|
|
79
|
+
* For example, given:
|
|
80
|
+
* app/parallel-routes/@audience/demographics/page.tsx
|
|
81
|
+
* This creates a route at /parallel-routes/demographics where:
|
|
82
|
+
* - children slot → parent's default.tsx
|
|
83
|
+
* - @audience slot → @audience/demographics/page.tsx (matched)
|
|
84
|
+
* - other slots → their default.tsx (fallback)
|
|
85
|
+
*/
|
|
86
|
+
function discoverSlotSubRoutes(routes, matcher) {
|
|
87
|
+
const syntheticRoutes = [];
|
|
88
|
+
const routesByPattern = new Map(routes.map((r) => [r.pattern, r]));
|
|
89
|
+
const applySlotSubPages = (route, slotPages, rawSegments) => {
|
|
90
|
+
route.parallelSlots = route.parallelSlots.map((slot) => {
|
|
91
|
+
const subPage = slotPages.get(slot.key);
|
|
92
|
+
if (subPage !== void 0) return {
|
|
93
|
+
...slot,
|
|
94
|
+
pagePath: subPage,
|
|
95
|
+
routeSegments: rawSegments
|
|
96
|
+
};
|
|
97
|
+
return slot;
|
|
98
|
+
});
|
|
99
|
+
};
|
|
100
|
+
for (const parentRoute of routes) {
|
|
101
|
+
if (parentRoute.parallelSlots.length === 0) continue;
|
|
102
|
+
if (!parentRoute.pagePath) continue;
|
|
103
|
+
const parentPageDir = path.dirname(parentRoute.pagePath);
|
|
104
|
+
const subPathMap = /* @__PURE__ */ new Map();
|
|
105
|
+
for (const slot of parentRoute.parallelSlots) {
|
|
106
|
+
if (path.dirname(slot.ownerDir) !== parentPageDir) continue;
|
|
107
|
+
const slotDir = slot.ownerDir;
|
|
108
|
+
if (!fs.existsSync(slotDir)) continue;
|
|
109
|
+
const subPages = findSlotSubPages(slotDir, matcher);
|
|
110
|
+
for (const { relativePath, pagePath } of subPages) {
|
|
111
|
+
const subSegments = relativePath.split(path.sep);
|
|
112
|
+
const convertedSubRoute = convertSegmentsToRouteParts(subSegments);
|
|
113
|
+
if (!convertedSubRoute) continue;
|
|
114
|
+
const { urlSegments } = convertedSubRoute;
|
|
115
|
+
const normalizedSubPath = urlSegments.join("/");
|
|
116
|
+
let subPathEntry = subPathMap.get(normalizedSubPath);
|
|
117
|
+
if (!subPathEntry) {
|
|
118
|
+
subPathEntry = {
|
|
119
|
+
rawSegments: subSegments,
|
|
120
|
+
converted: convertedSubRoute,
|
|
121
|
+
slotPages: /* @__PURE__ */ new Map()
|
|
122
|
+
};
|
|
123
|
+
subPathMap.set(normalizedSubPath, subPathEntry);
|
|
124
|
+
}
|
|
125
|
+
if (subPathEntry.slotPages.get(slot.key)) {
|
|
126
|
+
const pattern = joinRoutePattern(parentRoute.pattern, normalizedSubPath);
|
|
127
|
+
throw new Error(`You cannot have two routes that resolve to the same path ("${pattern}").`);
|
|
128
|
+
}
|
|
129
|
+
subPathEntry.slotPages.set(slot.key, pagePath);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
if (subPathMap.size === 0) continue;
|
|
133
|
+
const childrenDefault = findFile(parentPageDir, "default", matcher);
|
|
134
|
+
if (!childrenDefault) continue;
|
|
135
|
+
for (const { rawSegments, converted: convertedSubRoute, slotPages } of subPathMap.values()) {
|
|
136
|
+
const { urlSegments: urlParts, params: subParams, isDynamic: subIsDynamic } = convertedSubRoute;
|
|
137
|
+
const subUrlPath = urlParts.join("/");
|
|
138
|
+
const pattern = joinRoutePattern(parentRoute.pattern, subUrlPath);
|
|
139
|
+
const existingRoute = routesByPattern.get(pattern);
|
|
140
|
+
if (existingRoute) {
|
|
141
|
+
if (existingRoute.routePath && !existingRoute.pagePath) throw new Error(`You cannot have two routes that resolve to the same path ("${pattern}").`);
|
|
142
|
+
applySlotSubPages(existingRoute, slotPages, rawSegments);
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
const subSlots = parentRoute.parallelSlots.map((slot) => {
|
|
146
|
+
const subPage = slotPages.get(slot.key);
|
|
147
|
+
return {
|
|
148
|
+
...slot,
|
|
149
|
+
pagePath: subPage || null,
|
|
150
|
+
routeSegments: subPage ? rawSegments : null
|
|
151
|
+
};
|
|
152
|
+
});
|
|
153
|
+
const newRoute = {
|
|
154
|
+
pattern,
|
|
155
|
+
pagePath: childrenDefault,
|
|
156
|
+
routePath: null,
|
|
157
|
+
layouts: parentRoute.layouts,
|
|
158
|
+
templates: parentRoute.templates,
|
|
159
|
+
parallelSlots: subSlots,
|
|
160
|
+
loadingPath: parentRoute.loadingPath,
|
|
161
|
+
errorPath: parentRoute.errorPath,
|
|
162
|
+
layoutErrorPaths: parentRoute.layoutErrorPaths,
|
|
163
|
+
notFoundPath: parentRoute.notFoundPath,
|
|
164
|
+
notFoundPaths: parentRoute.notFoundPaths,
|
|
165
|
+
forbiddenPaths: parentRoute.forbiddenPaths,
|
|
166
|
+
forbiddenPath: parentRoute.forbiddenPath,
|
|
167
|
+
unauthorizedPath: parentRoute.unauthorizedPath,
|
|
168
|
+
unauthorizedPaths: parentRoute.unauthorizedPaths,
|
|
169
|
+
routeSegments: [...parentRoute.routeSegments, ...rawSegments],
|
|
170
|
+
templateTreePositions: parentRoute.templateTreePositions,
|
|
171
|
+
layoutTreePositions: parentRoute.layoutTreePositions,
|
|
172
|
+
isDynamic: parentRoute.isDynamic || subIsDynamic,
|
|
173
|
+
params: [...parentRoute.params, ...subParams],
|
|
174
|
+
rootParamNames: parentRoute.rootParamNames,
|
|
175
|
+
patternParts: [...parentRoute.patternParts, ...urlParts]
|
|
176
|
+
};
|
|
177
|
+
syntheticRoutes.push(newRoute);
|
|
178
|
+
routesByPattern.set(pattern, newRoute);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return syntheticRoutes;
|
|
182
|
+
}
|
|
183
|
+
const findSlotSubPagesCache = /* @__PURE__ */ new WeakMap();
|
|
184
|
+
function findSlotSubPages(slotDir, matcher) {
|
|
185
|
+
let perMatcher = findSlotSubPagesCache.get(matcher);
|
|
186
|
+
if (!perMatcher) {
|
|
187
|
+
perMatcher = /* @__PURE__ */ new Map();
|
|
188
|
+
findSlotSubPagesCache.set(matcher, perMatcher);
|
|
189
|
+
}
|
|
190
|
+
const cached = perMatcher.get(slotDir);
|
|
191
|
+
if (cached) return cached;
|
|
192
|
+
const results = [];
|
|
193
|
+
function scan(dir) {
|
|
194
|
+
if (!fs.existsSync(dir)) return;
|
|
195
|
+
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
196
|
+
for (const entry of entries) {
|
|
197
|
+
if (!entry.isDirectory()) continue;
|
|
198
|
+
if (matchInterceptConvention(entry.name)) continue;
|
|
199
|
+
if (entry.name.startsWith("_")) continue;
|
|
200
|
+
const subDir = path.join(dir, entry.name);
|
|
201
|
+
const page = findFile(subDir, "page", matcher);
|
|
202
|
+
if (page) {
|
|
203
|
+
const relativePath = path.relative(slotDir, subDir);
|
|
204
|
+
results.push({
|
|
205
|
+
relativePath,
|
|
206
|
+
pagePath: page
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
scan(subDir);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
scan(slotDir);
|
|
213
|
+
perMatcher.set(slotDir, results);
|
|
214
|
+
return results;
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Convert a file path relative to app/ into an AppRoute.
|
|
218
|
+
*/
|
|
219
|
+
function fileToAppRoute(file, appDir, type, matcher) {
|
|
220
|
+
return directoryToAppRoute(path.dirname(file), appDir, matcher, type === "page" ? path.join(appDir, file) : null, type === "route" ? path.join(appDir, file) : null);
|
|
221
|
+
}
|
|
222
|
+
function directoryToAppRoute(dir, appDir, matcher, pagePath, routePath) {
|
|
223
|
+
const segments = dir === "." ? [] : dir.split(path.sep);
|
|
224
|
+
const params = [];
|
|
225
|
+
let isDynamic = false;
|
|
226
|
+
const convertedRoute = convertSegmentsToRouteParts(segments);
|
|
227
|
+
if (!convertedRoute) return null;
|
|
228
|
+
const { urlSegments, params: routeParams, isDynamic: routeIsDynamic } = convertedRoute;
|
|
229
|
+
params.push(...routeParams);
|
|
230
|
+
isDynamic = routeIsDynamic;
|
|
231
|
+
const pattern = "/" + urlSegments.join("/");
|
|
232
|
+
const layouts = discoverLayouts(segments, appDir, matcher);
|
|
233
|
+
const templates = discoverTemplates(segments, appDir, matcher);
|
|
234
|
+
const templateTreePositions = computeLayoutTreePositions(appDir, templates);
|
|
235
|
+
const layoutTreePositions = computeLayoutTreePositions(appDir, layouts);
|
|
236
|
+
const layoutErrorPaths = discoverLayoutAlignedErrors(segments, appDir, matcher);
|
|
237
|
+
const routeDir = dir === "." ? appDir : path.join(appDir, dir);
|
|
238
|
+
const loadingPath = findFile(routeDir, "loading", matcher);
|
|
239
|
+
const errorPath = findFile(routeDir, "error", matcher);
|
|
240
|
+
const notFoundPath = discoverBoundaryFile(segments, appDir, "not-found", matcher);
|
|
241
|
+
const forbiddenPath = discoverBoundaryFile(segments, appDir, "forbidden", matcher);
|
|
242
|
+
const unauthorizedPath = discoverBoundaryFile(segments, appDir, "unauthorized", matcher);
|
|
243
|
+
const notFoundPaths = discoverBoundaryFilePerLayout(layouts, "not-found", matcher);
|
|
244
|
+
const forbiddenPaths = discoverBoundaryFilePerLayout(layouts, "forbidden", matcher);
|
|
245
|
+
const unauthorizedPaths = discoverBoundaryFilePerLayout(layouts, "unauthorized", matcher);
|
|
246
|
+
const parallelSlots = discoverInheritedParallelSlots(segments, appDir, routeDir, matcher);
|
|
247
|
+
return {
|
|
248
|
+
pattern: pattern === "/" ? "/" : pattern,
|
|
249
|
+
pagePath,
|
|
250
|
+
routePath,
|
|
251
|
+
layouts,
|
|
252
|
+
templates,
|
|
253
|
+
parallelSlots,
|
|
254
|
+
loadingPath,
|
|
255
|
+
errorPath,
|
|
256
|
+
layoutErrorPaths,
|
|
257
|
+
notFoundPath,
|
|
258
|
+
notFoundPaths,
|
|
259
|
+
forbiddenPaths,
|
|
260
|
+
forbiddenPath,
|
|
261
|
+
unauthorizedPath,
|
|
262
|
+
unauthorizedPaths,
|
|
263
|
+
routeSegments: segments,
|
|
264
|
+
templateTreePositions,
|
|
265
|
+
layoutTreePositions,
|
|
266
|
+
isDynamic,
|
|
267
|
+
params,
|
|
268
|
+
rootParamNames: computeRootParamNames(segments, layoutTreePositions),
|
|
269
|
+
patternParts: urlSegments
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
function dynamicParamNameFromSegment(segment) {
|
|
273
|
+
if (segment.startsWith("[[...") && segment.endsWith("]]")) return segment.slice(5, -2);
|
|
274
|
+
if (segment.startsWith("[...") && segment.endsWith("]")) return segment.slice(4, -1);
|
|
275
|
+
if (segment.startsWith("[") && segment.endsWith("]")) return segment.slice(1, -1);
|
|
276
|
+
return null;
|
|
277
|
+
}
|
|
278
|
+
function computeRootParamNames(routeSegments, layoutTreePositions) {
|
|
279
|
+
const rootLayoutPosition = layoutTreePositions[0];
|
|
280
|
+
if (rootLayoutPosition == null || rootLayoutPosition <= 0) return [];
|
|
281
|
+
const names = [];
|
|
282
|
+
for (const segment of routeSegments.slice(0, rootLayoutPosition)) {
|
|
283
|
+
const name = dynamicParamNameFromSegment(segment);
|
|
284
|
+
if (name && !names.includes(name)) names.push(name);
|
|
285
|
+
}
|
|
286
|
+
return names;
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Compute the tree position (directory depth from app root) for each layout.
|
|
290
|
+
* Root layout = 0, a layout at app/blog/ = 1, app/blog/(group)/ = 2.
|
|
291
|
+
* Counts ALL directory levels including route groups and parallel slots.
|
|
292
|
+
*/
|
|
293
|
+
function computeLayoutTreePositions(appDir, layouts) {
|
|
294
|
+
return layouts.map((layoutPath) => {
|
|
295
|
+
const layoutDir = path.dirname(layoutPath);
|
|
296
|
+
if (layoutDir === appDir) return 0;
|
|
297
|
+
return path.relative(appDir, layoutDir).split(path.sep).length;
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* Discover all layout files from root to the given directory.
|
|
302
|
+
* Each level of the directory tree may have a layout.tsx.
|
|
303
|
+
*/
|
|
304
|
+
function discoverLayouts(segments, appDir, matcher) {
|
|
305
|
+
const layouts = [];
|
|
306
|
+
const rootLayout = findFile(appDir, "layout", matcher);
|
|
307
|
+
if (rootLayout) layouts.push(rootLayout);
|
|
308
|
+
let currentDir = appDir;
|
|
309
|
+
for (const segment of segments) {
|
|
310
|
+
currentDir = path.join(currentDir, segment);
|
|
311
|
+
const layout = findFile(currentDir, "layout", matcher);
|
|
312
|
+
if (layout) layouts.push(layout);
|
|
313
|
+
}
|
|
314
|
+
return layouts;
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Discover all template files from root to the given directory.
|
|
318
|
+
* Each level of the directory tree may have a template.tsx.
|
|
319
|
+
* Templates are like layouts but re-mount on navigation.
|
|
320
|
+
*/
|
|
321
|
+
function discoverTemplates(segments, appDir, matcher) {
|
|
322
|
+
const templates = [];
|
|
323
|
+
const rootTemplate = findFile(appDir, "template", matcher);
|
|
324
|
+
if (rootTemplate) templates.push(rootTemplate);
|
|
325
|
+
let currentDir = appDir;
|
|
326
|
+
for (const segment of segments) {
|
|
327
|
+
currentDir = path.join(currentDir, segment);
|
|
328
|
+
const template = findFile(currentDir, "template", matcher);
|
|
329
|
+
if (template) templates.push(template);
|
|
330
|
+
}
|
|
331
|
+
return templates;
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Discover error.tsx files aligned with the layouts array.
|
|
335
|
+
* Walks the same directory levels as discoverLayouts and, for each level
|
|
336
|
+
* that contributes a layout entry, checks whether error.tsx also exists.
|
|
337
|
+
* Returns an array of the same length as discoverLayouts() would return,
|
|
338
|
+
* with the error path (or null) at each corresponding layout level.
|
|
339
|
+
*
|
|
340
|
+
* This enables interleaving ErrorBoundary components with layouts in the
|
|
341
|
+
* rendering tree, matching Next.js behavior where each segment independently
|
|
342
|
+
* wraps its children with an error boundary.
|
|
343
|
+
*/
|
|
344
|
+
function discoverLayoutAlignedErrors(segments, appDir, matcher) {
|
|
345
|
+
const errors = [];
|
|
346
|
+
if (findFile(appDir, "layout", matcher)) errors.push(findFile(appDir, "error", matcher));
|
|
347
|
+
let currentDir = appDir;
|
|
348
|
+
for (const segment of segments) {
|
|
349
|
+
currentDir = path.join(currentDir, segment);
|
|
350
|
+
if (findFile(currentDir, "layout", matcher)) errors.push(findFile(currentDir, "error", matcher));
|
|
351
|
+
}
|
|
352
|
+
return errors;
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* Discover the nearest boundary file (not-found, forbidden, unauthorized)
|
|
356
|
+
* by walking from the route's directory up to the app root.
|
|
357
|
+
* Returns the first (closest) file found, or null.
|
|
358
|
+
*/
|
|
359
|
+
function discoverBoundaryFile(segments, appDir, fileName, matcher) {
|
|
360
|
+
const dirs = [];
|
|
361
|
+
let dir = appDir;
|
|
362
|
+
dirs.push(dir);
|
|
363
|
+
for (const segment of segments) {
|
|
364
|
+
dir = path.join(dir, segment);
|
|
365
|
+
dirs.push(dir);
|
|
366
|
+
}
|
|
367
|
+
for (let i = dirs.length - 1; i >= 0; i--) {
|
|
368
|
+
const f = findFile(dirs[i], fileName, matcher);
|
|
369
|
+
if (f) return f;
|
|
370
|
+
}
|
|
371
|
+
return null;
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* Discover boundary files (not-found, forbidden, unauthorized) at each layout directory.
|
|
375
|
+
* Returns an array aligned with the layouts array, where each entry is the boundary
|
|
376
|
+
* file at that layout's directory, or null if none exists there.
|
|
377
|
+
*
|
|
378
|
+
* This is used for per-layout error boundaries. In Next.js, each layout level
|
|
379
|
+
* has its own boundary that wraps the layout's children. When notFound() is thrown
|
|
380
|
+
* from a layout, it propagates up to the parent layout's boundary.
|
|
381
|
+
*/
|
|
382
|
+
function discoverBoundaryFilePerLayout(layouts, fileName, matcher) {
|
|
383
|
+
return layouts.map((layoutPath) => {
|
|
384
|
+
return findFile(path.dirname(layoutPath), fileName, matcher);
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* Discover parallel slots inherited from ancestor directories.
|
|
389
|
+
*
|
|
390
|
+
* In Next.js, parallel slots belong to the layout that defines them. When a
|
|
391
|
+
* child route is rendered, its parent layout's slots must still be present.
|
|
392
|
+
* If the child doesn't have matching content in a slot, the slot's default.tsx
|
|
393
|
+
* is rendered instead.
|
|
394
|
+
*
|
|
395
|
+
* Walk from appDir through each segment to the route's directory. At each level
|
|
396
|
+
* that has @slot dirs, collect them. Slots at the route's own directory level
|
|
397
|
+
* use page.tsx; slots at ancestor levels use default.tsx only.
|
|
398
|
+
*/
|
|
399
|
+
function discoverInheritedParallelSlots(segments, appDir, routeDir, matcher) {
|
|
400
|
+
const slotMap = /* @__PURE__ */ new Map();
|
|
401
|
+
let currentDir = appDir;
|
|
402
|
+
const dirsToCheck = [];
|
|
403
|
+
let layoutIdx = findFile(appDir, "layout", matcher) ? 0 : -1;
|
|
404
|
+
dirsToCheck.push({
|
|
405
|
+
dir: appDir,
|
|
406
|
+
layoutIdx,
|
|
407
|
+
segmentIndex: 0
|
|
408
|
+
});
|
|
409
|
+
for (let i = 0; i < segments.length; i++) {
|
|
410
|
+
currentDir = path.join(currentDir, segments[i]);
|
|
411
|
+
if (findFile(currentDir, "layout", matcher)) layoutIdx++;
|
|
412
|
+
dirsToCheck.push({
|
|
413
|
+
dir: currentDir,
|
|
414
|
+
layoutIdx,
|
|
415
|
+
segmentIndex: i + 1
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
const routeHasLayout = layoutIdx >= 0;
|
|
419
|
+
for (const { dir, layoutIdx: lvlLayoutIdx, segmentIndex } of dirsToCheck) {
|
|
420
|
+
if (lvlLayoutIdx < 0 && routeHasLayout) continue;
|
|
421
|
+
const isOwnDir = dir === routeDir;
|
|
422
|
+
const slotLayoutIdx = Math.max(lvlLayoutIdx, 0);
|
|
423
|
+
const slotsAtLevel = discoverParallelSlots(dir, appDir, matcher);
|
|
424
|
+
const segmentsBelow = segments.slice(segmentIndex);
|
|
425
|
+
for (const slot of slotsAtLevel) if (isOwnDir) {
|
|
426
|
+
slot.layoutIndex = slotLayoutIdx;
|
|
427
|
+
slotMap.set(slot.key, slot);
|
|
428
|
+
} else {
|
|
429
|
+
const mirror = findMirroredSlotPage(slot.ownerDir, segmentsBelow, matcher);
|
|
430
|
+
let slotPatternParts;
|
|
431
|
+
let slotParamNames;
|
|
432
|
+
if (mirror) {
|
|
433
|
+
const ownerUrl = convertSegmentsToRouteParts([...segments.slice(0, segmentIndex)]);
|
|
434
|
+
slotPatternParts = [...ownerUrl?.urlSegments ?? [], ...mirror.slotUrlSegments];
|
|
435
|
+
slotParamNames = [...ownerUrl?.params ?? [], ...mirror.slotParamNames];
|
|
436
|
+
}
|
|
437
|
+
const inheritedSlot = {
|
|
438
|
+
...slot,
|
|
439
|
+
pagePath: mirror?.pagePath ?? null,
|
|
440
|
+
layoutIndex: slotLayoutIdx,
|
|
441
|
+
routeSegments: mirror?.segments ?? null,
|
|
442
|
+
slotPatternParts,
|
|
443
|
+
slotParamNames
|
|
444
|
+
};
|
|
445
|
+
slotMap.set(slot.key, inheritedSlot);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
return Array.from(slotMap.values());
|
|
449
|
+
}
|
|
450
|
+
/**
|
|
451
|
+
* Look for a page file inside a parallel slot directory that mirrors the
|
|
452
|
+
* route's path below the slot's owner. The match falls through two tiers:
|
|
453
|
+
* 1. Literal filesystem path — fast path when route and slot share shape.
|
|
454
|
+
* 2. Scored pattern compatibility — enumerate sub-pages, accept those
|
|
455
|
+
* whose URL pattern can match the route's URL space (slot dynamic
|
|
456
|
+
* markers may have different names than the route's, and slot
|
|
457
|
+
* catch-alls may subsume the route), and pick the most-specific via
|
|
458
|
+
* `scoreSlotPattern`. Exact URL-parts equality (e.g. through route
|
|
459
|
+
* groups appearing on only one side, like `(marketing)/about` ↔
|
|
460
|
+
* `@breadcrumbs/about`) naturally wins because all literal segments
|
|
461
|
+
* score highest.
|
|
462
|
+
*
|
|
463
|
+
* Returns the slot sub-page's absolute path, its raw filesystem segments
|
|
464
|
+
* (for `routeSegments`), and its URL parts / param names (for
|
|
465
|
+
* `slotPatternParts` / `slotParamNames`). Returns null when no mirror matches.
|
|
466
|
+
*/
|
|
467
|
+
function findMirroredSlotPage(slotDir, segmentsBelow, matcher) {
|
|
468
|
+
if (segmentsBelow.length === 0) return null;
|
|
469
|
+
const routeUrl = convertSegmentsToRouteParts([...segmentsBelow]);
|
|
470
|
+
const literalPage = findFile(path.join(slotDir, ...segmentsBelow), "page", matcher);
|
|
471
|
+
if (literalPage) return {
|
|
472
|
+
pagePath: literalPage,
|
|
473
|
+
segments: [...segmentsBelow],
|
|
474
|
+
slotUrlSegments: routeUrl?.urlSegments ?? [],
|
|
475
|
+
slotParamNames: routeUrl?.params ?? []
|
|
476
|
+
};
|
|
477
|
+
if (!routeUrl || routeUrl.urlSegments.length === 0) return null;
|
|
478
|
+
let best = null;
|
|
479
|
+
for (const { relativePath, pagePath } of findSlotSubPages(slotDir, matcher)) {
|
|
480
|
+
const slotSegments = relativePath.split(path.sep);
|
|
481
|
+
const slotUrl = convertSegmentsToRouteParts(slotSegments);
|
|
482
|
+
if (!slotUrl) continue;
|
|
483
|
+
if (!patternsCompatible(slotUrl.urlSegments, routeUrl.urlSegments)) continue;
|
|
484
|
+
const score = scoreSlotPattern(slotUrl.urlSegments);
|
|
485
|
+
if (!best || score > best.score) best = {
|
|
486
|
+
pagePath,
|
|
487
|
+
segments: slotSegments,
|
|
488
|
+
slotUrlSegments: slotUrl.urlSegments,
|
|
489
|
+
slotParamNames: slotUrl.params,
|
|
490
|
+
score
|
|
491
|
+
};
|
|
492
|
+
}
|
|
493
|
+
return best;
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
496
|
+
* Whether a slot pattern can match the same URL space as the route's URL
|
|
497
|
+
* parts (where the route's parts are themselves a pattern, since a route
|
|
498
|
+
* file like `[id]/page.tsx` produces `:id`).
|
|
499
|
+
*
|
|
500
|
+
* - `:name+` (catch-all) consumes one-or-more remaining segments.
|
|
501
|
+
* - `:name*` (optional catch-all) consumes zero-or-more.
|
|
502
|
+
* - `:name` (single dynamic) consumes exactly one segment, matching any
|
|
503
|
+
* route segment (literal or dynamic).
|
|
504
|
+
* - Literal slot segments must equal the route's segment exactly; a literal
|
|
505
|
+
* slot segment paired with a dynamic route segment is rejected because we
|
|
506
|
+
* can't know statically whether the runtime value will equal the literal.
|
|
507
|
+
* This also means a literal slot sub-page never matches a catch-all route
|
|
508
|
+
* (e.g. slot `about/page.tsx` is not bound to a route `[...slug]`) — the
|
|
509
|
+
* catch-all might or might not resolve to "about" at request time.
|
|
510
|
+
*/
|
|
511
|
+
function patternsCompatible(slotParts, routeParts) {
|
|
512
|
+
let i = 0;
|
|
513
|
+
let j = 0;
|
|
514
|
+
while (i < slotParts.length) {
|
|
515
|
+
const sp = slotParts[i];
|
|
516
|
+
if (sp.endsWith("+")) return j < routeParts.length;
|
|
517
|
+
if (sp.endsWith("*")) return true;
|
|
518
|
+
if (j >= routeParts.length) return false;
|
|
519
|
+
const rp = routeParts[j];
|
|
520
|
+
if (sp.startsWith(":")) {
|
|
521
|
+
i++;
|
|
522
|
+
j++;
|
|
523
|
+
continue;
|
|
524
|
+
}
|
|
525
|
+
if (rp.startsWith(":")) return false;
|
|
526
|
+
if (sp !== rp) return false;
|
|
527
|
+
i++;
|
|
528
|
+
j++;
|
|
529
|
+
}
|
|
530
|
+
return j === routeParts.length;
|
|
531
|
+
}
|
|
532
|
+
/**
|
|
533
|
+
* Score a slot pattern by specificity so the most-specific match wins:
|
|
534
|
+
* literal > single dynamic > catch-all > optional catch-all.
|
|
535
|
+
*
|
|
536
|
+
* Required catch-all (`:name+`, ≥1 segment) is more constrained than the
|
|
537
|
+
* optional variant (`:name*`, ≥0 segments), so it scores higher.
|
|
538
|
+
*/
|
|
539
|
+
function scoreSlotPattern(urlSegments) {
|
|
540
|
+
let score = 0;
|
|
541
|
+
for (const seg of urlSegments) if (seg.endsWith("*")) score += 1;
|
|
542
|
+
else if (seg.endsWith("+")) score += 2;
|
|
543
|
+
else if (seg.startsWith(":")) score += 3;
|
|
544
|
+
else score += 4;
|
|
545
|
+
return score;
|
|
546
|
+
}
|
|
547
|
+
/**
|
|
548
|
+
* Discover parallel route slots (@team, @analytics, etc.) in a directory.
|
|
549
|
+
* Returns a ParallelSlot for each @-prefixed subdirectory that has a page or default component.
|
|
550
|
+
*/
|
|
551
|
+
function discoverParallelSlots(dir, appDir, matcher) {
|
|
552
|
+
if (!fs.existsSync(dir)) return [];
|
|
553
|
+
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
554
|
+
const slots = [];
|
|
555
|
+
for (const entry of entries) {
|
|
556
|
+
if (!entry.isDirectory() || !entry.name.startsWith("@")) continue;
|
|
557
|
+
const slotName = entry.name.slice(1);
|
|
558
|
+
const slotDir = path.join(dir, entry.name);
|
|
559
|
+
const pagePath = findFile(slotDir, "page", matcher);
|
|
560
|
+
const defaultPath = findFile(slotDir, "default", matcher);
|
|
561
|
+
const interceptingRoutes = discoverInterceptingRoutes(slotDir, dir, appDir, matcher);
|
|
562
|
+
if (!pagePath && !defaultPath && interceptingRoutes.length === 0) continue;
|
|
563
|
+
slots.push({
|
|
564
|
+
key: `${slotName}@${path.relative(appDir, slotDir).replace(/\\/g, "/")}`,
|
|
565
|
+
name: slotName,
|
|
566
|
+
ownerDir: slotDir,
|
|
567
|
+
pagePath,
|
|
568
|
+
defaultPath,
|
|
569
|
+
layoutPath: findFile(slotDir, "layout", matcher),
|
|
570
|
+
loadingPath: findFile(slotDir, "loading", matcher),
|
|
571
|
+
errorPath: findFile(slotDir, "error", matcher),
|
|
572
|
+
interceptingRoutes,
|
|
573
|
+
layoutIndex: -1,
|
|
574
|
+
routeSegments: pagePath ? [] : null
|
|
575
|
+
});
|
|
576
|
+
}
|
|
577
|
+
return slots;
|
|
578
|
+
}
|
|
579
|
+
/**
|
|
580
|
+
* The interception convention prefix patterns.
|
|
581
|
+
* (.) — same level, (..) — one level up, (..)(..)" — two levels up, (...) — root
|
|
582
|
+
*/
|
|
583
|
+
const INTERCEPT_PATTERNS = [
|
|
584
|
+
{
|
|
585
|
+
prefix: "(...)",
|
|
586
|
+
convention: "..."
|
|
587
|
+
},
|
|
588
|
+
{
|
|
589
|
+
prefix: "(..)(..)",
|
|
590
|
+
convention: "../.."
|
|
591
|
+
},
|
|
592
|
+
{
|
|
593
|
+
prefix: "(..)",
|
|
594
|
+
convention: ".."
|
|
595
|
+
},
|
|
596
|
+
{
|
|
597
|
+
prefix: "(.)",
|
|
598
|
+
convention: "."
|
|
599
|
+
}
|
|
600
|
+
];
|
|
601
|
+
/**
|
|
602
|
+
* Discover intercepting routes inside a parallel slot directory.
|
|
603
|
+
*
|
|
604
|
+
* Intercepting routes use conventions like (.)photo, (..)feed, (...), etc.
|
|
605
|
+
* They intercept navigation to another route and render within the slot instead.
|
|
606
|
+
*
|
|
607
|
+
* @param slotDir - The parallel slot directory (e.g. app/feed/@modal)
|
|
608
|
+
* @param routeDir - The directory of the route that owns this slot (e.g. app/feed)
|
|
609
|
+
* @param appDir - The root app directory
|
|
610
|
+
*/
|
|
611
|
+
function discoverInterceptingRoutes(slotDir, routeDir, appDir, matcher) {
|
|
612
|
+
if (!fs.existsSync(slotDir)) return [];
|
|
613
|
+
const results = [];
|
|
614
|
+
scanForInterceptingPages(slotDir, routeDir, appDir, results, matcher);
|
|
615
|
+
return results;
|
|
616
|
+
}
|
|
617
|
+
/**
|
|
618
|
+
* Recursively scan a directory tree for page.tsx files that are inside
|
|
619
|
+
* intercepting route directories.
|
|
620
|
+
*/
|
|
621
|
+
function scanForInterceptingPages(currentDir, routeDir, appDir, results, matcher) {
|
|
622
|
+
if (!fs.existsSync(currentDir)) return;
|
|
623
|
+
const entries = fs.readdirSync(currentDir, { withFileTypes: true });
|
|
624
|
+
for (const entry of entries) {
|
|
625
|
+
if (!entry.isDirectory()) continue;
|
|
626
|
+
if (entry.name.startsWith("_")) continue;
|
|
627
|
+
const interceptMatch = matchInterceptConvention(entry.name);
|
|
628
|
+
if (interceptMatch) {
|
|
629
|
+
const restOfName = entry.name.slice(interceptMatch.prefix.length);
|
|
630
|
+
const interceptDir = path.join(currentDir, entry.name);
|
|
631
|
+
collectInterceptingPages(interceptDir, interceptDir, interceptMatch.convention, restOfName, routeDir, appDir, results, matcher);
|
|
632
|
+
} else scanForInterceptingPages(path.join(currentDir, entry.name), routeDir, appDir, results, matcher);
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
/**
|
|
636
|
+
* Match a directory name against interception convention prefixes.
|
|
637
|
+
*/
|
|
638
|
+
function matchInterceptConvention(name) {
|
|
639
|
+
for (const pattern of INTERCEPT_PATTERNS) if (name.startsWith(pattern.prefix)) return pattern;
|
|
640
|
+
return null;
|
|
641
|
+
}
|
|
642
|
+
/**
|
|
643
|
+
* Collect page.tsx files inside an intercepting route directory tree
|
|
644
|
+
* and compute their target URL patterns.
|
|
645
|
+
*/
|
|
646
|
+
function collectInterceptingPages(currentDir, interceptRoot, convention, interceptSegment, routeDir, appDir, results, matcher, parentLayoutPaths = []) {
|
|
647
|
+
const currentLayoutPath = findFile(currentDir, "layout", matcher);
|
|
648
|
+
const layoutPaths = currentLayoutPath ? [...parentLayoutPaths, currentLayoutPath] : parentLayoutPaths;
|
|
649
|
+
const page = findFile(currentDir, "page", matcher);
|
|
650
|
+
if (page) {
|
|
651
|
+
const targetPattern = computeInterceptTarget(convention, interceptSegment, currentDir, interceptRoot, routeDir, appDir);
|
|
652
|
+
if (targetPattern) results.push({
|
|
653
|
+
convention,
|
|
654
|
+
layoutPaths: [...layoutPaths],
|
|
655
|
+
targetPattern: targetPattern.pattern,
|
|
656
|
+
pagePath: page,
|
|
657
|
+
params: targetPattern.params
|
|
658
|
+
});
|
|
659
|
+
}
|
|
660
|
+
if (!fs.existsSync(currentDir)) return;
|
|
661
|
+
const entries = fs.readdirSync(currentDir, { withFileTypes: true });
|
|
662
|
+
for (const entry of entries) {
|
|
663
|
+
if (!entry.isDirectory()) continue;
|
|
664
|
+
if (entry.name.startsWith("_")) continue;
|
|
665
|
+
collectInterceptingPages(path.join(currentDir, entry.name), interceptRoot, convention, interceptSegment, routeDir, appDir, results, matcher, layoutPaths);
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
/**
|
|
669
|
+
* Check whether a path segment is invisible in the URL (route groups, parallel slots, ".").
|
|
670
|
+
*
|
|
671
|
+
* Used by computeInterceptTarget, convertSegmentsToRouteParts, and
|
|
672
|
+
* hasRemainingVisibleSegments — keep this the single source of truth.
|
|
673
|
+
*/
|
|
674
|
+
function isInvisibleSegment(segment) {
|
|
675
|
+
if (segment === ".") return true;
|
|
676
|
+
if (segment.startsWith("(") && segment.endsWith(")")) return true;
|
|
677
|
+
if (segment.startsWith("@")) return true;
|
|
678
|
+
return false;
|
|
679
|
+
}
|
|
680
|
+
/**
|
|
681
|
+
* Compute the target URL pattern for an intercepting route.
|
|
682
|
+
*
|
|
683
|
+
* Interception conventions (..), (..)(..)" climb by *visible route segments*
|
|
684
|
+
* (not filesystem directories). Route groups like (marketing) and parallel
|
|
685
|
+
* slots like @modal are invisible and must be skipped when counting levels.
|
|
686
|
+
*
|
|
687
|
+
* - (.) same level: resolve relative to routeDir
|
|
688
|
+
* - (..) one level up: climb 1 visible segment
|
|
689
|
+
* - (..)(..) two levels up: climb 2 visible segments
|
|
690
|
+
* - (...) root: resolve from appDir
|
|
691
|
+
*/
|
|
692
|
+
function computeInterceptTarget(convention, interceptSegment, currentDir, interceptRoot, routeDir, appDir) {
|
|
693
|
+
const routeSegments = path.relative(appDir, routeDir).split(path.sep).filter(Boolean);
|
|
694
|
+
let baseParts;
|
|
695
|
+
switch (convention) {
|
|
696
|
+
case ".":
|
|
697
|
+
baseParts = routeSegments;
|
|
698
|
+
break;
|
|
699
|
+
case "..":
|
|
700
|
+
case "../..": {
|
|
701
|
+
const levelsToClimb = convention === ".." ? 1 : 2;
|
|
702
|
+
let climbed = 0;
|
|
703
|
+
let cutIndex = routeSegments.length;
|
|
704
|
+
while (cutIndex > 0 && climbed < levelsToClimb) {
|
|
705
|
+
cutIndex--;
|
|
706
|
+
if (!isInvisibleSegment(routeSegments[cutIndex])) climbed++;
|
|
707
|
+
}
|
|
708
|
+
if (climbed < levelsToClimb) {
|
|
709
|
+
const interceptionRoute = formatInterceptionRoutePath(routeSegments, convention, interceptSegment, path.relative(interceptRoot, currentDir).split(path.sep).filter(Boolean));
|
|
710
|
+
if (convention === "..") throw new Error(`Invalid interception route: ${interceptionRoute}. Cannot use (..) marker at the root level, use (.) instead.`);
|
|
711
|
+
throw new Error(`Invalid interception route: ${interceptionRoute}. Cannot use (..)(..) marker at the root level or one level up.`);
|
|
712
|
+
}
|
|
713
|
+
baseParts = routeSegments.slice(0, cutIndex);
|
|
714
|
+
break;
|
|
715
|
+
}
|
|
716
|
+
case "...":
|
|
717
|
+
baseParts = [];
|
|
718
|
+
break;
|
|
719
|
+
default: return null;
|
|
720
|
+
}
|
|
721
|
+
const nestedParts = path.relative(interceptRoot, currentDir).split(path.sep).filter(Boolean);
|
|
722
|
+
const convertedTarget = convertSegmentsToRouteParts([
|
|
723
|
+
...baseParts,
|
|
724
|
+
interceptSegment,
|
|
725
|
+
...nestedParts
|
|
726
|
+
]);
|
|
727
|
+
if (!convertedTarget) return null;
|
|
728
|
+
const { urlSegments, params } = convertedTarget;
|
|
729
|
+
const pattern = "/" + urlSegments.join("/");
|
|
730
|
+
return {
|
|
731
|
+
pattern: pattern === "/" ? "/" : pattern,
|
|
732
|
+
params
|
|
733
|
+
};
|
|
734
|
+
}
|
|
735
|
+
function formatInterceptionRoutePath(routeSegments, convention, interceptSegment, nestedParts) {
|
|
736
|
+
const marker = markerForInterceptionConvention(convention);
|
|
737
|
+
const convertedRoute = convertSegmentsToRouteParts(routeSegments);
|
|
738
|
+
const routePath = [
|
|
739
|
+
...convertedRoute ? convertedRoute.urlSegments : routeSegments.filter((segment) => !isInvisibleSegment(segment)),
|
|
740
|
+
`${marker}${interceptSegment}`,
|
|
741
|
+
...nestedParts
|
|
742
|
+
].filter(Boolean).join("/");
|
|
743
|
+
return routePath ? `/${routePath}` : "/";
|
|
744
|
+
}
|
|
745
|
+
function markerForInterceptionConvention(convention) {
|
|
746
|
+
switch (convention) {
|
|
747
|
+
case ".": return "(.)";
|
|
748
|
+
case "..": return "(..)";
|
|
749
|
+
case "../..": return "(..)(..)";
|
|
750
|
+
case "...": return "(...)";
|
|
751
|
+
default: return "";
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
/**
|
|
755
|
+
* Find a file by name (without extension) in a directory.
|
|
756
|
+
* Checks configured pageExtensions.
|
|
757
|
+
*/
|
|
758
|
+
function findFile(dir, name, matcher) {
|
|
759
|
+
for (const ext of matcher.dottedExtensions) {
|
|
760
|
+
const filePath = path.join(dir, name + ext);
|
|
761
|
+
if (fs.existsSync(filePath)) return filePath;
|
|
762
|
+
}
|
|
763
|
+
return null;
|
|
764
|
+
}
|
|
765
|
+
/**
|
|
766
|
+
* Convert filesystem path segments to URL route parts, skipping invisible segments
|
|
767
|
+
* (route groups, @slots, ".") and converting dynamic segment syntax to Express-style
|
|
768
|
+
* patterns (e.g. "[id]" → ":id", "[...slug]" → ":slug+").
|
|
769
|
+
*/
|
|
770
|
+
function convertSegmentsToRouteParts(segments) {
|
|
771
|
+
const urlSegments = [];
|
|
772
|
+
const params = [];
|
|
773
|
+
let isDynamic = false;
|
|
774
|
+
for (let i = 0; i < segments.length; i++) {
|
|
775
|
+
const segment = segments[i];
|
|
776
|
+
if (isInvisibleSegment(segment)) continue;
|
|
777
|
+
const catchAllMatch = segment.match(/^\[\.\.\.([\w-]+)\]$/);
|
|
778
|
+
if (catchAllMatch) {
|
|
779
|
+
if (hasRemainingVisibleSegments(segments, i + 1)) return null;
|
|
780
|
+
isDynamic = true;
|
|
781
|
+
params.push(catchAllMatch[1]);
|
|
782
|
+
urlSegments.push(`:${catchAllMatch[1]}+`);
|
|
783
|
+
continue;
|
|
784
|
+
}
|
|
785
|
+
const optionalCatchAllMatch = segment.match(/^\[\[\.\.\.([\w-]+)\]\]$/);
|
|
786
|
+
if (optionalCatchAllMatch) {
|
|
787
|
+
if (hasRemainingVisibleSegments(segments, i + 1)) return null;
|
|
788
|
+
isDynamic = true;
|
|
789
|
+
params.push(optionalCatchAllMatch[1]);
|
|
790
|
+
urlSegments.push(`:${optionalCatchAllMatch[1]}*`);
|
|
791
|
+
continue;
|
|
792
|
+
}
|
|
793
|
+
const dynamicMatch = segment.match(/^\[([\w-]+)\]$/);
|
|
794
|
+
if (dynamicMatch) {
|
|
795
|
+
isDynamic = true;
|
|
796
|
+
params.push(dynamicMatch[1]);
|
|
797
|
+
urlSegments.push(`:${dynamicMatch[1]}`);
|
|
798
|
+
continue;
|
|
799
|
+
}
|
|
800
|
+
urlSegments.push(decodeRouteSegment(segment));
|
|
801
|
+
}
|
|
802
|
+
return {
|
|
803
|
+
urlSegments,
|
|
804
|
+
params,
|
|
805
|
+
isDynamic
|
|
806
|
+
};
|
|
807
|
+
}
|
|
808
|
+
function hasRemainingVisibleSegments(segments, startIndex) {
|
|
809
|
+
for (let i = startIndex; i < segments.length; i++) if (!isInvisibleSegment(segments[i])) return true;
|
|
810
|
+
return false;
|
|
811
|
+
}
|
|
812
|
+
function joinRoutePattern(basePattern, subPath) {
|
|
813
|
+
if (!subPath) return basePattern;
|
|
814
|
+
return basePattern === "/" ? `/${subPath}` : `${basePattern}/${subPath}`;
|
|
815
|
+
}
|
|
816
|
+
//#endregion
|
|
817
|
+
export { buildAppRouteGraph, computeRootParamNames };
|
|
818
|
+
|
|
819
|
+
//# sourceMappingURL=app-route-graph.js.map
|