Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Broken skip waiting process when service worker gets updated multiple times #2470

Closed
andreme opened this issue Apr 25, 2020 · 3 comments · Fixed by #2489
Closed

Broken skip waiting process when service worker gets updated multiple times #2470

andreme opened this issue Apr 25, 2020 · 3 comments · Fixed by #2489
Labels
Bug An issue with our existing, production codebase. workbox-window

Comments

@andreme
Copy link

andreme commented Apr 25, 2020

Library Affected:
workbox-window

Browser & Platform:
all browsers

Issue or Feature Request Description:
I'm basically following this recipe: https://developers.google.com/web/tools/workbox/guides/advanced-recipes#offer_a_page_reload_for_users
The prompt that is offered to the user is not blocking, so the user can keep working without updating immediately.

The problem I encounter is that after the service worker has been updated 2 times without the user reloading my app, I'm not receiving an event when the service worker is updated for the 3rd time, and hence I don't know where to send the skip waiting event to.

Scenario:

  1. App is running, only open in 1 tab
  2. Service worker is updated
    App receives the waiting event and handle to new SW
    App displays non blocking reload prompt
    User does not reload
  3. Service worker is updated again
    App receives the externalwaiting event and handle to new SW
    App is still displaying non blocking reload prompt
    User does not reload
  4. Service worker is updated again
    App does not receive waiting or externalwaiting event, and of course not the handle to the new SW
    App is still displaying non blocking reload prompt
  5. User tries to reload through prompt
    Since the app does not have the handle for the most recent SW, nothing happens

The change from waiting to externalwaiting event is caused by _updateFoundCount.
Not a problem, but it doesn't make sense to me, looks like the code only ever expects 1 waiting update.
https://github.com/GoogleChrome/workbox/blob/master/packages/workbox-window/src/Workbox.ts#L347

The reason for the missing externalwaiting event on the 3rd update is that the updatefound event listener is removed when the first "external" event is encountered:
https://github.com/GoogleChrome/workbox/blob/master/packages/workbox-window/src/Workbox.ts#L363
Commenting out that line fixed my problem.

@andreme
Copy link
Author

andreme commented Apr 25, 2020

I've found a workaround that works for me reliably. Storing the registration object from register and then sending the message to the registration.waiting SW.

registration = await wb.register();

navigator.serviceWorker.addEventListener('controllerchange', () => {
	window.location.reload();
});

// ...

function updateServiceWorker() {
	if (registration?.waiting) {
		messageSW(registration.waiting, {type: 'SKIP_WAITING'});
	}
}

@jeffposnick
Copy link
Contributor

I feel like #2394 might take care of this—if so, let's close this in favor of prioritizing that issue.

@jeffposnick jeffposnick added Bug An issue with our existing, production codebase. workbox-window labels Apr 28, 2020
@freshlymadebread
Copy link

I had the same problem.
The key issue is that the waiting message is not received in app ,not messageSW does not work.

messageSkipWaiting cant help me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug An issue with our existing, production codebase. workbox-window
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants