-
Notifications
You must be signed in to change notification settings - Fork 138
Update strategy for adding script[type="text/partytown"] to worker scripts
#1497
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
Changes from 1 commit
8ae650b
1a0d378
32c938f
1ff2525
a9cf46c
a75c062
56d4751
6c255e7
34405c2
0c2d7b2
6fb6553
bc843d5
9892937
bfb9e11
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -70,30 +70,35 @@ function wwo_register_default_scripts( WP_Scripts $scripts ): void { | |
| add_action( 'wp_default_scripts', 'wwo_register_default_scripts' ); | ||
|
|
||
| /** | ||
| * Adds `web-worker-offloading` as dependency to scripts with `worker` data. Also, marks their strategy as `async`. | ||
| * Prepends web-worker-offloading to the list of scripts to print if one of the queued scripts is offloaded to a worker. | ||
| * | ||
| * This is needed because scripts offloaded to a worker thread can be considered async. However, they may include `before` and `after` inline | ||
| * scripts that need sequential execution. Once marked as async, `filter_eligible_strategies()` determines if the | ||
| * script is eligible for async execution. If so, it will be offloaded to the worker thread. | ||
| * This also marks their strategy as `async`. This is needed because scripts offloaded to a worker thread can be | ||
| * considered async. However, they may include `before` and `after` inline scripts that need sequential execution. Once | ||
| * marked as async, `filter_eligible_strategies()` determines if the script is eligible for async execution. If so, it | ||
| * will be offloaded to the worker thread. | ||
| * | ||
| * @since n.e.x.t | ||
| * @since 0.1.0 | ||
| * | ||
| * @param string[]|mixed $to_do An array of enqueued script dependency handles. | ||
|
||
| * @return string[] Script handles. | ||
| */ | ||
| function wwo_update_worker_scripts_deps_and_strategy(): void { | ||
| foreach ( wp_scripts()->registered as $dep ) { | ||
| if ( | ||
| (bool) wp_scripts()->get_data( $dep->handle, 'worker' ) && | ||
| ! in_array( 'web-worker-offloading', wp_scripts()->registered[ $dep->handle ]->deps, true ) | ||
| ) { | ||
| wp_scripts()->registered[ $dep->handle ]->deps[] = 'web-worker-offloading'; | ||
| function wwo_filter_print_scripts_array( $to_do ): array { | ||
| $scripts = wp_scripts(); | ||
| foreach ( (array) $to_do as $handle ) { | ||
| if ( true === $scripts->get_data( $handle, 'worker' ) ) { | ||
| $scripts->set_group( 'web-worker-offloading', false, 0 ); // Try to print in the head. | ||
| array_unshift( $to_do, 'web-worker-offloading' ); | ||
|
|
||
| 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 ); | ||
| // TODO: This should be reconsidered because scripts needing to be offloaded will often have after scripts. See <https://github.com/WordPress/performance/pull/1497/files#r1733538721>. | ||
| if ( false === wp_scripts()->get_data( $handle, 'strategy' ) ) { | ||
| wp_script_add_data( $handle, 'strategy', 'async' ); // The 'defer' strategy would work as well. | ||
| wp_script_add_data( $handle, 'wwo_strategy_added', true ); | ||
| } | ||
| } | ||
| } | ||
| return $to_do; | ||
| } | ||
| add_action( 'wp_print_scripts', 'wwo_update_worker_scripts_deps_and_strategy' ); | ||
| add_filter( 'print_scripts_array', 'wwo_filter_print_scripts_array' ); | ||
|
|
||
| /** | ||
| * Updates script type for handles having `web-worker-offloading` as dependency. | ||
|
|
||
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!