Available in Chrome 42+
This sample is a modified version of the detailed registration sample. It shows off two new methods available to service workers:
skipWaiting()
:
During a service worker update, when there's an existing service worker that differs from the
updated service worker, the updated service worker is immediately considered
active,
instead of
waiting.
clients.claim()
:
Causes the activated service worker to take control over all open pages under the scope
immediately, rather than taking control only after the next navigation.
While both of those methods have independent uses, when used together they ensure that when there's an update to the underlying service worker, the update takes effect immediately and applies to both the current page and all other active pages. It's important that your new service worker code behaves properly when assuming control of a page that might have been initially loaded with an older version of your service worker and older cache entries. If you're not sure that it will, then relying on the default service lifecycle behavior may be more appropriate.
Various pieces of information about the service worker from the perspective of the registering page
can be found below.
Additionally, visit chrome://inspect/#service-workers
and click on the "inspect" link below
the registered service worker to view logging statements for the various actions the
service-worker.js
script is performing.
Service Workers available in your browser.
(Whether 'serviceWorker' in navigator
.)
This page currently controlled.
(Whether navigator.serviceWorker.controller
is set.)
The navigator.serviceWorker.register()
call .
The current registration represents the service worker.
Tip: With a newly registered service worker in the activated
state,
refresh the page to have the service worker take control.
You can also refresh with the Shift key held down to load
the page without the service worker controlling it.
State transitions of the service worker associated with the current registration: