title: ServiceWorker Update loop - Create react app
body: <p>I'm trying to implement a popup for the user that tells them that an update is available.</p>
  <p>I'm using Create React App and it's default configuration.
  I'm dispatching an action when it founds an update like so:</p>
  <p><strong>index.js</strong></p>
  <pre class="lang-js prettyprint-override"><code>serviceWorker.register({
      onSuccess: () =&gt; store.dispatch({ type: SW_UPDATE_FINISHED }),
      onUpdate: reg =&gt; store.dispatch({ type: SW_UPDATE, payload: reg })
  });
  </code></pre>
  <p>and I have a little component that should take care of showing the message and triggering the update:</p>
  <pre class="lang-js prettyprint-override"><code>const SwUpdate = () =&gt; {
    const swUpdate = useSelector(state =&gt; state.app.swUpdate);
    const swReg = useSelector(state =&gt; state.app.swReg);
    const dispatch = useDispatch();
  
    const updateSw = () =&gt; {
      const swWaiting = swReg &amp;&amp; swReg.waiting;
      if (swWaiting) {
        swWaiting.postMessage({ type: 'SKIP_WAITING' });
        dispatch({ type: SW_UPDATE_FINISHED });
        window.location.reload();
      } else {
        dispatch({ type: SW_UPDATE_FINISHED });
      }
    }
  
    return (
      &lt;Snackbar
        open={swUpdate}
        color=&quot;primary&quot;
        message=&quot;New version available! &quot;
        action={
          &lt;Button color=&quot;primary&quot; size=&quot;small&quot; onClick={updateSw}&gt;
            UPDATE
          &lt;/Button&gt;
        }
      /&gt;
    )
  }
  
  export default SwUpdate;
  </code></pre>
  <p>It does what I would think it should do, reloads the page...
  but...</p>
  <p>Again... you have an update! and everytime a do it manually from the inspector is the same story, I get a new SW and I'm not getting why that happens</p>
  <p><a href="https://i.stack.imgur.com/N26mL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/N26mL.png" alt="enter image description here" /></a></p>
  <p>I cannot seem to find an example of this happening to another person and I'm worried is something that I'm doing wrong, or something wrong with the CRA service worker, I only added the callbacks to the register's config argument and manage it on the specific component.</p>
  <p><strong>Edited: Add the service-worker code</strong></p>
  <p>I have the <code>serviceWorker.js</code> AS IS from Create react App:</p>
  <pre class="lang-js prettyprint-override"><code>// This optional code is used to register a service worker.
  // register() is not called by default.
  
  // This lets the app load faster on subsequent visits in production, and gives
  // it offline capabilities. However, it also means that developers (and users)
  // will only see deployed updates on subsequent visits to a page, after all the
  // existing tabs open on the page have been closed, since previously cached
  // resources are updated in the background.
  
  const isLocalhost = Boolean(
    window.location.hostname === 'localhost' ||
      // [::1] is the IPv6 localhost address.
      window.location.hostname === '[::1]' ||
      // 127.0.0.0/8 are considered localhost for IPv4.
      window.location.hostname.match(
        /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
      )
  );
  
  export function register(config) {
    if (process.env.NODE_ENV === 'production' &amp;&amp; 'serviceWorker' in navigator) {
      // The URL constructor is available in all browsers that support SW.
      const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href);
      if (publicUrl.origin !== window.location.origin) {
        // Our service worker won't work if PUBLIC_URL is on a different origin
        // from what our page is served on. This might happen if a CDN is used to
        // serve assets;
        return;
      }
  
      window.addEventListener('load', () =&gt; {
        const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
  
        if (isLocalhost) {
          // This is running on localhost. Let's check if a service worker still exists or not.
          checkValidServiceWorker(swUrl, config);
  
          // Add some additional logging to localhost, pointing developers to the
          // service worker/PWA documentation.
          navigator.serviceWorker.ready.then(() =&gt; {
            console.log(
              'This web app is being served cache-first by a service ' +
                'worker. To learn more, visit &lt;LINK&gt;'
            );
          });
        } else {
          // Is not localhost. Just register service worker
          registerValidSW(swUrl, config);
        }
      });
    }
  }
  
  function registerValidSW(swUrl, config) {
    navigator.serviceWorker
      .register(swUrl)
      .then(registration =&gt; {
        registration.onupdatefound = () =&gt; {
          const installingWorker = registration.installing;
          if (installingWorker == null) {
            return;
          }
  
          installingWorker.onstatechange = () =&gt; {
            if (installingWorker.state === 'installed') {
              if (navigator.serviceWorker.controller) {
                // At this point, the updated precached content has been fetched,
                // but the previous service worker will still serve the older
                // content until all client tabs are closed.
                console.log(
                  'New content is available and will be used when all ' +
                    'tabs for this page are closed. See '
                );
  
                // Execute callback
                if (config &amp;&amp; config.onUpdate) {
                  config.onUpdate(registration);
                }
              } else {
                // At this point, everything has been precached.
                // It's the perfect time to display a
                // &quot;Content is cached for offline use.&quot; message.
                console.log('Content is cached for offline use.');
  
                // Execute callback
                if (config &amp;&amp; config.onSuccess) {
                  config.onSuccess(registration);
                }
              }
            }
          };
        };
      })
      .catch(error =&gt; {
        console.error('Error during service worker registration:', error);
      });
  }
  
  function checkValidServiceWorker(swUrl, config) {
    // Check if the service worker can be found. If it can't reload the page.
    fetch(swUrl, {
      headers: { 'Service-Worker': 'script' }
    })
      .then(response =&gt; {
        // Ensure service worker exists, and that we really are getting a JS file.
        const contentType = response.headers.get('content-type');
        if (
          response.status === 404 ||
          (contentType != null &amp;&amp; contentType.indexOf('javascript') === -1)
        ) {
          // No service worker found. Probably a different app. Reload the page.
          navigator.serviceWorker.ready.then(registration =&gt; {
            registration.unregister().then(() =&gt; {
              window.location.reload();
            });
          });
        } else {
          // Service worker found. Proceed as normal.
          registerValidSW(swUrl, config);
        }
      })
      .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>I know that they use Workbox for the configuration, I'm testing all of this by running <code>npm run build</code> and <code>serve -s build</code> and reloading to try it out.</p>
  <p>ALSO, here's the service-worker file generated by Workbox's configuration of Create react app when building the app:</p>
  <pre class="lang-js prettyprint-override"><code>/**
   * Welcome to your Workbox-powered service worker!
   *
   * You'll need to register this file in your web app and you should
   * disable HTTP caching for this file too.
   *
   * The rest of the code is auto-generated. Please don't update this file
   * directly; instead, make changes to your Workbox build configuration
   * and re-run your build process.
   */
  
  importScripts(&quot;https://storage.googleapis.com/workbox-cdn/releases/4.3.1/workbox-sw.js&quot;);
  
  importScripts(
    &quot;/precache-manifest.a4724df64b745797c25a9173550ba2d3.js&quot;
  );
  
  self.addEventListener('message', (event) =&gt; {
    if (event.data &amp;&amp; event.data.type === 'SKIP_WAITING') {
      self.skipWaiting();
    }
  });
  
  workbox.core.clientsClaim();
  
  /**
   * The workboxSW.precacheAndRoute() method efficiently caches and responds to
   * requests for URLs in the manifest.
   */
  self.__precacheManifest = [].concat(self.__precacheManifest || []);
  workbox.precaching.precacheAndRoute(self.__precacheManifest, {});
  
  workbox.routing.registerNavigationRoute(workbox.precaching.getCacheKeyForURL(&quot;/index.html&quot;), {
    
    blacklist: [/^\/_/,/\/[^/?]+\.[^/]+$/],
  });
  </code></pre>