diff --git a/packages/next/build/index.ts b/packages/next/build/index.ts index 0db6f54f88d3..c4a0ced22e70 100644 --- a/packages/next/build/index.ts +++ b/packages/next/build/index.ts @@ -24,13 +24,12 @@ import { fileExists } from '../lib/file-exists' import { findPagesDir } from '../lib/find-pages-dir' import loadCustomRoutes, { CustomRoutes, - getRedirectStatus, - modifyRouteRegex, normalizeRouteRegex, Redirect, Rewrite, RouteType, } from '../lib/load-custom-routes' +import { getRedirectStatus, modifyRouteRegex } from '../lib/redirect-status' import { nonNullable } from '../lib/non-nullable' import { recursiveDelete } from '../lib/recursive-delete' import { verifyAndLint } from '../lib/verifyAndLint' diff --git a/packages/next/build/webpack/loaders/next-serverless-loader/page-handler.ts b/packages/next/build/webpack/loaders/next-serverless-loader/page-handler.ts index 0dd70dbc25c4..90eaa23c39b0 100644 --- a/packages/next/build/webpack/loaders/next-serverless-loader/page-handler.ts +++ b/packages/next/build/webpack/loaders/next-serverless-loader/page-handler.ts @@ -8,7 +8,7 @@ import { renderToHTML } from '../../../../server/render' import { tryGetPreviewData } from '../../../../server/api-utils/node' import { denormalizePagePath } from '../../../../shared/lib/page-path/denormalize-page-path' import { setLazyProp, getCookieParser } from '../../../../server/api-utils' -import { getRedirectStatus } from '../../../../lib/load-custom-routes' +import { getRedirectStatus } from '../../../../lib/redirect-status' import getRouteNoAssetPath from '../../../../shared/lib/router/utils/get-route-from-asset-path' import { PERMANENT_REDIRECT_STATUS } from '../../../../shared/lib/constants' import RenderResult from '../../../../server/render-result' diff --git a/packages/next/build/webpack/plugins/build-manifest-plugin.ts b/packages/next/build/webpack/plugins/build-manifest-plugin.ts index 55cae92b3917..dac8c7cc6bc6 100644 --- a/packages/next/build/webpack/plugins/build-manifest-plugin.ts +++ b/packages/next/build/webpack/plugins/build-manifest-plugin.ts @@ -1,3 +1,4 @@ +import type { Rewrite, CustomRoutes } from '../../../lib/load-custom-routes' import devalue from 'next/dist/compiled/devalue' import { webpack, sources } from 'next/dist/compiled/webpack/webpack' import { @@ -13,10 +14,8 @@ import { import { BuildManifest } from '../../../server/get-page-files' import getRouteFromEntrypoint from '../../../server/get-route-from-entrypoint' import { ampFirstEntryNamesMap } from './next-drop-client-page-plugin' -import { Rewrite } from '../../../lib/load-custom-routes' import { getSortedRoutes } from '../../../shared/lib/router/utils' import { spans } from './profiling-plugin' -import { CustomRoutes } from '../../../lib/load-custom-routes' type DeepMutable = { -readonly [P in keyof T]: DeepMutable } diff --git a/packages/next/lib/load-custom-routes.ts b/packages/next/lib/load-custom-routes.ts index caa0e2f8307c..f9b6ee393c55 100644 --- a/packages/next/lib/load-custom-routes.ts +++ b/packages/next/lib/load-custom-routes.ts @@ -3,9 +3,8 @@ import type { Token } from 'next/dist/compiled/path-to-regexp' import chalk from './chalk' import { escapeStringRegexp } from '../shared/lib/escape-regexp' -import { PERMANENT_REDIRECT_STATUS } from '../shared/lib/constants' -import { TEMPORARY_REDIRECT_STATUS } from '../shared/lib/constants' import { tryToParsePath } from './try-to-parse-path' +import { allowedStatusCodes } from './redirect-status' export type RouteHas = | { @@ -53,41 +52,14 @@ export type Redirect = { } ) -export const allowedStatusCodes = new Set([301, 302, 303, 307, 308]) const allowedHasTypes = new Set(['header', 'cookie', 'query', 'host']) const namedGroupsRegex = /\(\?<([a-zA-Z][a-zA-Z0-9]*)>/g -export function getRedirectStatus(route: { - statusCode?: number - permanent?: boolean -}): number { - return ( - route.statusCode || - (route.permanent ? PERMANENT_REDIRECT_STATUS : TEMPORARY_REDIRECT_STATUS) - ) -} - export function normalizeRouteRegex(regex: string) { // clean up un-necessary escaping from regex.source which turns / into \\/ return regex.replace(/\\\//g, '/') } -// for redirects we restrict matching /_next and for all routes -// we add an optional trailing slash at the end for easier -// configuring between trailingSlash: true/false -export function modifyRouteRegex(regex: string, restrictedPaths?: string[]) { - if (restrictedPaths) { - regex = regex.replace( - /\^/, - `^(?!${restrictedPaths - .map((path) => path.replace(/\//g, '\\/')) - .join('|')})` - ) - } - regex = regex.replace(/\$$/, '(?:\\/)?$') - return regex -} - function checkRedirect(route: Redirect): { invalidParts: string[] hadInvalidStatus: boolean diff --git a/packages/next/lib/redirect-status.ts b/packages/next/lib/redirect-status.ts new file mode 100644 index 000000000000..54ecf4816491 --- /dev/null +++ b/packages/next/lib/redirect-status.ts @@ -0,0 +1,30 @@ +import { PERMANENT_REDIRECT_STATUS } from '../shared/lib/constants' +import { TEMPORARY_REDIRECT_STATUS } from '../shared/lib/constants' + +export const allowedStatusCodes = new Set([301, 302, 303, 307, 308]) + +export function getRedirectStatus(route: { + statusCode?: number + permanent?: boolean +}): number { + return ( + route.statusCode || + (route.permanent ? PERMANENT_REDIRECT_STATUS : TEMPORARY_REDIRECT_STATUS) + ) +} + +// for redirects we restrict matching /_next and for all routes +// we add an optional trailing slash at the end for easier +// configuring between trailingSlash: true/false +export function modifyRouteRegex(regex: string, restrictedPaths?: string[]) { + if (restrictedPaths) { + regex = regex.replace( + /\^/, + `^(?!${restrictedPaths + .map((path) => path.replace(/\//g, '\\/')) + .join('|')})` + ) + } + regex = regex.replace(/\$$/, '(?:\\/)?$') + return regex +} diff --git a/packages/next/server/base-server.ts b/packages/next/server/base-server.ts index 072d714c3161..f1984f820aea 100644 --- a/packages/next/server/base-server.ts +++ b/packages/next/server/base-server.ts @@ -1,4 +1,4 @@ -import { __ApiPreviewProps } from './api-utils' +import type { __ApiPreviewProps } from './api-utils' import type { CustomRoutes } from '../lib/load-custom-routes' import type { DomainLocale } from './config' import type { DynamicRoutes, PageChecker, Route } from './router' @@ -27,7 +27,7 @@ import type { PayloadOptions } from './send-payload' import { join, resolve } from '../shared/lib/isomorphic/path' import { parse as parseQs } from 'querystring' import { format as formatUrl, parse as parseUrl } from 'url' -import { getRedirectStatus } from '../lib/load-custom-routes' +import { getRedirectStatus } from '../lib/redirect-status' import { NEXT_BUILTIN_DOCUMENT, NEXT_CLIENT_SSR_ENTRY_SUFFIX, diff --git a/packages/next/server/config-shared.ts b/packages/next/server/config-shared.ts index 416fc7cb01da..a41f84c65a40 100644 --- a/packages/next/server/config-shared.ts +++ b/packages/next/server/config-shared.ts @@ -1,6 +1,6 @@ import os from 'os' import type { webpack5 } from 'next/dist/compiled/webpack/webpack' -import { Header, Redirect, Rewrite } from '../lib/load-custom-routes' +import type { Header, Redirect, Rewrite } from '../lib/load-custom-routes' import { ImageConfig, ImageConfigComplete, diff --git a/packages/next/server/dev/hot-reloader.ts b/packages/next/server/dev/hot-reloader.ts index 996eca68a0e3..ce3c86860b25 100644 --- a/packages/next/server/dev/hot-reloader.ts +++ b/packages/next/server/dev/hot-reloader.ts @@ -1,10 +1,12 @@ +import type { webpack5 } from 'next/dist/compiled/webpack/webpack' +import type { NextConfigComplete } from '../config-shared' +import type { CustomRoutes } from '../../lib/load-custom-routes' import { getOverlayMiddleware } from 'next/dist/compiled/@next/react-dev-overlay/dist/middleware' import { IncomingMessage, ServerResponse } from 'http' import { WebpackHotMiddleware } from './hot-middleware' import { join, relative, isAbsolute } from 'path' import { UrlObject } from 'url' import { webpack, StringXor } from 'next/dist/compiled/webpack/webpack' -import type { webpack5 } from 'next/dist/compiled/webpack/webpack' import { createEntrypoints, createPagesMapping, @@ -40,8 +42,6 @@ import { withoutRSCExtensions, isMiddlewareFilename, } from '../../build/utils' -import { NextConfigComplete } from '../config-shared' -import { CustomRoutes } from '../../lib/load-custom-routes' import { DecodeError } from '../../shared/lib/utils' import { Span, trace } from '../../trace' import { getProperError } from '../../lib/is-error' diff --git a/packages/next/server/render.tsx b/packages/next/server/render.tsx index 42bf8bf58490..1bc29fbc3361 100644 --- a/packages/next/server/render.tsx +++ b/packages/next/server/render.tsx @@ -64,10 +64,7 @@ import { HtmlContext } from '../shared/lib/html-context' import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path' import { denormalizePagePath } from '../shared/lib/page-path/denormalize-page-path' import { getRequestMeta, NextParsedUrlQuery } from './request-meta' -import { - allowedStatusCodes, - getRedirectStatus, -} from '../lib/load-custom-routes' +import { allowedStatusCodes, getRedirectStatus } from '../lib/redirect-status' import RenderResult from './render-result' import isError from '../lib/is-error' import { diff --git a/packages/next/server/server-route-utils.ts b/packages/next/server/server-route-utils.ts index eecfcb14fff7..c21f07a98bcf 100644 --- a/packages/next/server/server-route-utils.ts +++ b/packages/next/server/server-route-utils.ts @@ -9,7 +9,7 @@ import type { Route } from './router' import type { BaseNextRequest } from './base-http' import type { ParsedUrlQuery } from 'querystring' -import { getRedirectStatus, modifyRouteRegex } from '../lib/load-custom-routes' +import { getRedirectStatus, modifyRouteRegex } from '../lib/redirect-status' import { getPathMatch } from '../shared/lib/router/utils/path-match' import { compileNonPath, diff --git a/packages/next/shared/lib/router/utils/resolve-rewrites.ts b/packages/next/shared/lib/router/utils/resolve-rewrites.ts index 6d612aaaa30a..66bd11d09e43 100644 --- a/packages/next/shared/lib/router/utils/resolve-rewrites.ts +++ b/packages/next/shared/lib/router/utils/resolve-rewrites.ts @@ -1,7 +1,7 @@ import type { ParsedUrlQuery } from 'querystring' +import type { Rewrite } from '../../../../lib/load-custom-routes' import { getPathMatch } from './path-match' import { matchHas, prepareDestination } from './prepare-destination' -import { Rewrite } from '../../../../lib/load-custom-routes' import { removeTrailingSlash } from './remove-trailing-slash' import { normalizeLocalePath } from '../../i18n/normalize-locale-path' import { removeBasePath } from '../../../../client/remove-base-path'