MohitDecodes
Back to blogsJavaScript

Progressive Web Apps (PWA): Service Workers and Caching Strategies

November 8, 2026 · 6 min read

A Progressive Web App isn't just a manifest JSON file that adds an install prompt. The real superpower is the **Service Worker** — a background script sitting between your web app and the network.

Pick the right caching strategy per asset type

  • Cache-First: best for hashed static assets (`app.a3f9c.js`, fonts, icons) that never change at the same URL.
  • Network-First: best for HTML pages and live API data where fresh content matters, falling back to cache only when offline.
  • Stale-While-Revalidate: serve cached content instantly for speed while refreshing the cache in the background for the next visit.
self.addEventListener("fetch", (event) => {
  if (event.request.destination === "image") {
    event.respondWith(
      caches.match(event.request).then((cached) => cached || fetch(event.request))
    );
  }
});

Watch out for the classic deployment trap

Never cache `sw.js` aggressively with long HTTP `Cache-Control` headers, and always clean up old cache versions in the `activate` event — otherwise users stay stuck on yesterday's bundle after you ship a bug fix.