Offline-First: The PWA Competitive Advantage
How Progressive Web Apps bridge the gap between web and native mobile.
Introduction
Users expect apps to work. Always. But networks fail. Users board flights, enter tunnels, lose signal in elevators. Native apps degrade gracefully. Web apps crash.
Progressive Web Apps (PWAs) change this. Service Workers cache content and handle offline scenarios. Your web app feels like a native app. Users stay engaged, even without connection.
This is part one of a two-part series on building PWAs that compete with native applications.
Problem Definition
Traditional web apps have a fatal flaw: they need internet.
- Network Dependency: No connection = blank screen. Users leave.
- Connection Instability: Slow 3G means slow load times. Users bounce.
- Data Waste: Downloading the same assets repeatedly wastes user data and battery.
- No Native Feel: Web apps don’t behave like native apps. No offline mode. No background sync.
Native apps solve this. They cache locally. They work offline. But native apps require distribution through app stores, take up device storage, and demand native developers.
PWAs bridge the gap: web apps with native capabilities.
Service Workers: The Foundation
A Service Worker is a JavaScript file that runs in the background, separate from the web page. It intercepts network requests and decides what to serve.
// Register the service worker
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/sw.js');
}
The service worker can serve cached content instead of making network requests:
// Inside sw.js
self.addEventListener('install', event => {
event.waitUntil(
caches.open('v1').then(cache => {
return cache.addAll([
'/',
'/styles.css',
'/app.js',
'/offline.html'
]);
})
);
});
Caching Strategies
The Stale-While-Revalidate strategy serves cached content instantly and updates it in the background:
- User requests a resource.
- Serve the cached version immediately (instant load).
- Fetch from the network in the background.
- Update the cache with new data.
- Next visit gets the fresh version.
This is ideal for content that updates occasionally (articles, product listings).
self.addEventListener('fetch', event => {
event.respondWith(
caches.match(event.request).then(cached => {
// Return cached version immediately
const fetchPromise = fetch(event.request).then(networkRes => {
// Update cache in the background
caches.open('dynamic').then(cache => {
cache.put(event.request, networkRes.clone());
});
return networkRes;
});
// Return cached OR fetch result
return cached || fetchPromise;
})
);
});
Network-First Strategy
For real-time data (chat messages, notifications), fetch from the network first:
self.addEventListener('fetch', event => {
event.respondWith(
fetch(event.request)
.then(networkRes => {
// Succeeded: cache and return
caches.open('dynamic').then(cache => {
cache.put(event.request, networkRes.clone());
});
return networkRes;
})
.catch(() => {
// Failed: return cached version
return caches.match(event.request)
.then(cached => cached || caches.match('/offline.html'));
})
);
});
Complete Example: E-Commerce PWA
Here’s a realistic offline-first e-commerce setup:
const CACHE_NAME = 'shop-v1';
const URLS_TO_CACHE = [
'/',
'/index.html',
'/styles/main.css',
'/scripts/app.js',
'/images/logo.png',
'/offline.html'
];
// Install: cache essential files
self.addEventListener('install', event => {
event.waitUntil(
caches.open(CACHE_NAME).then(cache => {
return cache.addAll(URLS_TO_CACHE);
})
);
});
// Fetch: implement smart caching
self.addEventListener('fetch', event => {
const { request } = event;
const url = new URL(request.url);
// API calls: network-first
if (url.pathname.startsWith('/api/')) {
event.respondWith(
fetch(request)
.then(res => {
caches.open('api-cache').then(cache => {
cache.put(request, res.clone());
});
return res;
})
.catch(() => caches.match(request))
);
}
// Static assets: cache-first
if (request.method === 'GET' &&
(url.pathname.match(/\.(js|css|png|jpg|gif)$/))) {
event.respondWith(
caches.match(request)
.then(cached => cached || fetch(request).then(res => {
caches.open('static-cache').then(cache => {
cache.put(request, res.clone());
});
return res;
}))
);
}
// HTML: stale-while-revalidate
event.respondWith(
caches.match(request).then(cached => {
const fetchPromise = fetch(request).then(res => {
caches.open(CACHE_NAME).then(cache => {
cache.put(request, res.clone());
});
return res;
});
return cached || fetchPromise;
})
);
});
// Activate: clean old caches
self.addEventListener('activate', event => {
event.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.map(name => {
if (name !== CACHE_NAME) {
return caches.delete(name);
}
})
);
})
);
});
Usage in HTML:
<!-- Show offline indicator -->
<div id="offline-banner" style="display: none;">
You are offline. Some features may be limited.
</div>
<script>
window.addEventListener('offline', () => {
document.getElementById('offline-banner').style.display = 'block';
});
window.addEventListener('online', () => {
document.getElementById('offline-banner').style.display = 'none';
});
</script>
This approach guarantees:
- Instant loads: Cached content serves immediately.
- Works offline: Users browse products even without connection.
- Smart updates: Real-time data refreshes in background.
- Data efficient: Reduces repeated downloads.
Conclusion
Offline-first design isn’t a luxury for PWAs. It’s the foundation. Service Workers intercept requests and make intelligent caching decisions.
Master stale-while-revalidate and network-first strategies, and your web app will feel native.
The next article covers push notifications and re-engagement—how to bring users back even after they close your app.