-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Service worker is causing double loads in Lighthouse #3089
Comments
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. |
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 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 |
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.) |
Were you thinking of enforcing that with the |
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. |
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. |
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. |
Closing this issue because it has been marked as stale for more than 7 days. |
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.
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: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:
@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.
The text was updated successfully, but these errors were encountered: