Skip to content

Commit 1827f36

Browse files
authored
Fix chunk file names in flight manifest (vercel#48583)
This PR corrects the file names of chunks in the flight manifest. Previously we assume that the chunk file is always named as `(requiredChunk.name || requiredChunk.id) + '-' + requiredChunk.hash` and located in `static/chunks`. This isn't always true (see the comment) especially when a chunk was generated via `import()`. Another mistake was that we assume that one chunk only generates one file, but it's actually possible that it depends on multiple files. This should address many of the "Chunk failed to load" errors. Closes [vercel#47173](vercel#47173), fixes NEXT-847 fix vercel#47173
1 parent 9b3bd73 commit 1827f36

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

packages/next/src/build/webpack/plugins/flight-manifest-plugin.ts

+11-7
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ export class ClientReferenceManifestPlugin {
135135
cssFiles: {},
136136
clientModules: {},
137137
}
138-
const dev = this.dev
139138

140139
const clientRequestsSet = new Set()
141140

@@ -256,13 +255,18 @@ export class ClientReferenceManifestPlugin {
256255
return null
257256
}
258257

259-
return (
260-
requiredChunk.id +
261-
':' +
262-
(requiredChunk.name || requiredChunk.id) +
263-
(dev ? '' : '-' + requiredChunk.hash)
264-
)
258+
// Get the actual chunk file names from the chunk file list.
259+
// It's possible that the chunk is generated via `import()`, in
260+
// that case the chunk file name will be '[name].[contenthash]'
261+
// instead of '[name]-[chunkhash]'.
262+
return [...requiredChunk.files].map((file) => {
263+
// It's possible that a chunk also emits CSS files, that will
264+
// be handled separatedly.
265+
if (!file.endsWith('.js')) return null
266+
return requiredChunk.id + ':' + file
267+
})
265268
})
269+
.flat()
266270
.filter(nonNullable)
267271
}
268272
const requiredChunks = getAppPathRequiredChunks()

packages/next/src/client/app-index.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ self.__next_require__ =
6969
// eslint-disable-next-line no-undef
7070
;(self as any).__next_chunk_load__ = (chunk: string) => {
7171
if (!chunk) return Promise.resolve()
72-
const [chunkId, chunkFileName] = chunk.split(':')
73-
chunkFilenameMap[chunkId] = `static/chunks/${chunkFileName}.js`
72+
const [chunkId, chunkFilePath] = chunk.split(':')
73+
chunkFilenameMap[chunkId] = chunkFilePath
7474

7575
// @ts-ignore
7676
// eslint-disable-next-line no-undef

0 commit comments

Comments
 (0)