I’m coding an Angular 17 app and I need to use Web Workers. The problem is that this is working in my development environment but not in my production build.
I do something like this:
// in a random component
this.worker = new Worker(new URL(`../../utils/workers/render.worker`, import.meta.url), {
type: 'module',
name: 'render-worker',
});
and this is working fine in dev mode. (ng serve)
The problem is that, when deploying this using these commands, it wont work in production:
ng build ${PROD} --source-map=false --deploy-url "https://${CDN_URL}/${VERSION}/"
# then I upload all the files to S3, and serve them using cloudfront.
Any ideas about these?
I tried to serve the worker code as an asset, but the problem is that the file wont be compiled / transpiled to JS if I do that.
I can see the worker compiled code in the final build renamed as: worker-Y57MQIPC.js
, so file is there, the only issue is that I don’t know how to access it.
I’ve also tried to hardcode the URL and do something like:
this.worker = new Worker(hardcodedURL)
but it seems that you can not load code from a different domain.
Note: I’m not using webpack. I’m using this builder: @angular-devkit/build-angular:browser-esbuild
Any ideas? I don’t know if this is an Angular issue or if I’m doing something wrong.
Any help is appreciated. Thanks in advance.