web2019/static/js/as.js

41 lines
1.2 KiB
JavaScript
Raw Normal View History

(function () {
var swRegistration;
var newWorker;
var isRefreshing = false;
var pathname = window.location.pathname;
var deferredPrompt;
window.addEventListener('beforeinstallprompt', (e) => {
console.log('[AS] Before install prompt');
// Prevent Chrome 67 and earlier from automatically showing the prompt
e.preventDefault();
// Stash the event so it can be triggered later.
deferredPrompt = e;
});
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);
});
});
}
})();