Service Worker
Subject: Computer and Technology noun /ˈsɜːr.vɪs ˌwɜːr.kər/ 1 definition
- web-development
- browser-apis
- progressive-web-apps
- offline-caching
-
Definition (noun)
Entry #92 · UpdatedA Service Worker is an event-driven JavaScript worker script that a web browser executes on a background thread independent of any web page lifecycle, functioning as a programmable network proxy. Because it operates outside the main UI thread and has no direct access to the document object model, it communicates with open pages via asynchronous messaging and wakes only to process events before terminating when idle. Restricted to secure HTTPS origins to prevent request tampering, a Service Worker intercepts outgoing HTTP requests, serves cached assets for offline reliability, and supports platform capabilities such as push notifications and background synchronization.
Common Forms
Offline-Caching Worker: Intercepts
fetchevents and serves versioned resources from the Cache Storage API using strategies such as cache-first or stale-while-revalidate to keep web applications functional offline.Push Notification Worker: Listens for server-sent
pushevents while an application is closed, displaying native system notifications and handling user interactions throughnotificationclickevents.Background Sync Worker: Uses the Background Sync API to defer failed network mutations, such as offline form submissions, and replay them automatically once connectivity is restored.
Framework-Generated Worker: Compiled at build time by tooling such as Workbox to generate precache asset manifests, handle cache expiration, and configure routing rules without manual lifecycle boilerplate.
Example
A commuter opens a progressive web news application on a train passing through a tunnel, and the registered Service Worker intercepts the page navigation request to serve the cached article shell from Cache Storage instead of showing a browser offline error. When the commuter submits a comment while disconnected, the worker queues the request with Background Sync and delivers it automatically as soon as cellular service returns.