Skip to content
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

Update strategy for adding script[type="text/partytown"] to worker scripts #1497

Merged
merged 14 commits into from
Sep 27, 2024

Conversation

thelovekesh
Copy link
Member

@thelovekesh thelovekesh commented Aug 25, 2024

Summary

Fixes #1468

Relevant technical choices

  • Update logic to use wp_script_add_data( $handle, 'worker', true ) to identify worker offloading scripts instead of identifying them by web-worker-offloading script dependency.
  • Register web-worker-offloading script during wp_default_scripts instead of wp_enqueue_scripts so that it can be used in the admin and to more closely align with how scripts are registered in core.
  • Use the print_scripts_array filter to prepend the web-worker-offloading script to the list of scripts being printed if one is offloaded to a worker. This is more performant than adding a script dependency at the print_scripts_array action since it only needs to iterate over the queued script handles as opposed to iterating over all registered scripts.

@thelovekesh thelovekesh added [Type] Enhancement A suggestion for improvement of an existing feature [Plugin] Web Worker Offloading Issues for the Web Worker Offloading plugin. labels Aug 25, 2024
@thelovekesh thelovekesh added the no milestone PRs that do not have a defined milestone for release label Aug 25, 2024
Copy link

github-actions bot commented Aug 25, 2024

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: thelovekesh <[email protected]>
Co-authored-by: westonruter <[email protected]>
Co-authored-by: adamsilverstein <[email protected]>

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

Comment on lines 85 to 86
wp_scripts()->registered[ $dep->handle ]->deps[] = 'web-worker-offloading';

Copy link
Member

Choose a reason for hiding this comment

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

I don't think we technically have to do this because when a script is made into a text/partytown script, it won't by definition do anything until the Partytown script has been loaded on the page. So the Partytown script could be located anywhere (and ideally it should always be in the footer!) But I suppose the benefit of doing this way is it would get added automatically. Still I think there could be a wp_print_footer_scripts action that checks if any of the enqueued scripts require WWO, and if so, it could then ultimately print the Partytown script in the footer.

Suggested change
wp_scripts()->registered[ $dep->handle ]->deps[] = 'web-worker-offloading';

Copy link
Member

Choose a reason for hiding this comment

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

I've refactored this in 0c2d7b2 to use the print_scripts_array filter to inject the web-worker-offloading script on demand. This has slight performance benefits as it doesn't require looping over all registered scripts and can instead just look at the scripts that have been enqueued for printing.

Comment on lines 87 to 90
if ( false === wp_scripts()->get_data( $dep->handle, 'strategy' ) ) {
wp_script_add_data( $dep->handle, 'strategy', 'async' ); // The 'defer' strategy would work as well.
wp_script_add_data( $dep->handle, 'wwo_strategy_added', true );
}
Copy link
Member

Choose a reason for hiding this comment

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

We may need to end up removing this logic if we are going to rely on inline after scripts also being made Partytown scripts as mentioned in #1455. But that can be done in another PR.

Copy link
Member

Choose a reason for hiding this comment

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

I'll address this in another PR.

}
add_action( 'wp_enqueue_scripts', 'wwo_init' );
add_action( 'wp_default_scripts', 'wwo_register_default_scripts' );
Copy link
Member

Choose a reason for hiding this comment

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

Nice!

*
* @since 0.1.0
*
* @param string[]|mixed $script_handles Array of script handles.
* @return string[] Array of script handles.
* @param string[]|mixed $to_do An array of enqueued script dependency handles.
Copy link
Member

Choose a reason for hiding this comment

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

why is $script_handles changed to $to_do?

Copy link
Member

Choose a reason for hiding this comment

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

Ah, because this is the name of the param passed to print_scripts_array. But I can change it back.

Copy link
Member

Choose a reason for hiding this comment

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

Reverted in bfb9e11

Copy link
Member

@adamsilverstein adamsilverstein left a comment

Choose a reason for hiding this comment

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

Looks great, thanks for working on this! One small question.

@westonruter westonruter merged commit 34c6297 into trunk Sep 27, 2024
14 of 15 checks passed
@westonruter westonruter deleted the update/wwo-script-strategy branch September 27, 2024 17:31
@thelovekesh
Copy link
Member Author

Thanks @westonruter for taking this to finish line ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no milestone PRs that do not have a defined milestone for release [Plugin] Web Worker Offloading Issues for the Web Worker Offloading plugin. [Type] Enhancement A suggestion for improvement of an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Reconsider approach of using script dependencies to opt-in a script for worker offloading
3 participants