crxjs / chrome-extension-tools

Bundling Chrome Extensions can be pretty complex. It doesn't have to be.
https://crxjs.dev/vite-plugin
2.78k stars 182 forks source link

window does not exist: Manifest v3 Service Workers Registration Error #127

Closed code3z closed 2 years ago

code3z commented 2 years ago

Google search terms

manifest v3 service worker registration error, chrome manifest v3 service worker registration failed

Describe the bug

When I use this plugin, the service worker is compiled along with a script meant for auto reloading. The script looks like this:

// SIMPLE RELOADER IMPORT
import "./assets/background-page-reloader-2cfe1622.js"
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}

function createCommonjsModule(fn) {
  var module = { exports: {} }
  return fn(module, module.exports), module.exports
}

When I remove this manually, everything works fine, but with the script, the service worker fails to register with an "unknown error."

I believe the window does not exist in a service worker.

How do we reproduce?

Clone this repo: https://github.com/code3z/mv3-rollup-debug

Expected behavior

I expected the service worker to register.

Actual behavior

Service worker registration failed
{
  "manifest_version": 3,
  "name": "svelte-tailwind-extension-boilerplate",
  "version": "0.0.1",
  "description": "DEVELOPMENT build with simple auto-reloader\n[2021-12-11 12:44:42] waiting for changes...",
  "background": {
    "service_worker": "background/index.js",
    "type": "module"
  },
  "permissions": [
    "activeTab"
  ],
  "action": {}
}
An unknown error occurred when fetching the script.
jacksteamdev commented 2 years ago

@code3z It looks like Chromium has changed the behavior of the background service worker. I moved the background page down next to the manifest, and things seem to work fine. :shrug:

image

image

We're focusing on v4 for now, new boilerplates and how-to guides are coming soon! I'll let you know when a Svelte guide is available. Sneak peak: HMR works great in v4.