2024-02-13 18:35:32 +03:00
|
|
|
---
|
|
|
|
layout: compress
|
|
|
|
permalink: /assets/js/dist/:basename.min.js
|
|
|
|
---
|
2024-01-27 21:22:33 +03:00
|
|
|
|
|
|
|
if ('serviceWorker' in navigator) {
|
2024-02-13 18:35:32 +03:00
|
|
|
const isEnabled = '{{ site.pwa.enabled }}' === 'true';
|
2024-01-27 21:22:33 +03:00
|
|
|
|
|
|
|
if (isEnabled) {
|
2024-02-13 18:35:32 +03:00
|
|
|
const swUrl = '{{ '/sw.min.js' | relative_url }}';
|
2024-04-17 01:10:01 +03:00
|
|
|
const notification = document.getElementById('notification');
|
|
|
|
const btnRefresh = notification.querySelector('.toast-body>button');
|
|
|
|
const popupWindow = bootstrap.Toast.getOrCreateInstance(notification);
|
2024-01-27 21:22:33 +03:00
|
|
|
|
|
|
|
navigator.serviceWorker.register(swUrl).then((registration) => {
|
2024-02-13 18:35:32 +03:00
|
|
|
{% comment %}In case the user ignores the notification{% endcomment %}
|
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-02-13 18:35:32 +03:00
|
|
|
{% comment %}Detect controller change and refresh all the opened tabs{% endcomment %}
|
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();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|