diff --git a/plugins/web-worker-offloading/load.php b/plugins/web-worker-offloading/load.php index 459cd9204..27973a339 100644 --- a/plugins/web-worker-offloading/load.php +++ b/plugins/web-worker-offloading/load.php @@ -47,3 +47,4 @@ require_once __DIR__ . '/helper.php'; require_once __DIR__ . '/hooks.php'; +require_once __DIR__ . '/third-party.php'; diff --git a/plugins/web-worker-offloading/readme.txt b/plugins/web-worker-offloading/readme.txt index 774ff9139..27d251fe7 100644 --- a/plugins/web-worker-offloading/readme.txt +++ b/plugins/web-worker-offloading/readme.txt @@ -11,15 +11,23 @@ Offload JavaScript execution to a Web Worker. == Description == -This plugin offloads JavaScript execution to a Web Worker, improving performance by freeing up the main thread. +This plugin offloads JavaScript execution to a Web Worker, improving performance by freeing up the main thread. This should translate into improved [Interaction to Next Paint](https://web.dev/articles/inp) (INP) scores. _This functionality is considered experimental._ -In order to opt-in a script to be loaded in a worker, simply add `worker` script data to a registered script. For example, +In order to opt in a script to be loaded in a worker, simply add `worker` script data to a registered script. For example, if you have a script registered with the handle of `foo`, opt-in to offload it to a web worker by doing: ` wp_script_add_data( 'foo', 'worker', true ); ` +Otherwise, the plugin currently ships with built-in integrations to offload Google Analytics to a web worker for the following plugins: + +* [Rank Math SEO](https://wordpress.org/plugins/seo-by-rank-math/) +* [Site Kit by Google](https://wordpress.org/plugins/google-site-kit/) +* [WooCommerce](https://wordpress.org/plugins/woocommerce/) + +Please monitor your analytics once activating to ensure all the expected events are being logged. At the same time, monitor your INP scores to check for improvement. + == Frequently Asked Questions == = Why are my offloaded scripts not working and I see a 404 error in the console for `partytown-sandbox-sw.html`? = diff --git a/plugins/web-worker-offloading/third-party.php b/plugins/web-worker-offloading/third-party.php new file mode 100644 index 000000000..29111254a --- /dev/null +++ b/plugins/web-worker-offloading/third-party.php @@ -0,0 +1,76 @@ +|mixed $configuration Configuration. + * @return array Configuration. + */ +function wwo_add_google_analytics_forwarded_events( $configuration ): array { + $configuration = (array) $configuration; + + $configuration['forward'][] = 'dataLayer.push'; + return $configuration; +} + +/** + * Adds a script to be offloaded to a worker. + * + * @param string $script_handle Script handle. + */ +function wwo_mark_script_for_offloading( string $script_handle ): void { + add_filter( + 'print_scripts_array', + static function ( $script_handles ) use ( $script_handle ) { + if ( in_array( $script_handle, (array) $script_handles, true ) ) { + wp_script_add_data( $script_handle, 'worker', true ); + } + return $script_handles; + } + ); +} + +/** + * Loads third party plugin integrations for active plugins. + * + * @since 0.1.0 + */ +function wwo_load_third_party_integrations(): void { + $plugins_with_integrations = array( + // TODO: google-site-kit. + // TODO: seo-by-rank-math. + 'woocommerce', + ); + + // Load corresponding file for each string in $plugins if the WordPress plugin is installed and active. + $active_plugin_slugs = array_filter( + array_map( + static function ( $plugin_file ) { + if ( is_string( $plugin_file ) && str_contains( $plugin_file, '/' ) ) { + return strtok( $plugin_file, '/' ); + } else { + return false; + } + }, + (array) get_option( 'active_plugins' ) + ) + ); + + foreach ( array_intersect( $active_plugin_slugs, $plugins_with_integrations ) as $plugin_slug ) { + require_once __DIR__ . '/third-party/' . $plugin_slug . '.php'; + } +} +add_action( 'plugins_loaded', 'wwo_load_third_party_integrations' ); diff --git a/plugins/web-worker-offloading/third-party/woocommerce.php b/plugins/web-worker-offloading/third-party/woocommerce.php new file mode 100644 index 000000000..1f7e2c278 --- /dev/null +++ b/plugins/web-worker-offloading/third-party/woocommerce.php @@ -0,0 +1,15 @@ +