Service Worker API
Service worker concepts and usage
Registration
Download, install and activate
-
Download -
Install -
Activate
-
A navigation to an in-scope page occurs. -
An event is fired on the service worker and it hasn't been downloaded in the last 24 hours.
Using static routing to control how resources are fetched
Other use case ideas
-
Background data synchronization. -
Responding to resource requests from other origins. -
Receiving centralized updates to expensive-to-calculate data such as geolocation or gyroscope, so multiple pages can make use of one set of data. -
Client-side compiling and dependency management of CoffeeScript, less, CJS/AMD modules, etc. for development purposes. -
Hooks for background services. -
Custom templating based on certain URL patterns. -
Performance enhancements, for example, pre-fetching resources that the user is likely to need soon, such as the next few pictures in a photo album. -
API mocking.
-
Background synchronization : Start up a service worker even when no users are at the site, so caches can be updated, etc. -
Reacting to push messages : Start up a service worker to send users a message to tell them new content is available. -
Reacting to a particular time & date. -
Entering a geo-fence.
Interfaces
Cache -
Represents the storage for Request / Response object pairs that are cached as part of the ServiceWorker life cycle. CacheStorage -
Represents the storage for Cache objects. It provides a master directory of all the named caches that a ServiceWorker can access, and maintains a mapping of string names to corresponding Cache objects. Client -
Represents the scope of a service worker client. A service worker client is either a document in a browser context or a SharedWorker , which is controlled by an active worker. Clients -
Represents a container for a list of Client objects; the main way to access the active service worker clients at the current origin. ExtendableEvent -
Extends the lifetime of the install and activate events dispatched on the ServiceWorkerGlobalScope , as part of the service worker lifecycle. This ensures that any functional events (like FetchEvent ) are not dispatched to the ServiceWorker , until it upgrades database schemas, and deletes outdated cache entries, etc. ExtendableMessageEvent -
The event object of a message event fired on a service worker (when a channel message is received on the ServiceWorkerGlobalScope from another context) — extends the lifetime of such events. FetchEvent -
The parameter passed into the onfetch handler, FetchEvent represents a fetch action that is dispatched on the ServiceWorkerGlobalScope of a ServiceWorker . It contains information about the request and resulting response, and provides the FetchEvent.respondWith() method, which allows us to provide an arbitrary response back to the controlled page. InstallEvent -
The parameter passed into an install event handler function, the InstallEvent interface represents an install action that is dispatched on the ServiceWorkerGlobalScope of a ServiceWorker . As a child of ExtendableEvent , it ensures that functional events such as FetchEvent are not dispatched during installation. -
Provides methods for managing the preloading of resources with a service worker. ServiceWorker -
Represents a service worker. Multiple browsing contexts (e.g. pages, workers, etc.) can be associated with the same ServiceWorker object. ServiceWorkerContainer -
Provides an object representing the service worker as an overall unit in the network ecosystem, including facilities to register, unregister, and update service workers, and access the state of service workers and their registrations. ServiceWorkerGlobalScope -
Represents the global execution context of a service worker. ServiceWorkerRegistration -
Represents a service worker registration. WindowClient -
Represents the scope of a service worker client that is a document in a browser context, controlled by an active worker. This is a special type of Client object, with some additional methods and properties available.
Extensions to other interfaces
Window.caches and WorkerGlobalScope.caches -
Returns the CacheStorage object associated with the current context. -
Returns a ServiceWorkerContainer object, which provides access to registration, removal, upgrade, and communication with the ServiceWorker objects for the associated document .
Specifications
|
---|
|
See also
-
Using Service Workers -
Service Worker Lifecycle -
Service workers basic code example -
Web APIs that are related to the Service Worker API: