Browser extension background service worker implementation

Our company is engaged in the development, support and maintenance of sites of any complexity. From simple one-page sites to large-scale cluster systems built on micro services. Experience of developers is confirmed by certificates from vendors.
Development and maintenance of all types of websites:
Informational websites or web applications
Business card websites, landing pages, corporate websites, online catalogs, quizzes, promo websites, blogs, news resources, informational portals, forums, aggregators
E-commerce websites or web applications
Online stores, B2B portals, marketplaces, online exchanges, cashback websites, exchanges, dropshipping platforms, product parsers
Business process management web applications
CRM systems, ERP systems, corporate portals, production management systems, information parsers
Electronic service websites or web applications
Classified ads platforms, online schools, online cinemas, website builders, portals for electronic services, video hosting platforms, thematic portals

These are just some of the technical types of websites we work with, and each of them can have its own specific features and functionality, as well as be customized to meet the specific needs and goals of the client.

Our competencies:
Development stages
Latest works
  • image_website-b2b-advance_0.png
    B2B ADVANCE company website development
    1217
  • image_web-applications_feedme_466_0.webp
    Development of a web application for FEEDME
    1161
  • image_websites_belfingroup_462_0.webp
    Website development for BELFINGROUP
    852
  • image_ecommerce_furnoro_435_0.webp
    Development of an online store for the company FURNORO
    1046
  • image_crm_enviok_479_0.webp
    Development of a web application for Enviok
    823
  • image_bitrix-bitrix-24-1c_fixper_448_0.png
    Website development for FIXPER company
    815

Implementation of Background Service Worker in Browser Extension

In Manifest V3, background script became a service worker. This is not just renaming — the lifecycle model changed. Service worker can be terminated by browser at any moment when there are no active tasks, and this breaks patterns that worked in MV2 with persistent background page.

Registration in manifest

{
  "manifest_version": 3,
  "background": {
    "service_worker": "background/sw.js",
    "type": "module"
  }
}

type: "module" allows using ES-modules and import inside service worker. Supported in Chrome 93+.

Lifecycle: what really happens

Browser starts service worker on:

  • extension install/update
  • message from content script or popup
  • alarm trigger (chrome.alarms)
  • network event (if subscribed)

After all handlers complete, browser may kill process after ~30 seconds of inactivity. Next event will raise it again — with clean state.

This means: no global state in memory. Variables don't survive restart.

// BAD — state lost on SW restart
let requestCount = 0;

chrome.runtime.onMessage.addListener(() => {
  requestCount++; // reset to 0 after restart
});

// GOOD — save in chrome.storage
chrome.runtime.onMessage.addListener(async () => {
  const { requestCount = 0 } = await chrome.storage.local.get('requestCount');
  await chrome.storage.local.set({ requestCount: requestCount + 1 });
});

Event handling: sync registration required

Event handlers must be registered synchronously at top level. If you register them inside async function or after await — browser may not "see" them when starting SW to handle event:

// background/sw.js

// CORRECT — sync registration at top level
chrome.runtime.onInstalled.addListener(onInstalled);
chrome.runtime.onMessage.addListener(onMessage);
chrome.alarms.onAlarm.addListener(onAlarm);

// Implementations can be async
async function onInstalled(details) {
  if (details.reason === 'install') {
    await chrome.storage.sync.set({ settings: defaultSettings });
  }
}

function onMessage(message, sender, sendResponse) {
  handleMessage(message, sender).then(sendResponse);
  return true; // must return true for async responses
}

Long-lived connections via Port

For tasks that take more than few seconds (streaming, polling), use chrome.runtime.connect(). Active connection keeps SW alive:

chrome.runtime.onConnect.addListener((port) => {
  if (port.name === 'long-running-task') {
    handleLongRunningTask(port);
  }
});

Timeline

Simple event handlers: 1–2 days. Complex with alarms, sync, long-running tasks: 3–5 days.