feat: add webpackPrefetch/webpackPreload/webpackFetchPriority support for URL-based assets#19695
Open
3ru wants to merge 23 commits into
Open
feat: add webpackPrefetch/webpackPreload/webpackFetchPriority support for URL-based assets#196953ru wants to merge 23 commits into
webpackPrefetch/webpackPreload/webpackFetchPriority support for URL-based assets#196953ru wants to merge 23 commits into
Conversation
CodSpeed Performance ReportMerging #19695 will improve performances by 20.58%Comparing Summary
Benchmarks breakdown
|
329587d to
13027da
Compare
f0ffcb7 to
2bb3de1
Compare
webpackPrefetch / webpackPreload / webpackFetchPriority support for Worker syntaxwebpackPrefetch/webpackPreload/webpackFetchPriority support for URL-based assets
2bb3de1 to
3cc5c91
Compare
Member
Author
Standards and constraints
Why numeric order is only for Worker (not URL)
|
xiaoxiaojx
reviewed
Jul 26, 2025
xiaoxiaojx
reviewed
Jul 26, 2025
xiaoxiaojx
reviewed
Jul 26, 2025
e059f1b to
9ff7ec7
Compare
76d6374 to
270fdeb
Compare
Member
Author
|
rebased |
270fdeb to
a84e769
Compare
xiaoxiaojx
reviewed
Aug 10, 2025
xiaoxiaojx
reviewed
Aug 10, 2025
a84e769 to
f7283bc
Compare
xiaoxiaojx
reviewed
Aug 10, 2025
xiaoxiaojx
reviewed
Aug 10, 2025
Member
|
Let's try to get this reviewd, patched and merged soon. |
… for Worker syntax
…upport for new URL() syntax
- Consolidate multiple runtime modules into unified AssetResourcePrefetchPlugin - Remove complex startup prefetch mechanism in favor of simpler inline approach
…PrefetchRuntimeModule`
- Introduced `preloadAs`, `preloadType`, and `preloadMedia` properties to `URLDependency` for better control over asset preloading.
…Type`, and `webpackPreloadMedia` values.
a8d39c0 to
aaae36e
Compare
Member
Author
|
Rebased this on the latest. |
Member
|
@3ru Thanks, a lot of tasks, still in my TODO 😞 |
alexander-akait
added a commit
that referenced
this pull request
May 22, 2026
Add a single `output.resourceHints` config that controls
`<link rel="prefetch">` / `<link rel="preload">` injection for every
URL-referenced asset in the bundle. The link is emitted at chunk
startup by a small runtime module that runs before any user module in
the chunk evaluates — for both initial and async chunks — so the
browser already has the response in flight by the time user code
references it.
Honored from three sources:
- JavaScript — `new URL(/* webpackPrefetch: true */ "./img.png", import.meta.url)`
and `new Worker(new URL(/* webpackPreload: true */ "./w.js", import.meta.url))`.
The hint comment must sit inside the inner `new URL(...)`; comments
anywhere else in `new Worker(...)` are ignored.
- CSS (`experiments.css`) — `/* webpackPreload: true */ url("./font.woff2")`.
- HTML (`experiments.html`) — `<!-- webpackPreload: true --> <img src="/asset?url=https%3A%2F%2Fgithub.com%2Fwebpack%2Fwebpack%2Fpull%2Fhero.png">`.
Recognized comment keys: `webpackPrefetch`, `webpackPreload`,
`webpackFetchPriority`, plus `webpackAs` / `webpackType` /
`webpackMedia` on JS `new URL(...)`. The `as` attribute is
auto-detected from the file extension when `webpackAs` is omitted.
Project-wide defaults via `output.resourceHints` (single rule or array
of rules; each rule accepts the standard webpack `test` / `include` /
`exclude` matchers):
output: {
resourceHints: [
{ test: /\.(png|jpg|webp)$/, prefetch: true, fetchPriority: "low" },
{ test: /\.woff2$/, preload: true, fetchPriority: "high" }
]
}
Per-call magic comments win over the project-wide default. CSP nonce
on the bundle's `<script>` tag is picked up automatically (via
`document.currentScript.nonce` for script output,
`document.querySelectorAll('script')` matched by `import.meta.url`
for `output.module: true`). Plugin authors can extend the generated
`<link>` markup via
`ResourceHintRuntimeModule.getCompilationHooks(compilation).linkPrefetch` /
`.linkPreload`, and new URL emitters can read the same project-wide
config via `ResourceHintPlugin.getCompilationResolver(compilation)`.
Tests under `test/configCases/asset-modules/url-prefetch-preload*` and
`test/configCases/worker/worker-prefetch-preload` cover JS / CSS / HTML
sources, single + array config form, ESM output, split-runtime,
CSP nonce, and per-call magic-comment overrides.
#19695
alexander-akait
added a commit
that referenced
this pull request
May 22, 2026
Add a single `output.resourceHints` config that controls
`<link rel="prefetch">` / `<link rel="preload">` injection for every
URL-referenced asset in the bundle. The link is emitted at chunk
startup by a small runtime module that runs before any user module in
the chunk evaluates — for both initial and async chunks — so the
browser already has the response in flight by the time user code
references it.
Honored from three sources:
- JavaScript — `new URL(/* webpackPrefetch: true */ "./img.png", import.meta.url)`
and `new Worker(new URL(/* webpackPreload: true */ "./w.js", import.meta.url))`.
The hint comment must sit inside the inner `new URL(...)`; comments
anywhere else in `new Worker(...)` are ignored.
- CSS (`experiments.css`) — `/* webpackPreload: true */ url("./font.woff2")`.
- HTML (`experiments.html`) — `<!-- webpackPreload: true --> <img src="/asset?url=https%3A%2F%2Fgithub.com%2Fwebpack%2Fwebpack%2Fpull%2Fhero.png">`.
Recognized comment keys: `webpackPrefetch`, `webpackPreload`,
`webpackFetchPriority`, plus `webpackAs` / `webpackType` /
`webpackMedia` on JS `new URL(...)`. The `as` attribute is
auto-detected from the file extension when `webpackAs` is omitted.
Project-wide defaults via `output.resourceHints` (single rule or array
of rules; each rule accepts the standard webpack `test` / `include` /
`exclude` matchers):
output: {
resourceHints: [
{ test: /\.(png|jpg|webp)$/, prefetch: true, fetchPriority: "low" },
{ test: /\.woff2$/, preload: true, fetchPriority: "high" }
]
}
Per-call magic comments win over the project-wide default. CSP nonce
on the bundle's `<script>` tag is picked up automatically (via
`document.currentScript.nonce` for script output,
`document.querySelectorAll('script')` matched by `import.meta.url`
for `output.module: true`). Plugin authors can extend the generated
`<link>` markup via
`ResourceHintRuntimeModule.getCompilationHooks(compilation).linkPrefetch` /
`.linkPreload`, and new URL emitters can read the same project-wide
config via `ResourceHintPlugin.getCompilationResolver(compilation)`.
Tests under `test/configCases/asset-modules/url-prefetch-preload*` and
`test/configCases/worker/worker-prefetch-preload` cover JS / CSS / HTML
sources, single + array config form, ESM output, split-runtime,
CSP nonce, and per-call magic-comment overrides.
#19695
alexander-akait
added a commit
that referenced
this pull request
Jun 4, 2026
Add a single `output.resourceHints` config that controls
`<link rel="prefetch">` / `<link rel="preload">` injection for every
URL-referenced asset in the bundle. The link is emitted at chunk
startup by a small runtime module that runs before any user module in
the chunk evaluates — for both initial and async chunks — so the
browser already has the response in flight by the time user code
references it.
Honored from three sources:
- JavaScript — `new URL(/* webpackPrefetch: true */ "./img.png", import.meta.url)`
and `new Worker(new URL(/* webpackPreload: true */ "./w.js", import.meta.url))`.
The hint comment must sit inside the inner `new URL(...)`; comments
anywhere else in `new Worker(...)` are ignored.
- CSS (`experiments.css`) — `/* webpackPreload: true */ url("./font.woff2")`.
- HTML (`experiments.html`) — `<!-- webpackPreload: true --> <img src="/asset?url=https%3A%2F%2Fgithub.com%2Fwebpack%2Fwebpack%2Fpull%2Fhero.png">`.
Recognized comment keys: `webpackPrefetch`, `webpackPreload`,
`webpackFetchPriority`, plus `webpackAs` / `webpackType` /
`webpackMedia` on JS `new URL(...)`. The `as` attribute is
auto-detected from the file extension when `webpackAs` is omitted.
Project-wide defaults via `output.resourceHints` (single rule or array
of rules; each rule accepts the standard webpack `test` / `include` /
`exclude` matchers):
output: {
resourceHints: [
{ test: /\.(png|jpg|webp)$/, prefetch: true, fetchPriority: "low" },
{ test: /\.woff2$/, preload: true, fetchPriority: "high" }
]
}
Per-call magic comments win over the project-wide default. CSP nonce
on the bundle's `<script>` tag is picked up automatically (via
`document.currentScript.nonce` for script output,
`document.querySelectorAll('script')` matched by `import.meta.url`
for `output.module: true`). Plugin authors can extend the generated
`<link>` markup via
`ResourceHintRuntimeModule.getCompilationHooks(compilation).linkPrefetch` /
`.linkPreload`, and new URL emitters can read the same project-wide
config via `ResourceHintPlugin.getCompilationResolver(compilation)`.
Tests under `test/configCases/asset-modules/url-prefetch-preload*` and
`test/configCases/worker/worker-prefetch-preload` cover JS / CSS / HTML
sources, single + array config form, ESM output, split-runtime,
CSP nonce, and per-call magic-comment overrides.
#19695
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Description
This PR refreshes prefetch/preload support across Worker chunks and
new URL()assets, aligns the behavior with dynamicimport(), and introduces new runtime helpers.fixes
webpackPrefetch/webpackPreload/webpackFetchPriorityfornew Workersyntax #19377webpackPrefetchandwebpackPreloadfornew Worker()chunk #19373webpackPrefetchfornew URL(...)#18524What kind of change does this PR introduce?
Adds magic comment support to configure resource hints:
new URL()assets (boolean flags + fetch priority + explicit preload attribute overrides)<link>elements at runtime:prefetchAssetandpreloadAssetSupported magic comments
/* webpackPrefetch: true | number *//* webpackPreload: true | number *//* webpackFetchPriority: "high" | "low" | "auto" */new URL()assets:/* webpackPrefetch: true *//* webpackPreload: true *//* webpackFetchPriority: "high" | "low" | "auto" *//* webpackPreloadAs: "<request-destination>" *//* webpackPreloadType: "<mime/type>" *//* webpackPreloadMedia: "<media-query>" */Notes:
import()), URL assets do not.webpackPreloadAs/webpackPreloadType/webpackPreloadMediaoverride the auto-detected defaults for URL assets.asattribute accepts Fetch Standard request destinations. Allowed values include:audio,audioworklet,document,embed,fetch,font,image,manifest,object,paintworklet,report,script,sharedworker,serviceworker,style,track,video,worker,xslt.Example Usage
Did you add tests for your changes?
Yes
Does this PR introduce a breaking change?
No
What needs to be documented once your changes are merged?
webpackPrefetch: true/webpackPreload: truewebpackFetchPriority: "high" | "low" | "auto"as(based on file type), with explicit overrides:webpackPreloadAs: "<request-destination>"(Fetch Request Destinations; includesaudio,audioworklet,document,embed,fetch,font,image,manifest,object,paintworklet,report,script,sharedworker,serviceworker,style,track,video,worker,xslt)webpackPreloadType: "<mime/type>"webpackPreloadMedia: "<media-query>"rel