From 6c7771e4447d6b18e48c97998f5215af1b338f45 Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Mon, 21 Mar 2022 18:35:18 -0700 Subject: [PATCH] Reduce hello-world middleware bundle size from 128k to 88k Moves two utiloty functions from `server/router.ts` into their own file. This avoids the middleware pulling in the full Next.js router into its bundle. --- packages/next/server/base-server.ts | 3 ++- packages/next/server/dev/next-dev-server.ts | 3 ++- packages/next/server/router-utils.ts | 17 +++++++++++++++++ packages/next/server/web/next-url.ts | 2 +- .../shared/lib/router/utils/parse-next-url.ts | 2 +- 5 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 packages/next/server/router-utils.ts diff --git a/packages/next/server/base-server.ts b/packages/next/server/base-server.ts index f02f7db199d..a575e1ad596 100644 --- a/packages/next/server/base-server.ts +++ b/packages/next/server/base-server.ts @@ -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 diff --git a/packages/next/server/dev/next-dev-server.ts b/packages/next/server/dev/next-dev-server.ts index 2a18039e648..b30545ae5bd 100644 --- a/packages/next/server/dev/next-dev-server.ts +++ b/packages/next/server/dev/next-dev-server.ts @@ -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' diff --git a/packages/next/server/router-utils.ts b/packages/next/server/router-utils.ts new file mode 100644 index 00000000000..630521434fd --- /dev/null +++ b/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 + '/')) + ) +} diff --git a/packages/next/server/web/next-url.ts b/packages/next/server/web/next-url.ts index 532b93e64ab..8ceaea68323 100644 --- a/packages/next/server/web/next-url.ts +++ b/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 diff --git a/packages/next/shared/lib/router/utils/parse-next-url.ts b/packages/next/shared/lib/router/utils/parse-next-url.ts index 57985ecfaf0..0e36e4b6191 100644 --- a/packages/next/shared/lib/router/utils/parse-next-url.ts +++ b/packages/next/shared/lib/router/utils/parse-next-url.ts @@ -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 }