Add output.resourceHints.dedupe to skip prefetch for already preloaded/prefetched chunks - #21386
Conversation
🦋 Changeset detectedLatest commit: debdbea 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 (ba6e94b). Install it locally:
npm i -D webpack@https://pkg.pr.new/webpack@ba6e94b
yarn add -D webpack@https://pkg.pr.new/webpack@ba6e94b
pnpm add -D webpack@https://pkg.pr.new/webpack@ba6e94b |
Hooray! CodSpeed harness just leveled up!The base and head of this comparison were measured with different runner settings, so their benchmark values are not directly comparable. What changed between base and head:
Re-run the base with the same settings to get a valid performance comparison. Comparing Footnotes |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #21386 +/- ##
=======================================
Coverage 93.91% 93.91%
=======================================
Files 621 621
Lines 73867 73874 +7
Branches 21372 21378 +6
=======================================
+ Hits 69375 69382 +7
Misses 4492 4492
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
6375957 to
7008911
Compare
7008911 to
8aece90
Compare
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 8aece90. Configure here.
8aece90 to
ab9b713
Compare
…hunks Some browsers (Chrome) issue a second network request when a <link rel="prefetch"> is inserted for a resource already hinted via markup. The new opt-in output.resourceHints.dedupe flag makes the JSONP and ESM chunk-loading runtimes scan existing <link> elements and skip the prefetch when a matching preload/prefetch already exists. Default false, so builds that do not need it pay no runtime cost. Refs #17496
ab9b713 to
debdbea
Compare

Summary
Some browsers (Chrome) fire a second network request when a
<link rel="prefetch">is inserted for a resource that was already hinted via markup (e.g. an SSR/HTML-injected<link rel="preload">). This adds an opt-inoutput.resourceHints.dedupe(defaultfalse) — a new boolean on the existingoutput.resourceHintsgroup — that makes the JSONP and ESM chunk-loading runtimes scan existing<link>elements and skip the prefetch when a matching preload/prefetch already exists. webpack already dedupes its own preload→prefetch viainstalledChunks, so this only covers externally-authored hints; keeping it opt-in means builds that don't need it pay no runtime cost. Refs #17496.What kind of change does this PR introduce?
feat — a new opt-in
output.resourceHintssub-option.Did you add tests for your changes?
Yes —
test/configCases/web/prefetch-preload-dedupe(classic/JSONP output) andtest/configCases/web/prefetch-preload-dedupe-module(ESM output), each asserting the already-preloaded chunk is skipped while others still prefetch; plus updatedDefaults, and CLI-flags snapshots.Does this PR introduce a breaking change?
No — the option defaults to
false, so existing builds are unchanged.If relevant, what needs to be documented once your changes are merged or what have you already documented?
The new
output.resourceHints.dedupeboolean should be documented alongside the otheroutput.resourceHintsoptions on webpack.js.org.Use of AI
AI (Claude) was used to reproduce the double-request in Chromium, implement the option across schema/defaults/runtime, write the tests, and regenerate the types/validators. All changes were reviewed and verified locally (targeted Jest suites +
tsc).Note
Low Risk
Opt-in config defaulting to false; behavior change is limited to browser prefetch injection when dedupe is explicitly enabled.
Overview
Adds
output.resourceHints.dedupe(defaultfalse) on the existingoutput.resourceHintsobject. When enabled, the JSONP and ESM chunk-loading runtimes scan existing<link>elements before appending a runtime<link rel="prefetch">and bail out if the samehrefis already covered byrel="prefetch",preload, ormodulepreload—addressing duplicate fetches in Chrome when SSR/HTML already injected preload hints.Wiring includes defaults (
lib/config/defaults.js), generated TypeScript declarations, a minor changeset, and an AGENTS.md note that runtime-emitting code must stay consistent across chunk-loading targets.Reviewed by Cursor Bugbot for commit debdbea. Bugbot is set up for automated code reviews on this repo. Configure here.