fix: prevent webpackPrefetch/Preload from leaking across same-named chunks#20994
Conversation
…hunks When two `import()` calls used the same `webpackChunkName` but only one specified `webpackPrefetch` (or `webpackPreload`), the directive was applied to every parent chunk that referenced the named chunk. This happened because `*Order` options were accumulated on the shared chunk group's options, losing the per-call-site origin. Resolve `*Order` options per parent→child edge by inspecting the blocks that bridge the edge instead of the child chunk group's accumulated options. Closes #12393.
🦋 Changeset detectedLatest commit: 1f87b2a The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
This PR is packaged and the instant preview is available (c3a5fc4). Install it locally:
npm i -D webpack@https://pkg.pr.new/webpack@c3a5fc4
yarn add -D webpack@https://pkg.pr.new/webpack@c3a5fc4
pnpm add -D webpack@https://pkg.pr.new/webpack@c3a5fc4 |
There was a problem hiding this comment.
Pull request overview
Fixes a bug where webpackPrefetch / webpackPreload could “leak” across different import() call sites that share the same webpackChunkName, by resolving *Order options per parent→child edge instead of reading accumulated options from the shared child chunk group.
Changes:
- Added
ChunkGroup.getChildOrderOptions(childGroup, chunkGraph)to aggregate*Ordervalues from the async blocks that actually connect a specific parent→child relationship. - Updated child-order resolution paths in
lib/ChunkGroup.jsandlib/Chunk.jsto use per-edge order options. - Added regression coverage via new stats/config test cases and a patch changeset.
Reviewed changes
Copilot reviewed 13 out of 14 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
lib/ChunkGroup.js |
Adds per-edge *Order aggregation and uses it when computing ordered children. |
lib/Chunk.js |
Switches child-id and child-of-type ordering logic to use per-edge order options. |
types.d.ts |
Exposes the new ChunkGroup.getChildOrderOptions API in typings. |
test/statsCases/prefetch-chunk-name-collision/* |
Adds a stats regression case + snapshot for chunk-name collisions with prefetch. |
test/configCases/web/prefetch-chunk-name-collision/* |
Adds a runtime regression case for prefetch leakage across entries sharing a named chunk. |
.changeset/prefetch-chunk-name-collision.md |
Records the patch-level fix in release notes. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| expect(prefetchLinks).toHaveLength(1); | ||
| expect(prefetchLinks[0].href).toBe("https://example.com/path/shared.js"); | ||
| // The import is what registered the prefetch directive at compile time. | ||
| import(/* webpackChunkName: "shared", webpackPrefetch: true */ "./shared"); |
| (node) => node._type === "link" && node.rel === "prefetch" | ||
| ); | ||
| expect(prefetchLinks).toHaveLength(1); | ||
| import(/* webpackChunkName: "shared" */ "./shared"); |
Codecov Report❌ Patch coverage is
❌ Your changes status has failed because you have indirect coverage changes. Learn more about Unexpected Coverage Changes and reasons for indirect coverage changes. Additional details and impacted files@@ Coverage Diff @@
## main #20994 +/- ##
=======================================
Coverage 90.94% 90.95%
=======================================
Files 573 573
Lines 58941 58964 +23
Branches 15888 15894 +6
=======================================
+ Hits 53605 53628 +23
Misses 5336 5336
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
The import() expressions only need to exist in the source for the parser to register their magic comments. Letting them run leaves a pending JSONP chunk-load timeout in the test harness that can surface as an unhandled rejection later in the suite.
Merging this PR will not alter performance
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing Footnotes
|
When two
import()calls used the samewebpackChunkNamebut only onespecified
webpackPrefetch(orwebpackPreload), the directive wasapplied to every parent chunk that referenced the named chunk. This
happened because
*Orderoptions were accumulated on the shared chunkgroup's options, losing the per-call-site origin.
Resolve
*Orderoptions per parent→child edge by inspecting the blocksthat bridge the edge instead of the child chunk group's accumulated
options. Closes #12393.