未验证 提交 ab701071 编写于 作者: J Jan Potoms 提交者: GitHub

Avoid pulling extra code in the bundles for trailingSlash logic (#14696)

* avoid pulling code in the bundle for `trailingSlash` logic when it's not enabled
* avoid cloning the url an extra time if normalizing the path doesn't change it
上级 6ff3a63a
......@@ -24,7 +24,7 @@ import { findPageFile } from '../server/lib/find-page-file'
import { GetStaticPaths } from 'next/types'
import { denormalizePagePath } from '../next-server/server/normalize-page-path'
import { BuildManifest } from '../next-server/server/get-page-files'
import { normalizeTrailingSlash } from '../next-server/lib/router/normalize-trailing-slash'
import { removePathTrailingSlash } from '../client/normalize-trailing-slash'
const fileGzipStats: { [k: string]: Promise<number> } = {}
const fsStatGzip = (file: string) => {
......@@ -574,7 +574,7 @@ export async function buildStaticPaths(
// For a string-provided path, we must make sure it matches the dynamic
// route.
if (typeof entry === 'string') {
entry = normalizeTrailingSlash(entry)
entry = removePathTrailingSlash(entry)
const result = _routeMatcher(entry)
if (!result) {
throw new Error(
......
......@@ -10,7 +10,7 @@ import {
} from '../next-server/lib/utils'
import Router from './router'
import { addBasePath } from '../next-server/lib/router/router'
import { normalizeTrailingSlash } from '../next-server/lib/router/normalize-trailing-slash'
import { normalizeTrailingSlash } from './normalize-trailing-slash'
function isLocal(href: string): boolean {
const url = parse(href, false, true)
......@@ -41,19 +41,11 @@ function memoizedFormatUrl(formatFunc: (href: Url, as?: Url) => FormatResult) {
}
}
function formatTrailingSlash(url: UrlObject): UrlObject {
return Object.assign({}, url, {
pathname:
url.pathname &&
normalizeTrailingSlash(url.pathname, !!process.env.__NEXT_TRAILING_SLASH),
})
}
function formatUrl(url: Url): string {
return (
url &&
formatWithValidation(
formatTrailingSlash(typeof url === 'object' ? url : parse(url))
normalizeTrailingSlash(typeof url === 'object' ? url : parse(url))
)
)
}
......
import { UrlObject } from 'url'
export function removePathTrailingSlash(path: string): string {
return path.endsWith('/') && path !== '/' ? path.slice(0, -1) : path
}
const normalizePathTrailingSlash = process.env.__NEXT_TRAILING_SLASH
? (path: string): string => {
if (/\.[^/]+\/?$/.test(path)) {
return removePathTrailingSlash(path)
} else if (path.endsWith('/')) {
return path
} else {
return path + '/'
}
}
: removePathTrailingSlash
export function normalizeTrailingSlash(url: UrlObject): UrlObject {
const normalizedPath =
url.pathname && normalizePathTrailingSlash(url.pathname)
return url.pathname === normalizedPath
? url
: Object.assign({}, url, { pathname: normalizedPath })
}
export function normalizeTrailingSlash(
path: string,
requireSlash?: boolean
): string {
if (requireSlash) {
if (!path.endsWith('/') && !/\.[^/]+$/.test(path)) {
return path + '/'
} else if (/\.[^/]+\/$/.test(path)) {
return path.slice(0, -1)
} else {
return path
}
} else {
if (path.endsWith('/') && path !== '/') {
return path.slice(0, -1)
} else {
return path
}
}
}
......@@ -15,7 +15,10 @@ import {
import { isDynamicRoute } from './utils/is-dynamic'
import { getRouteMatcher } from './utils/route-matcher'
import { getRouteRegex } from './utils/route-regex'
import { normalizeTrailingSlash } from './normalize-trailing-slash'
import {
normalizeTrailingSlash,
removePathTrailingSlash,
} from '../../../client/normalize-trailing-slash'
const basePath = (process.env.__NEXT_ROUTER_BASEPATH as string) || ''
......@@ -37,18 +40,10 @@ function prepareRoute(path: string) {
type Url = UrlObject | string
function formatTrailingSlash(url: UrlObject): UrlObject {
return Object.assign({}, url, {
pathname:
url.pathname &&
normalizeTrailingSlash(url.pathname, !!process.env.__NEXT_TRAILING_SLASH),
})
}
function formatUrl(url: Url): string {
return url
? formatWithValidation(
formatTrailingSlash(typeof url === 'object' ? url : parse(url))
normalizeTrailingSlash(typeof url === 'object' ? url : parse(url))
)
: url
}
......@@ -427,7 +422,7 @@ export default class Router implements BaseRouter {
// point by either next/link or router.push/replace so strip the
// basePath from the pathname to match the pages dir 1-to-1
pathname = pathname
? normalizeTrailingSlash(delBasePath(pathname), false)
? removePathTrailingSlash(delBasePath(pathname))
: pathname
if (!pathname || protocol) {
......
......@@ -62,7 +62,7 @@ import { compile as compilePathToRegex } from 'next/dist/compiled/path-to-regexp
import { loadEnvConfig } from '../../lib/load-env-config'
import './node-polyfill-fetch'
import { PagesManifest } from '../../build/webpack/plugins/pages-manifest-plugin'
import { normalizeTrailingSlash } from '../lib/router/normalize-trailing-slash'
import { removePathTrailingSlash } from '../../client/normalize-trailing-slash'
import getRouteFromAssetPath from '../lib/router/utils/get-route-from-asset-path'
const getCustomRouteMatcher = pathMatch(true)
......@@ -586,7 +586,7 @@ export default class Server {
}
// next.js core assumes page path without trailing slash
pathname = normalizeTrailingSlash(pathname, false)
pathname = removePathTrailingSlash(pathname)
if (params?.path?.[0] === 'api') {
const handled = await this.handleApiRequest(
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册