AboutBlogContact
Web DevelopmentJuly 22, 2016 2 min read 122Updated: June 22, 2026

Progressive Web Apps: Mastering Offline Strategies with Service Workers (2016)

AunimedaAunimeda
📋 Table of Contents

Progressive Web Apps: Mastering Offline Strategies with Service Workers

The mobile web in 2016 is often frustrating. "Lie-fi"-where the phone shows bars but the connection is dead-is the enemy of a good user experience. Google is pushing the Progressive Web App (PWA) concept to solve this, and the heart of the PWA is the Service Worker.

A Service Worker is a script that runs in the background, separate from your web page. It acts as a programmable network proxy.

The Lifecycle

Before we can use a Service Worker, we must register it.

// main.js
if ('serviceWorker' in navigator) {
  navigator.serviceWorker.register('/sw.js').then(function(registration) {
    console.log('SW registered with scope:', registration.scope);
  });
}

Strategy: Stale-While-Revalidate

One of the most powerful patterns is "Stale-While-Revalidate." It shows the user the cached content immediately (speed!) while simultaneously fetching the latest version from the network and updating the cache for the next visit.

// sw.js
const CACHE_NAME = 'v1-cache';

self.addEventListener('fetch', function(event) {
  event.respondWith(
    caches.open(CACHE_NAME).then(function(cache) {
      return cache.match(event.request).then(function(response) {
        // Start the network fetch
        var fetchPromise = fetch(event.request).then(function(networkResponse) {
          // Update the cache with the new version
          cache.put(event.request, networkResponse.clone());
          return networkResponse;
        });
        
        // Return the cached response if we have it, otherwise wait for network
        return response || fetchPromise;
      });
    })
  );
});

Pre-caching the "App Shell"

To make the app feel like a native app, we should cache the "App Shell"-the core HTML, CSS, and JS-during the install event.

// sw.js
self.addEventListener('install', function(event) {
  event.waitUntil(
    caches.open(CACHE_NAME).then(function(cache) {
      return cache.addAll([
        '/',
        '/styles/main.css',
        '/scripts/app.js',
        '/images/logo.png'
      ]);
    })
  );
});

In 2016, we are finally breaking the dependency on a constant internet connection. If you build it right, your web app can be faster and more reliable than many native apps on the market.


Aunimeda develops websites and web applications for businesses - corporate sites, e-commerce, portals, and custom platforms.

Contact us to discuss your web project. See also: Web Development, E-commerce Development

Read Also

SolidJS: Fine-Grained Reactivity and the Death of the Virtual DOM (2021)aunimeda
Web Development

SolidJS: Fine-Grained Reactivity and the Death of the Virtual DOM (2021)

React is great, but why are we re-running our whole component tree? SolidJS proves that fine-grained reactivity is the faster path.

Svelte 3: The Framework that Disappears (2019)aunimeda
Web Development

Svelte 3: The Framework that Disappears (2019)

Svelte 3 is here, and it's doing away with the Virtual DOM entirely. Reactivity is now a language feature, not a library feature.

The TypeScript Tipping Point: Why We Stopped Shipping Raw JavaScript in 2018aunimeda
Web Development

The TypeScript Tipping Point: Why We Stopped Shipping Raw JavaScript in 2018

The days of 'undefined is not a function' are numbered. In 2018, we have officially moved all new agency projects to TypeScript. Here is why static typing is the best investment for long-term project health.

Need IT development for your business?

We build websites, mobile apps and AI solutions. Free consultation.

Web Development

Get Consultation All articles