From 8e9f16f884e6d954b8e1824472102b106889c4d3 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Tue, 29 May 2018 15:06:50 +0300 Subject: [PATCH] feature(client) serviceWorker: add ability to reload page on cache update --- client/client.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/client/client.js b/client/client.js index f0ef8ed9..6f5e2c0b 100644 --- a/client/client.js +++ b/client/client.js @@ -612,7 +612,7 @@ function CloudCmdProto(Util, DOM) { }); }; - function serviceWorker() { + async function serviceWorker() { if (!navigator.serviceWorker) return; @@ -622,7 +622,20 @@ function CloudCmdProto(Util, DOM) { if (!isHTTPS && !isLocalhost) return; - runtime.register(); + const req = await runtime.register(); + + req.onupdatefound = () => { + const installingWorker = req.installing; + installingWorker.onstatechange = () => { + if (installingWorker.state !== 'installed' || !navigator.serviceWorker.controller) + return; + + const reload = location.reload.bind(location); + DOM.Dialog.confirm(TITLE, 'Update available. Do you want to realod page?', { + cancel: true + }).then(reload); + }; + }; } }