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

[workbox-window] Waiting event does not trigger sometimes. #2165

Closed
azizhk opened this issue Aug 5, 2019 · 4 comments
Closed

[workbox-window] Waiting event does not trigger sometimes. #2165

azizhk opened this issue Aug 5, 2019 · 4 comments
Labels
Needs More Info Waiting on additional information from the community. workbox-window

Comments

@azizhk
Copy link
Contributor

azizhk commented Aug 5, 2019

Library Affected: workbox-window
workbox-sw, workbox-build, etc.

Browser & Platform: Google Chrome Version 75.0.3770.142

Issue or Feature Request Description:
Your request here. Issue
Steps to reproduce (sometimes)

  1. Open tab and register service worker
const wb = new Workbox('/sw.js')
this.wb = wb
this.registration = await wb.register()
wb.addEventListener('waiting', this.onSWWaiting)
  1. Keep tab open for an hour, lock screen, switch to another tab.
  2. Build your service worker again, open your website in a new tab.
  3. In the new tab it shows that a new version is available, but it does not trigger in the long open tab.

Screenshot 2019-08-05 at 3 51 08 PM

I have the following Chrome extensions, I don't think any of Chrome apps are suspending the tab. (The js in the long running tab is still running)
Screenshot 2019-08-05 at 3 55 30 PM

@azizhk
Copy link
Contributor Author

azizhk commented Aug 5, 2019

Reproducing Repo: https://github.com/azizhk/next-workbox/
Can be deployed on Netlify. (Demo: https://mystifying-almeida-8880f8.netlify.com/)
So you can fork and deploy, open a tab, the run deploy again.

Netlify Commands:

  • Build Command: yarn build
  • Publish Directory: out

@jeffposnick
Copy link
Contributor

CC: @philipwalton to confirm.

This sounds to me like the scenario described at https://developers.google.com/web/tools/workbox/modules/workbox-window#when_an_unexpected_version_of_the_service_worker_is_found

There's an externalwaiting event that workbox-window fires when the updated service worker originated from a different tab.

Does that address your issue?

@jeffposnick jeffposnick added Needs More Info Waiting on additional information from the community. workbox-window labels Aug 5, 2019
@azizhk
Copy link
Contributor Author

azizhk commented Aug 5, 2019

Yup, I guess this must be it.
Is there some heuristic which determines whether waiting or externalwaiting gets triggered.
Because I had waiting triggered on me for some cases even when I installed the SW in a different tab.

@azizhk azizhk closed this as completed Aug 5, 2019
@philipwalton
Copy link
Member

Yeah, the heuristic is here. You can see the comments in the code:

// If the script URL passed to `navigator.serviceWorker.register()` is
// different from the current controlling SW's script URL, we know any
// successful registration calls will trigger an `updatefound` event.
// But if the registered script URL is the same as the current controlling
// SW's script URL, we'll only get an `updatefound` event if the file
// changed since it was last registered. This can be a problem if the user
// opens up the same page in a different tab, and that page registers
// a SW that triggers an update. It's a problem because this page has no
// good way of knowing whether the `updatefound` event came from the SW
// script it registered or from a registration attempt made by a newer
// version of the page running in another tab.
// To minimize the possibility of a false positive, we use the logic here:
let updateLikelyTriggeredExternally =
// Since we enforce only calling `register()` once, and since we don't
// add the `updatefound` event listener until the `register()` call, if
// `_updateFoundCount` is > 0 then it means this method has already
// been called, thus this SW must be external
this._updateFoundCount > 0 ||
// If the script URL of the installing SW is different from this
// instance's script URL, we know it's definitely not from our
// registration.
!urlsMatch(installingSW.scriptURL, this._scriptURL) ||
// If all of the above are false, then we use a time-based heuristic:
// Any `updatefound` event that occurs long after our registration is
// assumed to be external.
(performance.now() >
this._registrationTime + REGISTRATION_TIMEOUT_DURATION) ?
// If any of the above are not true, we assume the update was
// triggered by this instance.
true : false;

If you were able to deploy a new version of the service worker in less than a minute, you'd get a false positive on the heuristic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs More Info Waiting on additional information from the community. workbox-window
Projects
None yet
Development

No branches or pull requests

3 participants