-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlifecycle.html
34 lines (28 loc) · 1.28 KB
/
lifecycle.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<!DOCTYPE html>
An image will appear here in 3 seconds (<a href="https://thenounproject.com/term/cat/6725/">cat.svg by Marco Hernandez</a>, <a href="https://thenounproject.com/term/stallion/568432">horse.svg by Carla Dias</a>):
<script>
navigator.serviceWorker.register('/sw.js').then(reg => {
reg.installing; // the installing worker, or undefined
reg.waiting; // the waiting worker, or undefined
reg.active; // the active worker, or undefined
reg.addEventListener('updatefound', () => {
// A wild service worker has appeared in reg.installing!
const newWorker = reg.installing;
newWorker.state;
// "installing" - the install event has fired, but not yet complete
// "installed" - install complete
// "activating" - the activate event has fired, but not yet complete
// "activated" - fully active
// "redundant" - discarded. Either failed install, or it's been
// replaced by a newer version
newWorker.addEventListener('statechange', () => {
// newWorker.state has changed
});
});
});
navigator.serviceWorker.addEventListener('controllerchange', () => {
// This fires when the service worker controlling this page
// changes, eg a new worker has as skipped waiting and become
// the new active worker.
});
</script>