Debugging a Manifest V3 extension's service worker

784 views
Skip to first unread message

Alex

unread,
Jun 7, 2024, 10:25:37 AM6/7/24
to Chromium Extensions
Hi, I would like be able to import modules while debugging my extension's background service worker process from the "Inspect views" link on chrome://extensions

The thing is, while static import works in the extension during regular operation, it errors out when inspecting the service worker with "Cannot use import statement outside a module".

Dynamic import() errors out with "import() is disallowed on ServiceWorkerGlobalScope by the HTML specification"

importScripts() errors out with "Failed to execute 'importScripts' on 'WorkerGlobalScope': Module scripts don't support importScripts()."

The relevant portion of the extension's manifest:

```
  "background": {
    "service_worker": "js/background.js",
    "type": "module"
  },
```

What am I missing here? Thank you for any help!

woxxom

unread,
Jun 7, 2024, 12:39:59 PM6/7/24
to Chromium Extensions, Alex
The service worker specification doesn't dynamic imports because this technology is designed for web sites that are remote/external by nature. As I regularly point out, there are many such restrictions in the design of a service worker that don't make any sense for extensions, so the decision to use it as the only environment for the background script for extensions is irrational and is not justifiable by any imaginary gains it may provide in contrived edge cases.

As a workaround you can use importScripts in a classic script (i.e. you should remove "type":"module") after previously importing these scripts inside self.oninstall event handler, see this example: dynamic importScripts.

Alexei Miagkov

unread,
Jun 7, 2024, 1:01:06 PM6/7/24
to woxxom, Chromium Extensions
I agree with what you write about service workers and extensions.

Let me rephrase. Can one debug their existing MV3 extension without reworking how it handles dependencies?

Using static imports is simple and straightforward. Why isn't debugging also simple and straightforward?

If we are stuck with MV3, shouldn't we try to make MV3 better and more sensible?

woxxom

unread,
Jun 7, 2024, 1:47:52 PM6/7/24
to Chromium Extensions, Alexei Miagkov, Chromium Extensions, woxxom
>  Can one debug their existing MV3 extension without reworking how it handles dependencies?

No. Any dependency you want to load must be loaded inside the self.oninstall handler that runs one time when the extension is installed, updated, or reloaded.

>  Why isn't debugging also simple and straightforward?

Because this technology uses a different loader mechanism.
Because devtools doesn't support service workers fully.

> If we are stuck with MV3, shouldn't we try to make MV3 better and more sensible?

Yes, but how can we convince Chromium developers to prioritize our concerns? There's so much wrong, missing, or broken in ManifestV3 that fixing all that will require years if not decades with the current rate of progress where only a few developers are working on the core features fulltime. It's quite likely that our best hope will be for the web platform developers to implement dynamic imports, but they're not particularly interested in that, judging by the fact that nothing was done for all these years since the issue was reported.

Matt

unread,
Jun 13, 2024, 7:46:34 AM6/13/24
to Chromium Extensions, woxxom, Alexei Miagkov, Chromium Extensions
So surely then the sunsetting of MV2 should be delayed? I'm sure a lot of people are in favour of doing so.
Reply all
Reply to author
Forward
0 new messages