Description
Command
build
Description
It is possible to create a web worker using the new Worker(new URL('<workerfile>', import.meta.url))
syntax, and in the build output, the url will be replaced, so the worker script can be downloaded.
However, when additionally enforcing Trusted Types, at runtime an error can occur: "TypeError: Failed to construct 'Worker': This document requires 'TrustedScriptURL' assignment.", as the emitted script code would attempt to instantiate the Worker
using an URL
instance rather than a TrustedScriptUrl
.
While working around the TypeError
e.g. by employing the Angular DomSanitizer
like this: new Worker(this.#sanitizer.sanitize(SecurityContext.RESOURCE_URL, this.#sanitizer.bypassSecurityTrustResourceUrl(new URL('<workerfile>', import.meta.url).toString())))
, might prevent the TypeError
, it would also break the replacement of the worker url.
It might be helpful, if there were a way of being able to use both the build-time replacement of the worker url and TrustedScriptUrl
.
Describe the solution you'd like
No response
Describe alternatives you've considered
I have considered trying if switching to a builder different from @angular/build:application might provide better support for the use case.
I have considered disabling the enforcement of Trusted Types.
I have considered moving the worker script to a static url that can be used without replacement at build type.