DEV BOX

'26/12/02 UPDATE

WEB / SERVICE WORKER / FIELD NOTES

I deployed a new service worker. Why does refresh still show the old page?

A new service worker waits while the old one controls a tab. On Chromium 154 this note checks what a refresh and a hard reload change, how skipWaiting and reload-everything recipes can damage tabs and typed text, and runs a button approach and a worker removal.

You deployed a site that uses a service worker, and several refreshes later the old page is still there. Before you look at the server or the CDN, there is another place to check: the new worker may have finished installing and be waiting. After this note you can confirm that state with one console line, and tell which ways of moving to the new version damage typed text or tabs that are already open.

The example ran on 2026-11-02 on macOS 27.0.1 with a Node.js 24.21.0 server and a browser built on Chromium 154.0.8037.100 (--headless=new), with a new empty profile for each scenario. Reloads were sent with Page.reload from the DevTools Protocol, not the keyboard. The final script ran in full twice, and the output matched except for the time on its first line and one row of the count table shown later. Safari, Firefox, phone browsers and a windowed Chromium were not run. The site and versions are fictional. Apart from one case noted later, the HTML and scripts were sent with Cache-Control: no-cache to leave less room for the HTTP cache.

The server has v2, the refresh shows v1

The worker uses a cache-first design. On install it fills a cache named after its version, on activate it deletes other caches, and it looks in its own cache first for each request.

const VERSION = 'v1';
const CACHE = `app-${VERSION}`;
const FILES = ['/', '/app.js', `/chunk-${VERSION}.js`];

self.addEventListener('install', event => {
  event.waitUntil(caches.open(CACHE).then(cache => cache.addAll(FILES)));
});

self.addEventListener('activate', event => {
  event.waitUntil(caches.keys().then(keys => Promise.all(
    keys.filter(key => key !== CACHE).map(key => caches.delete(key))
  )));
});

self.addEventListener('fetch', event => {
  event.respondWith(
    caches.open(CACHE)
      .then(cache => cache.match(event.request))
      .then(hit => hit || fetch(event.request))
  );
});

After the server moved to v2, a refresh still showed v1 and the page request never reached the server. Right after the first refresh no worker was waiting either; one appeared about 9 seconds later. The spec tells the browser to run an update check while handling a page navigation and sets no such wait, so the 9 seconds is only a value measured on the first run in a fresh profile. More refreshes after that still showed v1. v2 appeared only after the tab was closed and a new one opened.

scenario.mjs reload output. After the server moves to v2, reload 1 shows page v1, waiting no, GET / 0. 9 s later waiting yes, GET / 1. Reloads 2 and 3 show page v1, waiting yes, GET / 0. Closing the tab and opening a new one shows page v2, waiting no, GET / 0.
The GET / column counts page requests that reached the server at that step. Reloads 1 to 3 show 0 with page v1, so the old page came from the v1 worker, not the server. The 1 at 9 s later is v2 filling its cache during install.

Why a refresh does not move it along

When an old worker is active, the Service Workers spec activates the new one only if the old worker has no pending events, and either no client is using the registration or the new worker asked to skip waiting. The web.dev lifecycle article (marked last updated 2016-09-29) explains why a refresh does not meet this, in terms of how navigation works. The current page does not go away until the response headers arrive, so the old worker is still controlling a client during the refresh.

DEV NOTES

The new worker stops at waiting

The path of v2 while v1 controls a tab

  1. v2 installs

    cache.addAll(FILES)

    • The fetched sw.js differs
    • from the old one: install runs
  2. v2 waits

    registration.waiting

    • It stops while v1 controls a tab
    • A reload overlaps old and new pages
  3. v2 activates

    activate

    • All tabs closed or moved away
    • or skipWaiting() was called

The author's shortened drawing of the web.dev lifecycle article and the Service Workers spec. The real lifecycle has more steps.

The 'v2 waits' card is the answer here. A reload briefly overlaps the old and new pages, so the number of tabs v1 controls never reaches zero, and v2 does not move on to the 'v2 activates' card.

Several tabs do not change this. In a run with tabs A and B open, refreshing A stayed on v1, and refreshing A after closing B also stayed on v1. Moving A to about:blank and back gave v2. This console line shows which state you are in. The output below it comes from a run in the same state.

navigator.serviceWorker.getRegistration().then(r => ({ active: r?.active?.state, waiting: r?.waiting?.state })).then(JSON.stringify)
// {"active":"activated","waiting":"installed"}

A hard reload can fool your check

The web.dev article says a force reload bypasses the worker and leaves the page uncontrolled. Sending it through the ignoreCache option of Page.reload, which the protocol documentation equates with Shift+refresh, rather than the keyboard also gave controller no. The trouble comes next. With no waiting worker yet, v2 showed once and the next plain refresh brought v1 back. With v2 already waiting, it usually moved to v2. The author's reading is that the page left control, so no client was using v1 any more; the spec does not describe this scene separately. It was not always so. With no pause between the hard reload and the next refresh, 1 round in 5 brought v1 back (0 in the repeated full run), and one full run made before the script was revised brought v1 back even after a 1 second pause. The counts are small, so do not read them as rates.

scenario.mjs hard output: with no waiting worker, hard reload gives page v2, controller no, and the next plain reload gives page v1, controller yes. The hardrace table counts 5 rounds per pause with v2 waiting: 0 s has v2 4 and v1 1; 1 s and 5 s have v2 5 and v1 0.
The hard group above has no waiting worker, and its plain reload line went back to page v1. The hardrace table below counts 5 rounds each with v2 already waiting, and its 0 s row has 1 round where v1 came back.

So when you check a deployment, seeing the new page after a hard reload does not by itself mean a plain refresh will show it. The table and the group before it are the counterexamples.

What skipWaiting brings along

The way to remove the wait is to call self.skipWaiting() in install, and MDN notes that calling it there is common. The version tested here also calls clients.claim() in activate. The wait goes away, but a v1 tab that is already open keeps its v1 HTML and is now controlled by the v2 worker. The web.dev article also warns that the old and new workers can each handle part of one page's requests.

A run reproduced this. The server removes the old chunk-v1.js at deploy time, and after v2 arrives the v1 tab loads the code for a screen it has not opened yet. Without skipWaiting the v1 worker served the file from its cache and the screen opened. With it, the load failed with a TypeError. With the old file left on the server, it opened again. This site lists that file in FILES ahead of time, so a file that was not precached may behave differently.

scenario.mjs chunk output. no skipWaiting: tab v1, waiting yes, ok chunk-v1. skip+claim: tab v1, waiting no, failed TypeError. skip+claim+kept: tab v1, waiting no, ok chunk-v1.
The tab column is v1 on all three rows. Only the skip+claim row failed to load a screen the tab had not opened yet. The tab still has v1 HTML, the v2 worker that took the request has no chunk-v1.js in its cache, and the server has removed the file.

Show a button, reload only the tab that pressed it

The other route the web.dev article offers is to call skipWaiting() after a user action. Add message handling to the worker and show a button when the page sees a waiting worker. But calling location.reload() on every controllerchange (update-reload-all.js) also reloads tabs where nobody pressed anything. In the run, the text being typed in tab B (unsaved note) and a value held in memory vanished with no warning. With the change below (update-prompt.js), only the pressing tab reloaded, so tab B kept its text and only a notice value was set. updateReady and notice in the example exist so the run can read results without a real screen; replace them with your own button and message.

// sw.js
self.addEventListener('message', event => {
  if (event.data === 'SKIP_WAITING') self.skipWaiting();
});

// page
function watch(reg) {
  if (reg.waiting) window.updateReady = true;
  reg.addEventListener('updatefound', () => {
    reg.installing.addEventListener('statechange', e => {
      if (e.target.state === 'installed' && navigator.serviceWorker.controller) window.updateReady = true;
    });
  });
}
navigator.serviceWorker.getRegistration().then(reg => reg && watch(reg));

let pressedHere = false;
window.pressUpdate = async () => {
  const reg = await navigator.serviceWorker.getRegistration();
  if (!reg?.waiting) return;
  pressedHere = true;
  reg.waiting.postMessage('SKIP_WAITING');
};
navigator.serviceWorker.addEventListener('controllerchange', () => {
  if (pressedHere) location.reload();
  else window.notice = 'new version: save your work, then reload';
});

That is not the end of it. Tab B still has v1 HTML and the v2 worker now takes its requests, so with old files removed, opening a new screen failed. Both the text and the new screen survived only when the old files stayed on the server. A run cannot say how long to keep them. It depends on how long tabs stay open, so decide for your service. It is safer to wrap dynamic import() in try/catch and, when it fails, ask the user to save their work and reload. The code above does not do this.

scenario.mjs prompt output. update-reload-all.js: tabs A and B become v2, B textarea is empty and memory is null. update-prompt.js with old files removed: B stays v1, text stays unsaved note, open screen failed TypeError. update-prompt.js with old files kept: text stays and open screen is ok chunk-v1.
The button was pressed only in tab A, and the typed text was in tab B. In the first group the B textarea line is empty; the lower two groups kept the text, and only the last one, with old files kept, has an ok B open screen line.

To remove a worker, do not just delete sw.js

Deleting sw.js from the server to get rid of a bad worker did not help. With /sw.js answering 404, two refreshes over 20 seconds left the registration in place and the page on v1. Deploying a worker that removes itself at the same URL did clear the registration, and the next refresh brought v2 from the server.

self.addEventListener('install', () => self.skipWaiting());
self.addEventListener('activate', event => {
  event.waitUntil(
    caches.keys()
      .then(keys => Promise.all(keys.map(key => caches.delete(key))))
      .then(() => self.registration.unregister())
  );
});

This code deletes every Cache API cache of the origin. Caches stored by other features on that origin go too, and offline behaviour ends, so choosing only the names to delete may be better. The new page must also stop calling register(), and the run left that call out. One more point: sw.js sent with max-age=31536000 still led to the new worker being found. updateViaCache defaulted to imports. According to the web.dev article, files loaded with importScripts() do follow cache headers; that part was not run.

When the old page shows up after a deploy, read waiting with the console line before you clear any cache. If it has a value, the problem is not the server but how the new version takes over. If it is empty, do not blame the server yet: in the 2026-11-02 run on Chromium 154, waiting was empty right after the first refresh and appeared about 9 seconds later, so check again after a short wait. Weigh each way of moving to the new version on two things: does it protect typed text in tabs, and are the files the old HTML will ask for still there? This order is the author's judgement, drawn from the runs and documents above.

THE END
RSS feedBack to the library
COMMENTS BOX

Add your perspective.

Share a question, another approach, or something you have tried.

Newest first

Checking sign-in…

Loading comments…