google-chromefirefoxcross-browserservice-workerbrowser-extension

Manifest v3 background scripts/service worker on Firefox


I'm trying to migrate my browser extension (that I expect to work on Chrome and Firefox) from manifest v2 to v3.

However, I am getting conflicting information about the background section. I did lots of research on google and stack overflow, and no one seems to agree on anything. Also, most information seems to be outdated. Of the best sources I found, lots of places mention that it should be migrated to service_worker (example), but it seems that Firefox should still use scripts instead (source).

But no matter what I do, I am getting errors. If I only use service worker:

"background": {
    "type": "module", // tried both with and without this option
    "service_worker": "background.ts"
}

The the build command from parcel is happy (seems to use parcel/transformer-webextension underneath), but web-ext fails catastrophically:

WebExtError: installTemporaryAddon: Error:
Error: Could not install add-on at '...':
Error: background.service_worker is currently disabled

Even if I provide the flag --firefox-preview which was supposed to fix this.

Which kinda makes sense, this well-written tutorial claims that Firefox is keeping using scripts for V3, just deprecating the persisent flag (which you can remove or set to false). That is fine, as I wasn't using that anyway.

"background": {
    "scripts": ["background.ts"],
    "persistent": false // `persistent` must either be false or omited; I tried both
}

In fact that is exactly how the official Firefox docs claim V3 should support.

But parcel complains that ^^^ Missing property service\_worker:

screenshot of parcel error

Ok, so let's try both:

"background": {
    "type": "module",
    "service_worker": "background.ts",
    "scripts": ["background.ts"]
},

But parcel is not happy, with Invalid Web Extension manifest:

screenshot of parcel error

So no matter what I try, I can't get both parcel and web-ext to be happy at the same time.

It seems that Chrome wants one thing, and Firefox wants another, but no matter what I try, I can't even run my extension on neither browser. So not only it seems I can't have a V3 extension for both browsers - I cannot have a V3 extension at all if I want to use both parcel and web-ext (which is indispensable afaik).

I am particularly concerned because according to official chrome sources, Manifest V3 is a "prerequisite for the Featured badge⁠" starting of now, and V2 will be removed by June.

So if I am already penalized for not using V3 now, and have less than 6 months to figure out how to, while none of the available tooling seems to support this version yet? I must be missing something...


Notes: these are the commands I am using to run parcel and web-ext:

        "watch": "parcel watch src/manifest.json --dist-dir distribution --no-cache --no-hmr",
        "start": "web-ext run --firefox-preview"

And these are the versions I am using, both the latest on NPM

        "parcel": "^2.8.2",
        "web-ext": "^7.4.0",

Solution

  • Currently, Firefox continues to use scripts and Google Chrome continues to use service_worker.
    In the latest Firefox/Google Chrome, no error will occur even if both are listed.

    So let's try:

    "background": {
        "service_worker": "background.ts",
        "scripts": ["background.ts"]
    },
    
    • Before Chrome 121, Chrome refuses to load a Manifest V3 extension with background.scripts or background.page present. From Chrome 121, their presence in a Manifest V3 extension is ignored.
    • Before Firefox 120, Firefox did not start the background page if service_worker was present (see Firefox bug 1860304). From Firefox 121, the background page starts as expected, regardless of the presence of service_worker.

    background - Mozilla | MDN

    Refs: Proposal: declaring background scripts in a neutral way · Issue #282 · w3c/webextensions