Service workers and speed: caching, and what it costs

A service worker can make repeat visits fast and pages work offline, but it adds startup time and the risk of stale pages. How it works and when it is worth it.

AdvancedExplainer6 min readUpdated October 30, 2026

In this guide
  1. What a service worker is
  2. How it can make a site faster
  3. What it costs
  4. Caching strategies
  5. A starting point
  6. Update safely
  7. When it is worth it
  8. How to measure it
  9. Check your own page
  10. Sources

What a service worker is

MDN says service workers essentially act as proxy servers that sit between web applications, the browser and the network. A service worker is an event-driven script registered for an origin and a path. It can intercept navigation and resource requests, modify them, and cache resources with fine control. It runs in a worker context: no access to the page, and a thread of its own. Synchronous XHR and Web Storage cannot be used inside it, and dynamic import() is not allowed, although static imports are. Service workers only work on secure origins, so the site must use HTTPS, with http://localhost as the exception for development.

Its life has stages. The page registers it, the browser downloads and installs it, and then it activates and starts to handle requests. Because that happens after the page has loaded, a service worker cannot speed up a visitor's first visit. It helps on the visits that follow.

A service worker is not the same as an ordinary web worker. See web workers for those.

How it can make a site faster

What it costs

Caching strategies

A caching strategy is the way a service worker's fetch handler combines the network and the cache. Workbox, the library from the Chrome team, describes five:

StrategyWhat it doesGood forWatch out for
Cache firstAnswers from the cache, and goes to the network only when the file is missingFiles with a version in the name, fonts, logosStale files if the name does not change
Network firstTries the network, and falls back to the cachePages that change often, dataA slow network makes the visitor wait
Stale-while-revalidateAnswers from the cache, then updates the cache from the network in the backgroundContent that matters but need not be the latest, such as avatarsShows the previous version once
Network onlyAlways asks the networkRequests that must never be cachedNo offline support
Cache onlyAnswers only from the cacheFiles stored at install timeFails if the file is not there

Match the strategy to the content. Versioned static files can go cache first, in the same way as a long max-age with immutable. HTML is the sensitive one. It carries the links to the current files, so a stale page can point to files that no longer exist.

A starting point

The code below turns on navigation preload and splits the work in two. Pages come from the network first, using the preloaded response, with the cache as a fallback when the visitor is offline. Other files come from the cache first. It is adapted from the MDN documentation, and it leaves out the part that fills the cache, which you would do when the worker installs or as files are fetched:

// sw.js
addEventListener('activate', (event) => {
  event.waitUntil((async () => {
    if (self.registration.navigationPreload) {
      await self.registration.navigationPreload.enable();
    }
  })());
});

addEventListener('fetch', (event) => {
  const { request } = event;
  event.respondWith((async () => {
    if (request.mode === 'navigate') {
      try {
        return (await event.preloadResponse) || (await fetch(request));
      } catch {
        return (await caches.match(request)) || Response.error();
      }
    }
    return (await caches.match(request)) || fetch(request);
  })());
});

For anything beyond this, a library is easier and safer than hand-written handlers. Workbox provides ready-made strategies and the code to manage the caches.

Update safely

When it is worth it

A service worker is worth the work for:

It is probably not worth it for a brochure site with few repeat visits, for pages that are personalized, or when nobody on the team can maintain it. A first visit is the one most sites lose, and a service worker cannot change that. A fast server, a CDN and good headers help every visit.

How to measure it

Check your own page

Run the free audit, which tests a first visit. A service worker will not change that result, so its score tells you how your site performs for a new visitor. If most of your traffic is repeat visitors, look at the real-visitor numbers as well.

Sources

Start here

Free website audit

Live Google data for any public page: mobile and desktop.

Mobile + desktop · Lighthouse lab test · Real-user Chrome data when Google has it

Rather have this done for you? Speed optimization · Book a call

Related guides

All guides →