[wp-trac] [WordPress Trac] #62709: Script modules integration with wp_resource_hints
WordPress Trac
noreply at wordpress.org
Fri Mar 13 05:31:50 UTC 2026
#62709: Script modules integration with wp_resource_hints
---------------------------+--------------------------------------
Reporter: jonsurrell | Owner: (none)
Type: enhancement | Status: new
Priority: normal | Milestone: 7.1
Component: Script Loader | Version: 6.5
Severity: minor | Resolution:
Keywords: needs-patch | Focuses: javascript, performance
---------------------------+--------------------------------------
Comment (by sanket.parmar):
Thanks for filing this — the gap is clear. Here are some thoughts on a
possible approach.
== The Core Issue
`wp_dependencies_unique_hosts()` currently only loops over `$wp_scripts`
and `$wp_styles`:
{{{
#!php
foreach ( array( $wp_scripts, $wp_styles ) as $dependencies ) {
// ...
}
}}}
`WP_Script_Modules` is a completely separate system and is never
consulted, so no `dns-prefetch` hints are emitted for external hosts used
by script modules.
== Possible Approaches
=== Option A: Hook into `wp_resource_hints` filter from within
`WP_Script_Modules`
One approach could be to add a new public method to `WP_Script_Modules` —
something like `filter_resource_hints( array $urls, string $relation_type
): array` — and register it from within `add_hooks()`:
{{{
#!php
add_filter( 'wp_resource_hints', array( $this, 'filter_resource_hints' ),
10, 2 );
}}}
The method would:
* Only act when `$relation_type === 'dns-prefetch'`
* Walk the queue and all dependencies (both static and dynamic)
* Parse the host from each `src` URL, filtering out same-site hosts
* Merge the resulting hosts into `$urls` and return
This keeps all the logic inside `WP_Script_Modules` and avoids changing
the signature or behavior of `wp_dependencies_unique_hosts()`.
=== Option B: Extend `wp_dependencies_unique_hosts()` directly
Alternatively, `wp_dependencies_unique_hosts()` could be extended to also
inspect `$wp_script_modules`. This would require exposing a getter on
`WP_Script_Modules` (e.g. `get_registered_srcs()` or similar), which may
partly overlap with the work in #60597.
== A Note on Redundancy
As @swissspidy raised — `print_script_module_preloads()` already emits
`rel="modulepreload"` for ''static'' dependencies, and `modulepreload`
implies a full connection (DNS + TCP + TLS). Adding `dns-prefetch` on top
of those may be redundant.
That said, two cases still seem clearly worth targeting:
1. '''Dynamic dependencies''' — declared as `'import' => 'dynamic'`,
these are never preloaded and currently receive no performance hint at
all.
2. '''Classic themes''' — `wp_resource_hints` fires in `wp_head` at
priority 2, while all module-related tags fire in `wp_footer` on classic
themes. Even for static deps, an early `dns-prefetch` in the head could
give the browser a head start before any module tag is encountered.
Option A could optionally be scoped to only emit `dns-prefetch` for
dynamic-dependency hosts (skipping those already covered by
`modulepreload`), though that could also be left as a follow-up to keep
the initial patch simpler.
== Tests
Any patch would likely need companion PHPUnit tests, probably alongside
`tests/phpunit/tests/general/wpResourceHints.php`, covering:
* Dynamic-dependency hosts appear in dns-prefetch
* Static-dependency hosts (already covered by modulepreload) are either
included or explicitly excluded, depending on the chosen design
* Same-site hosts are not included
Happy to help put together a patch if this direction looks reasonable.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/62709#comment:7>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list