'MV3 Serviceworker won't wake up when sent a message from the contentscript

Is it an expected behaviour for a serviceworker to not respond to or wake up when a message has been sent from a content script or popup. And if so , how do I wake a service worker to perform a task. I dont require a persistent SW, I just want it to run a certain function when I sent a message from a contentscript and after completion , it can go back to sleep again until it receives another message.

Background.js

   chrome.runtime.onMessage.addListener(async function (request, sender, sendResponse) {
      if(request.message === 'hello'){
        clearCookies();
      }
    });

ContentScript

chrome.runtime.sendMessage({ message: "hello" });


Solution 1:[1]

In 2022, there are still a many bugs with service workers in MV3. They are quite hard to debug, many developers may not even know if/why why their MV3 extension occasionally fails.

Like many other extension developer, I've been waiting for months on this issue to be resolved. Let's hope google's new default to MV3 will expedite a solution.

One fix some people used to mitigate the problem is to call the SW from a sister extension or a content script. It helps keep the SW awake, but it's obviously not optimal.

Wanted to comment but didn't have the reputation since I rarely comment. I just really felt your pain here as this issue deserve more visibility.

https://bugs.chromium.org/p/chromium/issues/detail?id=1024211#c31

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 felix m