Script Modules: add dns-prefetch resource hints support#11374
Script Modules: add dns-prefetch resource hints support#11374sanketio wants to merge 3 commits intoWordPress:trunkfrom
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
Description
wp_resource_hints()prints<link rel="dns-prefetch">tags for all external hosts used by enqueued scripts and styles, giving browsers an early signal to resolve DNS before requests are made. However,WP_Script_Moduleswas never wired into this system, so external hosts referenced by script modules received no DNS prefetch hints.This PR addresses #62709 by adding a new public method
WP_Script_Modules::filter_resource_hints(), registered on thewp_resource_hintsfilter insideadd_hooks(). The method collects all enqueued module IDs and their full dependency tree — including dynamic dependencies, which never receivemodulepreloadhints and are therefore the clearest gap — parses eachsrcfor its host, filters out same-site hosts, and appends the external URLs to thedns-prefetchhints array. Deduplication by host is handled downstream bywp_resource_hints()itself, as it does for scripts and styles.On classic themes,
wp_resource_hintsfires inwp_headat priority 2, while all module-related tags fire inwp_footer. This means even static dependency hosts will benefit from an early hint in the page<head>well before any module tag is processed.Changes
src/wp-includes/class-wp-script-modules.phpfilter_resource_hints( array $urls, string $relation_type ): arraypublic method.add_hooks()viawp_resource_hints.tests/phpunit/tests/script-modules/wpScriptModulesResourceHints.php(new file)wp_resource_hints()integration.How to test
src, e.g.:View the page source and confirm a
<link rel="dns-prefetch" href="https://github.com//cdn.example.com" />tag appears in<head>.Run the new unit tests:
Trac ticket: https://core.trac.wordpress.org/ticket/62709
Use of AI Tools
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.