Skip to content

Creating a WebWorker inside a WebWorker will the self.origin value being null. #17507

Closed
@hughfenghen

Description

@hughfenghen

Describe the bug

When the origin value is null, calling some APIs can trigger SecurityError or Cross-Origin error.

For example, executing await navigator.storage.getDirectory()
will throw the error: Error: SecurityError: Storage directory access is denied.

The reason is that a WebWorker was created using a Data URL.
image

The corresponding Vite code is at: https://github.com/vitejs/vite/blob/7b240e408ed83f172e9f88823eae3b4a9ba92674/packages/vite/src/node/plugins/worker.ts#L326C28-L326C38

I tried replacing self with window, which resolved the issue.

Reproduction

https://stackblitz.com/edit/vitejs-vite-hfhdfg?file=main.js

Steps to reproduce

  1. npm run build
  2. check dist/assets/index-[hash].js

System Info

N/A

Used Package Manager

npm

Logs

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions