Skip to content

Commit

Permalink
Skip client reference manifests for static metadata route handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
unstubbable committed Jan 14, 2025
1 parent 019afc3 commit 55cf714
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const cacheHeader = {
revalidate: 'public, max-age=0, must-revalidate',
}

type MetadataRouteLoaderOptions = {
export type MetadataRouteLoaderOptions = {
// Using separate argument to avoid json being parsed and hit error
// x-ref: https://github.com/vercel/next.js/pull/62615
filePath: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import { getModuleBuildInfo } from '../loaders/get-module-build-info'
import { getAssumedSourceType } from '../loaders/next-flight-loader'
import { isAppRouteRoute } from '../../../lib/is-app-route-route'
import { isMetadataRoute } from '../../../lib/metadata/is-metadata-route'
import type { MetadataRouteLoaderOptions } from '../loaders/next-metadata-route-loader'

interface Options {
dev: boolean
Expand Down Expand Up @@ -305,7 +306,12 @@ export class FlightClientEntryPlugin {
).request

if (entryRequest.endsWith(WEBPACK_RESOURCE_QUERIES.metadataRoute)) {
entryRequest = getMetadataRouteResource(entryRequest)
const { filePath, isDynamicRouteExtension } =
getMetadataRouteResource(entryRequest)

if (isDynamicRouteExtension === '1') {
entryRequest = filePath
}
}

const { clientComponentImports, actionImports, cssImports } =
Expand Down Expand Up @@ -1110,14 +1116,15 @@ function getModuleResource(mod: webpack.NormalModule): string {
}

if (mod.resource === `?${WEBPACK_RESOURCE_QUERIES.metadataRoute}`) {
return getMetadataRouteResource(mod.rawRequest)
return getMetadataRouteResource(mod.rawRequest).filePath
}

return modResource
}

function getMetadataRouteResource(request: string): string {
const query = request.split('next-metadata-route-loader?')[1]
function getMetadataRouteResource(request: string): MetadataRouteLoaderOptions {
// e.g. next-metadata-route-loader?filePath=<some-url-encoded-path>&isDynamicRouteExtension=1!?__next_metadata_route__
const query = request.split('!')[0].split('next-metadata-route-loader?')[1]

return parse(query).filePath as string
return parse(query) as MetadataRouteLoaderOptions
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,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'

const PLUGIN_NAME = 'TraceEntryPointsPlugin'
export const TRACE_IGNORES = [
Expand Down Expand Up @@ -115,6 +116,7 @@ export interface BuildTraceContext {
outputPath: string
depModArray: string[]
entryNameMap: Record<string, string>
absolutePathByEntryName: Record<string, string>
}
chunksTrace?: {
action: TurbotraceAction
Expand Down Expand Up @@ -241,19 +243,28 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance {
nodePath.join(outputPath, `${outputPrefix}${entrypoint.name}.js`)
)

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'
)
if (entrypoint.name.startsWith('app/') && this.appDir) {
const appDirRelativeEntryPath =
this.buildTraceContext.entriesTrace?.absolutePathByEntryName[
entrypoint.name
]?.replace(this.appDir, '')

if (clientManifestsForEntrypoint !== null) {
entryFiles.add(clientManifestsForEntrypoint)
// Include the client reference manifest in the trace, but not for
// static metadata routes, for which we don't generate those.
if (
appDirRelativeEntryPath &&
!isStaticMetadataRoute(appDirRelativeEntryPath)
) {
entryFiles.add(
nodePath.join(
outputPath,
outputPrefix,
entrypoint.name.replace(/%5F/g, '_') +
'_' +
CLIENT_REFERENCE_MANIFEST +
'.js'
)
)
}
}

Expand Down Expand Up @@ -315,6 +326,7 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance {
const entryNameMap = new Map<string, string>()
const entryModMap = new Map<string, any>()
const additionalEntries = new Map<string, Map<string, any>>()
const absolutePathByEntryName = new Map<string, string>()

const depModMap = new Map<string, any>()

Expand Down Expand Up @@ -356,6 +368,7 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance {
) {
entryModMap.set(absolutePath, entryMod)
entryNameMap.set(absolutePath, name)
absolutePathByEntryName.set(name, absolutePath)
}
}

Expand Down Expand Up @@ -441,6 +454,9 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance {
appDir: this.rootDir,
depModArray: Array.from(depModMap.keys()),
entryNameMap: Object.fromEntries(entryNameMap),
absolutePathByEntryName: Object.fromEntries(
absolutePathByEntryName
),
outputPath: compilation.outputOptions.path!,
}

Expand Down
Binary file not shown.

0 comments on commit 55cf714

Please # to comment.