2024-05-11 05:29:14 +03:00
|
|
|
import Toast from 'bootstrap/js/src/toast';
|
2024-01-27 21:22:33 +03:00
|
|
|
|
|
|
|
if ('serviceWorker' in navigator) {
|
2024-09-18 17:32:26 +03:00
|
|
|
// Get Jekyll config from URL parameters
|
|
|
|
const src = new URL(document.currentScript.src);
|
|
|
|
const register = src.searchParams.get('register');
|
|
|
|
const baseUrl = src.searchParams.get('baseurl');
|
|
|
|
|
|
|
|
if (register) {
|
|
|
|
const swUrl = `${baseUrl}/sw.min.js`;
|
2024-04-17 01:10:01 +03:00
|
|
|
const notification = document.getElementById('notification');
|
|
|
|
const btnRefresh = notification.querySelector('.toast-body>button');
|
2024-05-11 05:29:14 +03:00
|
|
|
const popupWindow = Toast.getOrCreateInstance(notification);
|
2024-01-27 21:22:33 +03:00
|
|
|
|
|
|
|
navigator.serviceWorker.register(swUrl).then((registration) => {
|
2024-09-18 17:32:26 +03:00
|
|
|
// Restore the update window that was last manually closed by the user
|
2024-01-27 21:22:33 +03:00
|
|
|
if (registration.waiting) {
|
2024-04-17 01:10:01 +03:00
|
|
|
popupWindow.show();
|
2024-01-27 21:22:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
registration.addEventListener('updatefound', () => {
|
|
|
|
registration.installing.addEventListener('statechange', () => {
|
|
|
|
if (registration.waiting) {
|
|
|
|
if (navigator.serviceWorker.controller) {
|
2024-04-17 01:10:01 +03:00
|
|
|
popupWindow.show();
|
2024-01-27 21:22:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-04-17 01:10:01 +03:00
|
|
|
btnRefresh.addEventListener('click', () => {
|
2024-01-27 21:22:33 +03:00
|
|
|
if (registration.waiting) {
|
|
|
|
registration.waiting.postMessage('SKIP_WAITING');
|
|
|
|
}
|
2024-04-17 01:10:01 +03:00
|
|
|
popupWindow.hide();
|
2024-01-27 21:22:33 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
let refreshing = false;
|
|
|
|
|
2024-05-11 05:29:14 +03:00
|
|
|
// Detect controller change and refresh all the opened tabs
|
2024-01-27 21:22:33 +03:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|