2018-04-17 18:38:48 -05:00
|
|
|
|
|
|
|
function trackInstall(worker, cb) {
|
|
|
|
worker.addEventListener('statechange', function() {
|
|
|
|
if (worker.state == 'installed') {
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-09-13 10:50:28 -05:00
|
|
|
function trackActive(worker, cb) {
|
|
|
|
worker.addEventListener('statechange', function() {
|
|
|
|
if (worker.state == 'activated') {
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-17 18:38:48 -05:00
|
|
|
export function swUpdate() {
|
|
|
|
navigator.serviceWorker.getRegistration().then(reg => {
|
|
|
|
if (reg && reg.waiting) {
|
2019-09-13 10:50:28 -05:00
|
|
|
trackActive(reg.waiting, () => window.location.reload(true));
|
2018-04-17 18:38:48 -05:00
|
|
|
reg.waiting.postMessage("skipWaiting");
|
2019-09-13 10:50:28 -05:00
|
|
|
} else {
|
2018-04-17 18:38:48 -05:00
|
|
|
window.location.reload(true);
|
|
|
|
}
|
|
|
|
});
|
2019-09-13 10:50:28 -05:00
|
|
|
|
2018-04-17 18:38:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export function swInit(store) {
|
|
|
|
|
|
|
|
const updateReady = () => store.commit("setUpdateAvailable", true);
|
2019-09-13 10:50:28 -05:00
|
|
|
const clearUpdateReady = () => store.commit("setUpdateAvailable", false);
|
2018-04-17 18:38:48 -05:00
|
|
|
|
|
|
|
if ('serviceWorker' in navigator) {
|
|
|
|
navigator.serviceWorker.register('/sw.js')
|
|
|
|
.then(function (reg) {
|
|
|
|
console.log('Registration succeeded. Scope is ' + reg.scope);
|
|
|
|
|
|
|
|
if (reg.waiting) {
|
2019-09-13 10:50:28 -05:00
|
|
|
console.log('waiting');
|
2018-04-17 18:38:48 -05:00
|
|
|
updateReady();
|
2019-09-13 10:50:28 -05:00
|
|
|
trackActive(reg.waiting, clearUpdateReady);
|
2018-04-17 18:38:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
reg.addEventListener('updatefound', function () {
|
2019-09-13 10:50:28 -05:00
|
|
|
if (reg.active) {
|
|
|
|
trackInstall(reg.installing, updateReady);
|
|
|
|
}
|
2018-04-17 18:38:48 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
}).catch(function (error) {
|
|
|
|
console.log('Registration failed with ' + error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|