Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 36 ยท Application and Storage

Service Workers and Cache Storage

Understand the service worker lifecycle, use the Application panel to update, bypass and unregister workers, inspect Cache storage and test offline mode.

A service worker is a script that sits between your page and the network. It can answer requests from a cache, make the site work offline, receive push messages and sync data in the background. It is also the source of the most baffling developer experience on the web: you fix a bug, reload, and still see the old version, because the old worker is quietly serving old files.

DevTools has dedicated tools for this. In this lesson you learn the lifecycle, the controls that save your sanity, and how to inspect what a worker cached.

The lifecycle

A worker goes through states, and DevTools shows them in Application > Service workers:

  1. Installing: the browser downloaded the script and ran its install event, usually to pre-cache files.
  2. Waiting (installed): a new version is ready but an older worker still controls open pages, so it waits.
  3. Activating / Activated: the activate event ran; the worker now controls pages (new pages, or old ones after a reload, or immediately if it calls clients.claim()).
  4. Redundant: replaced by a newer version.

The key rule: a new worker waits until every tab using the old one is closed, unless the code calls skipWaiting(). That is why "I deployed and nothing changed" happens. The panel shows both workers side by side and offers a skipWaiting link on the waiting one.

The controls you will use daily

At the top of the Service workers pane:

  • Offline: makes the page behave as if the network is gone, so you can test offline fallbacks. (The Network panel has a matching throttling option.)
  • Update on reload: installs the newest script on every refresh, skipping the waiting stage. Turn this on during development.
  • Bypass for network: ignores the worker and sends every request to the network. Handy to check whether a bug is in your code or in the cache.

For each registration you also have Update (check for a new script), Unregister, and test buttons for Push, Sync and Periodic sync events with custom payloads. A link opens the worker script's source and Inspect, which attaches a separate DevTools window to the worker so you can use breakpoints and the Console inside it. Logs from console.log in the worker only appear there, not in the page's Console.

Cache storage

Workers typically store responses with the Cache API. Open Application > Cache storage and expand a cache name (for example static-v3). The table lists each cached URL with status, content type, size and cache time. Select a row to preview the stored response; right-click to delete one entry, or delete the whole cache with the X.

Typical bugs you can spot here: a cache that keeps growing because old versions are never deleted, a cached HTML page that points to JavaScript files which no longer exist after a deploy, or a cached error response such as a 404 or 500.

Common caching strategies

The worker decides how to answer each request:

Strategy Behavior Good for
Cache first Return the cache; fetch only if missing Versioned static files
Network first Try the network; fall back to the cache HTML, API data that should be fresh
Stale while revalidate Return cache now, refresh it in the background Avatars, non-critical data

The stand-in below simulates a tiny worker with a Map as the cache and a fake network that can be turned off. It lets you try each strategy in the console.

JS Stand-in: simulate caching strategies

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The first line shows the classic stale-asset bug: cache-first returns v1 although the server has v2. This is why static assets are served under hashed file names (app.3f9a1c.js) so a new version is a new URL.

A worker's version-cleanup pattern

A worker should delete old caches in activate. Here is that logic as a stand-in you can run:

JS Stand-in: delete outdated caches on activate

Press Run (or Ctrl + Enter) to see the console output here.

No output.

In a real worker the same idea is written with caches.keys() and caches.delete(name) inside event.waitUntil(...).

Debugging checklist

  1. Open Application > Service workers. Is the version you expect activated? If a newer one is waiting, click skipWaiting or enable Update on reload.
  2. Tick Bypass for network and reload. If the bug disappears, a cached response is the cause.
  3. Open Cache storage and look for stale or error responses; delete them.
  4. Use Clear site data with "Unregister service workers" ticked for a perfectly clean start.
  5. Test offline with the Offline checkbox and check the fallback page.
  6. Remember a worker only works on HTTPS (and localhost).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does a new service worker version sometimes stay in the 'waiting' state?

    A new worker waits until no pages are controlled by the old one, unless skipWaiting() is called.
  2. 2. Which DevTools option ignores the service worker so every request goes to the network?

    Bypass for network sends requests straight to the network, which helps find out if the worker is the culprit.
  3. 3. Which strategy returns the cache immediately and updates it in the background?

    Stale while revalidate answers fast from the cache and refreshes it for next time.
  4. 4. Where do console.log calls made inside the service worker appear?

    The worker runs in its own context; use Inspect on the registration to see its console and set breakpoints.

Summary

  • Service workers go through install, waiting, activate and redundant stages.
  • Use Update on reload and Bypass for network while developing; use Offline to test fallbacks.
  • Cache storage shows what the worker saved; stale or error entries are common bugs.
  • Choose a strategy per resource, hash static file names and delete old caches on activate.

Next: The Memory Panel and Heap Snapshots