0Pricing
JavaScript Academy · Lesson

Background Sync and Updates

Update caches and sync when back online.

Keeping Caches Fresh

An offline-capable app must update gracefully: ship new assets, evict old ones, and retry failed actions when connectivity returns. Service workers provide tools for all three.

Versioned Cache Names

The simplest update mechanism is a versioned cache name. Bumping the version creates a fresh cache, leaving the old one to be cleaned up on activation.

const CACHE = 'app-v2'; // was 'app-v1'
self.addEventListener('install', (e) => {
  e.waitUntil(
    caches.open(CACHE).then(c => c.addAll(ASSETS))
  );
});

All lessons in this course

  1. Registering a Service Worker
  2. The Cache API
  3. Caching Strategies
  4. Background Sync and Updates
← Back to JavaScript Academy