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

Open
wants to merge 6 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 15 additions & 17 deletions plugins/web-worker-offloading/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,31 +68,30 @@ function wwo_init(): void {
add_action( 'wp_enqueue_scripts', 'wwo_init' );

/**
* Marks scripts with `web-worker-offloading` dependency as async.
* Adds `web-worker-offloading` as dependency to scripts with `worker` data. 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 0.1.0
*
* @param string[]|mixed $script_handles Array of script handles.
* @return string[] Array of script handles.
* @since n.e.x.t
*/
function wwo_update_script_strategy( $script_handles ): array {
$script_handles = array_intersect( (array) $script_handles, array_keys( wp_scripts()->registered ) );
foreach ( $script_handles as $handle ) {
if ( in_array( 'web-worker-offloading', wp_scripts()->registered[ $handle ]->deps, true ) ) {
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 );
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';

Comment on lines +85 to +86
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';

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 );
}
Comment on lines +87 to 90
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.

}
}

return $script_handles;
}
add_filter( 'print_scripts_array', 'wwo_update_script_strategy' );
add_action( 'wp_print_scripts', 'wwo_update_worker_scripts_deps_and_strategy' );

/**
* Updates script type for handles having `web-worker-offloading` as dependency.
Expand All @@ -106,8 +105,7 @@ function wwo_update_script_strategy( $script_handles ): array {
function wwo_update_script_type( $tag, string $handle ) {
if (
is_string( $tag ) &&
array_key_exists( $handle, wp_scripts()->registered ) &&
in_array( 'web-worker-offloading', wp_scripts()->registered[ $handle ]->deps, true )
(bool) wp_scripts()->get_data( $handle, 'worker' )
) {
$html_processor = new WP_HTML_Tag_Processor( $tag );

Expand Down
26 changes: 17 additions & 9 deletions plugins/web-worker-offloading/tests/test-web-worker-offloading.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,64 +107,72 @@ public static function data_update_script_types(): array {
),
'add-script-for-web-worker-offloading' => array(
'set_up' => static function (): void {
wp_enqueue_script( 'foo', 'https://example.com/foo.js', array( 'web-worker-offloading' ), '1.0.0', true );
wp_enqueue_script( 'foo', 'https://example.com/foo.js', array(), '1.0.0', true );
wp_script_add_data( 'foo', 'worker', true );
},
'expected' => '{{ wwo_config }}{{ wwo_inline_script }}<script type="text/partytown" src="https://example.com/foo.js?ver=1.0.0" id="foo-js" ></script>',
'doing_it_wrong' => false,
),
'add-defer-script-for-web-worker-offloading' => array(
'set_up' => static function (): void {
wp_enqueue_script( 'foo', 'https://example.com/foo.js', array( 'web-worker-offloading' ), '1.0.0', array( 'strategy' => 'defer' ) );
wp_enqueue_script( 'foo', 'https://example.com/foo.js', array(), '1.0.0', array( 'strategy' => 'defer' ) );
wp_script_add_data( 'foo', 'worker', true );
},
'expected' => '{{ wwo_config }}{{ wwo_inline_script }}<script type="text/partytown" src="https://example.com/foo.js?ver=1.0.0" id="foo-js" defer data-wp-strategy="defer"></script>',
'doing_it_wrong' => false,
),
'add-async-script-for-web-worker-offloading' => array(
'set_up' => static function (): void {
wp_enqueue_script( 'foo', 'https://example.com/foo.js', array( 'web-worker-offloading' ), '1.0.0', array( 'strategy' => 'async' ) );
wp_enqueue_script( 'foo', 'https://example.com/foo.js', array(), '1.0.0', array( 'strategy' => 'async' ) );
wp_script_add_data( 'foo', 'worker', true );
},
'expected' => '{{ wwo_config }}{{ wwo_inline_script }}<script type="text/partytown" src="https://example.com/foo.js?ver=1.0.0" id="foo-js" async data-wp-strategy="async"></script>',
'doing_it_wrong' => false,
),
'add-script-for-web-worker-offloading-with-before-data' => array(
'set_up' => static function (): void {
wp_enqueue_script( 'foo', 'https://example.com/foo.js', array( 'web-worker-offloading' ), '1.0.0', true );
wp_enqueue_script( 'foo', 'https://example.com/foo.js', array(), '1.0.0', true );
wp_add_inline_script( 'foo', 'console.log("Hello, World!");', 'before' );
wp_script_add_data( 'foo', 'worker', true );
},
'expected' => '{{ wwo_config }}{{ wwo_inline_script }}<script id="foo-js-before">console.log("Hello, World!");</script><script type="text/partytown" src="https://example.com/foo.js?ver=1.0.0" id="foo-js" ></script>',
'doing_it_wrong' => false,
),
'add-script-for-web-worker-offloading-with-after-data' => array(
'set_up' => static function (): void {
wp_enqueue_script( 'foo', 'https://example.com/foo.js', array( 'web-worker-offloading' ), '1.0.0', true );
wp_enqueue_script( 'foo', 'https://example.com/foo.js', array(), '1.0.0', true );
wp_add_inline_script( 'foo', 'console.log("Hello, World!");', 'after' );
wp_script_add_data( 'foo', 'worker', true );
},
'expected' => '{{ wwo_config }}{{ wwo_inline_script }}<script src="https://example.com/foo.js?ver=1.0.0" id="foo-js" ></script><script id="foo-js-after">console.log("Hello, World!");</script>',
'doing_it_wrong' => true,
),
'add-script-for-web-worker-offloading-with-before-and-after-data' => array(
'set_up' => static function (): void {
wp_enqueue_script( 'foo', 'https://example.com/foo.js', array( 'web-worker-offloading' ), '1.0.0', true );
wp_enqueue_script( 'foo', 'https://example.com/foo.js', array(), '1.0.0', true );
wp_add_inline_script( 'foo', 'console.log("Hello, World!");', 'before' );
wp_add_inline_script( 'foo', 'console.log("Hello, World!");', 'after' );
wp_script_add_data( 'foo', 'worker', true );
},
'expected' => '{{ wwo_config }}{{ wwo_inline_script }}<script id="foo-js-before">console.log("Hello, World!");</script><script src="https://example.com/foo.js?ver=1.0.0" id="foo-js" ></script><script id="foo-js-after">console.log("Hello, World!");</script>',
'doing_it_wrong' => true,
),
'add-async-script-for-web-worker-offloading-with-before-and-after-data' => array(
'set_up' => static function (): void {
wp_enqueue_script( 'foo', 'https://example.com/foo.js', array( 'web-worker-offloading' ), '1.0.0', array( 'strategy' => 'async' ) );
wp_enqueue_script( 'foo', 'https://example.com/foo.js', array(), '1.0.0', array( 'strategy' => 'async' ) );
wp_add_inline_script( 'foo', 'console.log("Hello, World!");', 'before' );
wp_add_inline_script( 'foo', 'console.log("Hello, World!");', 'after' );
wp_script_add_data( 'foo', 'worker', true );
},
'expected' => '{{ wwo_config }}{{ wwo_inline_script }}<script id="foo-js-before">console.log("Hello, World!");</script><script src="https://example.com/foo.js?ver=1.0.0" id="foo-js" data-wp-strategy="async"></script><script id="foo-js-after">console.log("Hello, World!");</script>',
'doing_it_wrong' => true,
),
'add-defer-script-for-web-worker-offloading-with-before-and-after-data' => array(
'set_up' => static function (): void {
wp_enqueue_script( 'foo', 'https://example.com/foo.js', array( 'web-worker-offloading' ), '1.0.0', array( 'strategy' => 'defer' ) );
wp_enqueue_script( 'foo', 'https://example.com/foo.js', array(), '1.0.0', array( 'strategy' => 'defer' ) );
wp_add_inline_script( 'foo', 'console.log("Hello, World!");', 'before' );
wp_add_inline_script( 'foo', 'console.log("Hello, World!");', 'after' );
wp_script_add_data( 'foo', 'worker', true );
},
'expected' => '{{ wwo_config }}{{ wwo_inline_script }}<script id="foo-js-before">console.log("Hello, World!");</script><script src="https://example.com/foo.js?ver=1.0.0" id="foo-js" data-wp-strategy="defer"></script><script id="foo-js-after">console.log("Hello, World!");</script>',
'doing_it_wrong' => true,
Expand All @@ -176,7 +184,7 @@ public static function data_update_script_types(): array {
* Test `wwo_update_script_type`.
*
* @covers ::wwo_update_script_type
* @covers ::wwo_update_script_strategy
* @covers ::wwo_update_worker_scripts_deps_and_strategy
*
* @dataProvider data_update_script_types
*
Expand Down