-
Notifications
You must be signed in to change notification settings - Fork 95
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
thelovekesh
wants to merge
6
commits into
trunk
Choose a base branch
from
update/wwo-script-strategy
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8ae650b
Update wwo script loading strategy
thelovekesh 1a0d378
Add function to add web-worker-offloading script as dep if worker is …
thelovekesh 32c938f
Update test cases for updating wwo script type
thelovekesh 1ff2525
Update function name for updating worker data scripts deps
thelovekesh a9cf46c
Update covers tag
thelovekesh a75c062
Fix spacing
thelovekesh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
@@ -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 ); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 awp_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.