A service worker is a JavaScript file that operates as a client-side proxy between a web application and the network. It intercepts all outgoing HTTP requests from a page, allowing developers to programmatically control the response. This enables a web app to serve cached assets when the network is unavailable, providing a reliable offline experience. Because it runs on a separate thread, it never blocks the main UI thread.
Glossary
Service Workers

What is a Service Worker?
A service worker is a script that the browser runs in the background, separate from a web page, acting as a programmable intercepting proxy to enable features like offline caching, background synchronization, and push notifications.
The lifecycle of a service worker involves distinct phases: install, activate, and fetch. During installation, critical resources are pre-cached using the Cache API. Once activated, the worker can manage network requests, implement custom caching strategies like stale-while-revalidate, and handle background sync events. Critically, service workers require HTTPS for security, preventing man-in-the-middle attacks on the programmable proxy.
Core Capabilities of Service Workers
Service workers act as a client-side proxy, intercepting all network requests from a page to enable offline experiences, background synchronization, and push notifications independent of the web page lifecycle.
Offline Interception & Caching
The service worker's fetch event acts as a programmable proxy, intercepting every HTTP request. This enables Cache-First, Network-First, or Stale-While-Revalidate strategies. By serving pre-cached assets from the Cache API, the application remains fully functional without a network connection, eliminating the 'No Internet' dinosaur.
Background Sync
Using the SyncManager interface, service workers defer network operations until stable connectivity returns. If a user submits a form while offline, the request is stored in IndexedDB and a sync event is registered. The browser fires the event when back online, ensuring reliable atomic data uploads without user intervention.
Push Notifications
Service workers decouple notifications from the browser tab lifecycle via the Push API and Notifications API. The worker listens for a push event from an application server, even when the page is closed, and displays a system notification. This relies on the Web Push Protocol and a subscription object generated by the browser's push service.
Lifecycle & Registration
A service worker follows a strict lifecycle: Registration -> Installation (ideal for pre-caching critical shell assets) -> Activation (ideal for clearing old caches). It terminates when idle and restarts on event. Registration via navigator.serviceWorker.register() scopes the worker to a specific directory path, preventing cross-origin interference.
HTTPS Requirement
Service workers require a secure context (HTTPS) to prevent man-in-the-middle attacks. Because the worker intercepts and modifies all network requests, arbitrary script injection would be catastrophic. The localhost exception exists purely for development convenience, but production deployment demands valid TLS certificates.
IndexedDB Storage
Service workers cannot access the DOM or localStorage (synchronous API). Instead, they use the asynchronous IndexedDB API for persistent, structured data storage. This is critical for queuing failed requests, storing API responses for offline navigation, and maintaining user session data outside the main thread.
Frequently Asked Questions
Clear, technical answers to the most common questions about the programmable network proxy that powers offline experiences, background sync, and push notifications.
A service worker is a JavaScript file that runs in the browser's background, completely separate from a web page, acting as a programmable network proxy between the browser and the network. It intercepts all network requests originating from pages under its scope, allowing developers to control how those requests are handled—whether served from the network, a local cache, or a custom response. Service workers are event-driven, terminating when idle and restarting when needed, making them fundamentally different from traditional JavaScript execution contexts. They enable features previously exclusive to native applications: offline functionality, background data synchronization, and push notifications. Because they sit between the page and the network, service workers require HTTPS to prevent man-in-the-middle attacks, with localhost being the only exception for development purposes.
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Related Terms
Service workers are the programmable network proxy at the heart of modern offline-first and performance-optimized web applications. Explore the core concepts, APIs, and architectural patterns that define the service worker lifecycle and its surrounding infrastructure.
Cache API
A persistent storage mechanism specifically designed for storing Request/Response pairs. Unlike localStorage, the Cache API is asynchronous and deeply integrated with service workers, allowing developers to programmatically cache network assets for offline use. It supports multiple named caches, enabling versioned cache management strategies.
- Key methods:
cache.add(),cache.put(),cache.match() - Storage quota: Managed by the browser's storage quota system
- Typical use: Storing application shell HTML, CSS, JS, and API responses
Cache-First Strategy
An offline-first caching pattern where the service worker intercepts a network request and checks the Cache API before falling back to the network. If a cached response exists, it is returned immediately, eliminating network latency. If not, the request is fetched from the network, and the response is optionally cached for future use.
- Ideal for: Static assets like CSS, JS, and fonts that change infrequently
- Trade-off: Risk of serving stale content if cache invalidation is not properly managed
- Implementation:
event.respondWith(caches.match(request) || fetch(request))
Network-First Strategy
A caching pattern that prioritizes freshness by attempting a network fetch first. Only if the network request fails—due to offline status or timeout—does the service worker fall back to a cached response. This pattern is essential for content that must be current.
- Ideal for: API responses, news feeds, and frequently updated data
- Race condition: Implement a network timeout to avoid indefinite loading
- Fallback: Requires a pre-populated cache to serve meaningful offline content
Stale-While-Revalidate
A hybrid strategy that serves a cached response immediately for speed, while simultaneously initiating a network request to update the cache for the next visit. This pattern, formalized in the stale-while-revalidate HTTP Cache-Control extension, is natively implemented in service workers using the Cache API.
- User experience: Instant load with eventual consistency
- Update mechanism:
event.waitUntil()ensures the cache update completes even if the page closes - Common use: Application shells and content that benefits from fast display but tolerates slight staleness
Background Sync API
A web API that enables service workers to defer network actions until the user has stable connectivity. When a user performs an action offline—such as sending a message or saving a form—the service worker registers a sync event. The browser fires this event automatically when connectivity is restored, ensuring eventual consistency without user intervention.
- Registration:
navigator.serviceWorker.ready.then(reg => reg.sync.register('tag')) - Event handler:
self.addEventListener('sync', event => { ... }) - Reliability: The browser retries failed syncs with exponential backoff
Push Notifications
A mechanism that allows servers to send messages to a service worker, which can then display a system notification to the user, even when the browser is closed. This requires the Push API for message delivery and the Notifications API for display. The service worker acts as the persistent listener, decoupled from any open web page.
- Subscription: Uses VAPID keys for application server authentication
- Event:
self.addEventListener('push', event => { ... }) - User consent: Requires explicit permission grant via
Notification.requestPermission()

About the author
Prasad Kumkar
CEO & MD, Inference Systems
Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.
His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us