Skip to content

Commit

Permalink
Do not create client reference manifest for metadata route handlers
Browse files Browse the repository at this point in the history
Follow-up fix from #70897
  • Loading branch information
unstubbable committed Oct 13, 2024
1 parent 189329f commit 652765d
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 17 deletions.
3 changes: 2 additions & 1 deletion packages/next/src/build/flying-shuttle/stitch-builds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
} from '../../shared/lib/constants'
import { normalizeAppPath } from '../../shared/lib/router/utils/app-paths'
import type { NextConfigComplete } from '../../server/config-shared'
import { isMetadataRoute } from '../../lib/metadata/is-metadata-route'

export async function stitchBuilds(
{
Expand Down Expand Up @@ -106,7 +107,7 @@ export async function stitchBuilds(
path.join(distDir, entryFile + '.nft.json')
)

if (type === 'app') {
if (type === 'app' && !isMetadataRoute(entry)) {
const clientRefManifestFile = path.join(
'server',
type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
} from '../utils'
import type { ChunkGroup } from 'webpack'
import { encodeURIPath } from '../../../shared/lib/encode-uri-path'
import { isMetadataRoute } from '../../../lib/metadata/is-metadata-route'

interface Options {
dev: boolean
Expand Down Expand Up @@ -519,9 +520,9 @@ export class ClientReferenceManifestPlugin {
manifestEntryFiles.push(entryName.replace(/\/page(\.[^/]+)?$/, '/page'))
}

// We also need to create manifests for route handler entrypoints to
// enable `'use cache'`.
if (/\/route$/.test(entryName)) {
// We also need to create manifests for route handler entrypoints
// (excluding metadata route handlers) to enable `'use cache'`.
if (/\/route$/.test(entryName) && !isMetadataRoute(entryName)) {
manifestEntryFiles.push(entryName)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { getModuleBuildInfo } from '../loaders/get-module-build-info'
import { getPageFilePath } from '../../entries'
import { resolveExternal } from '../../handle-externals'
import swcLoader from '../loaders/next-swc-loader'
import { isMetadataRoute } from '../../../lib/metadata/is-metadata-route'

const PLUGIN_NAME = 'TraceEntryPointsPlugin'
export const TRACE_IGNORES = [
Expand Down Expand Up @@ -277,15 +278,18 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance {
)

if (entrypoint.name.startsWith('app/')) {
// include the client reference manifest
const clientManifestsForEntrypoint = nodePath.join(
outputPath,
outputPrefix,
entrypoint.name.replace(/%5F/g, '_') +
'_' +
CLIENT_REFERENCE_MANIFEST +
'.js'
)
// Include the client reference manifest for pages and route handlers,
// excluding metadata route handlers.
const clientManifestsForEntrypoint = isMetadataRoute(entrypoint.name)
? null
: nodePath.join(
outputPath,
outputPrefix,
entrypoint.name.replace(/%5F/g, '_') +
'_' +
CLIENT_REFERENCE_MANIFEST +
'.js'
)

if (clientManifestsForEntrypoint !== null) {
entryFiles.add(clientManifestsForEntrypoint)
Expand Down
7 changes: 6 additions & 1 deletion packages/next/src/build/webpack/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
ModuleGraph,
} from 'webpack'
import type { ModuleGraphConnection } from 'webpack'
import { isMetadataRoute } from '../../lib/metadata/is-metadata-route'

export function traverseModules(
compilation: Compilation,
Expand Down Expand Up @@ -47,7 +48,11 @@ export function forEachEntryModule(
) {
for (const [name, entry] of compilation.entries.entries()) {
// Skip for entries under pages/
if (name.startsWith('pages/')) {
if (
name.startsWith('pages/') ||
// Skip for metadata route handlers
(name.startsWith('app/') && isMetadataRoute(name))
) {
continue
}

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 @@ -30,6 +30,7 @@ import { wait } from '../lib/wait'
import { setReferenceManifestsSingleton } from './app-render/encryption-utils'
import { createServerModuleMap } from './app-render/action-utils'
import type { DeepReadonly } from '../shared/lib/deep-readonly'
import { isMetadataRoute } from '../lib/metadata/is-metadata-route'

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

// Make sure to avoid loading the manifest for metadata route handlers.
const hasClientManifest = isAppPath && !isMetadataRoute(page)

// Load the manifest files first
const [
buildManifest,
Expand All @@ -150,7 +154,7 @@ async function loadComponentsImpl<N = any>({
loadManifestWithRetries<ReactLoadableManifest>(
join(distDir, REACT_LOADABLE_MANIFEST)
),
isAppPath
hasClientManifest
? loadClientReferenceManifest(
join(
distDir,
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/app-dir/hello-world/hello-world.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('hello-world', () => {
expect($('p').text()).toBe('hello world')
})

// Recommended for tests that need a full browser
// Recommended for tests that need a full browser.
it('should work using browser', async () => {
const browser = await next.browser('/')
expect(await browser.elementByCss('p').text()).toBe('hello world')
Expand All @@ -23,7 +23,7 @@ describe('hello-world', () => {
expect(html).toContain('hello world')
})

// In case you need to test the response object
// In case you need to test the response object.
it('should work with fetch', async () => {
const res = await next.fetch('/')
const html = await res.text()
Expand Down

0 comments on commit 652765d

Please sign in to comment.