Skip to content

Conversation

@samueljseay
Copy link
Contributor

@samueljseay samueljseay commented Oct 20, 2025

What?

Closes #70873

Why?

This improves the selective pre-loading of script modules, by using the same approach implemented in core in WordPress/wordpress-develop#10357 utilizing the wp_script_attributes filter to add data-wp-router-options to the script module tags that do need to pre loaded for client-side navigation.

How?

For versions of WP before 6.9 we add our own static registry of modules that should have the data attribute added. For everything else we rely on WordPress/wordpress-develop#10357 which will add script modules to preload based on their interactivity support declared in block.json.

Testing Instructions

  1. Run the Gutenberg plugin on a version of WP core older than 6.9 or prior to Interactivity API: First support for loadOnClientNavigation wordpress-develop#10357
  2. Add a sample page with the core image block and enable the enlarge on click feature of the block. Or create a new interactive block with interactivity enabled in the block.json
  3. Load the page in the front-end, investigate the page source to see the output script module tags
  4. Ensure that the script module tags for the image block or your custom block have the data-wp-router attribute with value { "loadOnClientNavigation": true }

@samueljseay samueljseay changed the title [WIP] Reuse wp_script_attributes filter for adding data-wp-router-options attribute to script module Reuse wp_script_attributes filter for adding data-wp-router-options attribute to script module Oct 21, 2025
@samueljseay samueljseay marked this pull request as ready for review October 21, 2025 09:28
@luisherranz luisherranz added Backport to Gutenberg RC Pull request that needs to be backported to a Gutenberg release candidate (RC) Backport to WP 6.9 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta and removed Backport to Gutenberg RC Pull request that needs to be backported to a Gutenberg release candidate (RC) labels Oct 21, 2025
Copy link
Member

@luisherranz luisherranz left a comment

Choose a reason for hiding this comment

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

Hey Sam, we need to move the compatibility code to the compat folder.

Apart from that, when the WordPress version is 6.9 or higher but Gutenberg is installed, Gutenberg replaces the Core scripts with its own scripts. In that case, we need to make sure that the directive is also being added correctly. That logic needs to be outside of the compat folder, but we can use the new methods introduced in WordPress 6.9.

*/
remove_action( 'wp_enqueue_scripts', 'wp_enqueue_stored_styles' );
remove_action( 'wp_footer', 'wp_enqueue_stored_styles', 1 );
if ( version_compare( get_bloginfo( 'version' ), '6.9.0', '<' ) ) {
Copy link
Member

Choose a reason for hiding this comment

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

We need to move this to the compat/wordpress-6.9 folder so that it gets deleted when Gutenberg no longer needs to support versions older than WordPress 6.9.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Gotcha thanks

@github-actions
Copy link

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 <westonruter@git.wordpress.org>
Co-authored-by: luisherranz <luisherranz@git.wordpress.org>
Co-authored-by: samueljseay <samueljseay@git.wordpress.org>
Co-authored-by: DAreRodz <darerodz@git.wordpress.org>

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

@luisherranz luisherranz enabled auto-merge (squash) October 22, 2025 05:40
@luisherranz luisherranz added Backport to Gutenberg RC Pull request that needs to be backported to a Gutenberg release candidate (RC) [Feature] Interactivity API API to add frontend interactivity to blocks. [Type] Bug An existing feature does not function as intended and removed Backport to WP 6.9 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta labels Oct 22, 2025
@luisherranz luisherranz merged commit 8fd4c71 into WordPress:trunk Oct 22, 2025
45 of 47 checks passed
@github-actions github-actions bot added this to the Gutenberg 22.0 milestone Oct 22, 2025
priethor pushed a commit that referenced this pull request Oct 22, 2025
…ttribute to script module (#72489)

* Reuse wp_script_attributes filter for adding data-wp-router-options attribute to script module

* Use wp_json_encode()

* Make gutenberg_script_module_add_router_options_attributes() more concise

* Improve phpdoc return tags

* Remove compat/wordpress-6.9/script-modules.php

* Update backport changelog

* (WIP) Use render_block_data to filter all blocks

co-authored: @samueljseay

* Adjust manual registration of blocks in e2e tests

* We should not need any manual registration now.

* Update changelog to match new wordpress-develop PR.

* Fix merge conflict mistake

* Remove redundant white space change

* Fix merge conflict mistake

* Move conditional code to compat, check for existence of new method when re-registering existing core blocks

* Fix typo in code comment.

Co-authored-by: Luis Herranz <luisherranz@gmail.com>

* Simplify the compatibility check.

---------

Co-authored-by: westonruter <westonruter@git.wordpress.org>
Co-authored-by: luisherranz <luisherranz@git.wordpress.org>
Co-authored-by: samueljseay <samueljseay@git.wordpress.org>
Co-authored-by: DAreRodz <darerodz@git.wordpress.org>
@priethor
Copy link
Contributor

I just cherry-picked this PR to the release/21.9 branch to get it included in the next release: 0926b18

@priethor priethor removed the Backport to Gutenberg RC Pull request that needs to be backported to a Gutenberg release candidate (RC) label Oct 22, 2025
ellatrix pushed a commit that referenced this pull request Nov 20, 2025
…ttribute to script module (#72489)

* Reuse wp_script_attributes filter for adding data-wp-router-options attribute to script module

* Use wp_json_encode()

* Make gutenberg_script_module_add_router_options_attributes() more concise

* Improve phpdoc return tags

* Remove compat/wordpress-6.9/script-modules.php

* Update backport changelog

* (WIP) Use render_block_data to filter all blocks

co-authored: @samueljseay

* Adjust manual registration of blocks in e2e tests

* We should not need any manual registration now.

* Update changelog to match new wordpress-develop PR.

* Fix merge conflict mistake

* Remove redundant white space change

* Fix merge conflict mistake

* Move conditional code to compat, check for existence of new method when re-registering existing core blocks

* Fix typo in code comment.

Co-authored-by: Luis Herranz <luisherranz@gmail.com>

* Simplify the compatibility check.

---------

Co-authored-by: westonruter <westonruter@git.wordpress.org>
Co-authored-by: luisherranz <luisherranz@git.wordpress.org>
Co-authored-by: samueljseay <samueljseay@git.wordpress.org>
Co-authored-by: DAreRodz <darerodz@git.wordpress.org>
@ellatrix
Copy link
Member

Even though these are PHP changes only, and already back ported in core, we should still backport it to the 6.9 branch in Gutenberg so that the test plugin is updated and the e2e tests run fine without GB active.

@ellatrix ellatrix added the Backport to WP 6.9 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta label Nov 24, 2025
@t-hamano t-hamano added Backport to WP 6.9 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta and removed Backport to WP 6.9 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta labels Nov 24, 2025
@t-hamano
Copy link
Contributor

For some reason, the automatic cherry-pick to the 6.9 branch isn't happening. I'll try re-adding the label.

@t-hamano
Copy link
Contributor

For some reason, the automatic cherry-pick to the 6.9 branch isn't happening. I'll try re-adding the label.

Unfortunately, the CI failed. Maybe it doesn't work for PRs submitted from forked repositories. I'd like to manually create a PR for the backport.

t-hamano pushed a commit that referenced this pull request Nov 24, 2025
…ttribute to script module (#72489)

* Reuse wp_script_attributes filter for adding data-wp-router-options attribute to script module

* Use wp_json_encode()

* Make gutenberg_script_module_add_router_options_attributes() more concise

* Improve phpdoc return tags

* Remove compat/wordpress-6.9/script-modules.php

* Update backport changelog

* (WIP) Use render_block_data to filter all blocks

co-authored: @samueljseay

* Adjust manual registration of blocks in e2e tests

* We should not need any manual registration now.

* Update changelog to match new wordpress-develop PR.

* Fix merge conflict mistake

* Remove redundant white space change

* Fix merge conflict mistake

* Move conditional code to compat, check for existence of new method when re-registering existing core blocks

* Fix typo in code comment.

Co-authored-by: Luis Herranz <luisherranz@gmail.com>

* Simplify the compatibility check.

---------

Co-authored-by: westonruter <westonruter@git.wordpress.org>
Co-authored-by: luisherranz <luisherranz@git.wordpress.org>
Co-authored-by: samueljseay <samueljseay@git.wordpress.org>
Co-authored-by: DAreRodz <darerodz@git.wordpress.org>
t-hamano pushed a commit that referenced this pull request Nov 24, 2025
…ttribute to script module (#72489)

* Reuse wp_script_attributes filter for adding data-wp-router-options attribute to script module

* Use wp_json_encode()

* Make gutenberg_script_module_add_router_options_attributes() more concise

* Improve phpdoc return tags

* Remove compat/wordpress-6.9/script-modules.php

* Update backport changelog

* (WIP) Use render_block_data to filter all blocks

co-authored: @samueljseay

* Adjust manual registration of blocks in e2e tests

* We should not need any manual registration now.

* Update changelog to match new wordpress-develop PR.

* Fix merge conflict mistake

* Remove redundant white space change

* Fix merge conflict mistake

* Move conditional code to compat, check for existence of new method when re-registering existing core blocks

* Fix typo in code comment.

Co-authored-by: Luis Herranz <luisherranz@gmail.com>

* Simplify the compatibility check.

---------

Co-authored-by: westonruter <westonruter@git.wordpress.org>
Co-authored-by: luisherranz <luisherranz@git.wordpress.org>
Co-authored-by: samueljseay <samueljseay@git.wordpress.org>
Co-authored-by: DAreRodz <darerodz@git.wordpress.org>
t-hamano added a commit that referenced this pull request Nov 24, 2025
…ttribute to script module (#72489) (#73512)

* Reuse wp_script_attributes filter for adding data-wp-router-options attribute to script module

* Use wp_json_encode()

* Make gutenberg_script_module_add_router_options_attributes() more concise

* Improve phpdoc return tags

* Remove compat/wordpress-6.9/script-modules.php

* Update backport changelog

* (WIP) Use render_block_data to filter all blocks

co-authored: @samueljseay

* Adjust manual registration of blocks in e2e tests

* We should not need any manual registration now.

* Update changelog to match new wordpress-develop PR.

* Fix merge conflict mistake

* Remove redundant white space change

* Fix merge conflict mistake

* Move conditional code to compat, check for existence of new method when re-registering existing core blocks

* Fix typo in code comment.



* Simplify the compatibility check.

---------

Co-authored-by: westonruter <westonruter@git.wordpress.org>
Co-authored-by: luisherranz <luisherranz@git.wordpress.org>
Co-authored-by: samueljseay <samueljseay@git.wordpress.org>
Co-authored-by: DAreRodz <darerodz@git.wordpress.org>
Co-authored-by: ellatrix <ellatrix@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
@t-hamano
Copy link
Contributor

This PR was manually backported into the wp/6.9 branch by #73512.

@t-hamano t-hamano added Backported to WP Core Pull request that has been successfully merged into WP Core and removed Backport to WP 6.9 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta labels Nov 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Backported to WP Core Pull request that has been successfully merged into WP Core [Feature] Interactivity API API to add frontend interactivity to blocks. [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

iAPI Router: Only load modules of interactive blocks on client-side navigation

6 participants