Skip to content

Support Web-Workers when enforcing Trusted Types in @angular/build:application builder #30703

Open
@Khyalis

Description

@Khyalis

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions