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

Add partytown support to run scripts in Worker Thread #271

Closed

Conversation

thelovekesh
Copy link
Member

@thelovekesh thelovekesh commented Mar 28, 2022

Summary

Fixes #176

Relevant technical choices

  • Add Web Worker module which allows running scripts within worker thread with the help of Partytown library.
  • Add the Partytown package in dependencies
  • Add npm script to copy Partytown lib files to module assets/js folder.
    • Currently, Partytown lib files are committed to the module.
    • We can also set up webpack to copy partytown js files in the module build folder when creating the release of the plugin.
    • Partytown library needs to be updated when a new version is released.
  • Add tests for the Web Worker Module.
  • Add partytown_configuration filter which can be used to update partytown configurations.

Screencast

1.mp4

Checklist

  • PR has either [Focus] or Infrastructure label.
  • PR has a [Type] label.
  • PR has a milestone or the no milestone label.

@mxbclang
Copy link
Contributor

@thelovekesh Can you please add [Focus] and [Type] labels, as well as a no milestone label? These are required for all PRs. Thanks!

@thelovekesh
Copy link
Member Author

@bethanylang I have updated the PR description but I don't have access to add the label in PR. Can you please do that for me?

@mxbclang mxbclang added [Type] Enhancement A suggestion for improvement of an existing feature Infrastructure Issues for the overall performance plugin infrastructure no milestone PRs that do not have a defined milestone for release labels Mar 29, 2022
@mxbclang mxbclang added [Focus] JavaScript and removed Infrastructure Issues for the overall performance plugin infrastructure labels Mar 29, 2022
);
}
}
add_action( 'wp_print_scripts', 'web_worker_partytown_worker_scripts' );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of wp_print_scripts, this should perhaps do wp_head. Otherwise, It can run multiple times as found in ampproject/amp-wp#6643.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe this should be refactored to have a script_loader_tag filter added at the top level.

@thelovekesh
Copy link
Member Author

@westonruter What if we consider this feature to be integrated into the PWA plugin instead of this one? By both means, this feature can be suggested to be merged in Core but from the PWA plugin, it will be more tested with PWA use cases as well. What do you think?

@westonruter
Copy link
Member

I think it makes sense as part of the Performance plugin, especially since the scope doesn't conflict.

@aristath
Copy link
Member

The goal of the performance plugin is to serve as a feature plugin for things that will - eventually (hopefully) - land in WP Core.
I love the idea of partytown, but at the same time, I'm not sure others would be as enthusiastic to add it to WP-Core... It's a new project, still in beta, relatively unproven. WordPress on the other hand avoids adding 3rd-party scripts unless there's no way around it, and usually prefers more "established" solutions because of its popularity & wide reach.

If the chances of something like this landing in core are slim because of WP's established attitude towards implementations like this, then perhaps there are alternatives we can explore?

@luisherranz
Copy link
Member

luisherranz commented Mar 30, 2022

I don't think that is something we need to decide now.

How Partytown will mature is hard to predict. Right now, it's just promising, but it is already recommended in Google's optimizing third-party scripts guide.

Having it here as a module means it will be exposed to more people than with a standalone plugin, and it will help us understand the best way to integrate it with WordPress. And one could even argue that the other way around is also true: people looking for Partytown in WordPress will find and install this plugin, and the rest of the modules will get more exposure and testing.

Over time, we can decide if the best approach is to use an external plugin or a pre-registered dependency in core.

modules/javascript/web-worker/load.php Outdated Show resolved Hide resolved
modules/javascript/web-worker/load.php Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
tests/modules/javascript/web-worker/web-worker-test.php Outdated Show resolved Hide resolved
tests/modules/javascript/web-worker/web-worker-test.php Outdated Show resolved Hide resolved
tests/modules/javascript/web-worker/web-worker-test.php Outdated Show resolved Hide resolved
Copy link
Member

@luisherranz luisherranz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been testing this and everything seems to work great. Great work @thelovekesh 🙂

Maybe in the future, we can add a debug toggle in the settings, but it can easily be activated using the configuration filter, so not a problem.

function partytown_debug_mode ( $config ) {
	$config["debug"] = true;
	return $config;
}
add_filter( 'partytown_configuration', 'partytown_debug_mode' );

module-i18n.php Outdated Show resolved Hide resolved
modules/javascript/web-worker/load.php Outdated Show resolved Hide resolved
@luisherranz
Copy link
Member

@thelovekesh, any progress here? Are you planning on finishing this PR? 🙂

@thelovekesh
Copy link
Member Author

@luisherranz yes and it's on my todo. Based on the above discussion I don't have much work to do on this PR. Once it got some reviews and ideas I will make changes accordingly.

@luisherranz
Copy link
Member

Awesome. Thanks @thelovekesh!

@erip2 erip2 mentioned this pull request Sep 12, 2022
function( $tag, $handle, $src ) use ( $partytown_handle ) {
if ( $handle === $partytown_handle ) {
$create_script_tag = sprintf(
'<script type="text/partytown" src="%1s" id="%2s"></script>',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why create a script tag from scratch? this could loose other attributes added by other filters. Instead can we replace (or set) just the type? Is changing the ID required/expected?

Copy link
Member

@adamsilverstein adamsilverstein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall this looks really good! I left some code feedback for things that could be improved.

Some questions:

  • are there any scripts loaded by core or core themes that are candidates for partytown loading?
  • how would this get used, do we expect plugin authors to add an integration when the Performance Lab plugin is active, or do we want to try to build some integrations for top plugins where we know this could be helpful?
  • how does this affect performance? it would be great to run some lab tests with some common candidates like analytics to measure the impact of this change.

@thelovekesh thelovekesh force-pushed the feature/add-partytown-module branch from efb9b70 to 55213ba Compare October 13, 2022 07:00
@thelovekesh thelovekesh mentioned this pull request Oct 13, 2022
3 tasks
@thelovekesh
Copy link
Member Author

Closed accidently due to commits dropped from the patch branch. I have opened a new one here - #556

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no milestone PRs that do not have a defined milestone for release [Type] Enhancement A suggestion for improvement of an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Partytown support
7 participants