Skip to content

Commit

Permalink
Reduce hello-world middleware bundle size from 128k to 88k (#35512)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cramforce committed Mar 22, 2022
1 parent 860c97c commit 6da7691
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/next/server/base-server.ts
Expand Up @@ -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'
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion packages/next/server/dev/next-dev-server.ts
Expand Up @@ -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'
Expand Down
17 changes: 17 additions & 0 deletions packages/next/server/router-utils.ts
@@ -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 + '/'))
)
}
2 changes: 1 addition & 1 deletion packages/next/server/web/next-url.ts
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/next/shared/lib/router/utils/parse-next-url.ts
Expand Up @@ -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 }
Expand Down

0 comments on commit 6da7691

Please sign in to comment.