Skip to content

Conversation

sirreal
Copy link
Member

@sirreal sirreal commented Oct 2, 2025

Proposed changes:

The Code block modifies the script modules actions in order to insert its own action between some of the Core actions.

This change attempts to abort and restore the Core actions in case they cannot be removed as expected.

Follow-up to #45181

The block enqueues an empty "dummy" module in order to add modules to the importmap. Without more invasive tools like Reflection, this method works. In order to avoid actually printing the empty module script tag and requesting the empty JavaScript file, an action is inserted to dequeue and re-enqueue the empty module at appropriate times. This requires moving the script modules printing actions so that the module can be dequeued and enqueued in the right order.

If, for some reason, there's a problem removing the existing actions, this process should abort and attempt to restore the previous behavior. This will result in the empty module being requested, which is harmless.

Other information:

  • Have you written new tests for your changes, if applicable?
  • Have you checked the E2E test CI results, and verified that your changes do not break them?
  • Have you tested your changes on WordPress.com, if applicable (if so, you'll see a generated comment below with a script to run)?

Jetpack product discussion

Does this pull request change what data or activity we track or use?

No.

Testing instructions:

The Core modules actions can be moved like this:

add_action(
	'after_setup_theme',
	function () {
		foreach ( array( 'wp_head', 'wp_footer', 'admin_print_footer_scripts' ) as $hook ) {
				remove_action( $hook, array( wp_script_modules(), 'print_import_map' ) );
				remove_action( $hook, array( wp_script_modules(), 'print_enqueued_script_modules' ) );
				remove_action( $hook, array( wp_script_modules(), 'print_script_module_preloads' ) );
				add_action( $hook, array( wp_script_modules(), 'print_import_map' ), 8 );
				add_action( $hook, array( wp_script_modules(), 'print_enqueued_script_modules' ), 8 );
				add_action( $hook, array( wp_script_modules(), 'print_script_module_preloads' ), 8 );
		}
	},
	80
);

If this is done on trunk, the core modules actions fail to be removed correctly so run twice. The result is duplicate script tags and preloads. This PR fixes that so that there are no duplicate tags. In both cases, the empty module script tag is printed (harmless).

Copy link
Contributor

github-actions bot commented Oct 2, 2025

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WoA dev site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin (WordPress.com Site Helper), and enable the wpcom-code-block/improve-actions-safety branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack-mu-wpcom-plugin wpcom-code-block/improve-actions-safety

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

Copy link
Contributor

github-actions bot commented Oct 2, 2025

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add a "[Status]" label (In Progress, Needs Review, ...).
  • ✅ Add a "[Type]" label (Bug, Enhancement, Janitorial, Task).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
  3. You can use GitHub's Reviewers functionality to request a review.
  4. When it's reviewed and merged, you will be pinged in Slack to deploy the changes to WordPress.com simple once the build is done.

If you have questions about anything, reach out in #jetpack-developers for guidance!

@github-actions github-actions bot added the [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. label Oct 2, 2025
Copy link

jp-launch-control bot commented Oct 2, 2025

Code Coverage Summary

Coverage changed in 1 file.

File Coverage Δ% Δ Uncovered
projects/packages/jetpack-mu-wpcom/src/features/wpcom-blocks/code/class-code-block.php 0/192 (0.00%) 0.00% 3 ❤️‍🩹

Full summary · PHP report

The code block modifies the script modules actions in order to provide
an opportunity to dequeue a script module that is not necessary.

This change bails and may restore removed actions in case there's a
problem removing the actions.

This would result in an empty script module being enqueued on the page.
It would be harmless other than adding an unnecessary request.
@sirreal sirreal force-pushed the wpcom-code-block/improve-actions-safety branch from ed3b4cb to f799e20 Compare October 3, 2025 11:21
@sirreal sirreal marked this pull request as ready for review October 3, 2025 11:22
@sirreal sirreal requested a review from a team October 3, 2025 11:22
@sirreal sirreal mentioned this pull request Oct 3, 2025
3 tasks
@dmsnell
Copy link
Member

dmsnell commented Oct 3, 2025

was this a triggered bug in practice? did it occur because of other code messing with those filters?

@sirreal
Copy link
Member Author

sirreal commented Oct 7, 2025

was this a triggered bug in practice? did it occur because of other code messing with those filters?

I was able to contrive a situation where this occurred by messing with the filters. It never happened on a real site, but to my knowledge the Code block with this logic was only only deployed on 3 real sites. It's hard to know whether this would be common or not.

Comment on lines +304 to +307
if ( ! remove_action( $hook, array( wp_script_modules(), 'print_enqueued_script_modules' ) ) ) {
continue;
}
if ( ! remove_action( $hook, array( wp_script_modules(), 'print_script_module_preloads' ) ) ) {
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 need to double check the order that script modules use in Core. I think these conditions should be reversed so that the last action is removed first. Then, if it needs to be restored, it would be put back at the end of the filter list. That potentially helps to preserve the original ordering.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[mu wpcom Feature] Wpcom Blocks [Package] Jetpack mu wpcom WordPress.com Features [Status] In Progress [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. [Type] Task

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants