Skip to content

Commit 415a9b6

Browse files
janicklas-ralphtimneutkensijjkTimer
committed
Fix modern + granularChunk hydration failing (vercel#9727)
* Adding native-url package * Bumping native-url version * Upgrading native-url * Logging stats object for debugging * Logging stats object for debugging * Adding try catch to the error lines * Experimenting with regex * Experimenting with regex * Experimenting with regex * Testing regex changes * Fixing defer-script test case to not include polyfill.js * Meging changes with existing polyfill work * Bumping version * adjust webpack config * Reduce size in size test * Remove 1kb from legacy * Bumping native-url version, includes fix for IE11 * Update lock file * Updating native-url, fixes issue on IE11 * Fix sourcemap being added in document * Adding Router as an app level dep. Fixes Router not being added as a dep to pages without Link when granularChunks is enabled * Fix typescript error * Fix modern + granularChunks hydration failing * Fix TS error * Update native-url version Co-authored-by: Tim Neutkens <tim@timneutkens.nl> Co-authored-by: JJ Kasper <jj@jjsweb.site> Co-authored-by: Joe Haddad <timer150@gmail.com>
1 parent 6de65fa commit 415a9b6

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

packages/next/build/webpack/plugins/next-esm-plugin.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -213,38 +213,53 @@ export default class NextEsmPlugin implements Plugin {
213213
compilation.namedChunkGroups
214214
)
215215

216+
const unnamedChunks: compilation.Chunk[] = []
216217
const childChunkFileMap = childCompilation.chunks.reduce(
217218
(
218219
chunkMap: { [key: string]: compilation.Chunk },
219220
chunk: compilation.Chunk
220221
) => {
221-
chunkMap[chunk.name] = chunk
222+
// Dynamic chunks may not have a name. It'll be null in such cases
223+
if (chunk.name === null) {
224+
unnamedChunks.push(chunk)
225+
} else {
226+
chunkMap[chunk.name] = chunk
227+
}
228+
222229
return chunkMap
223230
},
224231
{}
225232
)
226233

227-
// Merge files from similar chunks
234+
// Merge chunks - merge the files of chunks with the same name
228235
compilation.chunks.forEach((chunk: compilation.Chunk) => {
229236
const childChunk = childChunkFileMap[chunk.name]
230237

231-
if (childChunk?.files) {
238+
// Do not merge null named chunks since they are different
239+
if (chunk.name !== null && childChunk?.files) {
232240
delete childChunkFileMap[chunk.name]
233241
chunk.files.push(
234242
...childChunk.files.filter((v: any) => !chunk.files.includes(v))
235243
)
236244
}
237245
})
238246

239-
// Add modern only chunks
240-
compilation.chunks.push(...Object.values(childChunkFileMap))
247+
// Add modern only chunks into the main compilation
248+
compilation.chunks.push(
249+
...Object.values(childChunkFileMap),
250+
...unnamedChunks
251+
)
241252

242-
// Place modern only chunk inside the right entry point
253+
// Place modern only (unmerged) chunks inside the right entry point
243254
compilation.entrypoints.forEach((entryPoint, entryPointName) => {
244255
const childEntryPoint = childCompilation.entrypoints.get(entryPointName)
245256

246257
childEntryPoint.chunks.forEach((chunk: compilation.Chunk) => {
247-
if (childChunkFileMap.hasOwnProperty(chunk.name)) {
258+
if (
259+
// Add null named dynamic chunks since they weren't merged
260+
chunk.name === null ||
261+
childChunkFileMap.hasOwnProperty(chunk.name)
262+
) {
248263
entryPoint.chunks.push(chunk)
249264
}
250265
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy