Skip to content

Commit 7d4e781

Browse files
authored
Update web-workers.mdx
1 parent 94688f6 commit 7d4e781

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src/content/guides/web-workers.mdx

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,21 @@ Note that this is only available in ESM. `Worker` in CommonJS syntax is not supp
6767
6868
## Advanced: Cross-Domain and Dynamic Web Workers
6969
70-
In some setups you might need your worker script to be hosted on a different origin
71-
(for example, using a CDN or micro-frontend).
72-
Webpack supports this, but you must make the worker aware of the correct public path.
70+
In some setups, you may need your worker script to be hosted on a different origin
71+
(for example, on a CDN or within a micro-frontend).
72+
Webpack supports this, but the worker must know the correct **public path** at runtime.
7373
74-
### Setting a dynamic public path
74+
### Setting a dynamic public path safely
7575
76-
Inside the worker, explicitly set the same public path used by the main bundle:
76+
The recommended approach is to define `__webpack_public_path__` at the beginning of your worker entry file, before any imports.
77+
This lets webpack resolve chunk URLs correctly without editing generated code:
7778
7879
```js
7980
// worker.js
80-
/* global __webpack_public_path__, __webpack_require__ */
8181

82-
// Ensure the worker knows where to load chunks from
83-
__webpack_public_path__ = __webpack_require__.p = self.location.origin + '/assets/';
82+
// Define the public path dynamically before loading other modules
83+
/* eslint-disable camelcase */
84+
__webpack_public_path__ = self.location.origin + '/assets/';
85+
86+
// Then import or execute the rest of your worker logic
87+
importScripts('deep-thought.js');

0 commit comments

Comments
 (0)