Skip to content

Conversation

@westonruter
Copy link
Member

@westonruter westonruter commented May 21, 2025

This is to add support for the new $args parameter for wp_register_script_module()/wp_enqueue_script_module() for Core-61734 and proposed in WordPress/wordpress-develop#8815. When Gutenberg is active, the fetchpriority=low won't appear for script modules since the core script modules are overridden by Gutenberg.

@westonruter westonruter added [Type] Enhancement A suggestion for improvement. [Feature] Interactivity API API to add frontend interactivity to blocks. [Package] Interactivity /packages/interactivity [Feature] Script Modules API Related to the Script Modules API that adds support for native ES modules and import maps [Package] Interactivity Router /packages/interactivity-router labels May 21, 2025
@github-actions
Copy link

github-actions bot commented May 21, 2025

Flaky tests detected in 1ab3c34.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/17508853441
📝 Reported issues:

@westonruter westonruter marked this pull request as ready for review September 3, 2025 21:10
@westonruter westonruter requested a review from sirreal September 3, 2025 21:10
@github-actions
Copy link

github-actions bot commented Sep 3, 2025

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 props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: westonruter <[email protected]>
Co-authored-by: mukeshpanchal27 <[email protected]>
Co-authored-by: sirreal <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

pento pushed a commit to WordPress/wordpress-develop that referenced this pull request Sep 3, 2025
* Allow scripts and script modules to be registered with a `fetchpriority` of `auto` (default), `high`, `low`:
  * When registering a script, add a `fetchpriority` arg to go alongside the `strategy` arg which was added for loading scripts with the `defer` and `async` loading strategies. See #12009. 
  * For script modules, introduce an `$args` array parameter with a `fetchpriority` key to the `wp_register_script_module()`, and `wp_enqueue_script_module()` functions (and their respective underlying `WP_Script_Modules::register()` and `WP_Script_Modules::enqueue()` methods). This `$args` parameter corresponds with the same parameter used when registering non-module scripts.
  * Also for script modules, introduce `WP_Script_Modules::set_fetchpriority()` to override the `fetchpriority` for what was previously registered.
  * Emit a `_doing_it_wrong()` warning when an invalid `fetchpriority` value is used, and when `fetchpriority` is added to a script alias.
  * Include `fetchpriority` as an attribute on printed `SCRIPT` tags as well as on preload `LINK` tags for static script module dependencies.
* Use a `fetchpriority` of `low` by default for:
  * Script modules used with the Interactivity API. For overriding this default in blocks, see [WordPress/gutenberg#71366 Gutenberg#71366].
  * The `comment-reply` script.
* Improve type checks and type hints.

Developed in [#8815 GitHub PR], with [WordPress/gutenberg#70173 companion for Gutenberg].

Props westonruter, jonsurrell, swissspidy, luisherranz, kraftbj, audrasjb, dennysdionigi.
Fixes #61734.


git-svn-id: https://develop.svn.wordpress.org/trunk@60704 602fd350-edb4-49c9-b593-d223f7449a82
markjaquith pushed a commit to markjaquith/WordPress that referenced this pull request Sep 3, 2025
* Allow scripts and script modules to be registered with a `fetchpriority` of `auto` (default), `high`, `low`:
  * When registering a script, add a `fetchpriority` arg to go alongside the `strategy` arg which was added for loading scripts with the `defer` and `async` loading strategies. See #12009. 
  * For script modules, introduce an `$args` array parameter with a `fetchpriority` key to the `wp_register_script_module()`, and `wp_enqueue_script_module()` functions (and their respective underlying `WP_Script_Modules::register()` and `WP_Script_Modules::enqueue()` methods). This `$args` parameter corresponds with the same parameter used when registering non-module scripts.
  * Also for script modules, introduce `WP_Script_Modules::set_fetchpriority()` to override the `fetchpriority` for what was previously registered.
  * Emit a `_doing_it_wrong()` warning when an invalid `fetchpriority` value is used, and when `fetchpriority` is added to a script alias.
  * Include `fetchpriority` as an attribute on printed `SCRIPT` tags as well as on preload `LINK` tags for static script module dependencies.
* Use a `fetchpriority` of `low` by default for:
  * Script modules used with the Interactivity API. For overriding this default in blocks, see [WordPress/gutenberg#71366 Gutenberg#71366].
  * The `comment-reply` script.
* Improve type checks and type hints.

Developed in [WordPress/wordpress-develop#8815 GitHub PR], with [WordPress/gutenberg#70173 companion for Gutenberg].

Props westonruter, jonsurrell, swissspidy, luisherranz, kraftbj, audrasjb, dennysdionigi.
Fixes #61734.

Built from https://develop.svn.wordpress.org/trunk@60704


git-svn-id: http://core.svn.wordpress.org/trunk@60040 1a063a9b-81f0-0310-95a4-ce76da25c4cd
@westonruter westonruter added the Backported to WP Core Pull request that has been successfully merged into WP Core label Sep 3, 2025
github-actions bot pushed a commit to platformsh/wordpress-performance that referenced this pull request Sep 3, 2025
* Allow scripts and script modules to be registered with a `fetchpriority` of `auto` (default), `high`, `low`:
  * When registering a script, add a `fetchpriority` arg to go alongside the `strategy` arg which was added for loading scripts with the `defer` and `async` loading strategies. See #12009. 
  * For script modules, introduce an `$args` array parameter with a `fetchpriority` key to the `wp_register_script_module()`, and `wp_enqueue_script_module()` functions (and their respective underlying `WP_Script_Modules::register()` and `WP_Script_Modules::enqueue()` methods). This `$args` parameter corresponds with the same parameter used when registering non-module scripts.
  * Also for script modules, introduce `WP_Script_Modules::set_fetchpriority()` to override the `fetchpriority` for what was previously registered.
  * Emit a `_doing_it_wrong()` warning when an invalid `fetchpriority` value is used, and when `fetchpriority` is added to a script alias.
  * Include `fetchpriority` as an attribute on printed `SCRIPT` tags as well as on preload `LINK` tags for static script module dependencies.
* Use a `fetchpriority` of `low` by default for:
  * Script modules used with the Interactivity API. For overriding this default in blocks, see [WordPress/gutenberg#71366 Gutenberg#71366].
  * The `comment-reply` script.
* Improve type checks and type hints.

Developed in [WordPress/wordpress-develop#8815 GitHub PR], with [WordPress/gutenberg#70173 companion for Gutenberg].

Props westonruter, jonsurrell, swissspidy, luisherranz, kraftbj, audrasjb, dennysdionigi.
Fixes #61734.

Built from https://develop.svn.wordpress.org/trunk@60704


git-svn-id: https://core.svn.wordpress.org/trunk@60040 1a063a9b-81f0-0310-95a4-ce76da25c4cd
@westonruter
Copy link
Member Author

See r60704 for the the changeset which added the default fetchpriority for Interactivity API scripts.

See #71366 which would allow for values other than low to be used by default for block view script modules.

@westonruter westonruter added No Core Sync Required Indicates that any changes do not need to be synced to WordPress Core and removed Backported to WP Core Pull request that has been successfully merged into WP Core labels Sep 3, 2025
@westonruter westonruter requested a review from Mamaduka September 4, 2025 00:19

$path = gutenberg_url( "build-module/{$file_name}" );
wp_register_script_module( $script_module_id, $path, $script_module_data['dependencies'], $script_module_data['version'] );
wp_register_script_module( $script_module_id, $path, $script_module_data['dependencies'], $script_module_data['version'], $args ); // The $args parameter is new as of WP 6.9 per <https://core.trac.wordpress.org/ticket/61734>.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked to make sure extra arguments to functions don't cause errors/warnings/notices and that does seem to be the case 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, adding extra arguments is fine. No complaints going back even to PHP 4.3: https://3v4l.org/doYun

Any additional unnamed params could be obtained by the function via func_get_args().

Co-authored-by: Mukesh Panchal <[email protected]>
Copy link
Member

@sirreal sirreal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two things come to mind seeing this:

Script modules are registered with fetch priority. What if I enqueue a high fetch priority script that depends on a module registered with low? As the browser explores the dependency graph, will it determine that it should increase the priority of the dependency? Is that sort of priority dependence something that should be built into the core script modules system?

This makes all the Gutenberg script modules low. That's fine at the moment. Have you considered how this system may evolve as more modules are introduced? What mechanism might modules have to report what fetch priority they should have?


$path = gutenberg_url( "build-module/{$file_name}" );
wp_register_script_module( $script_module_id, $path, $script_module_data['dependencies'], $script_module_data['version'] );
wp_register_script_module( $script_module_id, $path, $script_module_data['dependencies'], $script_module_data['version'], $args ); // The $args parameter is new as of WP 6.9 per <https://core.trac.wordpress.org/ticket/61734>.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked to make sure extra arguments to functions don't cause errors/warnings/notices and that does seem to be the case 👍

}

/*
* All script modules in Gutenberg are (currently) related to the Interactivity API which prioritizes server-side rendering.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's @wordpress/a11y module that's not strictly an iAPI concept.

https://make.wordpress.org/core/2024/10/14/updates-to-script-modules-in-6-7/

I don't think other modules have been introduced, but I could have missed something. Should a11y be low as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strictly, but it is only used by the interactivity-router, right? And it is used as a dynamic import, so it exists exclusively in the importmap and so there is no fetchpriority to print.

Granted, it could end up getting added as a static dependency. If I try doing that, I see that with this Gutenberg PR it is printed with fetchpriority=low, but if I try WP Core trunk then it omits fetchpriority (so it is auto).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've addressed this in 1ab3c34.

And in core, I've addressed in WordPress/wordpress-develop#9770 via WordPress/wordpress-develop@e2d4676

@westonruter
Copy link
Member Author

@sirreal:

Script modules are registered with fetch priority. What if I enqueue a high fetch priority script that depends on a module registered with low? As the browser explores the dependency graph, will it determine that it should increase the priority of the dependency? Is that sort of priority dependence something that should be built into the core script modules system?

That's a great question. I put together a try-script-module-priorities plugin to test this.

The plugin registers three modules, foo, bar, baz. Module baz statically depends on bar, and bar statically depends on foo.

If baz has a fetchpriority of high but the static dependencies have a fetchpriority of low then this is the markup:

<script
  type="module"
  src="http://localhost:8000/wp-content/plugins/try-script-module-priorities/baz.js"
  id="baz-js-module"
  fetchpriority="high"
></script>
<link
  rel="modulepreload"
  href="http://localhost:8000/wp-content/plugins/try-script-module-priorities/bar.js"
  id="bar-js-modulepreload"
  fetchpriority="low"
/>
<link
  rel="modulepreload"
  href="http://localhost:8000/wp-content/plugins/try-script-module-priorities/foo.js"
  id="foo-js-modulepreload"
  fetchpriority="low"
/>

The result seems to be that the dependencies are not fetched with a high priority:

image

I see the same in Firefox and Safari.

It would be ideal that if a script module had a fetchpriority of high, that any dependent scripts would also get the same fetchpriority. This would be similar as was done for async/defer in WP_Scripts, but the key difference here is that there is not going to be any breakage caused by a differing priority. They all get executed once the DOM loads regardless.

This makes all the Gutenberg script modules low. That's fine at the moment. Have you considered how this system may evolve as more modules are introduced? What mechanism might modules have to report what fetch priority they should have?

For view script modules, this would be addressed by #71366. But for other utility script modules, I haven't filed an issue yet.

@westonruter
Copy link
Member Author

It would be ideal that if a script module had a fetchpriority of high, that any dependent scripts would also get the same fetchpriority. This would be similar as was done for async/defer in WP_Scripts, but the key difference here is that there is not going to be any breakage caused by a differing priority. They all get executed once the DOM loads regardless.

See WordPress/wordpress-develop#9770

@westonruter westonruter requested a review from sirreal September 6, 2025 02:39
@westonruter westonruter merged commit ef6db07 into trunk Sep 8, 2025
72 checks passed
@westonruter westonruter deleted the add/script-module-fetchpriority-low branch September 8, 2025 15:21
@github-actions github-actions bot added this to the Gutenberg 21.7 milestone Sep 8, 2025
adamsilverstein pushed a commit to adamsilverstein/gutenberg that referenced this pull request Sep 11, 2025
* Add fetchpriority=low support to script modules

* Update comment now that change landed in core

* Add reference to related GB issue

* Use multi-line comment

Co-authored-by: westonruter <[email protected]>
Co-authored-by: mukeshpanchal27 <[email protected]>
Co-authored-by: sirreal <[email protected]>

Co-authored-by: Mukesh Panchal <[email protected]>

* Add comment explaining why a11y is fetchpriority=low

Co-authored-by: Jon Surrell <[email protected]>

---------

Co-authored-by: Mukesh Panchal <[email protected]>
Co-authored-by: Jon Surrell <[email protected]>
jonnynews pushed a commit to spacedmonkey/wordpress-develop that referenced this pull request Sep 24, 2025
* Allow scripts and script modules to be registered with a `fetchpriority` of `auto` (default), `high`, `low`:
  * When registering a script, add a `fetchpriority` arg to go alongside the `strategy` arg which was added for loading scripts with the `defer` and `async` loading strategies. See #12009. 
  * For script modules, introduce an `$args` array parameter with a `fetchpriority` key to the `wp_register_script_module()`, and `wp_enqueue_script_module()` functions (and their respective underlying `WP_Script_Modules::register()` and `WP_Script_Modules::enqueue()` methods). This `$args` parameter corresponds with the same parameter used when registering non-module scripts.
  * Also for script modules, introduce `WP_Script_Modules::set_fetchpriority()` to override the `fetchpriority` for what was previously registered.
  * Emit a `_doing_it_wrong()` warning when an invalid `fetchpriority` value is used, and when `fetchpriority` is added to a script alias.
  * Include `fetchpriority` as an attribute on printed `SCRIPT` tags as well as on preload `LINK` tags for static script module dependencies.
* Use a `fetchpriority` of `low` by default for:
  * Script modules used with the Interactivity API. For overriding this default in blocks, see [WordPress/gutenberg#71366 Gutenberg#71366].
  * The `comment-reply` script.
* Improve type checks and type hints.

Developed in [WordPress#8815 GitHub PR], with [WordPress/gutenberg#70173 companion for Gutenberg].

Props westonruter, jonsurrell, swissspidy, luisherranz, kraftbj, audrasjb, dennysdionigi.
Fixes #61734.


git-svn-id: https://develop.svn.wordpress.org/trunk@60704 602fd350-edb4-49c9-b593-d223f7449a82
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] Interactivity API API to add frontend interactivity to blocks. [Feature] Script Modules API Related to the Script Modules API that adds support for native ES modules and import maps No Core Sync Required Indicates that any changes do not need to be synced to WordPress Core [Package] Interactivity Router /packages/interactivity-router [Package] Interactivity /packages/interactivity [Type] Enhancement A suggestion for improvement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants