Anarion
Anarion

Reputation: 2534

Chrome extension wtih manifestv3 - service worker doesn't wake up

I have a background srcipt defined like

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

and a content script

"content_scripts": [
    {
      "matches": [
        "*://youtube.com/*"
      ],
      "js": ["content-script.js"]
    }

background does the usual

runtime.onMessage.addListener

and content script

chrome.runtime.sendMessage({

But, at some rare situations (around 2-5% of users), the service worker stays inactive, despite a tab with youtube being open, so the sendMessage calls return nothing.

This is only solved with turning the extension off and back on.

Is there a way to wake up the service worker programmatically if it fell asleep? Is there a possible reason why service worker might not wake up? Uncaught exceptions, manifest settings?

I'm really lost here and it's extremely hard to debug, as it never happened to me during one month, and I can only debug it when a user agrees to have a call and share screen.

The extension has over a million users, but this is simply killing me.

The extension can be seen at https://github.com/Anarios/return-youtube-dislike/tree/main/Extensions/combined

This was confirmed to happen on the latest version of chrome.

Upvotes: 13

Views: 3149

Answers (1)

WofWca
WofWca

Reputation: 706

Looking at your extension's code, you already do what I'm going to suggest. But I'll leave it for others who might be stumbling upon this question.

Event listeners must be attached at the top-level of the background scrip / service worker. See MDN docs about this. That is, you should not call runtime.onMessage.addListener() inside of a, say, Promise or runtime.storage.local.get() or setTimeout() callback.

Upvotes: 0

Related Questions