Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

martinszeltins's avatar

How to clear a service worker cache for index.html page?

I just inherited a website where the whole site (including index.html) is cached by a service worker. I deleted the old website and migrated it to WordPress. The problem is that when users visit this website they still see the old site and I don't have a way to clear the cache.

I tried adding headers to Nginx Cache-Control "no-store, must-revalidate"; expires 0; but this had no effect since index.html page was not loaded from the server but directly from service worker cache.

It seems that there is no way for me to update this site at all. Old users are seeing the old version no matter what I do. Is there any way I can disable this service worker or force clear the cache at this point?

0 likes
13 replies
bobbybouwmann's avatar

@martinszeltins The only thing I can think of is doing a DNS redirect to a certain page where you run this script and redirect the user back to another page. It's not the most beautiful solution, but that would be a way to force this.

You can maybe even do this redirect on nginx or apache level. There is no way you're going to make this work in the new code base for sure.

martinszeltins's avatar

@bobbybouwmann already tried that and it looks like the service worker is caching the domain and does not even make a DNS query. So changing the DNS IP address has no effect.

It looks like there is no way to update this site and I have to tell users to perform a hard refresh or reinstall their browsers. This isn't a problem for new users though since they don't have that cached version.

Honestly, I'm out of ideas here.

Snapey's avatar

@martinszeltins are there any links on the index page that would direct to your server? You could intercept them and send back code to clear the worker?

martinszeltins's avatar

@Snapey good point, I could use those links but unless a user clicks on one of them the cache will remain. It seems that only the index.html page itself is cached by the service worker but not other resources. I will try to use some existing js file to inject a script to unregister the service worker.

Man, this is a tough situation.

martinszeltins's avatar

@Snapey oh shoot, it looks like all js files are also cached by the sw :( And waiting for users to click on a link to clear the cache is also not very realistic.

Am I out of options here?

bobbybouwmann's avatar

@martinszeltins I'm out of ideas as well. I guess you can now only contact your users through a different channel and tell them to use that.

martinszeltins's avatar

@martinbean The sw.js file is not versioned and is loaded from the cache (previous developers). So since the service worker is loaded from cache there doesn't seem to be a way to update its content.

martinbean's avatar

@martinszeltins No, you can. If you change the contents of the service worker file as described in the article I linked you to, it will trigger an update.

Please or to participate in this conversation.