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 Web Worker Offloading module #556

Conversation

thelovekesh
Copy link
Member

Summary

Fixes #176

Please check #271 for previous conversations.

Relevant technical choices

  • Add Web Worker module which allows running scripts within the worker thread with the help of the Partytown library.
  • Add the Partytown package to the 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.
@thelovekesh thelovekesh changed the title Feature/add partytown module Oct 13, 2022
@thelovekesh thelovekesh changed the title feature(javascript): add partytown module Oct 13, 2022
@mukeshpanchal27 mukeshpanchal27 added [Type] Enhancement A suggestion for improvement of an existing feature [Focus] JavaScript no milestone PRs that do not have a defined milestone for release Needs Review Anything that requires code review labels Oct 18, 2022
add_action( 'wp_print_scripts', 'perflab_web_worker_partytown_worker_scripts' );

/**
* Helper function to get all scripts tags which has `partytown` dependency.
Copy link
Member

Choose a reason for hiding this comment

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

Rather than using a partytown dependency, can we look for scripts that have a partytown-worker strategy (and maybe no dependencies)?

That way partytown could be enabled with:
wp_script_add_data( $handle, 'strategy', 'partytown-worker' );

Copy link
Member Author

Choose a reason for hiding this comment

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

While it's feasible to execute this, my inclination is towards considering partytown to be included in the $deps array as a dependency.

This preference stems from the fact that when a script loads as script[type="text/partytown"], it necessitates the presence of partytown. Thus, referring to it as a direct dependency seems logical.

Additionally, by adhering to the dependency approach, merely registering the partytown script suffices, as WordPress automatically enqueues it if another script depends on it else it will not be enqueued to the front-end.

Copy link
Member

Choose a reason for hiding this comment

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

That makes sense, however I can also see the use case for the wp_script_add_data approach. For example, an optimization plugin could use this to specify it wants a specific script loaded with partytown. I'm not sure you could add a dependency dynamically like that (I have to look).

This is fine as is for now, I will check if maybe we can make it work both ways.

@adamsilverstein
Copy link
Member

@thelovekesh apologies for dropping my attention here: I finally got back to this and left some feedback on the PR; I'd love to help get this over the finish line to land as a module.

@thelovekesh
Copy link
Member Author

Thanks @adamsilverstein. I will try to resolve the feedback and update you once done.

modules/javascript/partytown-web-worker/load.php Outdated Show resolved Hide resolved
modules/javascript/partytown-web-worker/load.php Outdated Show resolved Hide resolved
.github/workflows/deploy-dotorg.yml Outdated Show resolved Hide resolved
modules/javascript/partytown-web-worker/load.php Outdated Show resolved Hide resolved
modules/javascript/partytown-web-worker/load.php Outdated Show resolved Hide resolved
admin/load.php Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
@@ -0,0 +1,139 @@
<?php
/**
* Module Name: Partytown Web Worker
Copy link
Member

Choose a reason for hiding this comment

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

Do we really want to call the module Partytown?

IMHO usage of the Partytown library is just an implementation detail.

Copy link
Member

Choose a reason for hiding this comment

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

Agreed. A less-specific name would be "Web Worker Offloading". I asked Bard for some alternative names, and it suggested "Threadshift". I like that, but it is trademarked.

Copy link
Member Author

Choose a reason for hiding this comment

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

That makes sense. If you have a certain name in mind, do let me know, and I will edit it.

Copy link
Member

Choose a reason for hiding this comment

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

How about Web Worker Offloading for now?

Copy link
Member Author

Choose a reason for hiding this comment

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

SGTM, some more names:

  • Scripts offloading/offloader (in the description add a reference to Web Worker and/or Partytown).
  • Third-party JS offloader
  • Non-critical JS offloader

I will defer name selection to you.

Copy link
Member

Choose a reason for hiding this comment

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

Those are some good options but I like Web Worker Offloading.

The JS being offloaded may not be third-party. It may also be critical, but just offloaded so it can run in another thread.

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* Module Name: Partytown Web Worker
* Module Name: Web Worker Offloading
@westonruter
Copy link
Member

westonruter commented Feb 5, 2024

Since we're currently in the middle of re-plumbing for standalone plugins, I'm going to re-base this branch to a feature/partytown feature/web-worker-offloading branch. I don't think this feature should be released as a module but rather as a standalone plugin, right? cc @mukeshpanchal27 @joemcgill

@westonruter westonruter changed the base branch from trunk to feature/web-worker-offloading February 5, 2024 22:03
@@ -33,6 +34,7 @@ build-cs/composer.lock
node_modules/
vendor/

modules/js-and-css/partytown-web-worker/assets/js/partytown
Copy link
Member

Choose a reason for hiding this comment

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

This will necessitate introducing a .distignore file to replace the .gitattributes file as explained in #893 (comment).

Copy link
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

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

Since this would certainly get released as a standalone plugin on Wordpress.org, it seems we'd need to have the plugin actually do something without needing to code. Otherwise, I understand that the plugin review team would reject the plugin as a "framework plugin". I recall there was some discussion of what this could do by default, without there being a clear answer. Would there need to be a UI to select scripts to opt-in? Humm.

@@ -9,6 +9,8 @@
_x( 'Creates WebP versions for new JPEG image uploads if supported by the server.', 'module description', 'performance-lab' ),
_x( 'Enqueued Assets Health Check', 'module name', 'performance-lab' ),
_x( 'Adds a CSS and JS resource check in Site Health status.', 'module description', 'performance-lab' ),
_x( 'Partytown Web Worker', 'module name', 'performance-lab' ),
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
_x( 'Partytown Web Worker', 'module name', 'performance-lab' ),
_x( 'Web Worker Offloading', 'module name', 'performance-lab' ),
@@ -0,0 +1,139 @@
<?php
/**
* Module Name: Partytown Web Worker
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* Module Name: Partytown Web Worker
* Module Name: Web Worker Offloading
@@ -0,0 +1,139 @@
<?php
/**
* Test cases for Partytown Web Worker module.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* Test cases for Partytown Web Worker module.
* Test cases for Web Worker Offloading module.

$partytown_handles = array();
foreach ( $wp_scripts->registered as $handle => $script ) {
if ( ! empty( $script->deps ) && in_array( 'partytown', $script->deps, true ) ) {
Copy link
Member

Choose a reason for hiding this comment

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

Correct me if I'm wrong, but let's say you have an existing script in a theme like:

wp_enqueue_script( 'foo', 'https://example.com/foo.js' );

In order to opt-in this script to use Partytown, you'd have to do this if you wanted to opt-in from a separate plugin:

wp_scripts()->registered['foo']->deps[] = 'partytown';

Is that right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yup, that should do the trick.

@adamsilverstein
Copy link
Member

Since this would certainly get released as a standalone plugin on Wordpress.org, it seems we'd need to have the plugin actually do something without needing to code

We could have it automatically apply to analytics scripts where it is expected to work well - although those may not be enqueued with wp_scripts.

@thelovekesh thelovekesh changed the title Add partytown module Mar 1, 2024
@thelovekesh
Copy link
Member Author

We could have it automatically apply to analytics scripts where it is expected to work well - although those may not be enqueued with wp_scripts.

If the user wishes to offload these common scripts, it would be preferable if there was an opt-in form on some sort of options page. Alternatively, the user may utilize this as a framework to offload scripts.

There are two scenarios I can think of if the user opts in for script auto-offloading:

  • They should be enqueued with wp_scripts, and we could use something like wp_scripts()->registered['foo']->deps[] = 'partytown';
  • Alternatively, if any script tags are inline scripts added to the page's head or footer, we will need to parse every script tag on the page and offload the above-mentioned common scripts if available before the output buffer.
@westonruter
Copy link
Member

Just noting this can be updated to use the new standalone plugin structure.

Copy link
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

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

Approving to continue work in the feature branch

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.

Looking good! Lets keep iterating and testing in the branch.

@thelovekesh thelovekesh merged commit 1f949f4 into WordPress:feature/web-worker-offloading Apr 29, 2024
45 of 46 checks passed
@thelovekesh thelovekesh deleted the feature/add-partytown-module branch April 29, 2024 16:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Review Anything that requires code review no milestone PRs that do not have a defined milestone for release [Type] Enhancement A suggestion for improvement of an existing feature
5 participants