33 lines
937 B
JavaScript
33 lines
937 B
JavaScript
|
(function () {
|
||
|
|
||
|
var swRegistration;
|
||
|
var newWorker;
|
||
|
var isRefreshing = false;
|
||
|
var pathname = window.location.pathname;
|
||
|
var deferredPrompt;
|
||
|
|
||
|
if ('serviceWorker' in navigator) {
|
||
|
console.info('Service Worker is supported');
|
||
|
|
||
|
window.addEventListener('load', function () {
|
||
|
navigator.serviceWorker.register('/sw.js', {
|
||
|
updateViaCache: 'all',
|
||
|
}).then(function (registration) {
|
||
|
swRegistration = registration;
|
||
|
console.log('[SW] registration successful with scope: ', swRegistration.scope);
|
||
|
console.log('[SW] is waiting: ', swRegistration.waiting);
|
||
|
|
||
|
navigator.serviceWorker.addEventListener('controllerchange', function () {
|
||
|
if (isRefreshing) {
|
||
|
return;
|
||
|
}
|
||
|
window.location.reload();
|
||
|
isRefreshing = true;
|
||
|
});
|
||
|
}, function (err) {
|
||
|
console.log('[SW] registration failed: ', err);
|
||
|
});
|
||
|
});
|
||
|
}
|
||
|
})();
|