Skip to content

Commit

Permalink
Update offloading script handle from partytown to web-worker-offloader
Browse files Browse the repository at this point in the history
  • Loading branch information
thelovekesh committed May 31, 2024
1 parent 253f079 commit c06df49
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions plugins/web-worker-offloading/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
}

/**
* PartyTown Configuration
* Configuration for Web Worker Offloading.
*
* @since n.e.x.t
* @see https://partytown.builder.io/configuration
Expand All @@ -27,7 +27,7 @@ function wwo_configuration(): array {
);

/**
* Add configuration for PartyTown.
* Add configuration for Web Worker Offloading.
*
* @since n.e.x.t
* @see <https://partytown.builder.io/configuration>.
Expand All @@ -50,15 +50,15 @@ function wwo_init(): void {
}

wp_register_script(
'partytown',
'web-worker-offloader',
'',
array(),
WEB_WORKER_OFFLOADING_VERSION,
array( 'in_footer' => false )
);

wp_add_inline_script(
'partytown',
'web-worker-offloader',
sprintf(
'window.partytown = %s;',
wp_json_encode( wwo_configuration() )
Expand All @@ -67,43 +67,43 @@ function wwo_init(): void {
);

wp_add_inline_script(
'partytown',
'web-worker-offloader',
$partytown_js,
'after'
);
}
add_action( 'wp_enqueue_scripts', 'wwo_init' );

/**
* Helper function to get all scripts tags which has `partytown` dependency.
* Helper function to get all scripts tags which has `web-worker-offloader` dependency.
*
* @since n.e.x.t
*
* @return string[] Array of script handles.
*/
function wwo_get_partytown_handles(): array {
function wwo_get_web_worker_offloader_handles(): array {
/**
* Array of script handles which has `partytown` dependency.
* Array of script handles which has `web-worker-offloader` dependency.
*
* @var string[]
*/
static $partytown_handles = array();
static $web_worker_offloader_handles = array();

if ( ! empty( $partytown_handles ) ) {
return $partytown_handles;
if ( ! empty( $web_worker_offloader_handles ) ) {
return $web_worker_offloader_handles;
}

foreach ( wp_scripts()->registered as $handle => $script ) {
if ( ! empty( $script->deps ) && in_array( 'partytown', $script->deps, true ) ) {
$partytown_handles[] = $handle;
if ( ! empty( $script->deps ) && in_array( 'web-worker-offloader', $script->deps, true ) ) {
$web_worker_offloader_handles[] = $handle;
}
}

return $partytown_handles;
return $web_worker_offloader_handles;
}

/**
* Mark scripts with `partytown` dependency as async.
* Mark scripts with `web-worker-offloader` dependency as async.
*
* Why this is needed?
*
Expand All @@ -118,9 +118,9 @@ function wwo_get_partytown_handles(): array {
* @return string[] Array of script handles.
*/
function wwo_update_script_strategy( array $script_handles ): array {
$partytown_handles = wwo_get_partytown_handles();
$web_worker_offloader_handles = wwo_get_web_worker_offloader_handles();

foreach ( array_intersect( $script_handles, $partytown_handles ) as $handle ) {
foreach ( array_intersect( $script_handles, $web_worker_offloader_handles ) as $handle ) {
wp_script_add_data( $handle, 'strategy', 'async' );
}

Expand All @@ -129,19 +129,19 @@ function wwo_update_script_strategy( array $script_handles ): array {
add_filter( 'print_scripts_array', 'wwo_update_script_strategy', 10, 1 );

/**
* Update script type for handles having `partytown` as dependency.
* Update script type for handles having `web-worker-offloader` as dependency.
*
* @since n.e.x.t
*
* @param string $tag Script tag.
* @param string $handle Script handle.
*
* @return string $tag Script tag with type="text/partytown".
* @return string $tag Script tag with type="text/partytown" for eligible scripts.
*/
function wwo_update_script_type( string $tag, string $handle ): string {
$partytown_handles = wwo_get_partytown_handles();
$web_worker_offloader_handles = wwo_get_web_worker_offloader_handles();

if ( in_array( $handle, $partytown_handles, true ) ) {
if ( in_array( $handle, $web_worker_offloader_handles, true ) ) {
$html_processor = new WP_HTML_Tag_Processor( $tag );

while ( $html_processor->next_tag( array( 'tag_name' => 'SCRIPT' ) ) ) {
Expand Down

0 comments on commit c06df49

Please sign in to comment.