question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

workbox-window.update() should resolve to a result that tells you if an update was found or not

See original GitHub issue

Library 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:open
  • Created 3 years ago
  • Reactions:1
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
jeffposnickcommented, Jan 28, 2021

It’s true that as per the Service Worker specification, updatefound is fired whenever the service worker registration’s installing service worker changes. That includes the very first installation, in which the installing worker would change from null 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’s null, then the update is (most likely) due to an initial installation. If it’s not null, then the update is definitely due to “true” update.

1reaction
fridaystreetcommented, Jan 28, 2021

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

workbox-window - Chrome Developers
A module that helps with registering a service worker, managing updates, and responding to lifecycle events.
Read more >
workbox-window.update() should resolve to a result that tells you if ...
workbox -window.update() should resolve to a result that tells you if an update was found or not.
Read more >
Workbox 4: Implementing refresh-to-update-version flow using ...
The described approach works ONLY if we are good with modifying the service worker lifecycle by skipping the waiting phase (see lines 8...
Read more >
Inconsistent behaviour with workbox-window.update()
The user has just pressed the 'check for updates' button. · A new service worker version has been found on 2019-11-05 @ 15:23:14...
Read more >
Workbox - web.dev
Caching assets also involves updating them. Workbox helps with updating your assets in whatever way you decide is best. It could be keeping...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found