2020-05-09 19:28:18 +03:00
|
|
|
---
|
|
|
|
layout: compress
|
2021-04-19 10:01:07 +03:00
|
|
|
permalink: '/app.js'
|
2020-05-09 19:28:18 +03:00
|
|
|
---
|
|
|
|
|
2022-06-04 18:54:06 +03:00
|
|
|
const $notification = $('#notification');
|
|
|
|
const $btnRefresh = $('#notification .toast-body>button');
|
|
|
|
|
|
|
|
if ('serviceWorker' in navigator) {
|
2022-10-25 14:26:44 +03:00
|
|
|
/* Registering Service Worker */
|
|
|
|
navigator.serviceWorker.register('{{ "/sw.js" | relative_url }}')
|
|
|
|
.then(registration => {
|
|
|
|
|
|
|
|
/* in case the user ignores the notification */
|
|
|
|
if (registration.waiting) {
|
|
|
|
$notification.toast('show');
|
2022-06-09 23:33:42 +03:00
|
|
|
}
|
2022-10-25 14:26:44 +03:00
|
|
|
|
|
|
|
registration.addEventListener('updatefound', () => {
|
|
|
|
registration.installing.addEventListener('statechange', () => {
|
|
|
|
if (registration.waiting) {
|
|
|
|
if (navigator.serviceWorker.controller) {
|
|
|
|
$notification.toast('show');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
$btnRefresh.click(() => {
|
|
|
|
if (registration.waiting) {
|
|
|
|
registration.waiting.postMessage('SKIP_WAITING');
|
|
|
|
}
|
|
|
|
$notification.toast('hide');
|
|
|
|
});
|
2022-06-09 23:33:42 +03:00
|
|
|
});
|
|
|
|
|
2022-10-25 14:26:44 +03:00
|
|
|
let refreshing = false;
|
|
|
|
|
|
|
|
/* Detect controller change and refresh all the opened tabs */
|
|
|
|
navigator.serviceWorker.addEventListener('controllerchange', () => {
|
|
|
|
if (!refreshing) {
|
|
|
|
window.location.reload();
|
|
|
|
refreshing = true;
|
2022-06-04 18:54:06 +03:00
|
|
|
}
|
2022-10-25 14:26:44 +03:00
|
|
|
});
|
2022-06-04 18:54:06 +03:00
|
|
|
}
|
2022-10-25 14:26:44 +03:00
|
|
|
|