EMZETT.
Login

Service workers

In short: A service worker is a script that runs in the browser’s background and can intercept a page’s network requests. It enables offline mode, caching and push notifications.

In more detail: The service worker sits between page and network. For each request it decides whether to answer from cache, fetch from the network, or combine both. It has no access to the page itself but communicates via messages.

In Depth

  • Lifecycle: register, install (pre-cache files), activate, then serve requests.
  • Strategies: Cache-first (fast, for static files), network-first (fresh, for data), stale-while-revalidate (instantly old, new in the background).
  • Requirement: Only over HTTPS (or localhost).
  • Pitfall: A faulty service worker can stick in the browser. New versions must clean up the cache properly.
  • Related: Push API, background sync and installability as an app.

See also: Lighthouse and Core Web Vitals