What Is a Service Worker? PWA Caching and Offline Mode Explained
Service workers are scripts that run in the background, separate from your web page. They power offline mode, background sync and push notifications in Progressive Web Apps.
ProfitTool Team
Developer Tools
What a Service Worker Is
A service worker is a JavaScript file that runs in a separate thread from the main browser page. It acts as a programmable network proxy — intercepting requests, serving from cache, syncing in the background, and receiving push notifications.
Service workers are the foundation of Progressive Web Apps (PWAs).
Core Capabilities
Offline Mode
Service workers cache assets and API responses. When the user is offline, the service worker serves cached content:
self.addEventListener('fetch', (event) => {
event.respondWith(
caches.match(event.request).then(cached => cached || fetch(event.request))
);
});Push Notifications
Service workers receive push messages even when the browser tab is closed:
self.addEventListener('push', (event) => {
const data = event.data.json();
event.waitUntil(
self.registration.showNotification(data.title, {
body: data.body,
icon: '/icons/icon-192.png'
})
);
});Background Sync
Defers operations (form submissions, analytics) until connectivity is restored.
Lifecycle
- Register: main page registers the service worker
- Install: service worker downloads and caches assets
- Activate: old cache is cleared, new worker takes control
- Fetch: intercepts network requests
Important Constraints
- Service workers only run over HTTPS (or localhost for development)
- They do not have access to the DOM
- They are terminated when idle and restarted when needed
- Each scope (path) can have only one active service worker
Cache Strategies
| Strategy | Pattern | Use for |
|---|---|---|
| Cache-first | Serve cache, fallback to network | Static assets (CSS, images) |
| Network-first | Fetch network, fallback to cache | API data |
| Stale-while-revalidate | Serve cache, update in background | Semi-dynamic content |
| Cache-only | Serve cache only | App shell (fully offline) |
ProfitTool uses a service worker for offline capability — you can continue using tools even without a connection.
ProfitTool Team
Developer ToolsWritten and maintained by the ProfitTool team. Where an article references a ProfitTool utility, the behaviour described is the behaviour of the tool on this site — you can open it and check the output yourself.
More from the Knowledge Hub
How to Generate VAPID Keys with web-push, Node or OpenSSL
Generate the VAPID key pair that authenticates your push server to browser push services — using the web-push CLI, Node's crypto module, or OpenSSL.
Developer ToolsBase64 Encoding Explained: What It Is and How It Works
Base64 turns binary into 64 safe ASCII characters, at a 33% size cost. How the encoding works, what padding is for, and how Base64URL differs.
Developer ToolsCIDR Notation Explained: How IP Ranges and Subnets Work
CIDR notation writes an IP range as a prefix length. What /24 and /16 actually mean, how they map to subnet masks, and how to count the addresses in a block.