Service worker with skipWaiting() and claim() still results in stale content

I have SPA without any navigation and service worker caching all static content (HTML, JS, CSS, images).
The service worker perfectly serves content while offline but updates are complicated.

  • The service worker has stable name service-worker.js.
  • GIT commit hash is used as a cache name, thus deployment results in new content of the service worker.
  • Service worker calls skipWaiting() during install and clients.claim() during activate to immediately take control.
  • registration.update() is called after navigator.serviceWorker.register() to make sure the latest service worker version is used on each page refresh.

Despite all this, after deploying the new SPA version with changed content and service worker and refreshing the page (Ctrl + R) I still get stale content. But after the second page refresh (Ctrl + R) I get the updated content.

To not depend on refreshing the page twice, I added window.location.refresh() on controllerchange event. This way second page refresh happens automatically and transparently to user.

I understand that skipWaiting() and clients.claim() are races, so you’ll only see the updated content if the new service worker fetches, installs and activates before the page tries to load the static content. Are there a recommended solution to this problem?

service-worker.ts

import {manifest} from '@parcel/service-worker';

async function install(): Promise<void> {
  const cache = await caches.open(commitHash);
  await cache.addAll(['/', ...new Set(manifest)]);
}
self.addEventListener('install', event => {
  void self.skipWaiting();
  event.waitUntil(install());
});

async function activate(): Promise<void> {
  const keys = await caches.keys();
  await Promise.all(keys.filter(key => key !== commitHash).map(key => caches.delete(key)));
  await self.clients.claim();
}
self.addEventListener('activate', event => event.waitUntil(activate()));

async function cacheFallingBackToNetwork(request: Request): Promise<Response> {
  const cacheResponse = await caches.match(request);
  return cacheResponse || (await fetch(request));
}

self.addEventListener('fetch', (event: FetchEvent) => {
  const {request} = event;
  const url = new URL(request.url);
  if (url.origin === location.origin) {
    event.respondWith(
      cacheFallingBackToNetwork(request).catch(reason => {
        console.error(reason);
        return new Response('', {
          status: 500,
          statusText: 'offline',
        });
      })
    );
  }
});

app.ts

if ('serviceWorker' in navigator) {
  void navigator.serviceWorker
    .register(new URL('service-worker.ts', import.meta.url), {
      type: 'module',
      updateViaCache: 'none',
    })
    .then((registration: ServiceWorkerRegistration) => {
      void registration.update().catch(reason => console.error(reason));
    });

  let refreshing: boolean;
  navigator.serviceWorker.addEventListener('controllerchange', function () {
    if (refreshing) {
      return;
    }
    refreshing = true;
    window.location.reload();
  });
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật