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

Globals are not accessible in a service worker #17707

Closed
7 tasks done
budarin opened this issue Jul 17, 2024 · 1 comment
Closed
7 tasks done

Globals are not accessible in a service worker #17707

budarin opened this issue Jul 17, 2024 · 1 comment

Comments

@budarin
Copy link

budarin commented Jul 17, 2024

Describe the bug

The global variables described in the define section are not available in the serviceworker

Reproduction

https://github.com/budarin/vite-sw-globals

Steps to reproduce

  • run dev
  • to see in console an error in the service worker that a global variable named VERSION is not defined

System Info

System:
    OS: macOS 12.7.5
    CPU: (8) x64 Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
    Memory: 44.45 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 22.3.0 - ~/.nvm/versions/node/v22.3.0/bin/node
    Yarn: 1.22.18 - ~/.yarn/bin/yarn
    npm: 10.8.1 - ~/.nvm/versions/node/v22.3.0/bin/npm
    pnpm: 9.5.0 - ~/.nvm/versions/node/v22.3.0/bin/pnpm
  Browsers:
    Chrome: 126.0.6478.127
    Edge: 126.0.2592.102
    Safari: 17.5
    Safari Technology Preview: 16.4
  npmPackages:
    @vitejs/plugin-react: ^4.3.1 => 4.3.1 
    vite: ^5.3.4 => 5.3.4

Used Package Manager

pnpm

Logs

Click to expand!
[vite] connecting...
client:614 [vite] connected.
index.mts:1 Uncaught ReferenceError: VERSION is not defined
    at index.mts:1:24
(anonymous) @ index.mts:1
localhost/:1 Uncaught (in promise) TypeError: Failed to register a ServiceWorker for scope ('http://localhost:3000/src/sw/') with script ('http://localhost:3000/src/sw/index.mts'): ServiceWorker script evaluation failed

Validations

@hi-ogawa
Copy link
Collaborator

Vite relies on /@vite/env module during dev to inject define to runtime, but new URL("...". import.meta.url) js assets alone doesn't trigger such injection. Manually writing import "/@vite/env"` seems to work for the provided reproduction.

Since Vite doesn't support service worker out-of-the-box #2248, there's no expected behavior for this situation, so I think we can close this in favor of #2248.

@github-actions github-actions bot locked and limited conversation to collaborators Sep 7, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants