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-01-27 21:22:33 +03:00
|
|
|
const $notification = $('#notification');
|
|
|
|
const $btnRefresh = $('#notification .toast-body>button');
|
|
|
|
|
|
|
|
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) {
|
|
|
|
$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;
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|