Service workers essentially act as proxy servers that sit between web applications, the browser, and the network (when available). They are intended, among other things, to enable the creation of effective offline experiences, intercept network requests and take appropriate action based on whether the network is available, and update assets residing on the server. They will also allow access to push notifications and background sync APIs.
Service workers only run over HTTPS, for security reasons. Having modified network requests, wide open to man in the middle attacks would be really bad. In Firefox, Service Worker APIs are also hidden and cannot be used when the user is in private browsing mode.
Note: Service Workers win over previous attempts in this area such as; AppCache because they don't make assumptions about what you are trying to do, and then break when those assumptions are not exactly right; you have granular control over everything.
Note: Service workers make heavy use of promises, as generally they will wait for responses to come through, after which they will respond with a success or failure action. The promises architecture is ideal for this.
A service worker is first registered using the
ServiceWorkerContainer.register() method. If successful, your service worker will be downloaded to the client and attempt installation/activation (see below) for URLs accessed by the user inside the whole origin, or inside a subset specified by you.
At this point, your service worker will observe the following lifecycle:
The service worker is immediately downloaded when a user first accesses a service worker–controlled site/page.
After that, it is downloaded every 24 hours or so. It may be downloaded more frequently, but it must be downloaded every 24 hours to prevent bad scripts from being annoying for too long.
Installation is attempted when the downloaded file is found to be new — either different to an existing service worker (byte-wise compared), or the first service worker encountered for this page/site.
If this is the first time a service worker has been made available, installation is attempted, then after a successful installation, it is activated.
If there is an existing service worker available, the new version is installed in the background, but not yet activated — at this point it is called the worker in waiting. It is only activated when there are no longer any pages loaded that are still using the old service worker. As soon as there are no more pages to be loaded, the new service worker activates (becoming the active worker). Activation can happen sooner using
ServiceWorkerGlobalScope.skipWaiting() and existing pages can be claimed by the active worker using
You can listen out for the
InstallEvent; a standard action is to prepare your service worker for usage when this fires, for example by creating a cache using the built in storage API, and placing assets inside it that you'll want for running your app offline.
There is also an
activate event. The point where this event fires is generally a good time to clean up old caches and other things associated with the previous version of your service worker.
onactivate could take a while to complete, the service worker spec provides a
waitUntil method, once this is called
onactivate, it passes a promise. Functional events are not dispatched to the service worker until the promise is successfully resolved.
For a complete tutorial to show how to build up your first basic example, read Using Service Workers.
Service workers are also intended to be used for such things as:
In the future, service workers will be able to do a number of other useful things for the web platform that will bring it closer towards native app viability. Interestingly, other specifications can and will start to make use of the service worker context, for example:
Responseobject pairs that are cached as part of the
Cacheobjects. It provides a master directory of all the named caches that a
ServiceWorkercan access, and maintains a mapping of string names to corresponding
SharedWorker, which is controlled by an active worker.
Clientobjects; the main way to access the active service worker clients at the current origin.
activateevents 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.
messageevent fired on a service worker (when a channel message is received on the
ServiceWorkerGlobalScopefrom another context) — extends the lifetime of such events.
FetchEventrepresents a fetch action that is dispatched on the
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.
InstallEventinterface represents an install action that is dispatched on the
ServiceWorker. As a child of
ExtendableEvent, it ensures that functional events such as
FetchEventare not dispatched during installation.
ServiceWorkerContainerobject, which provides access to registration, removal, upgrade, and communication with the
ServiceWorkerobjects for the associated document.
NotificationEventinterface represents a notification click event that is dispatched on the
ServiceWorkerGlobalScope. Note that this interface is deprecated in modern browsers. Service worker messages will now use the
MessageEventinterface, for consistency with other web messaging features.
The SyncEvent interface represents a sync action that is dispatched on the
ServiceWorkerGlobalScope of a ServiceWorker.
Clientobject, with some additional methods and properties available.
|Service Workers||Working Draft||Initial definition.|
We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!
|Basic support||40||17||44 (44)||No support||24||11.1|
|install/activate events||40||17||44 (44)||No support||(Yes)||No support|
|fetch event/request/ ||40||17||44 (44)||No support||No support||No support|
|caches/cache||42||17||39 (39)||No support||No support||No support|
||57||No support||55 (55)||No support||No support||No support|
| ||59||No support||No support||No support||46||No support|
|Feature||Android Webview||Chrome Android||Edge Mobile||Firefox Android||IE Phone||Opera Android||Safari iOS|
|Basic support||No support||40||17||44.0 (44)||No support||(Yes)||11.1|
|install/activate events||No support||40||17||44.0 (44)||No support||(Yes)||No support|
|fetch event/request/ ||No support||40||17||44.0 (44)||No support||No support||No support|
|caches/cache||No support||40||17||39.0 (39)||No support||No support||No support|
||No support||57||No support||55.0 (55)||No support||No support||No support|
| ||No support||59||No support||No support||No support||46||No support|
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.