feat(update): проверка версии UI через Ping без периодических опросов

This commit is contained in:
AidarKC
2026-04-22 19:57:59 +03:00
parent 1a8d1c70fd
commit 78d6124f2a
5 changed files with 72 additions and 100 deletions
+1 -49
View File
@@ -1,49 +1,4 @@
const LS_KEY = 'shine-ui-webpush-subscription-v1';
const SW_UPDATE_CHECK_INTERVAL_MS = 5 * 60 * 1000;
let swUpdateIntervalId = null;
let controllerChangeHandled = false;
function setupServiceWorkerAutoUpdate(registration, log) {
if (!registration) return;
if (!controllerChangeHandled && 'serviceWorker' in navigator) {
controllerChangeHandled = true;
navigator.serviceWorker.addEventListener('controllerchange', () => {
log({
level: 'info',
source: 'web-push',
message: 'Service Worker обновился, перезагружаем UI',
});
window.location.reload();
});
}
registration.addEventListener('updatefound', () => {
const installing = registration.installing;
if (!installing) return;
log({
level: 'info',
source: 'web-push',
message: 'Найдено обновление Service Worker',
});
});
if (swUpdateIntervalId) {
window.clearInterval(swUpdateIntervalId);
swUpdateIntervalId = null;
}
const checkNow = () => {
registration.update().catch(() => {});
};
checkNow();
swUpdateIntervalId = window.setInterval(checkNow, SW_UPDATE_CHECK_INTERVAL_MS);
document.addEventListener('visibilitychange', () => {
if (document.visibilityState !== 'visible') return;
checkNow();
});
}
function urlBase64ToUint8Array(base64String) {
const padding = '='.repeat((4 - (base64String.length % 4)) % 4);
@@ -81,16 +36,13 @@ export async function initPwaPush({ authService, onLog = null }) {
}
try {
const registration = await navigator.serviceWorker.register('./firebase-messaging-sw.js', {
updateViaCache: 'none',
});
const registration = await navigator.serviceWorker.register('./firebase-messaging-sw.js');
log({
level: 'info',
source: 'web-push',
message: 'Service Worker зарегистрирован',
details: { scope: registration.scope },
});
setupServiceWorkerAutoUpdate(registration, log);
const permission = await Notification.requestPermission();
if (permission !== 'granted') {