Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undefined document introduced when using import.meta.url in worker #12611

Closed
7 tasks done
swwind opened this issue Mar 27, 2023 · 2 comments · Fixed by #12629
Closed
7 tasks done

Undefined document introduced when using import.meta.url in worker #12611

swwind opened this issue Mar 27, 2023 · 2 comments · Fixed by #12629
Labels
feat: web workers p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@swwind
Copy link

swwind commented Mar 27, 2023

Describe the bug

// main.js
new Worker(new URL("./worker.js", import.meta.url), { type: 'module' });
// worker.js
console.log(import.meta.url);

Build result

// main
new Worker(new URL("/assets/worker-f523fd11.js", self.location), {
  type: "module",
});
// worker
(function () {
  "use strict";
  console.log(
    (document.currentScript && document.currentScript.src) ||
      new URL("assets/worker-f523fd11.js", document.baseURI).href
  );
})();

I'm 100% sure document does not exist in web worker and my worker will panic.

Reproduction

https://stackblitz.com/edit/vitejs-vite-vyu6ig?file=worker.js,index.html&terminal=dev

Steps to reproduce

run vite build

System Info

-

Used Package Manager

npm

Logs

I think use typeof document != 'undefined' && before document.currentScript is better

Validations

@stackblitz
Copy link

stackblitz bot commented Mar 27, 2023

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

@vinayakkulkarni
Copy link

Yep can confirm, been having similar issue with using Web Worker in Nuxt 3

@sapphi-red sapphi-red added p3-minor-bug An edge case that only affects very specific usage (priority) feat: web workers labels Mar 27, 2023
vinayakkulkarni added a commit to geoql/expenses that referenced this issue Apr 1, 2023
https: //github.com/vitejs/vite/issues/12611
Signed-off-by: Vinayak Kulkarni <19776877+vinayakkulkarni@users.noreply.github.com>
@github-actions github-actions bot locked and limited conversation to collaborators Apr 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feat: web workers p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants