Has anyone tried workers with webpack 5/RW0.36.x?

I want to use use a web worker and the docs say that no loader is needed and make it look super simple but I haven’t had any luck. I’m guessing it’s webpack config but I’ve not managed to get anything working.

This question seems most similar to what I’m getting as using new URL(...) has an origin of file:// instead of http. and that leads to a error of Failed to construct 'Worker': Script at 'file:///Users/kurthutten/other/redwoodblog/app/web/src/helpers/cadPackages/jsCad/jscad-worker.worker.js'

But have also been reading through this, and even the minimal example isn’t working.

In the mean time it’s working by putting javascript in the public folder but that not a long term solution.

I bit of follow up, I’m using the webpack 4 worker-loader to load the worker and it seems to be working fine.

Would still like to do it the webpack 5 way in future.

I needed to use a web worker. And found this thread.

When I tried locally it just worked now with webpack 5. But when I tried to deploy I get this error

var worker = new Worker(new URL('./searchWorker.js', import.meta.url));
                                                            ^^^^
SyntaxError: Cannot use 'import.meta' outside a module
    at compileFunction (<anonymous>)
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1031:15)

Anyone know how to fix this error?

I should have pasted the full error above, but I didn’t… If I had, you would have been able to see that the error was from the prerender phase.

So I worked around it by lazyloading the component with the worker, and then just not run the lazyloading code when prerendering, and instead load a component that looks the same, but that doesn’t have the logic.