diff --git a/src/content/guides/web-workers.mdx b/src/content/guides/web-workers.mdx index 19caf47b5d3e..48d04bc53aaa 100644 --- a/src/content/guides/web-workers.mdx +++ b/src/content/guides/web-workers.mdx @@ -62,4 +62,22 @@ import { Worker } from 'worker_threads'; new Worker(new URL('./worker.js', import.meta.url)); ``` +--- Note that this is only available in ESM. `Worker` in CommonJS syntax is not supported by either webpack or Node.js. + +### Cross-Domain and Dynamic Web Workers + +In some setups, you may need your worker script to be hosted on a different origin + +The recommended approach is to define `__webpack_public_path__` at the beginning of your worker entry file, before any imports. +This lets webpack resolve chunk URLs correctly without editing generated code: + +```js +// worker.js + +// Define the public path dynamically before loading other modules +/* eslint-disable camelcase */ +__webpack_public_path__ = self.location.origin + '/assets/'; + +// Then import or execute the rest of your worker logic +importScripts('deep-thought.js');