fix (maybe) for page not reloading after updating service worker
This commit is contained in:
parent
994880f454
commit
d1439b3f09
|
|
@ -786,30 +786,40 @@ $(function (){
|
||||||
|
|
||||||
Promise.resolve().then(async () => {
|
Promise.resolve().then(async () => {
|
||||||
const wb = new Workbox('sw.js');
|
const wb = new Workbox('sw.js');
|
||||||
|
let latest_sw = null;
|
||||||
|
|
||||||
function showSkipWaitingPrompt(event) {
|
function showSkipWaitingPrompt(event) {
|
||||||
if (confirmBox) {
|
latest_sw = event.sw;
|
||||||
confirmBox.close({ ignoreDelay: true });
|
|
||||||
|
if (!confirmBox) {
|
||||||
|
confirmBox = new jBox('Confirm', {
|
||||||
|
attach: null,
|
||||||
|
content: "A new version is available. Update the page?",
|
||||||
|
confirmButton: `<span class="update-confirm-button">Update</span>`,
|
||||||
|
cancelButton: 'Not now',
|
||||||
|
closeOnConfirm: false,
|
||||||
|
async confirm() {
|
||||||
|
/* The SW should signal us to reload, but do it after 20s regardless. */
|
||||||
|
setTimeout(() => { window.location.reload(); }, 20000);
|
||||||
|
messageSW(latest_sw, {type: 'SKIP_WAITING'});
|
||||||
|
$('.update-confirm-button').text('Updating…');
|
||||||
|
},
|
||||||
|
onClose() {
|
||||||
|
if (confirmBox === this) {
|
||||||
|
confirmBox = null;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
onCloseComplete() {
|
||||||
|
this.destroy();
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
confirmBox.open();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
confirmBox = new jBox('Confirm', {
|
function reloadForUpdate(event) {
|
||||||
attach: null,
|
window.location.reload();
|
||||||
content: "A new version is available. Update the page?",
|
|
||||||
confirmButton: `<span class="update-confirm-button">Update</span>`,
|
|
||||||
cancelButton: 'Not now',
|
|
||||||
closeOnConfirm: false,
|
|
||||||
confirm() {
|
|
||||||
/* The SW should signal us to reload, but do it after 20s regardless. */
|
|
||||||
setTimeout(() => { window.location.reload(); }, 20000);
|
|
||||||
messageSW(event.sw, {type: 'SKIP_WAITING'});
|
|
||||||
$('.update-confirm-button').text('Updating…');
|
|
||||||
},
|
|
||||||
onCloseComplete() {
|
|
||||||
this.destroy();
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
confirmBox.open();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
wb.addEventListener('installed', (event) => {
|
wb.addEventListener('installed', (event) => {
|
||||||
|
|
@ -822,23 +832,18 @@ $(function (){
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
wb.addEventListener('controlling', (event) => {
|
|
||||||
if (event.isUpdate) {
|
|
||||||
window.location.reload();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
wb.addEventListener('waiting', showSkipWaitingPrompt);
|
wb.addEventListener('waiting', showSkipWaitingPrompt);
|
||||||
wb.addEventListener('externalwaiting', showSkipWaitingPrompt);
|
wb.addEventListener('externalwaiting', showSkipWaitingPrompt);
|
||||||
|
|
||||||
|
wb.addEventListener('controlling', reloadForUpdate);
|
||||||
|
wb.addEventListener('externalactivated', reloadForUpdate);
|
||||||
|
|
||||||
const registration = await wb.register();
|
const registration = await wb.register();
|
||||||
|
|
||||||
if ('update' in registration) {
|
/* Check for updates every 4h without reloading the page. */
|
||||||
/* Check for updates every 4h without reloading the page. */
|
setInterval(() => { wb.update(); }, 4*3600*1000);
|
||||||
setInterval(() => { registration.update(); }, 4*3600*1000);
|
|
||||||
} else {
|
window.Admin.workbox = wb;
|
||||||
console.log('service worker update method not supported, disabling update checks');
|
|
||||||
}
|
|
||||||
}).catch((err) => {
|
}).catch((err) => {
|
||||||
console.error('failed to register the service worker', err);
|
console.error('failed to register the service worker', err);
|
||||||
disableNotify();
|
disableNotify();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue