rsnext/packages/next/server/require.ts
Jimmy Lai 6d29713023
perf: refactor path logic in router + add LRU cache (#41365)
I'm investigating the runtime perf of the node server and this was one of the hot spot I stumbled upon.

This diff:
- refactors getPagePath to not throw when it doesn't find a path: this function is used to check if a path exists, we don't want that kind of overhead there
- adds a LRU cache to cache the result of the evaluation

Results:

before:
> 110k requests in 11.01s, 285 MB read

after
>135k requests in 11.01s, 348 MB read

on an autocannon run



## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
2022-10-20 15:42:50 +00:00

127 lines
3.2 KiB
TypeScript

import { promises } from 'fs'
import { join } from 'path'
import {
FONT_MANIFEST,
PAGES_MANIFEST,
SERVER_DIRECTORY,
APP_PATHS_MANIFEST,
} from '../shared/lib/constants'
import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path'
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'
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 | 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
if (appDirEnabled) {
appPathsManifest = require(join(serverBuildPath, APP_PATHS_MANIFEST))
}
const pagesManifest = require(join(
serverBuildPath,
PAGES_MANIFEST
)) as PagesManifest
try {
page = denormalizePagePath(normalizePagePath(page))
} catch (err) {
console.error(err)
throw new PageNotFoundError(page)
}
const checkManifest = (manifest: PagesManifest) => {
let curPath = manifest[page]
if (!manifest[curPath] && locales) {
const manifestNoLocales: typeof pagesManifest = {}
for (const key of Object.keys(manifest)) {
manifestNoLocales[normalizeLocalePath(key, locales).pathname] =
pagesManifest[key]
}
curPath = manifestNoLocales[page]
}
return curPath
}
let pagePath: string | undefined
if (appPathsManifest) {
pagePath = checkManifest(appPathsManifest)
}
if (!pagePath) {
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 pagePath
}
export function requirePage(
page: string,
distDir: string,
appDirEnabled?: boolean
): any {
const pagePath = getPagePath(page, distDir, undefined, appDirEnabled)
if (pagePath.endsWith('.html')) {
return promises.readFile(pagePath, 'utf8').catch((err) => {
throw new MissingStaticPage(page, err.message)
})
}
return require(pagePath)
}
export function requireFontManifest(distDir: string) {
const serverBuildPath = join(distDir, SERVER_DIRECTORY)
const fontManifest = require(join(serverBuildPath, FONT_MANIFEST))
return fontManifest
}