[go: nahoru, domu]

Skip to content
This repository has been archived by the owner on Mar 14, 2024. It is now read-only.

Service worker is causing double loads in Lighthouse #3089

Closed
robdodson opened this issue May 31, 2020 · 8 comments
Closed

Service worker is causing double loads in Lighthouse #3089

robdodson opened this issue May 31, 2020 · 8 comments
Labels
eng - bug Something broke! stale

Comments

@robdodson
Copy link
Contributor

Describe the bug
Our LH and PSI perf scores seem much worse than they should be. Locally I get around 87 in LH and 61(!) in PSI.

image


Looking at the trace I noticed that the page seems to load twice, even if the url contains a trailing slash. Here's an example from localhost:8080/. Note the stats like LCP get logged twice:

image

After poking around a bit I think I've narrowed the issue down to our service worker. If I just comment out the lines that register the SW my local LH score jumps to 100. I also noticed that we don't register the sw on the staging site and sure enough its PSI score is ~30 points higher:

image

@samthor I know you've got a lot going on with /live/ so if you'd like I could have the rest of the team dig into this to isolate and fix the issue.

@robdodson robdodson added the eng - bug Something broke! label May 31, 2020
@samthor
Copy link
Contributor
samthor commented Jun 1, 2020

So I think in moving some of the service worker stuff around (and just trying to be aggressive about making sure a user has the latest version), we have this code block:

  const isFirstInstall = !navigator.serviceWorker.controller;
  if (isFirstInstall) {
    // Watch for the brand new Service Worker to be activated. We claim this foreground page
    // inside the Service Worker, so this event will fire when it is activated.
    navigator.serviceWorker.addEventListener('controllerchange', () => {
      ensurePartialCache(true);
    });
  } else {
    // This isn't the first install, but ensure some partials are up-to-date.
    ensurePartialCache();

    // We claim active clients if the Service Worker's architecture rev changes. We can't
    // reliably force a reload via the Client interface as it's unsupported in Safari.
    navigator.serviceWorker.addEventListener('controllerchange', () => {
      window.location.reload();
    });
  }

There's nothing wrong with this, except that in the second path, the 'controllerchange' event really does fire all the time a SW changes—not just on "version rev" like it used to.

Now this is actually pretty good, because pages like the web.dev/live have new JS and CSS, and in dev (or whatever) I often see the old version for a second before the SW updates and forces a refresh. But it means that for every new deploy where the SW data changes (master partial change?), we reload, which might be what you're seeing.

@jeffposnick
Copy link
Contributor

One other point: there's a post about SW registration best practices at https://developers.google.com/web/fundamentals/primers/service-workers/registration

It can often be a good idea to delay SW registration until after you've detected that all of the page's content has loaded (either via window.onload or some other event). That way, the process of starting up the SW thread and downloading the precached URLs won't contend with your main page's loading.

I had thought that Lighthouse tests start with an empty cache/SW registrations, but perhaps that has changed? Assuming it is still the case, it seems like isFirstInstall would always be true.

@samthor
Copy link
Contributor
samthor commented Jun 16, 2020

I like your suggestion, @jeffposnick. We don't need to rush into installing the SW.

I don't think #3171 is quite right @robdodson as I'd really like to keep the "major revision forces a reload" behavior. (Even though this issue is still valid, in that we're reloading all the time now.)

@robdodson
Copy link
Contributor Author

I'd really like to keep the "major revision forces a reload" behavior.

Were you thinking of enforcing that with the serviceWorkerArchitecture string in sw.js?

@stale
Copy link
stale bot commented Jul 16, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. To prevent this from happening, leave a comment.

@stale stale bot added the stale label Jul 16, 2020
@robdodson
Copy link
Contributor Author

I think #3362 is the intended solution for this. My understanding is if the user is online we'll try to fetch the latest partial, and if it has a version mismatch with the assets, then we'll fetch the latest assets as well.

@stale stale bot removed the stale label Jul 20, 2020
@stale
Copy link
stale bot commented Aug 19, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. To prevent this from happening, leave a comment.

@stale stale bot added the stale label Aug 19, 2020
@stale
Copy link
stale bot commented Aug 26, 2020

Closing this issue because it has been marked as stale for more than 7 days.

@stale stale bot closed this as completed Aug 26, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
eng - bug Something broke! stale
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants