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

Optimize next-app-loader resolving speed #50745

Merged
merged 6 commits into from
Jun 5, 2023
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
4 changes: 2 additions & 2 deletions packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
PAGES_DIR_ALIAS,
INSTRUMENTATION_HOOK_FILENAME,
} from '../lib/constants'
import { fileExists } from '../lib/file-exists'
import { FileType, fileExists } from '../lib/file-exists'
import { findPagesDir } from '../lib/find-pages-dir'
import loadCustomRoutes, {
CustomRoutes,
Expand Down Expand Up @@ -588,7 +588,7 @@ export default async function build(
for (const page in mappedPages) {
const hasPublicPageFile = await fileExists(
path.join(publicDir, page === '/' ? '/index' : page),
'file'
FileType.File
)
if (hasPublicPageFile) {
conflictingPublicFiles.push(page)
Expand Down
41 changes: 11 additions & 30 deletions packages/next/src/build/webpack/loaders/metadata/discover.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type webpack from 'webpack'
import type {
CollectingMetadata,
PossibleStaticMetadataFileNameConvention,
Expand All @@ -7,6 +6,7 @@ import path from 'path'
import { stringify } from 'querystring'
import { STATIC_METADATA_IMAGES } from '../../../../lib/metadata/is-metadata-route'
import { WEBPACK_RESOURCE_QUERIES } from '../../../../lib/constants'
import { MetadataResolver } from '../next-app-loader'

const METADATA_TYPE = 'metadata'

Expand All @@ -16,16 +16,14 @@ async function enumMetadataFiles(
filename: string,
extensions: readonly string[],
{
resolvePath,
loaderContext,
metadataResolver,
// When set to true, possible filename without extension could: icon, icon0, ..., icon9
numericSuffix,
}: {
resolvePath: (pathname: string) => Promise<string>
loaderContext: webpack.LoaderContext<any>
metadataResolver: MetadataResolver
numericSuffix: boolean
}
) {
): Promise<string[]> {
const collectedFiles: string[] = []

const possibleFileNames = [filename].concat(
Expand All @@ -36,19 +34,9 @@ async function enumMetadataFiles(
: []
)
for (const name of possibleFileNames) {
for (const ext of extensions) {
const pathname = path.join(dir, `${name}.${ext}`)
try {
const resolved = await resolvePath(pathname)
loaderContext.addDependency(resolved)

collectedFiles.push(resolved)
} catch (err: any) {
if (!err.message.includes("Can't resolve")) {
throw err
}
loaderContext.addMissingDependency(pathname)
}
const resolved = await metadataResolver(path.join(dir, name), extensions)
if (resolved) {
collectedFiles.push(resolved)
}
}

Expand All @@ -59,16 +47,14 @@ export async function createStaticMetadataFromRoute(
resolvedDir: string,
{
segment,
resolvePath,
metadataResolver,
isRootLayoutOrRootPage,
loaderContext,
pageExtensions,
basePath,
}: {
segment: string
resolvePath: (pathname: string) => Promise<string>
metadataResolver: MetadataResolver
isRootLayoutOrRootPage: boolean
loaderContext: webpack.LoaderContext<any>
pageExtensions: string[]
basePath: string
}
Expand All @@ -82,11 +68,6 @@ export async function createStaticMetadataFromRoute(
manifest: undefined,
}

const opts = {
resolvePath,
loaderContext,
}

async function collectIconModuleIfExists(
type: PossibleStaticMetadataFileNameConvention
) {
Expand All @@ -96,7 +77,7 @@ export async function createStaticMetadataFromRoute(
resolvedDir,
'manifest',
staticManifestExtension.concat(pageExtensions),
{ ...opts, numericSuffix: false }
{ metadataResolver, numericSuffix: false }
)
if (manifestFile.length > 0) {
hasStaticMetadataFiles = true
Expand All @@ -116,7 +97,7 @@ export async function createStaticMetadataFromRoute(
...STATIC_METADATA_IMAGES[type].extensions,
...(type === 'favicon' ? [] : pageExtensions),
],
{ ...opts, numericSuffix: true }
{ metadataResolver, numericSuffix: true }
)
resolvedMetadataFiles
.sort((a, b) => a.localeCompare(b))
Expand Down