lighthouse icon indicating copy to clipboard operation
lighthouse copied to clipboard

Service Worker state was retained between passes, leading to an incorrect result

Open tusharmath opened this issue 8 years ago • 2 comments

Scenario I use service worker to render shells on the clientside itself this gives me a significant boost in time to first paint. To render the shell I need to create the complete HTML on the client side. The HTML contains links to assets such as CSS and JS. These assets are generated via webpack and have a hash appended to their name. For that I need to make a request inside the service worker to fetch the manifest file. The file looks something like this —

// inside sw.js
const manifestJSON = fetch('/webpack.manifest.json').then(i => i.json())

/* response */
{
  "main.css": "main-4da4b1f47abac15d0459884a4218ebe0.css",
  "main.js": "main-7f2d1dd16a24ae09824e.js",
  "sw.js": "sw-1cf6e8eb12a4299116e2.js"
}

Inside my sw.js, I was making the HTTP request in the beginning of the file. This is an issue because when the service worker starts and if the application is offline this time, the promise returned by the fetch above will simply throw an exception. As of now we will still be getting a 100/100 PWA score because the in the tests, fetch happens only once and the response is saved in the variable manifestJSON and reused for all other tests.

Where as on a mobile device the SW automatically stops after sometime. Visiting the page again in offline mode restarts the SW but this time it isn't able to fetch the manifest file which causes the it to crash.

General Test Case

  • Register/activate a service worker successfully
  • Stop the service worker
  • Go offline
  • Reload page

Pass Case 200 response is still received

Fail Case Page Fails to load

Current Case A false positive is received.

I would be glad to send a PR if someone is accepting :)

tusharmath avatar Jul 23 '17 14:07 tusharmath

Thanks for the detailed report! It's also possible that #709 will address this issue for you though killing the service worker in between passes would be an extra precaution we could take. 👍

patrickhulce avatar Sep 28 '17 23:09 patrickhulce

Aye.

  1. We think https://github.com/GoogleChrome/lighthouse/issues/709 might fix this because the SW will be set correctly.
  2. We could possibly stop the service worker between the passes. Might as well. If you're interested in contributing this, we could review that PR. It'll be hard though!

paulirish avatar Sep 28 '17 23:09 paulirish