From 8118f74241ffbdd56d267a755210e7156e917255 Mon Sep 17 00:00:00 2001 From: Alexey Date: Thu, 27 Nov 2025 03:02:49 +0300 Subject: [PATCH 1/2] docs(guides): clarify Web Workers usage with publicPath from variable --- src/content/guides/web-workers.mdx | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/content/guides/web-workers.mdx b/src/content/guides/web-workers.mdx index 19caf47b5d3e..2064f0faf5ad 100644 --- a/src/content/guides/web-workers.mdx +++ b/src/content/guides/web-workers.mdx @@ -52,6 +52,31 @@ self.onmessage = ({ data: { question } }) => { }; ``` +## Set a public path from a variable + +When you set `__webpack_public_path__` from a variable, and use `publicPath` equal to `auto`, worker chunks will get a separate runtime, and Webpack runtime will set `publicPath` to automatically calculated public path, that is probably is not what you expect. + +To work around this issue, you need to set `__webpack_public_path__` from within the worker code. Here is an example: + +**worker.js** + +```js +self.onmessage = ({ data: { publicPath, ...otherData } }) => { + if (publicPath) { + __webpack_public_path__ = publicPath; + } + + // rest of the worker code +} +``` + +**app.js** + +```js +const worker = new Worker(new URL('./worker.js', import.meta.url)); +worker.postMessage({publicPath: window.__MY_GLOBAL_PUBLIC_PATH_VAR__}); +``` + ## Node.js Similar syntax is supported in Node.js (>= 12.17.0): From 60a32262309432c0c4c149119308805c154fdf7e Mon Sep 17 00:00:00 2001 From: Alexey Date: Thu, 27 Nov 2025 20:09:02 +0500 Subject: [PATCH 2/2] chore: formatting --- src/content/guides/web-workers.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/guides/web-workers.mdx b/src/content/guides/web-workers.mdx index 2064f0faf5ad..66be82bb2518 100644 --- a/src/content/guides/web-workers.mdx +++ b/src/content/guides/web-workers.mdx @@ -74,7 +74,7 @@ self.onmessage = ({ data: { publicPath, ...otherData } }) => { ```js const worker = new Worker(new URL('./worker.js', import.meta.url)); -worker.postMessage({publicPath: window.__MY_GLOBAL_PUBLIC_PATH_VAR__}); +worker.postMessage({ publicPath: window.__MY_GLOBAL_PUBLIC_PATH_VAR__ }); ``` ## Node.js