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

perf: refactor path logic in router + add LRU cache #41365

Merged
merged 13 commits into from Oct 20, 2022
14 changes: 7 additions & 7 deletions packages/next/server/next-server.ts
Expand Up @@ -74,7 +74,7 @@ import BaseServer, {
NoFallbackError,
RequestContext,
} from './base-server'
import { getPagePath, requireFontManifest } from './require'
import { getMaybePagePath, getPagePath, requireFontManifest } from './require'
import { denormalizePagePath } from '../shared/lib/page-path/denormalize-page-path'
import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path'
import { loadComponents } from './load-components'
Expand Down Expand Up @@ -327,12 +327,12 @@ export default class NextNodeServer extends BaseServer {
}

protected async hasPage(pathname: string): Promise<boolean> {
let found = false
try {
found = !!this.getPagePath(pathname, this.nextConfig.i18n?.locales)
} catch (_) {}

return found
return !!getMaybePagePath(
pathname,
this.distDir,
this.nextConfig.i18n?.locales,
this.hasAppDir
)
}

protected getBuildId(): string {
Expand Down
46 changes: 43 additions & 3 deletions packages/next/server/require.ts
Expand Up @@ -11,13 +11,33 @@ import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path'
import { denormalizePagePath } from '../shared/lib/page-path/denormalize-page-path'
import type { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin'
import { PageNotFoundError, MissingStaticPage } from '../shared/lib/utils'
import LRUCache from 'next/dist/compiled/lru-cache'

export function getPagePath(
const pagePathCache =
process.env.NODE_ENV === 'development'
? {
get: (_key: string) => {
return null
},
set: () => {},
has: () => false,
}
: new LRUCache<string, string | null>({
max: 1000,
})

export function getMaybePagePath(
page: string,
distDir: string,
locales?: string[],
appDirEnabled?: boolean
): string {
): string | null {
const cacheKey = `${page}:${locales}`

if (pagePathCache.has(cacheKey)) {
return pagePathCache.get(cacheKey) as string | null
}

const serverBuildPath = join(distDir, SERVER_DIRECTORY)
let appPathsManifest: undefined | PagesManifest

Expand Down Expand Up @@ -60,10 +80,30 @@ export function getPagePath(
pagePath = checkManifest(pagesManifest)
}

if (!pagePath) {
pagePathCache.set(cacheKey, null)
return null
}

const path = join(serverBuildPath, pagePath)
pagePathCache.set(cacheKey, path)

return path
}

export function getPagePath(
page: string,
distDir: string,
locales?: string[],
appDirEnabled?: boolean
): string {
const pagePath = getMaybePagePath(page, distDir, locales, appDirEnabled)

if (!pagePath) {
throw new PageNotFoundError(page)
}
return join(serverBuildPath, pagePath)

return pagePath
}

export function requirePage(
Expand Down