workbox-window.update() should resolve to a result that tells you if an update was found or not
See original GitHub issueLibrary Affected: workbox-window 5.1.2
Browser & Platform: N/A
Issue or Feature Request Description:
As reported at the end of #2430 (and previously at the end of #2130), there is currently no way of telling if a call to wb.update()
resulted in anything. This is not great from a UX point of view if the call to wb.update()
happens when the user performs some deliberate act intended to check for updates.
At the moment you have to do this:
function checkForUpdates(wb) {
wb.update().then(function () {
let reg = wb.p;
if (reg.installing || reg.waiting) {
sendMessageToUser('Update found!');
}
else {
sendMessageToUser('You are already on the latest version of this app.')
}
});
}
But this is fragile. The p
field is not part of the public interface. It might be better if the promise resolved to a boolean result:
function checkForUpdates(wb) {
wb.update().then(function (updateFound) {
if (updateFound) {
sendMessageToUser('Update found!');
}
else {
sendMessageToUser('You are already on the latest version of this app.')
}
});
}
Further tests reveal that, if a user cancels an update (see recipe referred to in #2430), listeners for waiting
and externalwaiting
(added using wb.addEventListener()
) no longer fire if the ‘check for updates’ button is pressed again with a waiting service worker already in the background. To handle this you really need a result to be returned from wb.update()
based on the state of the registration.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:9 (3 by maintainers)
Top GitHub Comments
It’s true that as per the Service Worker specification,
updatefound
is fired whenever the service worker registration’sinstalling
service worker changes. That includes the very first installation, in which theinstalling
worker would change fromnull
to the new service worker.There are a few things you could check for if you need to distinguish a “true” update from an initial installation. I think the easiest is probably just checking
navigator.serviceWorker.controller
. If it’snull
, then the update is (most likely) due to an initial installation. If it’s notnull
, then the update is definitely due to “true” update.@TheParad0X never noticed any issue on initial install. Are you sure it’s called on initial install? I wouldn’t have thought it would be triggered until after the first sw is installed as there would be nothing for it to detect an update against?
I think from memory it’s doing a chksum check against sw, so if it runs. update() against the same sw that called it, then it’s just ignored (ie updatefound isn’t triggered)
I could be wrong, but that was my understanding of how it works.