51 lines
1.3 KiB
JavaScript
51 lines
1.3 KiB
JavaScript
|
if ('serviceWorker' in navigator) {
|
||
|
const isEnabled = 'true' === 'true';
|
||
|
|
||
|
if (isEnabled) {
|
||
|
const swUrl = '/sw.min.js';
|
||
|
const $notification = $('#notification');
|
||
|
const $btnRefresh = $('#notification .toast-body>button');
|
||
|
|
||
|
navigator.serviceWorker.register(swUrl).then((registration) => {
|
||
|
|
||
|
if (registration.waiting) {
|
||
|
$notification.toast('show');
|
||
|
}
|
||
|
|
||
|
registration.addEventListener('updatefound', () => {
|
||
|
registration.installing.addEventListener('statechange', () => {
|
||
|
if (registration.waiting) {
|
||
|
if (navigator.serviceWorker.controller) {
|
||
|
$notification.toast('show');
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
});
|
||
|
|
||
|
$btnRefresh.on('click', () => {
|
||
|
if (registration.waiting) {
|
||
|
registration.waiting.postMessage('SKIP_WAITING');
|
||
|
}
|
||
|
$notification.toast('hide');
|
||
|
});
|
||
|
});
|
||
|
|
||
|
let refreshing = false;
|
||
|
|
||
|
|
||
|
navigator.serviceWorker.addEventListener('controllerchange', () => {
|
||
|
if (!refreshing) {
|
||
|
window.location.reload();
|
||
|
refreshing = true;
|
||
|
}
|
||
|
});
|
||
|
} else {
|
||
|
navigator.serviceWorker.getRegistrations().then(function (registrations) {
|
||
|
for (let registration of registrations) {
|
||
|
registration.unregister();
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
|