-
Notifications
You must be signed in to change notification settings - Fork 835
jetpack-mu-wpcom: Code Block / Improve safety when modifying modules actions #45357
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Conversation
Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.
Interested in more tips and information?
|
Thank you for your PR! When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:
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:
If you have questions about anything, reach out in #jetpack-developers for guidance! |
Code Coverage SummaryCoverage changed in 1 file.
|
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.
ed3b4cb
to
f799e20
Compare
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. |
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' ) ) ) { |
There was a problem hiding this comment.
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.
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:
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:
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).