Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce hello-world middleware bundle size from 128k to 88k #35512

Merged
merged 2 commits into from Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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