Skip to content

Commit a112cd9

Browse files
authored
docs(guides): clarify Web Workers usage with publicPath from variable
1 parent 539ccb2 commit a112cd9

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/content/guides/web-workers.mdx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,31 @@ self.onmessage = ({ data: { question } }) => {
5252
};
5353
```
5454
55+
## Set a public path from a variable
56+
57+
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.
58+
59+
To work around this issue, you need to set `__webpack_public_path__` from within the worker code. Here is an example:
60+
61+
**worker.js**
62+
63+
```js
64+
self.onmessage = ({ data: { publicPath, ...otherData } }) => {
65+
if (publicPath) {
66+
__webpack_public_path__ = publicPath;
67+
}
68+
69+
// rest of the worker code
70+
}
71+
```
72+
73+
**app.js**
74+
75+
```js
76+
const worker = new Worker(new URL('./worker.js', import.meta.url));
77+
worker.postMessage({ publicPath: window.__MY_GLOBAL_PUBLIC_PATH_VAR__ });
78+
```
79+
5580
## Node.js
5681
5782
Similar syntax is supported in Node.js (>= 12.17.0):

0 commit comments

Comments
 (0)