1

After preparing the migration of my chrome manifest V2 extension to manifest V3 and reading about the problems with persistent service workers I prepared myself for a battle with the unknown. My V2 background script uses a whole bunch of globally declared variables and I expected I need to refactor that.

But to my great surprise my extension background script seems to work out of the box without any trouble in manifest V3. My extension uses externally_connectable. The typical use case for my extension is that the user can navigate to my website 'bla.com' and from there it can send jobs to the extension background script.

My manifest says:

  "externally_connectable": {
    "matches": [
      "*://localhost/*",
      "https://*.bla.com/*"
    ]
  }

My background script listens to external messages and connects:

chrome.runtime.onMessageExternal.addListener( (message, sender, sendResponse) => {
    log('received external message', message);
});

chrome.runtime.onConnectExternal.addListener(function(port) {
    messageExternalPort = port;

    if (messageExternalPort && typeof messageExternalPort.onDisconnect === 'function') {
        messageExternalPort.onDisconnect(function () {
            messageExternalPort = null;
        })
    }
});

From bla.com I send messages to the extension as follows

chrome.runtime.sendMessage(EXTENSION_ID, { type: "collect" });

From bla.com I receive messages from the extension as follows

const setUpExtensionListener = () => {
    // Connect to chrome extension
    this.port = chrome.runtime.connect(EXTENSION_ID, { name: 'query' });

    // Add listener
    this.port.onMessage.addListener(handleExtensionMessage);
}

I tested all scenarios including the anticipation of the famous service worker unload after 5 minutes or 30 seconds inactivity, but it all seems to work. Good for me, but something is itchy. I cannot find any documentation that explains precisely under which circumstances the service worker is unloaded. I do not understand why things seem to work out of the box in my situation and why so many others experience problems. Can anybody explain or refer to proper documentation. Thanks in advance.

timboektoe
  • 115
  • 1
  • 2
  • 13
  • Most likely you have devtools open for the service worker, which is why it doesn't unload. – wOxxOm Oct 16 '22 at 19:38
  • Nope, this is not the case. I retested all scenario's with dev tools closed and all still works. (even after letting my computer idle for > 20mins) Thanks for your reply by the way. – timboektoe Oct 19 '22 at 07:42
  • It's either a bug or you have something else going on, maybe a nativeMessaging host. The SW correctly terminates here as it should when using your code as posted. – wOxxOm Oct 19 '22 at 10:02
  • No I haven't, neither do I have any other extension installed. I do always have a tab with an 'externally_connectable' page opened in the browser by the way. Could that be the reason the Service Worker is not terminated? (my chrome version is: 106.0.5249.119) – timboektoe Oct 19 '22 at 10:10
  • The existence of a web page tab has no bearing on the lifetime of the service worker unless the script in the tab constantly re-establishes the port as shown in the linked answer or similarly. – wOxxOm Oct 19 '22 at 10:11
  • I have now tested the extension on various machines, windows and mac, in chrome and edge and all works. The tab with the website that is whitelisted as externally connectable is always open during testing, but does not re-establish the port. I am tempted to show you this in a meeting. :-) (Thank you very much for taking time to answer my question by the way) – timboektoe Oct 24 '22 at 14:51
  • 1
    Well, it contradicts everything a lot of people have observed over the past years, so unless I see the full code I don't have any new ideas. – wOxxOm Oct 24 '22 at 17:03

0 Answers0