title: workbox-webpack-plugin service worker does not fetch from cache when online
body: <p>I have a SW that is working fine on localhost, but does not fetch from cache online. As usual it was working fine, but somehow it stopped. </p>
  
  <p>Files are cached but requests always go to the network. I have checked the files on dev tools.</p>
  
  <p>I am also not sure about the cache expiration set up. </p>
  
  <p>You can see it online <a href="https://www.soeezauto.ma" rel="nofollow noreferrer">at this site:</a> </p>
  
  <p>Here is the relevant code:</p>
  
  <p>registerServiceWorker.js</p>
  
  <pre><code>const isLocalhost = Boolean(
    window.location.hostname === 'localhost' ||
    window.location.hostname === '[::1]' ||
    window.location.hostname.match(
      /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
    )
  );
  
  export default function register() {
    if ('serviceWorker' in navigator) {
      const publicUrl = new URL(process.env.PUBLIC_URL, window.location);
      if (publicUrl.origin !== window.location.origin) {
        return;
      }
    window.addEventListener('load', () =&gt; {
      const swUrl = `/dist/sw-dist.js`;
  
      if (isLocalhost) {
        checkValidServiceWorker(swUrl);
        navigator.serviceWorker.ready.then(() =&gt; {
          console.log(
            'This web app is being served cache-first by a service ' +
              'worker.'
          );
        });
      } else {
        registerValidSW(swUrl);
      }
    });
  }
  }
  
  function registerValidSW(swUrl) {
  navigator.serviceWorker
    .register(swUrl)
    .then(registration =&gt; {
      registration.onupdatefound = () =&gt; {
        const installingWorker = registration.installing;
        installingWorker.onstatechange = () =&gt; {
          if (installingWorker.state === 'installed') {
            registration.pushManager.subscribe({userVisibleOnly: true});
            if (navigator.serviceWorker.controller) {
              console.log('New content is available; please refresh.');
            } else {
              console.log('Content is cached for offline use.');
            }
          }
        };
      };
    })
    .catch(error =&gt; {
      console.log('error', error);
      console.error('Error during service worker registration:', error);
    });
  }
  
  function checkValidServiceWorker(swUrl) {
  fetch(swUrl)
    .then(response =&gt; {
      if (
        response.status === 404 ||
        response.headers.get('content-type').indexOf('javascript') === -1
      ) {
        navigator.serviceWorker.ready.then(registration =&gt; {
          registration.unregister().then(() =&gt; {
            window.location.reload();
          });
        });
      } else {
        // Service worker found. Proceed as normal.
        registerValidSW(swUrl);
      }
    })
    .catch(() =&gt; {
      console.log(
        'No internet connection found. App is running in offline mode.'
      );
    });
  }
  
  export function unregister() {
  if ('serviceWorker' in navigator) {
    navigator.serviceWorker.ready.then(registration =&gt; {
      registration.unregister();
    });
  }
  }
  </code></pre>
  
  <p>The distribution code for the service worker is below. You may see the online version using cacheFirst strategy, as I tried both. </p>
  
  <pre><code>importScripts("precache-manifest.1d6e1c2332794b82f85bd1c2e608d2b6.js", "https://storage.googleapis.com/workbox-cdn/releases/3.6.3/workbox-sw.js");
  
  
  workbox.skipWaiting();
  workbox.clientsClaim();
  workbox.routing.registerRoute(
    new RegExp('/dist/img/*'),
    workbox.strategies.staleWhileRevalidate({
      cacheName: 'img-cache',
      plugins: [
          new workbox.expiration.Plugin({
              maxAgeSeconds: 360 * 24 * 60 * 60,
          }),
      ],
    })
  );
  workbox.routing.registerRoute(
    new RegExp('/dist*'),
    workbox.strategies.staleWhileRevalidate({
      cacheName: 'js-cache',
      plugins: [
          new workbox.expiration.Plugin({
              maxAgeSeconds: 30 * 24 * 60 * 60,
          }),
      ],
    })
  );
  workbox.routing.registerRoute(
    new RegExp('/dist/css*'),
    workbox.strategies.staleWhileRevalidate({
      cacheName: 'css-cache',
      plugins: [
          new workbox.expiration.Plugin({
              maxAgeSeconds: 10 * 24 * 60 * 60,
          }),
      ],
    })
  );
  
  workbox.precaching.precacheAndRoute(self.__precacheManifest || []);
  </code></pre>