Service Workers & Offline Caching
Also Known As
service worker
Workbox
cache strategies
offline caching
TL;DR
A JavaScript worker that intercepts network requests and serves responses from cache — enabling offline access, faster loads, and background sync.
Explanation
Service Workers run in a separate thread, intercept fetch events, and implement caching strategies: Cache First (serve from cache, fall back to network — ideal for static assets), Network First (try network, fall back to cache — for fresh content), Stale While Revalidate (serve cache immediately, update in background — balance of speed and freshness), Cache Only, and Network Only. Workbox (Google) simplifies service worker patterns. Lifecycle: install (precache static assets), activate (clean old caches), fetch (intercept requests). Service workers require HTTPS (or localhost).
Common Misconception
✗ Service workers are complex to implement from scratch — Workbox provides high-level caching strategies with two lines of configuration, abstracting the complex service worker lifecycle.
Why It Matters
A site without a service worker re-downloads static assets on every visit — a service worker with Cache First for assets reduces repeat visit load time to near-zero for cached resources.
Common Mistakes
- Cache First for HTML pages — users see stale content after deploys.
- No cache versioning — old caches never cleaned, storage grows indefinitely.
- Caching third-party resources without CORS headers — cached responses may be opaque.
- Not handling service worker update lifecycle — new service worker waits for old tabs to close.
Code Examples
✗ Vulnerable
// Cache everything forever — stale content after deploy:
self.addEventListener('fetch', e => {
e.respondWith(
caches.match(e.request).then(r => r || fetch(e.request))
);
// HTML pages cached forever — users see old version after deploy!
});
✓ Fixed
// Workbox — strategy per resource type:
import { registerRoute } from 'workbox-routing';
import { CacheFirst, NetworkFirst, StaleWhileRevalidate } from 'workbox-strategies';
// Static assets: cache forever (content-hashed filenames):
registerRoute(({url}) => url.pathname.match(/\.(js|css|woff2)$/),
new CacheFirst({ cacheName: 'assets-v1' }));
// HTML pages: network first, cache fallback:
registerRoute(({request}) => request.mode === 'navigate',
new NetworkFirst({ cacheName: 'pages-v1', networkTimeoutSeconds: 3 }));
// API: stale while revalidate:
registerRoute(({url}) => url.pathname.startsWith('/api/'),
new StaleWhileRevalidate({ cacheName: 'api-v1' }));
References
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
16 Mar 2026
Edited
22 Mar 2026
Views
22
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
No pings yesterday
Amazonbot 8
Perplexity 5
Unknown AI 3
Ahrefs 2
Google 2
How they use it
crawler 18
crawler_json 1
pre-tracking 1
Related categories
⚡
DEV INTEL
Tools & Severity
🟡 Medium
⚙ Fix effort: High
⚡ Quick Fix
Use Workbox with a network-first strategy for HTML (always fresh) and cache-first for versioned assets (always fast) — don't cache API responses that include user data
📦 Applies To
javascript ES2015
web
🔍 Detection Hints
Service worker caching all responses including authenticated API responses; no offline fallback page; stale HTML served after deployment
Auto-detectable:
✓ Yes
lighthouse
workbox
chrome-devtools
⚠ Related Problems
🤖 AI Agent
Confidence: Medium
False Positives: Medium
✗ Manual fix
Fix: Medium
Context: File
Tests: Update