Web Worker Offloading
by WordPress Performance Team on WordPress.org
Offloads select JavaScript execution to a Web Worker to reduce work on the main thread and improve the Interaction to Next Paint (INP) metric.
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 (INP) scores.
⚠ This functionality is 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,
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 );
Unlike with the script loading strategies (async/defer), any inline before/after scripts associated with the worker-offloaded registered script will also be offloaded to the worker, whereas with the script strategies an inline after script would block the script from being delayed.
Otherwise, the plugin currently ships with built-in integrations to offload Google Analytics to a web worker for the following plugin:
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.
This plugin relies on the Partytown 🎉 library by Builder.io, released under the MIT license. This library is in beta and there are quite a few open bugs.
The Partytown configuration can be modified via the plwwo_configuration
filter. For example:
However, not all of the configuration options can be serialized to JSON in this way, for example the resolveUrl
configuration is a function. To specify this, you can add an inline script as follows.
{
if (type === 'script') {
const proxyUrl = new URL('https://my-reverse-proxy.example.com/');
proxyUrl.searchParams.append('url', url.href);
return proxyUrl;
}
return url;
},
};
JS,
'before'
);
}
);
There are also many configuration options which are not documented, so refer to the TypeScript definitions.