How can I force service worker to clear cache?

845 Views Asked by At

I have developed a PWA. An issue I have is that when a new version is released, I increase the version number in the service worker file (sw.js) and when it runs the cache is regenerated nicely. The version number is the cache name so a change should trigger a new cache which it does when it runs.

The issue here is when it runs.

Some of my clients find it difficult to 'get' the latest version because their browser does not want to load the new version and insists on serving the old version until I instruct the client to manually clear the browser cache and reload Chrome.

In my project, I have my service worker registered in a js file called app.js.

Maybe the issue is that sw.js is imported registered in the app.js file with the following line:

.register('/public/sw.js')

My question is whether or not it would be ok to use a cache busting technique on app.js so that this files runs every single time and then, when I make changes in the sw.js file (indicated by a new version number) the old cache will not be used but a new cache created.

For cache busting app.js I would use

               var versionUpdate = (new Date()).getTime();  

               var script = document.createElement("script");  

               script.type = "text/javascript";  

               script.src = "/js/app.js?v=" + versionUpdate;  

               document.body.appendChild(script);  

To those in the know about PWAs... Would this cause any adverse effects?

Thank you

0

There are 0 best solutions below