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] skip loading client manifest for static metadata routes #77260

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import picomatch from 'next/dist/compiled/picomatch'
import { getModuleBuildInfo } from '../loaders/get-module-build-info'
import { getPageFilePath } from '../../entries'
import { resolveExternal } from '../../handle-externals'
import { isStaticMetadataRoute } from '../../../lib/metadata/is-metadata-route'
import { isStaticMetadataRoutePage } from '../../../lib/metadata/is-metadata-route'
import { getCompilationSpan } from '../utils'

const PLUGIN_NAME = 'TraceEntryPointsPlugin'
Expand Down Expand Up @@ -243,7 +243,7 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance {

const entryIsStaticMetadataRoute =
appDirRelativeEntryPath &&
isStaticMetadataRoute(appDirRelativeEntryPath)
isStaticMetadataRoutePage(appDirRelativeEntryPath)

// Include the client reference manifest in the trace, but not for
// static metadata routes, for which we don't generate those.
Expand Down
20 changes: 15 additions & 5 deletions packages/next/src/lib/metadata/is-metadata-route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { PageExtensions } from '../../build/page-extensions-type'
import { normalizePathSep } from '../../shared/lib/page-path/normalize-path-sep'
import { isAppRouteRoute } from '../is-app-route-route'

export const STATIC_METADATA_IMAGES = {
icon: {
Expand Down Expand Up @@ -127,15 +128,24 @@ export function isMetadataRouteFile(
)
}

export function isStaticMetadataRouteFile(appDirRelativePath: string) {
export function isStaticMetadataRoutePage(appDirRelativePath: string) {
return isMetadataRouteFile(appDirRelativePath, [], true)
}

export function isStaticMetadataRoute(page: string) {
// Check if the route is a static metadata route, with /route suffix
// e.g. /favicon.ico/route, /icon.png/route, etc.
// But skip the text routes like robots.txt since they might also be dynamic.
// Checking route path is not enough to determine if text routes is dynamic.
export function isStaticMetadataRoute(route: string) {
const pathname = route.slice(0, -'/route'.length)
return (
page === '/robots' ||
page === '/manifest' ||
isStaticMetadataRouteFile(page)
isAppRouteRoute(route) &&
isStaticMetadataRoutePage(pathname) &&
// These routes can either be built by static or dynamic entrypoints,
// so we assume they're dynamic
pathname !== '/robots.txt' &&
pathname !== '/manifest.webmanifest' &&
!pathname.endsWith('/sitemap.xml')
)
}

Expand Down
6 changes: 5 additions & 1 deletion packages/next/src/server/load-components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { setReferenceManifestsSingleton } from './app-render/encryption-utils'
import { createServerModuleMap } from './app-render/action-utils'
import type { DeepReadonly } from '../shared/lib/deep-readonly'
import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path'
import { isStaticMetadataRoute } from '../lib/metadata/is-metadata-route'

export type ManifestItem = {
id: number | string
Expand Down Expand Up @@ -195,6 +196,9 @@ async function loadComponentsImpl<N = any>({
)
}

// Make sure to avoid loading the manifest for static metadata routes for better performance.
const hasClientManifest = !isStaticMetadataRoute(page)

// Load the manifest files first
//
// Loading page-specific manifests shouldn't throw an error if the manifest couldn't be found, so
Expand Down Expand Up @@ -223,7 +227,7 @@ async function loadComponentsImpl<N = any>({
join(distDir, `${DYNAMIC_CSS_MANIFEST}.json`),
manifestLoadAttempts
).catch(() => undefined),
isAppPath
isAppPath && hasClientManifest
? tryLoadClientReferenceManifest(
join(
distDir,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class DevAppRouteRouteMatcherProvider extends FileCacheRouteMatcherProvid
true
)

if (!isStaticMetadataRoute(page) && isEntryMetadataRouteFile) {
if (isEntryMetadataRouteFile && !isStaticMetadataRoute(page)) {
// Matching dynamic metadata routes.
// Add 2 possibilities for both single and multiple routes:
{
Expand Down
Loading