Reduce hello-world middleware bundle size from 128k to 88k (#35512)
Moves two utility functions from `server/router.ts` into their own file. This avoids the middleware pulling in the full Next.js router into its bundle. There are probably more opportunities like this, but this is a good start. Middleware should likely be bundled by a non-chunking optimizing compiler.
This commit is contained in:
parent
860c97ccf5
commit
6da769129e
5 changed files with 23 additions and 4 deletions
|
@ -42,7 +42,7 @@ import {
|
|||
import * as envConfig from '../shared/lib/runtime-config'
|
||||
import { DecodeError, normalizeRepeatedSlashes } from '../shared/lib/utils'
|
||||
import { isTargetLikeServerless } from './utils'
|
||||
import Router, { replaceBasePath, route } from './router'
|
||||
import Router, { route } from './router'
|
||||
import { setRevalidateHeaders } from './send-payload/revalidate-headers'
|
||||
import { IncrementalCache } from './incremental-cache'
|
||||
import { execOnce } from '../shared/lib/utils'
|
||||
|
@ -64,6 +64,7 @@ import { addRequestMeta, getRequestMeta } from './request-meta'
|
|||
import { createHeaderRoute, createRedirectRoute } from './server-route-utils'
|
||||
import { PrerenderManifest } from '../build'
|
||||
import { ImageConfigComplete } from '../shared/lib/image-config'
|
||||
import { replaceBasePath } from './router-utils'
|
||||
|
||||
export type FindComponentsResult = {
|
||||
components: LoadComponentsReturnType
|
||||
|
|
|
@ -41,7 +41,8 @@ import {
|
|||
} from '../../shared/lib/router/utils'
|
||||
import Server, { WrappedBuildError } from '../next-server'
|
||||
import { normalizePagePath } from '../normalize-page-path'
|
||||
import Router, { hasBasePath, replaceBasePath, route } from '../router'
|
||||
import Router, { route } from '../router'
|
||||
import { hasBasePath, replaceBasePath } from '../router-utils'
|
||||
import { eventCliSession } from '../../telemetry/events'
|
||||
import { Telemetry } from '../../telemetry/storage'
|
||||
import { setGlobal } from '../../trace'
|
||||
|
|
17
packages/next/server/router-utils.ts
Normal file
17
packages/next/server/router-utils.ts
Normal file
|
@ -0,0 +1,17 @@
|
|||
export function replaceBasePath(pathname: string, basePath: string): string {
|
||||
// ensure basePath is only stripped if it matches exactly
|
||||
// and doesn't contain extra chars e.g. basePath /docs
|
||||
// should replace for /docs, /docs/, /docs/a but not /docsss
|
||||
if (hasBasePath(pathname, basePath)) {
|
||||
pathname = pathname.substr(basePath.length)
|
||||
if (!pathname.startsWith('/')) pathname = `/${pathname}`
|
||||
}
|
||||
return pathname
|
||||
}
|
||||
|
||||
export function hasBasePath(pathname: string, basePath: string): boolean {
|
||||
return (
|
||||
typeof pathname === 'string' &&
|
||||
(pathname === basePath || pathname.startsWith(basePath + '/'))
|
||||
)
|
||||
}
|
|
@ -1,8 +1,8 @@
|
|||
import type { PathLocale } from '../../shared/lib/i18n/normalize-locale-path'
|
||||
import type { DomainLocale, I18NConfig } from '../config-shared'
|
||||
import { getLocaleMetadata } from '../../shared/lib/i18n/get-locale-metadata'
|
||||
import { replaceBasePath } from '../router'
|
||||
import cookie from 'next/dist/compiled/cookie'
|
||||
import { replaceBasePath } from '../router-utils'
|
||||
|
||||
interface Options {
|
||||
base?: string | URL
|
||||
|
|
|
@ -4,7 +4,7 @@ import { parseUrl } from './parse-url'
|
|||
import type { NextConfig, DomainLocale } from '../../../../server/config-shared'
|
||||
import type { ParsedUrl } from './parse-url'
|
||||
import type { PathLocale } from '../../i18n/normalize-locale-path'
|
||||
import { hasBasePath, replaceBasePath } from '../../../../server/router'
|
||||
import { hasBasePath, replaceBasePath } from '../../../../server/router-utils'
|
||||
|
||||
interface Params {
|
||||
headers?: { [key: string]: string | string[] | undefined }
|
||||
|
|
Loading…
Reference in a new issue