Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/content/guides/web-workers.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,26 @@ 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
(for example, on a CDN or within a micro-frontend).
Webpack supports this, but the worker must know the correct **public path** at runtime.

### Setting a dynamic public path safely

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');
Loading