@@ -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