外掛說明
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.
⚠ 這是實驗性功能。 ⚠
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:
計畫支援 Site Kit by Google 及 Rank Math SEO。
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:
<?php
add_filter( 'plwwo_configuration', function ( $config ) {
$config['mainWindowAccessors'][] = 'wp'; // Make the wp global available in the worker (e.g. wp.i18n and wp.hooks).
return $config;
} );
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.
<?php
add_action(
'wp_enqueue_scripts',
function () {
wp_add_inline_script(
'web-worker-offloading',
<<<JS
window.partytown = {
...(window.partytown || {}),
resolveUrl: (url, location, type) => {
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.
常見問題集
-
Why are my offloaded scripts not working and I see a 404 error in the console for `partytown-sandbox-sw.html`?
-
If you find that your offloaded scripts aren’t working while also seeing a 404 error in the console for a file at
/wp-content/plugins/web-worker-offloading/build/partytown-sandbox-sw.html?1727389399791
then it’s likely you have Chrome DevTools open with the “Bypass for Network” toggle enabled in the Application panel. -
在何處可以回報安全性程式碼錯誤?
-
效能團隊及 WordPress 社群對安全性程式碼錯誤一向嚴陣以待,我們非常感謝使用者披露相關發現所付出的心力,並會盡全力解決使用者提出的問題。
如需回報安全性問題,請參考 WordPress HackerOne 計畫。
-
如何為這個外掛做出貢獻?
-
非常感謝各位的各種貢獻!請參考核心程式效能團隊手冊以進一步了解參與的方式。
外掛原始程式碼儲存於 WordPress/performance 在 GitHub 上的存放庫。
使用者評論
這個外掛目前沒有任何使用者評論。