Persistent Service Worker in Chrome Extension

Service worker (SW) can’t be persistent by definition and the browser must forcibly terminate all of SW connections such as network requests or runtime ports after a certain time, which in Chrome is 5 minutes. The inactivity timer when no such requests or ports are open is even shorter: 30 seconds.

Chromium team currently considers this behavior intentional and good, however this only applies to extensions that observe infrequent events, so they’ll run just a few times a day thus reducing browser memory footprint between the runs e.g. webRequest/webNavigation events with urls filter for a rarely visited site. These extensions can be reworked to maintain the state, example. Unfortunately, such an idyll is unsustainable in many cases.

Known problems

  • Problem 1: Chrome 106 and older doesn’t wake up SW for webRequest events.

    Although you can try to subscribe to an API like chrome.webNavigation as shown in the other answers, but it helps only with events that occur after the worker starts.

  • Problem 2: the worker randomly stops waking up for events.

    The workaround may be to call chrome.runtime.reload().

  • Problem 3: Chrome 109 and older doesn’t prolong SW lifetime for a new chrome API event in an already running background script. It means that when the event occurred in the last milliseconds of the 30-second inactivity timeout your code won’t be able to run anything asynchronous reliably. It means that your extension will be perceived as unreliable by the user.

  • Problem 4: worse performance than MV2 in case the extension maintains a socket connection or the state (variables) takes a long time to rebuild or you observe frequent events like these:

    • chrome.tabs.onUpdated/onActivated,
    • chrome.webNavigation if not scoped to a rare url,
    • chrome.webRequest if not scoped to a rare url or type,
    • chrome.runtime.onMessage/onConnect for messages from content script in all tabs.

    Starting SW for a new event is essentially like opening a new tab. Creating the environment takes ~50ms, running the entire SW script may take 100ms (or even 1000ms depending on the amount of code), reading the state from storage and rebuilding/hydrating it may take 1ms (or 1000ms depending on the complexity of data). Even with an almost empty script it’d be at least 50ms, which is quite a huge overhead to call the event listener, which takes only 1ms.

    SW may restart hundreds of times a day, because such events are generated in response to user actions that have natural gaps in them e.g. clicked a tab then wrote something, during which the SW is terminated and restarted again for a new event thus wearing down CPU, disk, battery, often introducing a frequent perceivable lag of the extension’s reaction.

“Persistent” service worker with offscreen API

Courtesy of Keven Augusto.

In Chrome 109 and newer you can use offscreen API to create an offscreen document and send some message from it every 30 second or less, to keep service worker running. Currently this document’s lifetime is not limited (only audio playback is limited, which we don’t use), but it’s likely to change in the future.

  • manifest.json

      "permissions": ["offscreen"]
    
  • offscreen.html

    <script src="offscreen.js"></script>
    
  • offscreen.js

    // send a message every 20 sec to service worker
    setInterval(() => {
      chrome.runtime.sendMessage({ keepAlive: true });
    }, 20000);
    
  • background.js

    // create the offscreen document if it doesn't already exist
    async function createOffscreen() {
      if (await chrome.offscreen.hasDocument?.()) return;
      await chrome.offscreen.createDocument({
        url: 'offscreen.html',
        reasons: ['BLOBS'],
        justification: 'keep service worker running',
      });
    }
    chrome.runtime.onStartup.addListener(() => {
      createOffscreen();
    });
    // a message from an offscreen document every 20 second resets the inactivity timer
    chrome.runtime.onMessage.addListener(msg => {
      if (msg.keepAlive) console.log('keepAlive');
    });
    

“Persistent” service worker while nativeMessaging host is connected

In Chrome 105 and newer the service worker will run as long as it’s connected to a nativeMessaging host via chrome.runtime.connectNative. If the host process is terminated due to a crash or user action, the port will be closed, and the SW will terminate as usual. You can guard against it by listening to port’s onDisconnect event and call chrome.runtime.connectNative again.

“Persistent” service worker while a connectable tab is present

Downsides:

  • The need for an open web page tab
  • Broad host permissions (like <all_urls> or *://*/*) for content scripts which puts most extensions into the slow review queue in the web store.

Warning! If you already connect ports, don’t use this workaround, use another one for ports below.

Warning! Also implement the workaround for sendMessage (below) if you use sendMessage.

  • manifest.json, the relevant part:

      "permissions": ["scripting"],
      "host_permissions": ["<all_urls>"],
      "background": {"service_worker": "bg.js"}
    
    
  • background service worker bg.js:

    const onUpdate = (tabId, info, tab) => /^https?:/.test(info.url) && findTab([tab]);
    findTab();
    chrome.runtime.onConnect.addListener(port => {
      if (port.name === 'keepAlive') {
        setTimeout(() => port.disconnect(), 250e3);
        port.onDisconnect.addListener(() => findTab());
      }
    });
    async function findTab(tabs) {
      if (chrome.runtime.lastError) { /* tab was closed before setTimeout ran */ }
      for (const {id: tabId} of tabs || await chrome.tabs.query({url: '*://*/*'})) {
        try {
          await chrome.scripting.executeScript({target: {tabId}, func: connect});
          chrome.tabs.onUpdated.removeListener(onUpdate);
          return;
        } catch (e) {}
      }
      chrome.tabs.onUpdated.addListener(onUpdate);
    }
    function connect() {
      chrome.runtime.connect({name: 'keepAlive'})
        .onDisconnect.addListener(connect);
    }
    
  • all your other extension pages like the popup or options:

    ;(function connect() {
      chrome.runtime.connect({name: 'keepAlive'})
        .onDisconnect.addListener(connect);
    })();
    

If you also use sendMessage

In Chrome 99-101 you need to always call sendResponse() in your chrome.runtime.onMessage listener even if you don’t need the response. This is a bug in MV3. Also, make sure you do it in less than 5 minutes time, otherwise call sendResponse immediately and send a new message back via chrome.tabs.sendMessage (to the tab) or chrome.runtime.sendMessage (to the popup) after the work is done.

If you already use ports e.g. chrome.runtime.connect

Warning! If you also connect more ports to the service worker you need to reconnect each one before its 5 minutes elapse e.g. in 295 seconds. This is crucial in Chrome versions before 104, which killed SW regardless of additional connected ports. In Chrome 104 and newer this bug is fixed but you’ll still need to reconnect them, because their 5-minute lifetime hasn’t changed, so the easiest solution is to reconnect the same way in all versions of Chrome: e.g. every 295 seconds.

  • background script example:

    chrome.runtime.onConnect.addListener(port => {
      if (port.name !== 'foo') return;
      port.onMessage.addListener(onMessage);
      port.onDisconnect.addListener(deleteTimer);
      port._timer = setTimeout(forceReconnect, 250e3, port);
    });
    function onMessage(msg, port) {
      console.log('received', msg, 'from', port.sender);
    }
    function forceReconnect(port) {
      deleteTimer(port);
      port.disconnect();
    }
    function deleteTimer(port) {
      if (port._timer) {
        clearTimeout(port._timer);
        delete port._timer;
      }
    }
    
  • client script example e.g. a content script:

    let port;
    function connect() {
      port = chrome.runtime.connect({name: 'foo'});
      port.onDisconnect.addListener(connect);
      port.onMessage.addListener(msg => {
        console.log('received', msg, 'from bg');
      });
    }
    connect();
    

“Forever”, via a dedicated tab, while the tab is open

Instead of using the SW, open a new tab with an extension page inside, so this page will act as a “visible background page” i.e. the only thing the SW would do is open this tab. You can also open it from the action popup.

chrome.tabs.create({url: 'bg.html'})

It’ll have the same abilities as the persistent background page of ManifestV2 but a) it’s visible and b) not accessible via chrome.extension.getBackgroundPage (which can be replaced with chrome.extension.getViews).

Downsides:

  • consumes more memory,
  • wastes space in the tab strip,
  • distracts the user,
  • when multiple extensions open such a tab, the downsides snowball and become a real PITA.

You can make it a little more bearable for your users by adding info/logs/charts/dashboard to the page and also add a beforeunload listener to prevent the tab from being accidentally closed.

Caution regarding persistence

You still need to save/restore the state (variables) because there’s no such thing as a persistent service worker and those workarounds have limits as described above, so the worker can terminate. You can maintain the state in a storage, example.

Note that you shouldn’t make your worker persistent just to simplify state/variable management. Do it only to restore the performance worsened by restarting the worker in case your state is very expensive to rebuild or if you hook into frequent events listed in the beginning of this answer.

Future of ManifestV3

Let’s hope Chromium will provide an API to control this behavior without the need to resort to such dirty hacks and pathetic workarounds. Meanwhile describe your use case in crbug.com/1152255 if it isn’t already described there to help Chromium team become aware of the established fact that many extensions may need a persistent background script for an arbitrary duration of time and that at least one such extension may be installed by the majority of extension users.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)