Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

start_url does not respond with a 200 when offline: The start_url did respond, but not via a service worker. Lighthouse Audit problem

I am creating a PWA that works offline with a service worker.

Right now it works correctly, but there is a problem in Lighthouse Audit.

When I run Lighthouse, in the PWA section I get this problem: start_url does not respond with a 200 when offline The start_url did respond, but not via a service worker.

How do I pass that audit, even if there are other audits that say that I have successfully installed a service worker?

My website is here: https://nariohtools.com and the service worker is here: https://nariohtools.com/sw.js

Thanks in advance.

like image 848
prialgon Avatar asked Jan 25 '23 14:01

prialgon


2 Answers

Just in case you struggle with this, there is a bug in Lighthouse that has been fixed in Chrome version 89. https://github.com/antfu/vite-plugin-pwa/issues/20#issuecomment-773019940

like image 29
Valerio Gentile Avatar answered Jan 30 '23 10:01

Valerio Gentile


The related code is here:

caches.open(CACHE_NAME).then((cache) => {
  return fetch(evt.request)

You are opening the cache but you're not using the cached response and the request is forwarded to the network:

Use something like this instead:

caches.open(CACHE_NAME).then(cache => {
  return cache.match(evt.request).then(cacheResponse => cacheResponse || fetch(evt.request).then(networkResponse => {
  cache.put(evt.request, networkResponse.clone());
  return networkResponse;
}));
like image 110
Guerric P Avatar answered Jan 30 '23 12:01

Guerric P