1

An old site had a service worker called service-worker.js, now I'm using GatsbyJS gatsby-plugin-offline and automatically create a sw.js. The problem is that the new sw.js won't update the site, it installs but not take the place of the old one.

Is there a way to fix this behavior?

I read about self.skipWaiting() but the Gatsby plugin already use skipWaiting().

jessmzn
  • 122
  • 3
  • 11
  • How do you know that it doesn't take the place of the old one? What do you mean by "new sw.js won't update the site"? – pate May 23 '19 at 08:37
  • In the dev tools, it shows installing, but don't take the place of the old service-worker.js file. But I found a solution, I'll post below – jessmzn May 23 '19 at 13:27

1 Answers1

0

I found a solution following this answer https://stackoverflow.com/a/49772107/2517560

Added this code to gatsby-browser.js

if ('serviceWorker' in navigator) {
    navigator.serviceWorker.getRegistrations()
        .then(function(registrations) {
            for(let registration of registrations) {
               if(registration.active.scriptURL == 'http://localhost/my-push/myworker.js'){ registration.unregister(); }
            }
        });
}

Unregistering the service-worker.js file, the sw.js file is the only one now.

jessmzn
  • 122
  • 3
  • 11