Service worker registration failed. Chrome extension
Asked Answered
H

3

49

I don't understand how to migrate from manifest v2 to v3 in part of Service Worker. Occurs error Service worker registration failed

// manifest.json
"background": {
    "service_worker": "/script/background/background.js"
},
// background.js
chrome.runtime.onInstalled.addListener(onInstalled);

enter image description here

Hesitant answered 9/2, 2021 at 7:50 Comment(1)
I run into this error because I naively substituted the addListener with addEventListener, as I was instructed by my IDE. Apparently the "addListener" method is a very much correct method used in chrome API and has nothing to do with deprecated DOM related methodsHurryscurry
M
66

Please find below the cause for your specific issue and the cause for not getting the details of failure in the console log.

  • Before Chrome 93, the service worker file must be in the root path where manifest.json is.

    This is a limitation of Service Worker specification, relaxed for extensions since Chrome 93.

    If, for whatever reason, you want to allow your extension to be used in older Chrome, the correct manifest.json should look like this:

    "background": {
      "service_worker": "background.js"
    },
    

    Conversely, to use an arbitrary path you need to prevent installation in older Chrome:

    "minimum_chrome_version": "93",
    "background": {
      "service_worker": "js/bg/worker-loader.js",
      "type": "module"
    },
    

    Type module is optional and is supported since Chrome 92. You can import ES modules statically for now. The support for dynamic imports is in development.

    In any Chrome version you can use importScripts('/path/foo.js', '/path/bar.js'); to import scripts from other directories.

  • If the worker script throws an error at installation, the worker won't be registered and you will not be getting the error information triggered by your service worker in the console, but only get "Service worker registration failed". This behavior is due to a bug bug in Chrome versions earlier than Chrome 93.

    Solution:

    • use Chrome 93 or newer.

    Limited workaround:

    Typical causes:

    • accessing an undeclared variable
    • syntax error like an unclosed parenthesis
    • accessing a chrome API without declaring it in manifest.json's permissions field
    • a crash in the worker process
Mede answered 9/2, 2021 at 9:0 Comment(2)
In my experiments, if the error (e.g. a syntax error) is nested deep inside the try catch, the problem is not solved as suggested. And so I find that the real solution can be simply upgrading to Chrome 93 if you can afford working off the beta channel, which at the time of writing is available on the official Chrome Beta channel. This will install a beta version of Chrome side-by-side. Surely Chrome 93 will be on the stable chrome channel eventually, making this a non-issue.Nationalize
"minimum_chrome_version" takes a string not a number. source : developer.chrome.com/docs/extensions/mv3/manifestBeaston
P
28

If the same error is still here after the script was moved to the root folder as wOxxOm mentioned, than you probably have an error in your background.js file. However at the time of this post there was no adequate error message on this, other than generic Service worker registration failed. Try to follow Simeon's workaround described here For example you could wrap your v2 background.js script into an error-catching service worker and import your old scripts in it:

// manifest.json
{
  "name": "Throw on Register!",
  "version": "1.0",
  "manifest_version": 3,
  "background": {
    "service_worker": "background-wrapper.js"
  }
}

// background-wrapper.js file
try {
  importScripts("background.js");
} catch (e) {
  console.error(e);
}

// background.js
console.log("start");
throw new Error("lol");
console.log("end");

After that your service worker will be registered as it is as simple as possible and you will have the error info in the console. In my case it looks like that: example Happy moving to v3 :)

Pastor answered 2/3, 2021 at 10:8 Comment(0)
D
4

Service workers must be registered at root level: they cannot be in a nested directory.

Since background.js is service worker now it can't be inside nested folder

Reference: https://developer.chrome.com/docs/extensions/mv3/mv3-migration-checklist/#api_checklist

Degree answered 8/7, 2021 at 16:46 Comment(1)
Correct! Service workers must be registered at root level: they cannot be in a nested directory.Zachariahzacharias

© 2022 - 2024 — McMap. All rights reserved.