Unverified Commit cf9591cd authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Ensure App Router and Pages Router modules can't share chunks (#50327)

## What?

Ensures the name of the chunk is not the same between two modules in different layers.
E.g. if you import 'button-library' in App Router and Pages Router we don't want these to be bundled in the same chunk as they're never used on the same page.

## How?

Added the layer to the chunk name hash generation in splitChunks. Ensures the two modules don't have the same chunk name generated, as discussed with @sokra.

Follow-up to #50324
parent 5831d0c1
...@@ -1672,6 +1672,10 @@ export default async function getBaseWebpackConfig( ...@@ -1672,6 +1672,10 @@ export default async function getBaseWebpackConfig(
framework: { framework: {
chunks: 'all', chunks: 'all',
name: 'framework', name: 'framework',
// Ensures the framework chunk is not created for App Router.
layer(layer: any) {
return layer === null || layer === undefined
},
test(module: any) { test(module: any) {
const resource = module.nameForCondition?.() const resource = module.nameForCondition?.()
return resource return resource
...@@ -1696,6 +1700,7 @@ export default async function getBaseWebpackConfig( ...@@ -1696,6 +1700,7 @@ export default async function getBaseWebpackConfig(
) )
}, },
name(module: { name(module: {
layer: string | null | undefined
type: string type: string
libIdent?: Function libIdent?: Function
updateHash: (hash: crypto.Hash) => void updateHash: (hash: crypto.Hash) => void
...@@ -1712,6 +1717,13 @@ export default async function getBaseWebpackConfig( ...@@ -1712,6 +1717,13 @@ export default async function getBaseWebpackConfig(
hash.update(module.libIdent({ context: dir })) hash.update(module.libIdent({ context: dir }))
} }
// Ensures the name of the chunk is not the same between two modules in different layers
// E.g. if you import 'button-library' in App Router and Pages Router we don't want these to be bundled in the same chunk
// as they're never used on the same page.
if (module.layer) {
hash.update(module.layer)
}
return hash.digest('hex').substring(0, 8) return hash.digest('hex').substring(0, 8)
}, },
priority: 30, priority: 30,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment