Webpack 5 support for SharedWorkers?

414 Views Asked by At

I want to use SharedWorkers with my React-App. As stated in the docs (https://webpack.js.org/guides/web-workers/), Webpack 5 supports Web Workers in general. So it should also support SharedWorkers as one implementation of Web Workers.

The shown example works as expected:

new Worker(new URL('./worker.js', import.meta.url));

But if i change the line to

new SharedWorker(new URL('./worker.js', import.meta.url));

nothing happens. The worker code does not get executed and no error is shown in the console.

Can someone help me getting Shared Workers working with Webpack 5 (using CRA and react-scripts 5.0.1)

1

There are 1 best solutions below

0
On

I faced with the same problem, what works for me is to put the worker.js file under the public folder, and then this connection works

new SharedWorker(new URL('./worker.js', import.meta.url));