inital commit

This commit is contained in:
2026-01-01 15:25:19 +05:30
commit f0ae49465a
36361 changed files with 4894111 additions and 0 deletions

View File

@@ -0,0 +1,66 @@
import { parseUrl } from '../../../lib/url';
import { warnOnce } from '../../../build/output/log';
import { isCsrfOriginAllowed } from '../../app-render/csrf-protection';
function warnOrBlockRequest(res, origin, mode) {
const originString = origin ? `from ${origin}` : '';
if (mode === 'warn') {
warnOnce(`Cross origin request detected ${originString} to /_next/* resource. In a future major version of Next.js, you will need to explicitly configure "allowedDevOrigins" in next.config to allow this.\nRead more: https://nextjs.org/docs/app/api-reference/config/next-config-js/allowedDevOrigins`);
return false;
}
warnOnce(`Blocked cross-origin request ${originString} to /_next/* resource. To allow this, configure "allowedDevOrigins" in next.config\nRead more: https://nextjs.org/docs/app/api-reference/config/next-config-js/allowedDevOrigins`);
if ('statusCode' in res) {
res.statusCode = 403;
}
res.end('Unauthorized');
return true;
}
function isInternalDevEndpoint(req) {
if (!req.url) return false;
try {
// TODO: We should standardize on a single prefix for this
const isMiddlewareRequest = req.url.includes('/__nextjs');
const isInternalAsset = req.url.includes('/_next');
// Static media requests are excluded, as they might be loaded via CSS and would fail
// CORS checks.
const isIgnoredRequest = req.url.includes('/_next/image') || req.url.includes('/_next/static/media');
return !isIgnoredRequest && (isInternalAsset || isMiddlewareRequest);
} catch (err) {
return false;
}
}
export const blockCrossSite = (req, res, allowedDevOrigins, hostname)=>{
// in the future, these will be blocked by default when allowed origins aren't configured.
// for now, we warn when allowed origins aren't configured
const mode = typeof allowedDevOrigins === 'undefined' ? 'warn' : 'block';
const allowedOrigins = [
'*.localhost',
'localhost',
...allowedDevOrigins || []
];
if (hostname) {
allowedOrigins.push(hostname);
}
// only process internal URLs/middleware
if (!isInternalDevEndpoint(req)) {
return false;
}
// block non-cors request from cross-site e.g. script tag on
// different host
if (req.headers['sec-fetch-mode'] === 'no-cors' && req.headers['sec-fetch-site'] === 'cross-site') {
return warnOrBlockRequest(res, undefined, mode);
}
// ensure websocket requests from allowed origin
const rawOrigin = req.headers['origin'];
if (rawOrigin) {
const parsedOrigin = parseUrl(rawOrigin);
if (parsedOrigin) {
const originLowerCase = parsedOrigin.hostname.toLowerCase();
if (!isCsrfOriginAllowed(originLowerCase, allowedOrigins)) {
return warnOrBlockRequest(res, originLowerCase, mode);
}
}
}
return false;
};
//# sourceMappingURL=block-cross-site.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,33 @@
import path from '../../../shared/lib/isomorphic/path';
import { normalizePagePath } from '../../../shared/lib/page-path/normalize-page-path';
import { isDynamicRoute } from '../../../shared/lib/router/utils/is-dynamic';
import { getNamedRouteRegex } from '../../../shared/lib/router/utils/route-regex';
import { normalizeRouteRegex } from '../../../lib/load-custom-routes';
import { escapeStringRegexp } from '../../../shared/lib/escape-regexp';
export function buildDataRoute(page, buildId) {
const pagePath = normalizePagePath(page);
const dataRoute = path.posix.join('/_next/data', buildId, `${pagePath}.json`);
let dataRouteRegex;
let namedDataRouteRegex;
let routeKeys;
if (isDynamicRoute(page)) {
const routeRegex = getNamedRouteRegex(dataRoute, {
prefixRouteKeys: true,
includeSuffix: true,
excludeOptionalTrailingSlash: true
});
dataRouteRegex = normalizeRouteRegex(routeRegex.re.source);
namedDataRouteRegex = routeRegex.namedRegex;
routeKeys = routeRegex.routeKeys;
} else {
dataRouteRegex = normalizeRouteRegex(new RegExp(`^${path.posix.join('/_next/data', escapeStringRegexp(buildId), `${pagePath}\\.json`)}$`).source);
}
return {
page,
routeKeys,
dataRouteRegex,
namedDataRouteRegex
};
}
//# sourceMappingURL=build-data-route.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/server/lib/router-utils/build-data-route.ts"],"sourcesContent":["import path from '../../../shared/lib/isomorphic/path'\nimport { normalizePagePath } from '../../../shared/lib/page-path/normalize-page-path'\nimport { isDynamicRoute } from '../../../shared/lib/router/utils/is-dynamic'\nimport { getNamedRouteRegex } from '../../../shared/lib/router/utils/route-regex'\nimport { normalizeRouteRegex } from '../../../lib/load-custom-routes'\nimport { escapeStringRegexp } from '../../../shared/lib/escape-regexp'\n\nexport function buildDataRoute(page: string, buildId: string) {\n const pagePath = normalizePagePath(page)\n const dataRoute = path.posix.join('/_next/data', buildId, `${pagePath}.json`)\n\n let dataRouteRegex: string\n let namedDataRouteRegex: string | undefined\n let routeKeys: { [named: string]: string } | undefined\n\n if (isDynamicRoute(page)) {\n const routeRegex = getNamedRouteRegex(dataRoute, {\n prefixRouteKeys: true,\n includeSuffix: true,\n excludeOptionalTrailingSlash: true,\n })\n\n dataRouteRegex = normalizeRouteRegex(routeRegex.re.source)\n namedDataRouteRegex = routeRegex.namedRegex\n routeKeys = routeRegex.routeKeys\n } else {\n dataRouteRegex = normalizeRouteRegex(\n new RegExp(\n `^${path.posix.join(\n '/_next/data',\n escapeStringRegexp(buildId),\n `${pagePath}\\\\.json`\n )}$`\n ).source\n )\n }\n\n return {\n page,\n routeKeys,\n dataRouteRegex,\n namedDataRouteRegex,\n }\n}\n"],"names":["path","normalizePagePath","isDynamicRoute","getNamedRouteRegex","normalizeRouteRegex","escapeStringRegexp","buildDataRoute","page","buildId","pagePath","dataRoute","posix","join","dataRouteRegex","namedDataRouteRegex","routeKeys","routeRegex","prefixRouteKeys","includeSuffix","excludeOptionalTrailingSlash","re","source","namedRegex","RegExp"],"mappings":"AAAA,OAAOA,UAAU,sCAAqC;AACtD,SAASC,iBAAiB,QAAQ,oDAAmD;AACrF,SAASC,cAAc,QAAQ,8CAA6C;AAC5E,SAASC,kBAAkB,QAAQ,+CAA8C;AACjF,SAASC,mBAAmB,QAAQ,kCAAiC;AACrE,SAASC,kBAAkB,QAAQ,oCAAmC;AAEtE,OAAO,SAASC,eAAeC,IAAY,EAAEC,OAAe;IAC1D,MAAMC,WAAWR,kBAAkBM;IACnC,MAAMG,YAAYV,KAAKW,KAAK,CAACC,IAAI,CAAC,eAAeJ,SAAS,GAAGC,SAAS,KAAK,CAAC;IAE5E,IAAII;IACJ,IAAIC;IACJ,IAAIC;IAEJ,IAAIb,eAAeK,OAAO;QACxB,MAAMS,aAAab,mBAAmBO,WAAW;YAC/CO,iBAAiB;YACjBC,eAAe;YACfC,8BAA8B;QAChC;QAEAN,iBAAiBT,oBAAoBY,WAAWI,EAAE,CAACC,MAAM;QACzDP,sBAAsBE,WAAWM,UAAU;QAC3CP,YAAYC,WAAWD,SAAS;IAClC,OAAO;QACLF,iBAAiBT,oBACf,IAAImB,OACF,CAAC,CAAC,EAAEvB,KAAKW,KAAK,CAACC,IAAI,CACjB,eACAP,mBAAmBG,UACnB,GAAGC,SAAS,OAAO,CAAC,EACpB,CAAC,CAAC,EACJY,MAAM;IAEZ;IAEA,OAAO;QACLd;QACAQ;QACAF;QACAC;IACF;AACF"}

View File

@@ -0,0 +1,22 @@
import path from '../../../shared/lib/isomorphic/path';
import { normalizePagePath } from '../../../shared/lib/page-path/normalize-page-path';
import { getNamedRouteRegex } from '../../../shared/lib/router/utils/route-regex';
import { RSC_SEGMENT_SUFFIX, RSC_SEGMENTS_DIR_SUFFIX } from '../../../lib/constants';
export const SEGMENT_PATH_KEY = 'nextSegmentPath';
export function buildPrefetchSegmentDataRoute(page, segmentPath) {
const pagePath = normalizePagePath(page);
const destination = path.posix.join(`${pagePath}${RSC_SEGMENTS_DIR_SUFFIX}`, `${segmentPath}${RSC_SEGMENT_SUFFIX}`);
const { namedRegex } = getNamedRouteRegex(destination, {
prefixRouteKeys: true,
includePrefix: true,
includeSuffix: true,
excludeOptionalTrailingSlash: true,
backreferenceDuplicateKeys: true
});
return {
destination,
source: namedRegex
};
}
//# sourceMappingURL=build-prefetch-segment-data-route.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/server/lib/router-utils/build-prefetch-segment-data-route.ts"],"sourcesContent":["import path from '../../../shared/lib/isomorphic/path'\nimport { normalizePagePath } from '../../../shared/lib/page-path/normalize-page-path'\nimport { getNamedRouteRegex } from '../../../shared/lib/router/utils/route-regex'\nimport {\n RSC_SEGMENT_SUFFIX,\n RSC_SEGMENTS_DIR_SUFFIX,\n} from '../../../lib/constants'\n\nexport const SEGMENT_PATH_KEY = 'nextSegmentPath'\n\nexport type PrefetchSegmentDataRoute = {\n source: string\n destination: string\n}\n\nexport function buildPrefetchSegmentDataRoute(\n page: string,\n segmentPath: string\n): PrefetchSegmentDataRoute {\n const pagePath = normalizePagePath(page)\n\n const destination = path.posix.join(\n `${pagePath}${RSC_SEGMENTS_DIR_SUFFIX}`,\n `${segmentPath}${RSC_SEGMENT_SUFFIX}`\n )\n\n const { namedRegex } = getNamedRouteRegex(destination, {\n prefixRouteKeys: true,\n includePrefix: true,\n includeSuffix: true,\n excludeOptionalTrailingSlash: true,\n backreferenceDuplicateKeys: true,\n })\n\n return {\n destination,\n source: namedRegex,\n }\n}\n"],"names":["path","normalizePagePath","getNamedRouteRegex","RSC_SEGMENT_SUFFIX","RSC_SEGMENTS_DIR_SUFFIX","SEGMENT_PATH_KEY","buildPrefetchSegmentDataRoute","page","segmentPath","pagePath","destination","posix","join","namedRegex","prefixRouteKeys","includePrefix","includeSuffix","excludeOptionalTrailingSlash","backreferenceDuplicateKeys","source"],"mappings":"AAAA,OAAOA,UAAU,sCAAqC;AACtD,SAASC,iBAAiB,QAAQ,oDAAmD;AACrF,SAASC,kBAAkB,QAAQ,+CAA8C;AACjF,SACEC,kBAAkB,EAClBC,uBAAuB,QAClB,yBAAwB;AAE/B,OAAO,MAAMC,mBAAmB,kBAAiB;AAOjD,OAAO,SAASC,8BACdC,IAAY,EACZC,WAAmB;IAEnB,MAAMC,WAAWR,kBAAkBM;IAEnC,MAAMG,cAAcV,KAAKW,KAAK,CAACC,IAAI,CACjC,GAAGH,WAAWL,yBAAyB,EACvC,GAAGI,cAAcL,oBAAoB;IAGvC,MAAM,EAAEU,UAAU,EAAE,GAAGX,mBAAmBQ,aAAa;QACrDI,iBAAiB;QACjBC,eAAe;QACfC,eAAe;QACfC,8BAA8B;QAC9BC,4BAA4B;IAC9B;IAEA,OAAO;QACLR;QACAS,QAAQN;IACV;AACF"}

View File

@@ -0,0 +1,28 @@
import escapePathDelimiters from '../../../shared/lib/router/utils/escape-path-delimiters';
import { DecodeError } from '../../../shared/lib/utils';
/**
* We only encode path delimiters for path segments from
* getStaticPaths so we need to attempt decoding the URL
* to match against and only escape the path delimiters
* this allows non-ascii values to be handled e.g.
* Japanese characters.
* */ function decodePathParams(pathname) {
// TODO: investigate adding this handling for non-SSG
// pages so non-ascii names also work there.
return pathname.split('/').map((seg)=>{
try {
seg = escapePathDelimiters(decodeURIComponent(seg), true);
} catch (_) {
// An improperly encoded URL was provided
throw Object.defineProperty(new DecodeError('Failed to decode path param(s).'), "__NEXT_ERROR_CODE", {
value: "E539",
enumerable: false,
configurable: true
});
}
return seg;
}).join('/');
}
export { decodePathParams };
//# sourceMappingURL=decode-path-params.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/server/lib/router-utils/decode-path-params.ts"],"sourcesContent":["import escapePathDelimiters from '../../../shared/lib/router/utils/escape-path-delimiters'\nimport { DecodeError } from '../../../shared/lib/utils'\n\n/**\n * We only encode path delimiters for path segments from\n * getStaticPaths so we need to attempt decoding the URL\n * to match against and only escape the path delimiters\n * this allows non-ascii values to be handled e.g.\n * Japanese characters.\n * */\nfunction decodePathParams(pathname: string): string {\n // TODO: investigate adding this handling for non-SSG\n // pages so non-ascii names also work there.\n return pathname\n .split('/')\n .map((seg) => {\n try {\n seg = escapePathDelimiters(decodeURIComponent(seg), true)\n } catch (_) {\n // An improperly encoded URL was provided\n throw new DecodeError('Failed to decode path param(s).')\n }\n return seg\n })\n .join('/')\n}\n\nexport { decodePathParams }\n"],"names":["escapePathDelimiters","DecodeError","decodePathParams","pathname","split","map","seg","decodeURIComponent","_","join"],"mappings":"AAAA,OAAOA,0BAA0B,0DAAyD;AAC1F,SAASC,WAAW,QAAQ,4BAA2B;AAEvD;;;;;;GAMG,GACH,SAASC,iBAAiBC,QAAgB;IACxC,qDAAqD;IACrD,4CAA4C;IAC5C,OAAOA,SACJC,KAAK,CAAC,KACNC,GAAG,CAAC,CAACC;QACJ,IAAI;YACFA,MAAMN,qBAAqBO,mBAAmBD,MAAM;QACtD,EAAE,OAAOE,GAAG;YACV,yCAAyC;YACzC,MAAM,qBAAkD,CAAlD,IAAIP,YAAY,oCAAhB,qBAAA;uBAAA;4BAAA;8BAAA;YAAiD;QACzD;QACA,OAAOK;IACT,GACCG,IAAI,CAAC;AACV;AAEA,SAASP,gBAAgB,GAAE"}

View File

@@ -0,0 +1,472 @@
import path from 'path';
import fs from 'fs/promises';
import * as Log from '../../../build/output/log';
import setupDebug from 'next/dist/compiled/debug';
import { LRUCache } from '../lru-cache';
import loadCustomRoutes from '../../../lib/load-custom-routes';
import { modifyRouteRegex } from '../../../lib/redirect-status';
import { FileType, fileExists } from '../../../lib/file-exists';
import { recursiveReadDir } from '../../../lib/recursive-readdir';
import { isDynamicRoute } from '../../../shared/lib/router/utils';
import { escapeStringRegexp } from '../../../shared/lib/escape-regexp';
import { getPathMatch } from '../../../shared/lib/router/utils/path-match';
import { getRouteRegex } from '../../../shared/lib/router/utils/route-regex';
import { getRouteMatcher } from '../../../shared/lib/router/utils/route-matcher';
import { pathHasPrefix } from '../../../shared/lib/router/utils/path-has-prefix';
import { normalizeLocalePath } from '../../../shared/lib/i18n/normalize-locale-path';
import { removePathPrefix } from '../../../shared/lib/router/utils/remove-path-prefix';
import { getMiddlewareRouteMatcher } from '../../../shared/lib/router/utils/middleware-route-matcher';
import { APP_PATH_ROUTES_MANIFEST, BUILD_ID_FILE, FUNCTIONS_CONFIG_MANIFEST, MIDDLEWARE_MANIFEST, PAGES_MANIFEST, PRERENDER_MANIFEST, ROUTES_MANIFEST } from '../../../shared/lib/constants';
import { normalizePathSep } from '../../../shared/lib/page-path/normalize-path-sep';
import { normalizeMetadataRoute } from '../../../lib/metadata/get-metadata-route';
import { RSCPathnameNormalizer } from '../../normalizers/request/rsc';
import { PrefetchRSCPathnameNormalizer } from '../../normalizers/request/prefetch-rsc';
import { encodeURIPath } from '../../../shared/lib/encode-uri-path';
const debug = setupDebug('next:router-server:filesystem');
export const buildCustomRoute = (type, item, basePath, caseSensitive)=>{
const restrictedRedirectPaths = [
'/_next'
].map((p)=>basePath ? `${basePath}${p}` : p);
const match = getPathMatch(item.source, {
strict: true,
removeUnnamedParams: true,
regexModifier: !item.internal ? (regex)=>modifyRouteRegex(regex, type === 'redirect' ? restrictedRedirectPaths : undefined) : undefined,
sensitive: caseSensitive
});
return {
...item,
...type === 'rewrite' ? {
check: true
} : {},
match
};
};
export async function setupFsCheck(opts) {
const getItemsLru = !opts.dev ? new LRUCache(1024 * 1024, function length(value) {
if (!value) return 0;
return (value.fsPath || '').length + value.itemPath.length + value.type.length;
}) : undefined;
// routes that have _next/data endpoints (SSG/SSP)
const nextDataRoutes = new Set();
const publicFolderItems = new Set();
const nextStaticFolderItems = new Set();
const legacyStaticFolderItems = new Set();
const appFiles = new Set();
const pageFiles = new Set();
let dynamicRoutes = [];
let middlewareMatcher = ()=>false;
const distDir = path.join(opts.dir, opts.config.distDir);
const publicFolderPath = path.join(opts.dir, 'public');
const nextStaticFolderPath = path.join(distDir, 'static');
const legacyStaticFolderPath = path.join(opts.dir, 'static');
let customRoutes = {
redirects: [],
rewrites: {
beforeFiles: [],
afterFiles: [],
fallback: []
},
headers: []
};
let buildId = 'development';
let prerenderManifest;
if (!opts.dev) {
var _middlewareManifest_middleware_, _middlewareManifest_middleware;
const buildIdPath = path.join(opts.dir, opts.config.distDir, BUILD_ID_FILE);
try {
buildId = await fs.readFile(buildIdPath, 'utf8');
} catch (err) {
if (err.code !== 'ENOENT') throw err;
throw Object.defineProperty(new Error(`Could not find a production build in the '${opts.config.distDir}' directory. Try building your app with 'next build' before starting the production server. https://nextjs.org/docs/messages/production-start-no-build-id`), "__NEXT_ERROR_CODE", {
value: "E427",
enumerable: false,
configurable: true
});
}
try {
for (const file of (await recursiveReadDir(publicFolderPath))){
// Ensure filename is encoded and normalized.
publicFolderItems.add(encodeURIPath(normalizePathSep(file)));
}
} catch (err) {
if (err.code !== 'ENOENT') {
throw err;
}
}
try {
for (const file of (await recursiveReadDir(legacyStaticFolderPath))){
// Ensure filename is encoded and normalized.
legacyStaticFolderItems.add(encodeURIPath(normalizePathSep(file)));
}
Log.warn(`The static directory has been deprecated in favor of the public directory. https://nextjs.org/docs/messages/static-dir-deprecated`);
} catch (err) {
if (err.code !== 'ENOENT') {
throw err;
}
}
try {
for (const file of (await recursiveReadDir(nextStaticFolderPath))){
// Ensure filename is encoded and normalized.
nextStaticFolderItems.add(path.posix.join('/_next/static', encodeURIPath(normalizePathSep(file))));
}
} catch (err) {
if (opts.config.output !== 'standalone') throw err;
}
const routesManifestPath = path.join(distDir, ROUTES_MANIFEST);
const prerenderManifestPath = path.join(distDir, PRERENDER_MANIFEST);
const middlewareManifestPath = path.join(distDir, 'server', MIDDLEWARE_MANIFEST);
const functionsConfigManifestPath = path.join(distDir, 'server', FUNCTIONS_CONFIG_MANIFEST);
const pagesManifestPath = path.join(distDir, 'server', PAGES_MANIFEST);
const appRoutesManifestPath = path.join(distDir, APP_PATH_ROUTES_MANIFEST);
const routesManifest = JSON.parse(await fs.readFile(routesManifestPath, 'utf8'));
prerenderManifest = JSON.parse(await fs.readFile(prerenderManifestPath, 'utf8'));
const middlewareManifest = JSON.parse(await fs.readFile(middlewareManifestPath, 'utf8').catch(()=>'{}'));
const functionsConfigManifest = JSON.parse(await fs.readFile(functionsConfigManifestPath, 'utf8').catch(()=>'{}'));
const pagesManifest = JSON.parse(await fs.readFile(pagesManifestPath, 'utf8'));
const appRoutesManifest = JSON.parse(await fs.readFile(appRoutesManifestPath, 'utf8').catch(()=>'{}'));
for (const key of Object.keys(pagesManifest)){
// ensure the non-locale version is in the set
if (opts.config.i18n) {
pageFiles.add(normalizeLocalePath(key, opts.config.i18n.locales).pathname);
} else {
pageFiles.add(key);
}
}
for (const key of Object.keys(appRoutesManifest)){
appFiles.add(appRoutesManifest[key]);
}
const escapedBuildId = escapeStringRegexp(buildId);
for (const route of routesManifest.dataRoutes){
if (isDynamicRoute(route.page)) {
const routeRegex = getRouteRegex(route.page);
dynamicRoutes.push({
...route,
regex: routeRegex.re.toString(),
match: getRouteMatcher({
// TODO: fix this in the manifest itself, must also be fixed in
// upstream builder that relies on this
re: opts.config.i18n ? new RegExp(route.dataRouteRegex.replace(`/${escapedBuildId}/`, `/${escapedBuildId}/(?<nextLocale>[^/]+?)/`)) : new RegExp(route.dataRouteRegex),
groups: routeRegex.groups
})
});
}
nextDataRoutes.add(route.page);
}
for (const route of routesManifest.dynamicRoutes){
dynamicRoutes.push({
...route,
match: getRouteMatcher(getRouteRegex(route.page))
});
}
if ((_middlewareManifest_middleware = middlewareManifest.middleware) == null ? void 0 : (_middlewareManifest_middleware_ = _middlewareManifest_middleware['/']) == null ? void 0 : _middlewareManifest_middleware_.matchers) {
var _middlewareManifest_middleware_1, _middlewareManifest_middleware1;
middlewareMatcher = getMiddlewareRouteMatcher((_middlewareManifest_middleware1 = middlewareManifest.middleware) == null ? void 0 : (_middlewareManifest_middleware_1 = _middlewareManifest_middleware1['/']) == null ? void 0 : _middlewareManifest_middleware_1.matchers);
} else if (functionsConfigManifest == null ? void 0 : functionsConfigManifest.functions['/_middleware']) {
middlewareMatcher = getMiddlewareRouteMatcher(functionsConfigManifest.functions['/_middleware'].matchers ?? [
{
regexp: '.*',
originalSource: '/:path*'
}
]);
}
customRoutes = {
redirects: routesManifest.redirects,
rewrites: routesManifest.rewrites ? Array.isArray(routesManifest.rewrites) ? {
beforeFiles: [],
afterFiles: routesManifest.rewrites,
fallback: []
} : routesManifest.rewrites : {
beforeFiles: [],
afterFiles: [],
fallback: []
},
headers: routesManifest.headers
};
} else {
// dev handling
customRoutes = await loadCustomRoutes(opts.config);
prerenderManifest = {
version: 4,
routes: {},
dynamicRoutes: {},
notFoundRoutes: [],
preview: {
previewModeId: require('crypto').randomBytes(16).toString('hex'),
previewModeSigningKey: require('crypto').randomBytes(32).toString('hex'),
previewModeEncryptionKey: require('crypto').randomBytes(32).toString('hex')
}
};
}
const headers = customRoutes.headers.map((item)=>buildCustomRoute('header', item, opts.config.basePath, opts.config.experimental.caseSensitiveRoutes));
const redirects = customRoutes.redirects.map((item)=>buildCustomRoute('redirect', item, opts.config.basePath, opts.config.experimental.caseSensitiveRoutes));
const rewrites = {
beforeFiles: customRoutes.rewrites.beforeFiles.map((item)=>buildCustomRoute('before_files_rewrite', item)),
afterFiles: customRoutes.rewrites.afterFiles.map((item)=>buildCustomRoute('rewrite', item, opts.config.basePath, opts.config.experimental.caseSensitiveRoutes)),
fallback: customRoutes.rewrites.fallback.map((item)=>buildCustomRoute('rewrite', item, opts.config.basePath, opts.config.experimental.caseSensitiveRoutes))
};
const { i18n } = opts.config;
const handleLocale = (pathname, locales)=>{
let locale;
if (i18n) {
const i18nResult = normalizeLocalePath(pathname, locales || i18n.locales);
pathname = i18nResult.pathname;
locale = i18nResult.detectedLocale;
}
return {
locale,
pathname
};
};
debug('nextDataRoutes', nextDataRoutes);
debug('dynamicRoutes', dynamicRoutes);
debug('pageFiles', pageFiles);
debug('appFiles', appFiles);
let ensureFn;
const normalizers = {
// Because we can't know if the app directory is enabled or not at this
// stage, we assume that it is.
rsc: new RSCPathnameNormalizer(),
prefetchRSC: opts.config.experimental.ppr ? new PrefetchRSCPathnameNormalizer() : undefined
};
return {
headers,
rewrites,
redirects,
buildId,
handleLocale,
appFiles,
pageFiles,
dynamicRoutes,
nextDataRoutes,
exportPathMapRoutes: undefined,
devVirtualFsItems: new Set(),
prerenderManifest,
middlewareMatcher: middlewareMatcher,
ensureCallback (fn) {
ensureFn = fn;
},
async getItem (itemPath) {
const originalItemPath = itemPath;
const itemKey = originalItemPath;
const lruResult = getItemsLru == null ? void 0 : getItemsLru.get(itemKey);
if (lruResult) {
return lruResult;
}
const { basePath } = opts.config;
const hasBasePath = pathHasPrefix(itemPath, basePath);
// Return null if path doesn't start with basePath
if (basePath && !hasBasePath) {
return null;
}
// Remove basePath if it exists.
if (basePath && hasBasePath) {
itemPath = removePathPrefix(itemPath, basePath) || '/';
}
// Simulate minimal mode requests by normalizing RSC and postponed
// requests.
if (opts.minimalMode) {
var _normalizers_prefetchRSC;
if ((_normalizers_prefetchRSC = normalizers.prefetchRSC) == null ? void 0 : _normalizers_prefetchRSC.match(itemPath)) {
itemPath = normalizers.prefetchRSC.normalize(itemPath, true);
} else if (normalizers.rsc.match(itemPath)) {
itemPath = normalizers.rsc.normalize(itemPath, true);
}
}
if (itemPath !== '/' && itemPath.endsWith('/')) {
itemPath = itemPath.substring(0, itemPath.length - 1);
}
let decodedItemPath = itemPath;
try {
decodedItemPath = decodeURIComponent(itemPath);
} catch {}
if (itemPath === '/_next/image') {
return {
itemPath,
type: 'nextImage'
};
}
const itemsToCheck = [
[
this.devVirtualFsItems,
'devVirtualFsItem'
],
[
nextStaticFolderItems,
'nextStaticFolder'
],
[
legacyStaticFolderItems,
'legacyStaticFolder'
],
[
publicFolderItems,
'publicFolder'
],
[
appFiles,
'appFile'
],
[
pageFiles,
'pageFile'
]
];
for (let [items, type] of itemsToCheck){
let locale;
let curItemPath = itemPath;
let curDecodedItemPath = decodedItemPath;
const isDynamicOutput = type === 'pageFile' || type === 'appFile';
if (i18n) {
var _i18n_domains;
const localeResult = handleLocale(itemPath, // legacy behavior allows visiting static assets under
// default locale but no other locale
isDynamicOutput ? undefined : [
i18n == null ? void 0 : i18n.defaultLocale,
// default locales from domains need to be matched too
...((_i18n_domains = i18n.domains) == null ? void 0 : _i18n_domains.map((item)=>item.defaultLocale)) || []
]);
if (localeResult.pathname !== curItemPath) {
curItemPath = localeResult.pathname;
locale = localeResult.locale;
try {
curDecodedItemPath = decodeURIComponent(curItemPath);
} catch {}
}
}
if (type === 'legacyStaticFolder') {
if (!pathHasPrefix(curItemPath, '/static')) {
continue;
}
curItemPath = curItemPath.substring('/static'.length);
try {
curDecodedItemPath = decodeURIComponent(curItemPath);
} catch {}
}
if (type === 'nextStaticFolder' && !pathHasPrefix(curItemPath, '/_next/static')) {
continue;
}
const nextDataPrefix = `/_next/data/${buildId}/`;
if (type === 'pageFile' && curItemPath.startsWith(nextDataPrefix) && curItemPath.endsWith('.json')) {
items = nextDataRoutes;
// remove _next/data/<build-id> prefix
curItemPath = curItemPath.substring(nextDataPrefix.length - 1);
// remove .json postfix
curItemPath = curItemPath.substring(0, curItemPath.length - '.json'.length);
const curLocaleResult = handleLocale(curItemPath);
curItemPath = curLocaleResult.pathname === '/index' ? '/' : curLocaleResult.pathname;
locale = curLocaleResult.locale;
try {
curDecodedItemPath = decodeURIComponent(curItemPath);
} catch {}
}
let matchedItem = items.has(curItemPath);
// check decoded variant as well
if (!matchedItem && !opts.dev) {
matchedItem = items.has(curDecodedItemPath);
if (matchedItem) curItemPath = curDecodedItemPath;
else {
// x-ref: https://github.com/vercel/next.js/issues/54008
// There're cases that urls get decoded before requests, we should support both encoded and decoded ones.
// e.g. nginx could decode the proxy urls, the below ones should be treated as the same:
// decoded version: `/_next/static/chunks/pages/blog/[slug]-d4858831b91b69f6.js`
// encoded version: `/_next/static/chunks/pages/blog/%5Bslug%5D-d4858831b91b69f6.js`
try {
// encode the special characters in the path and retrieve again to determine if path exists.
const encodedCurItemPath = encodeURIPath(curItemPath);
matchedItem = items.has(encodedCurItemPath);
} catch {}
}
}
if (matchedItem || opts.dev) {
let fsPath;
let itemsRoot;
switch(type){
case 'nextStaticFolder':
{
itemsRoot = nextStaticFolderPath;
curItemPath = curItemPath.substring('/_next/static'.length);
break;
}
case 'legacyStaticFolder':
{
itemsRoot = legacyStaticFolderPath;
break;
}
case 'publicFolder':
{
itemsRoot = publicFolderPath;
break;
}
default:
{
break;
}
}
if (itemsRoot && curItemPath) {
fsPath = path.posix.join(itemsRoot, curItemPath);
}
// dynamically check fs in development so we don't
// have to wait on the watcher
if (!matchedItem && opts.dev) {
const isStaticAsset = [
'nextStaticFolder',
'publicFolder',
'legacyStaticFolder'
].includes(type);
if (isStaticAsset && itemsRoot) {
let found = fsPath && await fileExists(fsPath, FileType.File);
if (!found) {
try {
// In dev, we ensure encoded paths match
// decoded paths on the filesystem so check
// that variation as well
const tempItemPath = decodeURIComponent(curItemPath);
fsPath = path.posix.join(itemsRoot, tempItemPath);
found = await fileExists(fsPath, FileType.File);
} catch {}
if (!found) {
continue;
}
}
} else if (type === 'pageFile' || type === 'appFile') {
var _ensureFn;
const isAppFile = type === 'appFile';
if (ensureFn && await ((_ensureFn = ensureFn({
type,
itemPath: isAppFile ? normalizeMetadataRoute(curItemPath) : curItemPath
})) == null ? void 0 : _ensureFn.catch(()=>'ENSURE_FAILED')) === 'ENSURE_FAILED') {
continue;
}
} else {
continue;
}
}
// i18n locales aren't matched for app dir
if (type === 'appFile' && locale && locale !== (i18n == null ? void 0 : i18n.defaultLocale)) {
continue;
}
const itemResult = {
type,
fsPath,
locale,
itemsRoot,
itemPath: curItemPath
};
getItemsLru == null ? void 0 : getItemsLru.set(itemKey, itemResult);
return itemResult;
}
}
getItemsLru == null ? void 0 : getItemsLru.set(itemKey, null);
return null;
},
getDynamicRoutes () {
// this should include data routes
return this.dynamicRoutes;
},
getMiddlewareMatchers () {
return this.middlewareMatcher;
}
};
}
//# sourceMappingURL=filesystem.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,6 @@
const REACT_POSTPONE_TYPE = Symbol.for('react.postpone');
export function isPostpone(error) {
return typeof error === 'object' && error !== null && error.$$typeof === REACT_POSTPONE_TYPE;
}
//# sourceMappingURL=is-postpone.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/server/lib/router-utils/is-postpone.ts"],"sourcesContent":["const REACT_POSTPONE_TYPE: symbol = Symbol.for('react.postpone')\n\nexport function isPostpone(error: any): boolean {\n return (\n typeof error === 'object' &&\n error !== null &&\n error.$$typeof === REACT_POSTPONE_TYPE\n )\n}\n"],"names":["REACT_POSTPONE_TYPE","Symbol","for","isPostpone","error","$$typeof"],"mappings":"AAAA,MAAMA,sBAA8BC,OAAOC,GAAG,CAAC;AAE/C,OAAO,SAASC,WAAWC,KAAU;IACnC,OACE,OAAOA,UAAU,YACjBA,UAAU,QACVA,MAAMC,QAAQ,KAAKL;AAEvB"}

View File

@@ -0,0 +1,103 @@
import url from 'url';
import { stringifyQuery } from '../../server-route-utils';
import { Duplex } from 'stream';
import { DetachedPromise } from '../../../lib/detached-promise';
export async function proxyRequest(req, res, parsedUrl, upgradeHead, reqBody, proxyTimeout) {
const { query } = parsedUrl;
delete parsedUrl.query;
parsedUrl.search = stringifyQuery(req, query);
const target = url.format(parsedUrl);
const HttpProxy = require('next/dist/compiled/http-proxy');
const proxy = new HttpProxy({
target,
changeOrigin: true,
ignorePath: true,
ws: true,
// we limit proxy requests to 30s by default, in development
// we don't time out WebSocket requests to allow proxying
proxyTimeout: proxyTimeout === null ? undefined : proxyTimeout || 30000,
headers: {
'x-forwarded-host': req.headers.host || ''
}
});
let finished = false;
// http-proxy does not properly detect a client disconnect in newer
// versions of Node.js. This is caused because it only listens for the
// `aborted` event on the our request object, but it also fully reads
// and closes the request object. Node **will not** fire `aborted` when
// the request is already closed. Listening for `close` on our response
// object will detect the disconnect, and we can abort the proxy's
// connection.
proxy.on('proxyReq', (proxyReq)=>{
res.on('close', ()=>proxyReq.destroy());
});
proxy.on('proxyRes', (proxyRes)=>{
if (res.destroyed) {
proxyRes.destroy();
} else {
res.on('close', ()=>proxyRes.destroy());
}
});
proxy.on('proxyRes', (proxyRes, innerReq, innerRes)=>{
const cleanup = (err)=>{
// cleanup event listeners to allow clean garbage collection
proxyRes.removeListener('error', cleanup);
proxyRes.removeListener('close', cleanup);
innerRes.removeListener('error', cleanup);
innerRes.removeListener('close', cleanup);
// destroy all source streams to propagate the caught event backward
innerReq.destroy(err);
proxyRes.destroy(err);
};
proxyRes.once('error', cleanup);
proxyRes.once('close', cleanup);
innerRes.once('error', cleanup);
innerRes.once('close', cleanup);
});
const detached = new DetachedPromise();
proxy.on('error', (err)=>{
console.error(`Failed to proxy ${target}`, err);
if (!finished) {
finished = true;
detached.reject(err);
if (!res.destroyed) {
if (!(res instanceof Duplex)) {
res.statusCode = 500;
}
res.end('Internal Server Error');
}
}
});
// If upgrade head is present or the response is a Duplex stream, treat as
// WebSocket request.
if (upgradeHead || res instanceof Duplex) {
proxy.on('proxyReqWs', (proxyReq)=>{
proxyReq.on('close', ()=>{
if (!finished) {
finished = true;
detached.resolve(true);
}
});
});
proxy.ws(req, res, upgradeHead);
detached.resolve(true);
} else {
proxy.on('proxyReq', (proxyReq)=>{
proxyReq.on('close', ()=>{
if (!finished) {
finished = true;
detached.resolve(true);
}
});
});
proxy.web(req, res, {
buffer: reqBody
});
}
// When the proxy finishes proxying the request, shut down the proxy.
return detached.promise.finally(()=>{
proxy.close();
});
}
//# sourceMappingURL=proxy-request.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,566 @@
import url from 'url';
import path from 'node:path';
import setupDebug from 'next/dist/compiled/debug';
import { getCloneableBody } from '../../body-streams';
import { filterReqHeaders, ipcForbiddenHeaders } from '../server-ipc/utils';
import { stringifyQuery } from '../../server-route-utils';
import { formatHostname } from '../format-hostname';
import { toNodeOutgoingHttpHeaders } from '../../web/utils';
import { isAbortError } from '../../pipe-readable';
import { getHostname } from '../../../shared/lib/get-hostname';
import { getRedirectStatus } from '../../../lib/redirect-status';
import { normalizeRepeatedSlashes } from '../../../shared/lib/utils';
import { getRelativeURL } from '../../../shared/lib/router/utils/relativize-url';
import { addPathPrefix } from '../../../shared/lib/router/utils/add-path-prefix';
import { pathHasPrefix } from '../../../shared/lib/router/utils/path-has-prefix';
import { detectDomainLocale } from '../../../shared/lib/i18n/detect-domain-locale';
import { normalizeLocalePath } from '../../../shared/lib/i18n/normalize-locale-path';
import { removePathPrefix } from '../../../shared/lib/router/utils/remove-path-prefix';
import { NextDataPathnameNormalizer } from '../../normalizers/request/next-data';
import { BasePathPathnameNormalizer } from '../../normalizers/request/base-path';
import { addRequestMeta } from '../../request-meta';
import { compileNonPath, matchHas, parseDestination, prepareDestination } from '../../../shared/lib/router/utils/prepare-destination';
import { NEXT_REWRITTEN_PATH_HEADER, NEXT_REWRITTEN_QUERY_HEADER, NEXT_ROUTER_STATE_TREE_HEADER, RSC_HEADER } from '../../../client/components/app-router-headers';
import { getSelectedParams } from '../../../client/components/router-reducer/compute-changed-path';
import { isInterceptionRouteRewrite } from '../../../lib/generate-interception-routes-rewrites';
import { parseAndValidateFlightRouterState } from '../../app-render/parse-and-validate-flight-router-state';
const debug = setupDebug('next:router-server:resolve-routes');
export function getResolveRoutes(fsChecker, config, opts, renderServer, renderServerOpts, ensureMiddleware) {
const routes = [
// _next/data with middleware handling
{
match: ()=>({}),
name: 'middleware_next_data'
},
...opts.minimalMode ? [] : fsChecker.headers,
...opts.minimalMode ? [] : fsChecker.redirects,
// check middleware (using matchers)
{
match: ()=>({}),
name: 'middleware'
},
...opts.minimalMode ? [] : fsChecker.rewrites.beforeFiles,
// check middleware (using matchers)
{
match: ()=>({}),
name: 'before_files_end'
},
// we check exact matches on fs before continuing to
// after files rewrites
{
match: ()=>({}),
name: 'check_fs'
},
...opts.minimalMode ? [] : fsChecker.rewrites.afterFiles,
// we always do the check: true handling before continuing to
// fallback rewrites
{
check: true,
match: ()=>({}),
name: 'after files check: true'
},
...opts.minimalMode ? [] : fsChecker.rewrites.fallback
];
async function resolveRoutes({ req, res, isUpgradeReq, invokedOutputs }) {
var _req_socket, _req_headers_xforwardedproto;
let finished = false;
let resHeaders = {};
let matchedOutput = null;
let parsedUrl = url.parse(req.url || '', true);
let didRewrite = false;
const urlParts = (req.url || '').split('?', 1);
const urlNoQuery = urlParts[0];
// this normalizes repeated slashes in the path e.g. hello//world ->
// hello/world or backslashes to forward slashes, this does not
// handle trailing slash as that is handled the same as a next.config.js
// redirect
if (urlNoQuery == null ? void 0 : urlNoQuery.match(/(\\|\/\/)/)) {
parsedUrl = url.parse(normalizeRepeatedSlashes(req.url), true);
return {
parsedUrl,
resHeaders,
finished: true,
statusCode: 308
};
}
// TODO: inherit this from higher up
const protocol = (req == null ? void 0 : (_req_socket = req.socket) == null ? void 0 : _req_socket.encrypted) || ((_req_headers_xforwardedproto = req.headers['x-forwarded-proto']) == null ? void 0 : _req_headers_xforwardedproto.includes('https')) ? 'https' : 'http';
// When there are hostname and port we build an absolute URL
const initUrl = config.experimental.trustHostHeader ? `https://${req.headers.host || 'localhost'}${req.url}` : opts.port ? `${protocol}://${formatHostname(opts.hostname || 'localhost')}:${opts.port}${req.url}` : req.url || '';
addRequestMeta(req, 'initURL', initUrl);
addRequestMeta(req, 'initQuery', {
...parsedUrl.query
});
addRequestMeta(req, 'initProtocol', protocol);
if (!isUpgradeReq) {
addRequestMeta(req, 'clonableBody', getCloneableBody(req));
}
const maybeAddTrailingSlash = (pathname)=>{
if (config.trailingSlash && !config.skipMiddlewareUrlNormalize && !pathname.endsWith('/')) {
return `${pathname}/`;
}
return pathname;
};
let domainLocale;
let defaultLocale;
let initialLocaleResult = undefined;
if (config.i18n) {
var _parsedUrl_pathname;
const hadTrailingSlash = (_parsedUrl_pathname = parsedUrl.pathname) == null ? void 0 : _parsedUrl_pathname.endsWith('/');
const hadBasePath = pathHasPrefix(parsedUrl.pathname || '', config.basePath);
initialLocaleResult = normalizeLocalePath(removePathPrefix(parsedUrl.pathname || '/', config.basePath), config.i18n.locales);
domainLocale = detectDomainLocale(config.i18n.domains, getHostname(parsedUrl, req.headers));
defaultLocale = (domainLocale == null ? void 0 : domainLocale.defaultLocale) || config.i18n.defaultLocale;
addRequestMeta(req, 'defaultLocale', defaultLocale);
addRequestMeta(req, 'locale', initialLocaleResult.detectedLocale || defaultLocale);
// ensure locale is present for resolving routes
if (!initialLocaleResult.detectedLocale && !initialLocaleResult.pathname.startsWith('/_next/')) {
parsedUrl.pathname = addPathPrefix(initialLocaleResult.pathname === '/' ? `/${defaultLocale}` : addPathPrefix(initialLocaleResult.pathname || '', `/${defaultLocale}`), hadBasePath ? config.basePath : '');
if (hadTrailingSlash) {
parsedUrl.pathname = maybeAddTrailingSlash(parsedUrl.pathname);
}
}
}
const checkLocaleApi = (pathname)=>{
if (config.i18n && pathname === urlNoQuery && (initialLocaleResult == null ? void 0 : initialLocaleResult.detectedLocale) && pathHasPrefix(initialLocaleResult.pathname, '/api')) {
return true;
}
};
async function checkTrue() {
const pathname = parsedUrl.pathname || '';
if (checkLocaleApi(pathname)) {
return;
}
if (!(invokedOutputs == null ? void 0 : invokedOutputs.has(pathname))) {
const output = await fsChecker.getItem(pathname);
if (output) {
if (config.useFileSystemPublicRoutes || didRewrite || output.type !== 'appFile' && output.type !== 'pageFile') {
return output;
}
}
}
const dynamicRoutes = fsChecker.getDynamicRoutes();
let curPathname = parsedUrl.pathname;
if (config.basePath) {
if (!pathHasPrefix(curPathname || '', config.basePath)) {
return;
}
curPathname = (curPathname == null ? void 0 : curPathname.substring(config.basePath.length)) || '/';
}
const localeResult = fsChecker.handleLocale(curPathname || '');
for (const route of dynamicRoutes){
// when resolving fallback: false the
// render worker may return a no-fallback response
// which signals we need to continue resolving.
// TODO: optimize this to collect static paths
// to use at the routing layer
if (invokedOutputs == null ? void 0 : invokedOutputs.has(route.page)) {
continue;
}
const params = route.match(localeResult.pathname);
if (params) {
const pageOutput = await fsChecker.getItem(addPathPrefix(route.page, config.basePath || ''));
// i18n locales aren't matched for app dir
if ((pageOutput == null ? void 0 : pageOutput.type) === 'appFile' && (initialLocaleResult == null ? void 0 : initialLocaleResult.detectedLocale)) {
continue;
}
if (pageOutput && (curPathname == null ? void 0 : curPathname.startsWith('/_next/data'))) {
addRequestMeta(req, 'isNextDataReq', true);
}
if (config.useFileSystemPublicRoutes || didRewrite) {
return pageOutput;
}
}
}
}
const normalizers = {
basePath: config.basePath && config.basePath !== '/' ? new BasePathPathnameNormalizer(config.basePath) : undefined,
data: new NextDataPathnameNormalizer(fsChecker.buildId)
};
async function handleRoute(route) {
let curPathname = parsedUrl.pathname || '/';
if (config.i18n && route.internal) {
const hadTrailingSlash = curPathname.endsWith('/');
if (config.basePath) {
curPathname = removePathPrefix(curPathname, config.basePath);
}
const hadBasePath = curPathname !== parsedUrl.pathname;
const localeResult = normalizeLocalePath(curPathname, config.i18n.locales);
const isDefaultLocale = localeResult.detectedLocale === defaultLocale;
if (isDefaultLocale) {
curPathname = localeResult.pathname === '/' && hadBasePath ? config.basePath : addPathPrefix(localeResult.pathname, hadBasePath ? config.basePath : '');
} else if (hadBasePath) {
curPathname = curPathname === '/' ? config.basePath : addPathPrefix(curPathname, config.basePath);
}
if ((isDefaultLocale || hadBasePath) && hadTrailingSlash) {
curPathname = maybeAddTrailingSlash(curPathname);
}
}
let params = route.match(curPathname);
if ((route.has || route.missing) && params) {
const hasParams = matchHas(req, parsedUrl.query, route.has, route.missing);
if (hasParams) {
Object.assign(params, hasParams);
} else {
params = false;
}
}
if (params) {
if (fsChecker.exportPathMapRoutes && route.name === 'before_files_end') {
for (const exportPathMapRoute of fsChecker.exportPathMapRoutes){
const result = await handleRoute(exportPathMapRoute);
if (result) {
return result;
}
}
}
if (route.name === 'middleware_next_data' && parsedUrl.pathname) {
var _fsChecker_getMiddlewareMatchers;
if ((_fsChecker_getMiddlewareMatchers = fsChecker.getMiddlewareMatchers()) == null ? void 0 : _fsChecker_getMiddlewareMatchers.length) {
var _normalizers_basePath;
let normalized = parsedUrl.pathname;
// Remove the base path if it exists.
const hadBasePath = (_normalizers_basePath = normalizers.basePath) == null ? void 0 : _normalizers_basePath.match(parsedUrl.pathname);
if (hadBasePath && normalizers.basePath) {
normalized = normalizers.basePath.normalize(normalized, true);
}
let updated = false;
if (normalizers.data.match(normalized)) {
updated = true;
addRequestMeta(req, 'isNextDataReq', true);
normalized = normalizers.data.normalize(normalized, true);
}
if (config.i18n) {
const curLocaleResult = normalizeLocalePath(normalized, config.i18n.locales);
if (curLocaleResult.detectedLocale) {
addRequestMeta(req, 'locale', curLocaleResult.detectedLocale);
}
}
// If we updated the pathname, and it had a base path, re-add the
// base path.
if (updated) {
if (hadBasePath) {
normalized = path.posix.join(config.basePath, normalized);
}
// Re-add the trailing slash (if required).
normalized = maybeAddTrailingSlash(normalized);
parsedUrl.pathname = normalized;
}
}
}
if (route.name === 'check_fs') {
const pathname = parsedUrl.pathname || '';
if ((invokedOutputs == null ? void 0 : invokedOutputs.has(pathname)) || checkLocaleApi(pathname)) {
return;
}
const output = await fsChecker.getItem(pathname);
if (output && !(config.i18n && (initialLocaleResult == null ? void 0 : initialLocaleResult.detectedLocale) && pathHasPrefix(pathname, '/api'))) {
if (config.useFileSystemPublicRoutes || didRewrite || output.type !== 'appFile' && output.type !== 'pageFile') {
matchedOutput = output;
if (output.locale) {
addRequestMeta(req, 'locale', output.locale);
}
return {
parsedUrl,
resHeaders,
finished: true,
matchedOutput
};
}
}
}
if (!opts.minimalMode && route.name === 'middleware') {
const match = fsChecker.getMiddlewareMatchers();
if (// @ts-expect-error BaseNextRequest stuff
match == null ? void 0 : match(parsedUrl.pathname, req, parsedUrl.query)) {
if (ensureMiddleware) {
await ensureMiddleware(req.url);
}
const serverResult = await (renderServer == null ? void 0 : renderServer.initialize(renderServerOpts));
if (!serverResult) {
throw Object.defineProperty(new Error(`Failed to initialize render server "middleware"`), "__NEXT_ERROR_CODE", {
value: "E222",
enumerable: false,
configurable: true
});
}
addRequestMeta(req, 'invokePath', '');
addRequestMeta(req, 'invokeOutput', '');
addRequestMeta(req, 'invokeQuery', {});
addRequestMeta(req, 'middlewareInvoke', true);
debug('invoking middleware', req.url, req.headers);
let middlewareRes = undefined;
let bodyStream = undefined;
try {
try {
await serverResult.requestHandler(req, res, parsedUrl);
} catch (err) {
if (!('result' in err) || !('response' in err.result)) {
throw err;
}
middlewareRes = err.result.response;
res.statusCode = middlewareRes.status;
if (middlewareRes.body) {
bodyStream = middlewareRes.body;
} else if (middlewareRes.status) {
bodyStream = new ReadableStream({
start (controller) {
controller.enqueue('');
controller.close();
}
});
}
}
} catch (e) {
// If the client aborts before we can receive a response object
// (when the headers are flushed), then we can early exit without
// further processing.
if (isAbortError(e)) {
return {
parsedUrl,
resHeaders,
finished: true
};
}
throw e;
}
if (res.closed || res.finished || !middlewareRes) {
return {
parsedUrl,
resHeaders,
finished: true
};
}
const middlewareHeaders = toNodeOutgoingHttpHeaders(middlewareRes.headers);
debug('middleware res', middlewareRes.status, middlewareHeaders);
if (middlewareHeaders['x-middleware-override-headers']) {
const overriddenHeaders = new Set();
let overrideHeaders = middlewareHeaders['x-middleware-override-headers'];
if (typeof overrideHeaders === 'string') {
overrideHeaders = overrideHeaders.split(',');
}
for (const key of overrideHeaders){
overriddenHeaders.add(key.trim());
}
delete middlewareHeaders['x-middleware-override-headers'];
// Delete headers.
for (const key of Object.keys(req.headers)){
if (!overriddenHeaders.has(key)) {
delete req.headers[key];
}
}
// Update or add headers.
for (const key of overriddenHeaders.keys()){
const valueKey = 'x-middleware-request-' + key;
const newValue = middlewareHeaders[valueKey];
const oldValue = req.headers[key];
if (oldValue !== newValue) {
req.headers[key] = newValue === null ? undefined : newValue;
}
delete middlewareHeaders[valueKey];
}
}
if (!middlewareHeaders['x-middleware-rewrite'] && !middlewareHeaders['x-middleware-next'] && !middlewareHeaders['location']) {
middlewareHeaders['x-middleware-refresh'] = '1';
}
delete middlewareHeaders['x-middleware-next'];
for (const [key, value] of Object.entries({
...filterReqHeaders(middlewareHeaders, ipcForbiddenHeaders)
})){
if ([
'content-length',
'x-middleware-rewrite',
'x-middleware-redirect',
'x-middleware-refresh'
].includes(key)) {
continue;
}
// for set-cookie, the header shouldn't be added to the response
// as it's only needed for the request to the middleware function.
if (key === 'x-middleware-set-cookie') {
req.headers[key] = value;
continue;
}
if (value) {
resHeaders[key] = value;
req.headers[key] = value;
}
}
if (middlewareHeaders['x-middleware-rewrite']) {
const value = middlewareHeaders['x-middleware-rewrite'];
const destination = getRelativeURL(value, initUrl);
resHeaders['x-middleware-rewrite'] = destination;
parsedUrl = url.parse(destination, true);
if (parsedUrl.protocol) {
return {
parsedUrl,
resHeaders,
finished: true
};
}
if (config.i18n) {
const curLocaleResult = normalizeLocalePath(parsedUrl.pathname || '', config.i18n.locales);
if (curLocaleResult.detectedLocale) {
addRequestMeta(req, 'locale', curLocaleResult.detectedLocale);
}
}
}
if (middlewareHeaders['location']) {
const value = middlewareHeaders['location'];
const rel = getRelativeURL(value, initUrl);
resHeaders['location'] = rel;
parsedUrl = url.parse(rel, true);
return {
parsedUrl,
resHeaders,
finished: true,
statusCode: middlewareRes.status
};
}
if (middlewareHeaders['x-middleware-refresh']) {
return {
parsedUrl,
resHeaders,
finished: true,
bodyStream,
statusCode: middlewareRes.status
};
}
}
}
// handle redirect
if (('statusCode' in route || 'permanent' in route) && route.destination) {
const { parsedDestination } = prepareDestination({
appendParamsToQuery: false,
destination: route.destination,
params: params,
query: parsedUrl.query
});
const { query } = parsedDestination;
delete parsedDestination.query;
parsedDestination.search = stringifyQuery(req, query);
parsedDestination.pathname = normalizeRepeatedSlashes(parsedDestination.pathname);
return {
finished: true,
// @ts-expect-error custom ParsedUrl
parsedUrl: parsedDestination,
statusCode: getRedirectStatus(route)
};
}
// handle headers
if (route.headers) {
const hasParams = Object.keys(params).length > 0;
for (const header of route.headers){
let { key, value } = header;
if (hasParams) {
key = compileNonPath(key, params);
value = compileNonPath(value, params);
}
if (key.toLowerCase() === 'set-cookie') {
if (!Array.isArray(resHeaders[key])) {
const val = resHeaders[key];
resHeaders[key] = typeof val === 'string' ? [
val
] : [];
}
;
resHeaders[key].push(value);
} else {
resHeaders[key] = value;
}
}
}
// handle rewrite
if (route.destination) {
let rewriteParams = params;
try {
// An interception rewrite might reference a dynamic param for a route the user
// is currently on, which wouldn't be extractable from the matched route params.
// This attempts to extract the dynamic params from the provided router state.
if (isInterceptionRouteRewrite(route)) {
const stateHeader = req.headers[NEXT_ROUTER_STATE_TREE_HEADER.toLowerCase()];
if (stateHeader) {
rewriteParams = {
...getSelectedParams(parseAndValidateFlightRouterState(stateHeader)),
...params
};
}
}
} catch (err) {
// this is a no-op -- we couldn't extract dynamic params from the provided router state,
// so we'll just use the params from the route matcher
}
// We extract the search params of the destination so we can set it on
// the response headers. We don't want to use the following
// `parsedDestination` as the query object is mutated.
const { search: destinationSearch, pathname: destinationPathname } = parseDestination({
destination: route.destination,
params: rewriteParams,
query: parsedUrl.query
});
const { parsedDestination } = prepareDestination({
appendParamsToQuery: true,
destination: route.destination,
params: rewriteParams,
query: parsedUrl.query
});
if (parsedDestination.protocol) {
return {
// @ts-expect-error custom ParsedUrl
parsedUrl: parsedDestination,
finished: true
};
}
// Set the rewrite headers only if this is a RSC request.
if (req.headers[RSC_HEADER.toLowerCase()] === '1') {
// We set the rewritten path and query headers on the response now
// that we know that the it's not an external rewrite.
if (parsedUrl.pathname !== destinationPathname) {
res.setHeader(NEXT_REWRITTEN_PATH_HEADER, destinationPathname);
}
if (destinationSearch) {
res.setHeader(NEXT_REWRITTEN_QUERY_HEADER, // remove the leading ? from the search
destinationSearch.slice(1));
}
}
if (config.i18n) {
const curLocaleResult = normalizeLocalePath(removePathPrefix(parsedDestination.pathname, config.basePath), config.i18n.locales);
if (curLocaleResult.detectedLocale) {
addRequestMeta(req, 'locale', curLocaleResult.detectedLocale);
}
}
didRewrite = true;
parsedUrl.pathname = parsedDestination.pathname;
Object.assign(parsedUrl.query, parsedDestination.query);
}
// handle check: true
if (route.check) {
const output = await checkTrue();
if (output) {
return {
parsedUrl,
resHeaders,
finished: true,
matchedOutput: output
};
}
}
}
}
for (const route of routes){
const result = await handleRoute(route);
if (result) {
return result;
}
}
return {
finished,
parsedUrl,
resHeaders,
matchedOutput
};
}
return resolveRoutes;
}
//# sourceMappingURL=resolve-routes.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,704 @@
import { getPageStaticInfo } from '../../../build/analysis/get-page-static-info';
import { createDefineEnv } from '../../../build/swc';
import fs from 'fs';
import { mkdir } from 'fs/promises';
import url from 'url';
import path from 'path';
import qs from 'querystring';
import Watchpack from 'next/dist/compiled/watchpack';
import { loadEnvConfig } from '@next/env';
import findUp from 'next/dist/compiled/find-up';
import { buildCustomRoute } from './filesystem';
import * as Log from '../../../build/output/log';
import HotReloaderWebpack from '../../dev/hot-reloader-webpack';
import { setGlobal } from '../../../trace/shared';
import loadJsConfig from '../../../build/load-jsconfig';
import { createValidFileMatcher } from '../find-page-file';
import { eventCliSession } from '../../../telemetry/events';
import { getDefineEnv } from '../../../build/webpack/plugins/define-env-plugin';
import { getSortedRoutes } from '../../../shared/lib/router/utils';
import { getStaticInfoIncludingLayouts, sortByPageExts } from '../../../build/entries';
import { verifyTypeScriptSetup } from '../../../lib/verify-typescript-setup';
import { verifyPartytownSetup } from '../../../lib/verify-partytown-setup';
import { getRouteRegex } from '../../../shared/lib/router/utils/route-regex';
import { normalizeAppPath } from '../../../shared/lib/router/utils/app-paths';
import { buildDataRoute } from './build-data-route';
import { getRouteMatcher } from '../../../shared/lib/router/utils/route-matcher';
import { normalizePathSep } from '../../../shared/lib/page-path/normalize-path-sep';
import { createClientRouterFilter } from '../../../lib/create-client-router-filter';
import { absolutePathToPage } from '../../../shared/lib/page-path/absolute-path-to-page';
import { generateInterceptionRoutesRewrites } from '../../../lib/generate-interception-routes-rewrites';
import { CLIENT_STATIC_FILES_PATH, DEV_CLIENT_PAGES_MANIFEST, DEV_CLIENT_MIDDLEWARE_MANIFEST, PHASE_DEVELOPMENT_SERVER, TURBOPACK_CLIENT_MIDDLEWARE_MANIFEST } from '../../../shared/lib/constants';
import { getMiddlewareRouteMatcher } from '../../../shared/lib/router/utils/middleware-route-matcher';
import { isMiddlewareFile, NestedMiddlewareError, isInstrumentationHookFile, getPossibleMiddlewareFilenames, getPossibleInstrumentationHookFilenames } from '../../../build/utils';
import { devPageFiles } from '../../../build/webpack/plugins/next-types-plugin/shared';
import { HMR_ACTIONS_SENT_TO_BROWSER } from '../../dev/hot-reloader-types';
import { PAGE_TYPES } from '../../../lib/page-types';
import { createHotReloaderTurbopack } from '../../dev/hot-reloader-turbopack';
import { generateEncryptionKeyBase64 } from '../../app-render/encryption-utils-server';
import { isMetadataRouteFile } from '../../../lib/metadata/is-metadata-route';
import { normalizeMetadataPageToRoute } from '../../../lib/metadata/get-metadata-route';
import { createEnvDefinitions } from '../experimental/create-env-definitions';
import { JsConfigPathsPlugin } from '../../../build/webpack/plugins/jsconfig-paths-plugin';
import { store as consoleStore } from '../../../build/output/store';
import { ModuleBuildError, TurbopackInternalError } from '../../../shared/lib/turbopack/utils';
async function verifyTypeScript(opts) {
let usingTypeScript = false;
const verifyResult = await verifyTypeScriptSetup({
dir: opts.dir,
distDir: opts.nextConfig.distDir,
intentDirs: [
opts.pagesDir,
opts.appDir
].filter(Boolean),
typeCheckPreflight: false,
tsconfigPath: opts.nextConfig.typescript.tsconfigPath,
disableStaticImages: opts.nextConfig.images.disableStaticImages,
hasAppDir: !!opts.appDir,
hasPagesDir: !!opts.pagesDir
});
if (verifyResult.version) {
usingTypeScript = true;
}
return usingTypeScript;
}
export async function propagateServerField(opts, field, args) {
var _opts_renderServer_instance, _opts_renderServer;
await ((_opts_renderServer = opts.renderServer) == null ? void 0 : (_opts_renderServer_instance = _opts_renderServer.instance) == null ? void 0 : _opts_renderServer_instance.propagateServerField(opts.dir, field, args));
}
async function startWatcher(opts) {
const { nextConfig, appDir, pagesDir, dir, resetFetch } = opts;
const { useFileSystemPublicRoutes } = nextConfig;
const usingTypeScript = await verifyTypeScript(opts);
const distDir = path.join(opts.dir, opts.nextConfig.distDir);
// we ensure the types directory exists here
if (usingTypeScript) {
const distTypesDir = path.join(distDir, 'types');
if (!fs.existsSync(distTypesDir)) {
await mkdir(distTypesDir, {
recursive: true
});
}
}
setGlobal('distDir', distDir);
setGlobal('phase', PHASE_DEVELOPMENT_SERVER);
const validFileMatcher = createValidFileMatcher(nextConfig.pageExtensions, appDir);
const serverFields = {};
// Update logging state once based on next.config.js when initializing
consoleStore.setState({
logging: nextConfig.logging !== false
});
const hotReloader = opts.turbo ? await createHotReloaderTurbopack(opts, serverFields, distDir, resetFetch) : new HotReloaderWebpack(opts.dir, {
appDir,
pagesDir,
distDir,
config: opts.nextConfig,
buildId: 'development',
encryptionKey: await generateEncryptionKeyBase64({
isBuild: false,
distDir
}),
telemetry: opts.telemetry,
rewrites: opts.fsChecker.rewrites,
previewProps: opts.fsChecker.prerenderManifest.preview,
resetFetch
});
await hotReloader.start();
if (opts.nextConfig.experimental.nextScriptWorkers) {
await verifyPartytownSetup(opts.dir, path.join(distDir, CLIENT_STATIC_FILES_PATH));
}
opts.fsChecker.ensureCallback(async function ensure(item) {
if (item.type === 'appFile' || item.type === 'pageFile') {
await hotReloader.ensurePage({
clientOnly: false,
page: item.itemPath,
isApp: item.type === 'appFile',
definition: undefined
});
}
});
let resolved = false;
let prevSortedRoutes = [];
await new Promise(async (resolve, reject)=>{
if (pagesDir) {
// Watchpack doesn't emit an event for an empty directory
fs.readdir(pagesDir, (_, files)=>{
if (files == null ? void 0 : files.length) {
return;
}
if (!resolved) {
resolve();
resolved = true;
}
});
}
const pages = pagesDir ? [
pagesDir
] : [];
const app = appDir ? [
appDir
] : [];
const directories = [
...pages,
...app
];
const rootDir = pagesDir || appDir;
const files = [
...getPossibleMiddlewareFilenames(path.join(rootDir, '..'), nextConfig.pageExtensions),
...getPossibleInstrumentationHookFilenames(path.join(rootDir, '..'), nextConfig.pageExtensions)
];
let nestedMiddleware = [];
const envFiles = [
'.env.development.local',
'.env.local',
'.env.development',
'.env'
].map((file)=>path.join(dir, file));
files.push(...envFiles);
// tsconfig/jsconfig paths hot-reloading
const tsconfigPaths = [
path.join(dir, 'tsconfig.json'),
path.join(dir, 'jsconfig.json')
];
files.push(...tsconfigPaths);
const wp = new Watchpack({
ignored: (pathname)=>{
return !files.some((file)=>file.startsWith(pathname)) && !directories.some((d)=>pathname.startsWith(d) || d.startsWith(pathname));
}
});
const fileWatchTimes = new Map();
let enabledTypeScript = usingTypeScript;
let previousClientRouterFilters;
let previousConflictingPagePaths = new Set();
wp.on('aggregated', async ()=>{
var _serverFields_middleware, _serverFields_middleware1;
let middlewareMatchers;
const routedPages = [];
const knownFiles = wp.getTimeInfoEntries();
const appPaths = {};
const pageNameSet = new Set();
const conflictingAppPagePaths = new Set();
const appPageFilePaths = new Map();
const pagesPageFilePaths = new Map();
let envChange = false;
let tsconfigChange = false;
let conflictingPageChange = 0;
let hasRootAppNotFound = false;
const { appFiles, pageFiles } = opts.fsChecker;
appFiles.clear();
pageFiles.clear();
devPageFiles.clear();
const sortedKnownFiles = [
...knownFiles.keys()
].sort(sortByPageExts(nextConfig.pageExtensions));
for (const fileName of sortedKnownFiles){
if (!files.includes(fileName) && !directories.some((d)=>fileName.startsWith(d))) {
continue;
}
const meta = knownFiles.get(fileName);
const watchTime = fileWatchTimes.get(fileName);
// If the file is showing up for the first time or the meta.timestamp is changed since last time
const watchTimeChange = watchTime === undefined || watchTime && watchTime !== (meta == null ? void 0 : meta.timestamp);
fileWatchTimes.set(fileName, meta == null ? void 0 : meta.timestamp);
if (envFiles.includes(fileName)) {
if (watchTimeChange) {
envChange = true;
}
continue;
}
if (tsconfigPaths.includes(fileName)) {
if (fileName.endsWith('tsconfig.json')) {
enabledTypeScript = true;
}
if (watchTimeChange) {
tsconfigChange = true;
}
continue;
}
if ((meta == null ? void 0 : meta.accuracy) === undefined || !validFileMatcher.isPageFile(fileName)) {
continue;
}
const isAppPath = Boolean(appDir && normalizePathSep(fileName).startsWith(normalizePathSep(appDir) + '/'));
const isPagePath = Boolean(pagesDir && normalizePathSep(fileName).startsWith(normalizePathSep(pagesDir) + '/'));
const rootFile = absolutePathToPage(fileName, {
dir: dir,
extensions: nextConfig.pageExtensions,
keepIndex: false,
pagesType: PAGE_TYPES.ROOT
});
if (isMiddlewareFile(rootFile)) {
var _staticInfo_middleware;
const staticInfo = await getStaticInfoIncludingLayouts({
pageFilePath: fileName,
config: nextConfig,
appDir: appDir,
page: rootFile,
isDev: true,
isInsideAppDir: isAppPath,
pageExtensions: nextConfig.pageExtensions
});
if (nextConfig.output === 'export') {
Log.error('Middleware cannot be used with "output: export". See more info here: https://nextjs.org/docs/advanced-features/static-html-export');
continue;
}
serverFields.actualMiddlewareFile = rootFile;
await propagateServerField(opts, 'actualMiddlewareFile', serverFields.actualMiddlewareFile);
middlewareMatchers = ((_staticInfo_middleware = staticInfo.middleware) == null ? void 0 : _staticInfo_middleware.matchers) || [
{
regexp: '.*',
originalSource: '/:path*'
}
];
continue;
}
if (isInstrumentationHookFile(rootFile)) {
serverFields.actualInstrumentationHookFile = rootFile;
await propagateServerField(opts, 'actualInstrumentationHookFile', serverFields.actualInstrumentationHookFile);
continue;
}
if (fileName.endsWith('.ts') || fileName.endsWith('.tsx')) {
enabledTypeScript = true;
}
if (!(isAppPath || isPagePath)) {
continue;
}
// Collect all current filenames for the TS plugin to use
devPageFiles.add(fileName);
let pageName = absolutePathToPage(fileName, {
dir: isAppPath ? appDir : pagesDir,
extensions: nextConfig.pageExtensions,
keepIndex: isAppPath,
pagesType: isAppPath ? PAGE_TYPES.APP : PAGE_TYPES.PAGES
});
if (isAppPath && appDir && isMetadataRouteFile(fileName.replace(appDir, ''), nextConfig.pageExtensions, true)) {
const staticInfo = await getPageStaticInfo({
pageFilePath: fileName,
nextConfig: {},
page: pageName,
isDev: true,
pageType: PAGE_TYPES.APP
});
pageName = normalizeMetadataPageToRoute(pageName, !!(staticInfo.generateSitemaps || staticInfo.generateImageMetadata));
}
if (!isAppPath && pageName.startsWith('/api/') && nextConfig.output === 'export') {
Log.error('API Routes cannot be used with "output: export". See more info here: https://nextjs.org/docs/advanced-features/static-html-export');
continue;
}
if (isAppPath) {
const isRootNotFound = validFileMatcher.isRootNotFound(fileName);
hasRootAppNotFound = true;
if (isRootNotFound) {
continue;
}
if (!isRootNotFound && !validFileMatcher.isAppRouterPage(fileName)) {
continue;
}
// Ignore files/directories starting with `_` in the app directory
if (normalizePathSep(pageName).includes('/_')) {
continue;
}
const originalPageName = pageName;
pageName = normalizeAppPath(pageName).replace(/%5F/g, '_');
if (!appPaths[pageName]) {
appPaths[pageName] = [];
}
appPaths[pageName].push(originalPageName);
if (useFileSystemPublicRoutes) {
appFiles.add(pageName);
}
if (routedPages.includes(pageName)) {
continue;
}
} else {
if (useFileSystemPublicRoutes) {
pageFiles.add(pageName);
// always add to nextDataRoutes for now but in future only add
// entries that actually use getStaticProps/getServerSideProps
opts.fsChecker.nextDataRoutes.add(pageName);
}
}
;
(isAppPath ? appPageFilePaths : pagesPageFilePaths).set(pageName, fileName);
if (appDir && pageNameSet.has(pageName)) {
conflictingAppPagePaths.add(pageName);
} else {
pageNameSet.add(pageName);
}
/**
* If there is a middleware that is not declared in the root we will
* warn without adding it so it doesn't make its way into the system.
*/ if (/[\\\\/]_middleware$/.test(pageName)) {
nestedMiddleware.push(pageName);
continue;
}
routedPages.push(pageName);
}
const numConflicting = conflictingAppPagePaths.size;
conflictingPageChange = numConflicting - previousConflictingPagePaths.size;
if (conflictingPageChange !== 0) {
if (numConflicting > 0) {
let errorMessage = `Conflicting app and page file${numConflicting === 1 ? ' was' : 's were'} found, please remove the conflicting files to continue:\n`;
for (const p of conflictingAppPagePaths){
const appPath = path.relative(dir, appPageFilePaths.get(p));
const pagesPath = path.relative(dir, pagesPageFilePaths.get(p));
errorMessage += ` "${pagesPath}" - "${appPath}"\n`;
}
hotReloader.setHmrServerError(Object.defineProperty(new Error(errorMessage), "__NEXT_ERROR_CODE", {
value: "E394",
enumerable: false,
configurable: true
}));
} else if (numConflicting === 0) {
hotReloader.clearHmrServerError();
await propagateServerField(opts, 'reloadMatchers', undefined);
}
}
previousConflictingPagePaths = conflictingAppPagePaths;
let clientRouterFilters;
if (nextConfig.experimental.clientRouterFilter) {
clientRouterFilters = createClientRouterFilter(Object.keys(appPaths), nextConfig.experimental.clientRouterFilterRedirects ? (nextConfig._originalRedirects || []).filter((r)=>!r.internal) : [], nextConfig.experimental.clientRouterFilterAllowedRate);
if (!previousClientRouterFilters || JSON.stringify(previousClientRouterFilters) !== JSON.stringify(clientRouterFilters)) {
envChange = true;
previousClientRouterFilters = clientRouterFilters;
}
}
if (!usingTypeScript && enabledTypeScript) {
// we tolerate the error here as this is best effort
// and the manual install command will be shown
await verifyTypeScript(opts).then(()=>{
tsconfigChange = true;
}).catch(()=>{});
}
if (envChange || tsconfigChange) {
var _hotReloader_activeWebpackConfigs;
if (envChange) {
var _nextConfig_experimental;
const { loadedEnvFiles } = loadEnvConfig(dir, process.env.NODE_ENV === 'development', Log, true, (envFilePath)=>{
Log.info(`Reload env: ${envFilePath}`);
});
if (usingTypeScript && ((_nextConfig_experimental = nextConfig.experimental) == null ? void 0 : _nextConfig_experimental.typedEnv)) {
// do not await, this is not essential for further process
createEnvDefinitions({
distDir,
loadedEnvFiles: [
...loadedEnvFiles,
{
path: nextConfig.configFileName,
env: nextConfig.env,
contents: ''
}
]
});
}
await propagateServerField(opts, 'loadEnvConfig', [
{
dev: true,
forceReload: true,
silent: true
}
]);
}
let tsconfigResult;
if (tsconfigChange) {
try {
tsconfigResult = await loadJsConfig(dir, nextConfig);
} catch (_) {
/* do we want to log if there are syntax errors in tsconfig while editing? */ }
}
if (hotReloader.turbopackProject) {
const hasRewrites = opts.fsChecker.rewrites.afterFiles.length > 0 || opts.fsChecker.rewrites.beforeFiles.length > 0 || opts.fsChecker.rewrites.fallback.length > 0;
await hotReloader.turbopackProject.update({
defineEnv: createDefineEnv({
isTurbopack: true,
clientRouterFilters,
config: nextConfig,
dev: true,
distDir,
fetchCacheKeyPrefix: opts.nextConfig.experimental.fetchCacheKeyPrefix,
hasRewrites,
// TODO: Implement
middlewareMatchers: undefined
})
});
}
(_hotReloader_activeWebpackConfigs = hotReloader.activeWebpackConfigs) == null ? void 0 : _hotReloader_activeWebpackConfigs.forEach((config, idx)=>{
const isClient = idx === 0;
const isNodeServer = idx === 1;
const isEdgeServer = idx === 2;
const hasRewrites = opts.fsChecker.rewrites.afterFiles.length > 0 || opts.fsChecker.rewrites.beforeFiles.length > 0 || opts.fsChecker.rewrites.fallback.length > 0;
if (tsconfigChange) {
var _config_resolve_plugins, _config_resolve;
(_config_resolve = config.resolve) == null ? void 0 : (_config_resolve_plugins = _config_resolve.plugins) == null ? void 0 : _config_resolve_plugins.forEach((plugin)=>{
// look for the JsConfigPathsPlugin and update with
// the latest paths/baseUrl config
if (plugin instanceof JsConfigPathsPlugin && tsconfigResult) {
var _config_resolve_modules, _config_resolve, _jsConfig_compilerOptions;
const { resolvedBaseUrl, jsConfig } = tsconfigResult;
const currentResolvedBaseUrl = plugin.resolvedBaseUrl;
const resolvedUrlIndex = (_config_resolve = config.resolve) == null ? void 0 : (_config_resolve_modules = _config_resolve.modules) == null ? void 0 : _config_resolve_modules.findIndex((item)=>item === (currentResolvedBaseUrl == null ? void 0 : currentResolvedBaseUrl.baseUrl));
if (resolvedBaseUrl) {
if (resolvedBaseUrl.baseUrl !== (currentResolvedBaseUrl == null ? void 0 : currentResolvedBaseUrl.baseUrl)) {
// remove old baseUrl and add new one
if (resolvedUrlIndex && resolvedUrlIndex > -1) {
var _config_resolve_modules1, _config_resolve1;
(_config_resolve1 = config.resolve) == null ? void 0 : (_config_resolve_modules1 = _config_resolve1.modules) == null ? void 0 : _config_resolve_modules1.splice(resolvedUrlIndex, 1);
}
// If the resolvedBaseUrl is implicit we only remove the previous value.
// Only add the baseUrl if it's explicitly set in tsconfig/jsconfig
if (!resolvedBaseUrl.isImplicit) {
var _config_resolve_modules2, _config_resolve2;
(_config_resolve2 = config.resolve) == null ? void 0 : (_config_resolve_modules2 = _config_resolve2.modules) == null ? void 0 : _config_resolve_modules2.push(resolvedBaseUrl.baseUrl);
}
}
}
if ((jsConfig == null ? void 0 : (_jsConfig_compilerOptions = jsConfig.compilerOptions) == null ? void 0 : _jsConfig_compilerOptions.paths) && resolvedBaseUrl) {
Object.keys(plugin.paths).forEach((key)=>{
delete plugin.paths[key];
});
Object.assign(plugin.paths, jsConfig.compilerOptions.paths);
plugin.resolvedBaseUrl = resolvedBaseUrl;
}
}
});
}
if (envChange) {
var _config_plugins;
(_config_plugins = config.plugins) == null ? void 0 : _config_plugins.forEach((plugin)=>{
// we look for the DefinePlugin definitions so we can
// update them on the active compilers
if (plugin && typeof plugin.definitions === 'object' && plugin.definitions.__NEXT_DEFINE_ENV) {
const newDefine = getDefineEnv({
isTurbopack: false,
clientRouterFilters,
config: nextConfig,
dev: true,
distDir,
fetchCacheKeyPrefix: opts.nextConfig.experimental.fetchCacheKeyPrefix,
hasRewrites,
isClient,
isEdgeServer,
isNodeOrEdgeCompilation: isNodeServer || isEdgeServer,
isNodeServer,
middlewareMatchers: undefined
});
Object.keys(plugin.definitions).forEach((key)=>{
if (!(key in newDefine)) {
delete plugin.definitions[key];
}
});
Object.assign(plugin.definitions, newDefine);
}
});
}
});
await hotReloader.invalidate({
reloadAfterInvalidation: envChange
});
}
if (nestedMiddleware.length > 0) {
Log.error(Object.defineProperty(new NestedMiddlewareError(nestedMiddleware, dir, pagesDir || appDir), "__NEXT_ERROR_CODE", {
value: "E394",
enumerable: false,
configurable: true
}).message);
nestedMiddleware = [];
}
// Make sure to sort parallel routes to make the result deterministic.
serverFields.appPathRoutes = Object.fromEntries(Object.entries(appPaths).map(([k, v])=>[
k,
v.sort()
]));
await propagateServerField(opts, 'appPathRoutes', serverFields.appPathRoutes);
// TODO: pass this to fsChecker/next-dev-server?
serverFields.middleware = middlewareMatchers ? {
match: null,
page: '/',
matchers: middlewareMatchers
} : undefined;
await propagateServerField(opts, 'middleware', serverFields.middleware);
serverFields.hasAppNotFound = hasRootAppNotFound;
opts.fsChecker.middlewareMatcher = ((_serverFields_middleware = serverFields.middleware) == null ? void 0 : _serverFields_middleware.matchers) ? getMiddlewareRouteMatcher((_serverFields_middleware1 = serverFields.middleware) == null ? void 0 : _serverFields_middleware1.matchers) : undefined;
const interceptionRoutes = generateInterceptionRoutesRewrites(Object.keys(appPaths), opts.nextConfig.basePath).map((item)=>buildCustomRoute('before_files_rewrite', item, opts.nextConfig.basePath, opts.nextConfig.experimental.caseSensitiveRoutes));
opts.fsChecker.rewrites.beforeFiles.push(...interceptionRoutes);
const exportPathMap = typeof nextConfig.exportPathMap === 'function' && await (nextConfig.exportPathMap == null ? void 0 : nextConfig.exportPathMap.call(nextConfig, {}, {
dev: true,
dir: opts.dir,
outDir: null,
distDir: distDir,
buildId: 'development'
})) || {};
const exportPathMapEntries = Object.entries(exportPathMap || {});
if (exportPathMapEntries.length > 0) {
opts.fsChecker.exportPathMapRoutes = exportPathMapEntries.map(([key, value])=>buildCustomRoute('before_files_rewrite', {
source: key,
destination: `${value.page}${value.query ? '?' : ''}${qs.stringify(value.query)}`
}, opts.nextConfig.basePath, opts.nextConfig.experimental.caseSensitiveRoutes));
}
try {
// we serve a separate manifest with all pages for the client in
// dev mode so that we can match a page after a rewrite on the client
// before it has been built and is populated in the _buildManifest
const sortedRoutes = getSortedRoutes(routedPages);
opts.fsChecker.dynamicRoutes = sortedRoutes.map((page)=>{
const regex = getRouteRegex(page);
return {
regex: regex.re.toString(),
match: getRouteMatcher(regex),
page
};
});
const dataRoutes = [];
for (const page of sortedRoutes){
const route = buildDataRoute(page, 'development');
const routeRegex = getRouteRegex(route.page);
dataRoutes.push({
...route,
regex: routeRegex.re.toString(),
match: getRouteMatcher({
// TODO: fix this in the manifest itself, must also be fixed in
// upstream builder that relies on this
re: opts.nextConfig.i18n ? new RegExp(route.dataRouteRegex.replace(`/development/`, `/development/(?<nextLocale>[^/]+?)/`)) : new RegExp(route.dataRouteRegex),
groups: routeRegex.groups
})
});
}
opts.fsChecker.dynamicRoutes.unshift(...dataRoutes);
if (!(prevSortedRoutes == null ? void 0 : prevSortedRoutes.every((val, idx)=>val === sortedRoutes[idx]))) {
const addedRoutes = sortedRoutes.filter((route)=>!prevSortedRoutes.includes(route));
const removedRoutes = prevSortedRoutes.filter((route)=>!sortedRoutes.includes(route));
// emit the change so clients fetch the update
hotReloader.send({
action: HMR_ACTIONS_SENT_TO_BROWSER.DEV_PAGES_MANIFEST_UPDATE,
data: [
{
devPagesManifest: true
}
]
});
addedRoutes.forEach((route)=>{
hotReloader.send({
action: HMR_ACTIONS_SENT_TO_BROWSER.ADDED_PAGE,
data: [
route
]
});
});
removedRoutes.forEach((route)=>{
hotReloader.send({
action: HMR_ACTIONS_SENT_TO_BROWSER.REMOVED_PAGE,
data: [
route
]
});
});
}
prevSortedRoutes = sortedRoutes;
if (!resolved) {
resolve();
resolved = true;
}
} catch (e) {
if (!resolved) {
reject(e);
resolved = true;
} else {
Log.warn('Failed to reload dynamic routes:', e);
}
} finally{
// Reload the matchers. The filesystem would have been written to,
// and the matchers need to re-scan it to update the router.
await propagateServerField(opts, 'reloadMatchers', undefined);
}
});
wp.watch({
directories: [
dir
],
startTime: 0
});
});
const clientPagesManifestPath = `/_next/${CLIENT_STATIC_FILES_PATH}/development/${DEV_CLIENT_PAGES_MANIFEST}`;
opts.fsChecker.devVirtualFsItems.add(clientPagesManifestPath);
const devMiddlewareManifestPath = `/_next/${CLIENT_STATIC_FILES_PATH}/development/${DEV_CLIENT_MIDDLEWARE_MANIFEST}`;
opts.fsChecker.devVirtualFsItems.add(devMiddlewareManifestPath);
const devTurbopackMiddlewareManifestPath = `/_next/${CLIENT_STATIC_FILES_PATH}/development/${TURBOPACK_CLIENT_MIDDLEWARE_MANIFEST}`;
opts.fsChecker.devVirtualFsItems.add(devTurbopackMiddlewareManifestPath);
async function requestHandler(req, res) {
var _parsedUrl_pathname, _parsedUrl_pathname1, _parsedUrl_pathname2;
const parsedUrl = url.parse(req.url || '/');
if ((_parsedUrl_pathname = parsedUrl.pathname) == null ? void 0 : _parsedUrl_pathname.includes(clientPagesManifestPath)) {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json; charset=utf-8');
res.end(JSON.stringify({
pages: prevSortedRoutes.filter((route)=>!opts.fsChecker.appFiles.has(route))
}));
return {
finished: true
};
}
if (((_parsedUrl_pathname1 = parsedUrl.pathname) == null ? void 0 : _parsedUrl_pathname1.includes(devMiddlewareManifestPath)) || ((_parsedUrl_pathname2 = parsedUrl.pathname) == null ? void 0 : _parsedUrl_pathname2.includes(devTurbopackMiddlewareManifestPath))) {
var _serverFields_middleware;
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json; charset=utf-8');
res.end(JSON.stringify(((_serverFields_middleware = serverFields.middleware) == null ? void 0 : _serverFields_middleware.matchers) || []));
return {
finished: true
};
}
return {
finished: false
};
}
function logErrorWithOriginalStack(err, type) {
if (err instanceof ModuleBuildError) {
// Errors that may come from issues from the user's code
Log.error(err.message);
} else if (err instanceof TurbopackInternalError) {
// An internal Turbopack error that has been handled by next-swc, written
// to disk and a simplified message shown to user on the Rust side.
} else if (type === 'warning') {
Log.warn(err);
} else if (type === 'app-dir') {
Log.error(err);
} else if (type) {
Log.error(`${type}:`, err);
} else {
Log.error(err);
}
}
return {
serverFields,
hotReloader,
requestHandler,
logErrorWithOriginalStack,
async ensureMiddleware (requestUrl) {
if (!serverFields.actualMiddlewareFile) return;
return hotReloader.ensurePage({
page: serverFields.actualMiddlewareFile,
clientOnly: false,
definition: undefined,
url: requestUrl
});
}
};
}
export async function setupDevBundler(opts) {
const isSrcDir = path.relative(opts.dir, opts.pagesDir || opts.appDir || '').startsWith('src');
const result = await startWatcher(opts);
opts.telemetry.record(eventCliSession(path.join(opts.dir, opts.nextConfig.distDir), opts.nextConfig, {
webpackVersion: 5,
isSrcDir,
turboFlag: !!opts.turbo,
cliCommand: 'dev',
appDir: !!opts.appDir,
pagesDir: !!opts.pagesDir,
isCustomServer: !!opts.isCustomServer,
hasNowJson: !!await findUp('now.json', {
cwd: opts.dir
})
}));
return result;
}
// Returns a trace rewritten through Turbopack's sourcemaps
//# sourceMappingURL=setup-dev-bundler.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,3 @@
export { };
//# sourceMappingURL=types.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/server/lib/router-utils/types.ts"],"sourcesContent":["export type PropagateToWorkersField =\n | 'actualMiddlewareFile'\n | 'actualInstrumentationHookFile'\n | 'reloadMatchers'\n | 'loadEnvConfig'\n | 'appPathRoutes'\n | 'middleware'\n | 'renderOpts'\n"],"names":[],"mappings":"AAAA,WAOgB"}