-
Notifications
You must be signed in to change notification settings - Fork 7
/
sw-controller.php
48 lines (41 loc) · 1.02 KB
/
sw-controller.php
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?
header('Content-Type: application/javascript');
?>
function _registerServiceWorker() {
if (!navigator.serviceWorker) return;
navigator.serviceWorker.register('/sw.js').then(function(reg) {
if (!navigator.serviceWorker.controller) {
return;
}
if (reg.waiting) {
_updateReady(reg.waiting);
return;
}
if (reg.installing) {
_trackInstalling(reg.installing);
return;
}
reg.addEventListener('updatefound', function() {
_trackInstalling(reg.installing);
});
});
// Ensure refresh is only called once.
// This works around a bug in "force update on reload".
var refreshing;
navigator.serviceWorker.addEventListener('controllerchange', function() {
if (refreshing) return;
window.location.reload();
refreshing = true;
});
};
_registerServiceWorker();
function _trackInstalling(worker) {
worker.addEventListener('statechange', function() {
if (worker.state == 'installed') {
_updateReady(worker);
}
});
};
function _updateReady(worker) {
worker.postMessage({action: 'skipWaiting'});
};