From fb0aa6998f3223007c231ddfa60c3b44aeaf45f6 Mon Sep 17 00:00:00 2001 From: Payel Bera Date: Sat, 20 Dec 2025 11:04:59 +0530 Subject: [PATCH] docs: improve clarity around publicPath usage in workers --- src/content/guides/web-workers.mdx | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/content/guides/web-workers.mdx b/src/content/guides/web-workers.mdx index 66be82bb2518..fb27137ad24f 100644 --- a/src/content/guides/web-workers.mdx +++ b/src/content/guides/web-workers.mdx @@ -67,7 +67,7 @@ self.onmessage = ({ data: { publicPath, ...otherData } }) => { } // rest of the worker code -} +}; ``` **app.js** @@ -77,8 +77,18 @@ const worker = new Worker(new URL('./worker.js', import.meta.url)); worker.postMessage({ publicPath: window.__MY_GLOBAL_PUBLIC_PATH_VAR__ }); ``` +**When to use this:** + +This pattern is only required when a worker needs to load additional chunks and the asset base URL is determined at runtime (for example, when using a CDN or a multi-domain deployment). + +Since workers run in an isolated global scope, the automatically detected public path may differ from the one used by the main thread. In such cases, the public path (`__webpack_public_path__`) must be explicitly passed to the worker and set inside the worker runtime. + +> Note: This is an advanced use case. If your worker does not load additional chunks or your assets are served from a static, same-origin path, you typically do not need to set `__webpack_public_path__` manually. + ## Node.js +This section describes using Web Workers in a Node.js environment via the `worker_threads` module. + Similar syntax is supported in Node.js (>= 12.17.0): ```js