-
Notifications
You must be signed in to change notification settings - Fork 99
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
Conversation
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 If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
wp_scripts()->registered[ $dep->handle ]->deps[] = 'web-worker-offloading'; | ||
|
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 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.
wp_scripts()->registered[ $dep->handle ]->deps[] = 'web-worker-offloading'; |
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'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.
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 ); | ||
} |
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.
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.
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'll address this in another PR.
… update/wwo-script-strategy
} | ||
add_action( 'wp_enqueue_scripts', 'wwo_init' ); | ||
add_action( 'wp_default_scripts', 'wwo_register_default_scripts' ); |
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.
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. |
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.
why is $script_handles
changed to $to_do
?
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.
Ah, because this is the name of the param passed to print_scripts_array
. But I can change it back.
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.
Reverted in bfb9e11
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.
Looks great, thanks for working on this! One small question.
Thanks @westonruter for taking this to finish line ❤️ |
Summary
Fixes #1468
Relevant technical choices
wp_script_add_data( $handle, 'worker', true )
to identify worker offloading scripts instead of identifying them byweb-worker-offloading
script dependency.web-worker-offloading
script duringwp_default_scripts
instead ofwp_enqueue_scripts
so that it can be used in the admin and to more closely align with how scripts are registered in core.print_scripts_array
filter to prepend theweb-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 theprint_scripts_array
action since it only needs to iterate over the queued script handles as opposed to iterating over all registered scripts.