CSS not rendering in offline mode using Service Worker in Chrome

0

I'm trying to get my PWA to work in offline mode. So far, it's serving all the files from localhost, but the CSS doesn't render. All files requested from the cache are getting status 200. (javascript and html are fully functional) Here's my service-worker code.

self.addEventListener("install", (event) => {
  event.waitUntil(
    caches.open("v1").then((cache) => {
      return cache.addAll([
        "list of assets",        
      ]);
    })
  );
});

self.addEventListener("fetch", function (event) {
  event.respondWith(
    caches.open("v1").then((cache) => {
      if (event.request.url == "http://localhost:3000/") {
        return cache
          .match("http://localhost:3000/index.html")
          .then((response) => {
            console.log({ response });
            return response;
          });
      } else {
        try {
          return cache.match(event.request).then((response) => {
            console.log(response);
            if (response != undefined) {
              console.log({ response: "Loading asset from cache." });
              return response;
            } else {
              let asset = fetch(event.request);
              cache.add(asset);
              return asset;
            }
          });
        } catch (error) {
          console.error(error);
        }
      }
    })
  );
});

1

0

Did you try listing your stylesheet where you have 'list of assets'?

return cache.addAll([
    ‘./css/styles.css'
  ]);
2021-11-23 02:43:10

Yes, it is there, and getting status 200 when requested; I forgot to mention that I'm also using vite. It only happens on local host, works fine on a regular host.
TK421

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................