vitejs / vite

Next generation frontend tooling. It's fast!
http://vitejs.dev
MIT License
66.18k stars 5.92k forks source link

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

Closed hughfenghen closed 3 days ago

hughfenghen commented 4 weeks ago

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

stackblitz[bot] commented 4 weeks ago

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.