Service Workers

  • Terminate when not in use

  • Listen to events like typing in url and stuff

    • Don't register async, it might not work

  • Communicate with content script to activate page actions and has access to chrome apis

  • Chrome terminates a service worker:

    • After 30 seconds of inactivity. Receiving an event or calling an extension API resets this timer.

    • When a single request, such as an event or API call, takes longer than 5 minutes to process.

    • When a fetch() response takes more than 30 seconds to arrive.

Transform Timers into Alarms#

DOM-based timers, such as window.setTimeout() or window.setInterval(), are not honored

  let timeout = 1000 * 60 * 3;  // 3 minutes in milliseconds
  window.setTimeout(function() {
    alert('Hello, world!');
  }, timeout);

Instead, use the alarms API.

  chrome.alarms.create({delayInMinutes: 3.0})

Then add a listener.

  chrome.alarms.onAlarm.addListener(function() {
    alert("Hello, world!")
  });

async function waitUntil(promise) = {
  const keepAlive = setInterval(chrome.runtime.getPlatformInfo, 25 * 1000);
  try {
    await promise;
  } finally {
    clearInterval(keepAlive);
  }
}

waitUntil(someExpensiveCalculation());

Last updated