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
- Registering a Service Worker
- The Cache API
- Caching Strategies
- Background Sync and Updates