fix(PWA): sometimes update notification is not triggered
This commit is contained in:
parent
4c45f3788e
commit
96af7291ea
1 changed files with 22 additions and 35 deletions
|
@ -3,50 +3,37 @@ layout: compress
|
||||||
permalink: '/app.js'
|
permalink: '/app.js'
|
||||||
---
|
---
|
||||||
|
|
||||||
const keyWaiting = 'sw-waiting';
|
|
||||||
const $notification = $('#notification');
|
const $notification = $('#notification');
|
||||||
const $btnRefresh = $('#notification .toast-body>button');
|
const $btnRefresh = $('#notification .toast-body>button');
|
||||||
|
|
||||||
function skipWating(registration) {
|
|
||||||
registration.waiting.postMessage('SKIP_WAITING');
|
|
||||||
localStorage.removeItem(keyWaiting);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ('serviceWorker' in navigator) {
|
if ('serviceWorker' in navigator) {
|
||||||
/* Registering Service Worker */
|
/* Registering Service Worker */
|
||||||
navigator.serviceWorker.register('{{ "/sw.js" | relative_url }}')
|
navigator.serviceWorker.register('{{ "/sw.js" | relative_url }}')
|
||||||
.then(registration => {
|
.then(registration => {
|
||||||
if (registration) {
|
|
||||||
registration.addEventListener('updatefound', () => {
|
|
||||||
let serviceWorker = registration.installing;
|
|
||||||
|
|
||||||
serviceWorker.addEventListener('statechange', () => {
|
/* in case the user ignores the notification */
|
||||||
if (serviceWorker.state === 'installed') {
|
if (registration.waiting) {
|
||||||
|
$notification.toast('show');
|
||||||
|
}
|
||||||
|
|
||||||
|
registration.addEventListener('updatefound', () => {
|
||||||
|
registration.installing.addEventListener('statechange', () => {
|
||||||
|
if (registration.waiting) {
|
||||||
if (navigator.serviceWorker.controller) {
|
if (navigator.serviceWorker.controller) {
|
||||||
$notification.toast('show');
|
$notification.toast('show');
|
||||||
/* in case the user ignores the notification */
|
|
||||||
localStorage.setItem(keyWaiting, true);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
$btnRefresh.click(() => {
|
$btnRefresh.click(() => {
|
||||||
skipWating(registration);
|
if (registration.waiting) {
|
||||||
|
registration.waiting.postMessage('SKIP_WAITING');
|
||||||
|
}
|
||||||
$notification.toast('hide');
|
$notification.toast('hide');
|
||||||
});
|
});
|
||||||
|
|
||||||
if (localStorage.getItem(keyWaiting)) {
|
|
||||||
if (registration.waiting) {
|
|
||||||
/* there's a new Service Worker waiting to be activated */
|
|
||||||
$notification.toast('show');
|
|
||||||
} else {
|
|
||||||
/* closed all open pages after receiving notification */
|
|
||||||
localStorage.removeItem(keyWaiting);
|
|
||||||
}
|
}
|
||||||
}
|
);
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
let refreshing = false;
|
let refreshing = false;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue