Skip to content

Commit b9d8b13

Browse files
committed
[perf] skip loading client manifest for static metadata routes
1 parent 3ab033d commit b9d8b13

File tree

4 files changed

+23
-8
lines changed

4 files changed

+23
-8
lines changed

packages/next/src/build/webpack/plugins/next-trace-entrypoints-plugin.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import picomatch from 'next/dist/compiled/picomatch'
1818
import { getModuleBuildInfo } from '../loaders/get-module-build-info'
1919
import { getPageFilePath } from '../../entries'
2020
import { resolveExternal } from '../../handle-externals'
21-
import { isStaticMetadataRoute } from '../../../lib/metadata/is-metadata-route'
21+
import { isStaticMetadataRoutePathname } from '../../../lib/metadata/is-metadata-route'
2222
import { getCompilationSpan } from '../utils'
2323

2424
const PLUGIN_NAME = 'TraceEntryPointsPlugin'
@@ -243,7 +243,7 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance {
243243

244244
const entryIsStaticMetadataRoute =
245245
appDirRelativeEntryPath &&
246-
isStaticMetadataRoute(appDirRelativeEntryPath)
246+
isStaticMetadataRoutePathname(appDirRelativeEntryPath)
247247

248248
// Include the client reference manifest in the trace, but not for
249249
// static metadata routes, for which we don't generate those.

packages/next/src/lib/metadata/is-metadata-route.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { PageExtensions } from '../../build/page-extensions-type'
22
import { normalizePathSep } from '../../shared/lib/page-path/normalize-path-sep'
3+
import { isAppRouteRoute } from '../is-app-route-route'
34

45
export const STATIC_METADATA_IMAGES = {
56
icon: {
@@ -127,15 +128,25 @@ export function isMetadataRouteFile(
127128
)
128129
}
129130

130-
export function isStaticMetadataRouteFile(appDirRelativePath: string) {
131+
function isStaticMetadataRoutePage(appDirRelativePath: string) {
131132
return isMetadataRouteFile(appDirRelativePath, [], true)
132133
}
133134

134-
export function isStaticMetadataRoute(page: string) {
135+
// Check if the route is a static metadata route, with /route suffix
136+
// e.g. /robots.txt/route, /sitemap.xml/route, /favicon.ico/route, /manifest/route, /icon/route, etc.
137+
export function isStaticMetadataRoute(route: string) {
138+
const pathname = route.slice(0, -'/route'.length)
139+
return isAppRouteRoute(route) && isStaticMetadataRoutePage(pathname)
140+
}
141+
142+
// Check if the page is a static metadata route file
143+
// e.g. /robots, /manifest, /favicon.ico, /sitemap.xml, /icon.png, etc.
144+
export function isStaticMetadataRoutePathname(page: string) {
135145
return (
146+
// TODO: avoid directly checking pathnames as they can also be page routes
136147
page === '/robots' ||
137148
page === '/manifest' ||
138-
isStaticMetadataRouteFile(page)
149+
isStaticMetadataRoutePage(page)
139150
)
140151
}
141152

packages/next/src/server/load-components.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { setReferenceManifestsSingleton } from './app-render/encryption-utils'
3333
import { createServerModuleMap } from './app-render/action-utils'
3434
import type { DeepReadonly } from '../shared/lib/deep-readonly'
3535
import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path'
36+
import { isStaticMetadataRoute } from '../lib/metadata/is-metadata-route'
3637

3738
export type ManifestItem = {
3839
id: number | string
@@ -195,6 +196,9 @@ async function loadComponentsImpl<N = any>({
195196
)
196197
}
197198

199+
// Make sure to avoid loading the manifest for static metadata routes for better performance.
200+
const hasClientManifest = !isStaticMetadataRoute(page)
201+
198202
// Load the manifest files first
199203
//
200204
// Loading page-specific manifests shouldn't throw an error if the manifest couldn't be found, so
@@ -223,7 +227,7 @@ async function loadComponentsImpl<N = any>({
223227
join(distDir, `${DYNAMIC_CSS_MANIFEST}.json`),
224228
manifestLoadAttempts
225229
).catch(() => undefined),
226-
isAppPath
230+
isAppPath && hasClientManifest
227231
? tryLoadClientReferenceManifest(
228232
join(
229233
distDir,

packages/next/src/server/route-matcher-providers/dev/dev-app-route-route-matcher-provider.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { isAppRouteRoute } from '../../../lib/is-app-route-route'
77
import { DevAppNormalizers } from '../../normalizers/built/app'
88
import {
99
isMetadataRouteFile,
10-
isStaticMetadataRoute,
10+
isStaticMetadataRoutePathname,
1111
} from '../../../lib/metadata/is-metadata-route'
1212
import { normalizeMetadataPageToRoute } from '../../../lib/metadata/get-metadata-route'
1313
import path from '../../../shared/lib/isomorphic/path'
@@ -53,7 +53,7 @@ export class DevAppRouteRouteMatcherProvider extends FileCacheRouteMatcherProvid
5353
true
5454
)
5555

56-
if (!isStaticMetadataRoute(page) && isEntryMetadataRouteFile) {
56+
if (!isStaticMetadataRoutePathname(page) && isEntryMetadataRouteFile) {
5757
// Matching dynamic metadata routes.
5858
// Add 2 possibilities for both single and multiple routes:
5959
{

0 commit comments

Comments
 (0)