Skip to content

Using new window.SharedWorker causes unexpected inlining of worker script #9901

@silverwind

Description

@silverwind

Describe the bug

When a worker script is initialized like

const worker = new window.SharedWorker(new URL('worker.js', import.meta.url));

it will be inlined as base64, which breaks things like WorkerGlobalScope.location inside it during the prod build (but not during dev build, as dev build never inlines). By removing the window, e.g.

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

it works again and the script is exempt from inlining as expected. I would expect both variants to work the same way as it's pretty common to feature-detect via window.SharedWorker and then use the same expression when instantiating it:

if (window.SharedWorker) {
  new window.SharedWorker(...)
}

Specifically this code does not properly check for window, globalThis, or other prefixes that could be used before globals like Worker, SharedWorker or URL.

Reproduction

https://github.com/silverwind/vite-sharedworker

System Info

not relevant

Used Package Manager

npm

Logs

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Rejected

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions