Ng Tong Sheng
Ng Tong Sheng

Reputation: 21

How to clear service-worker.js cache on firebase

How do I force client machine to clear the service worker cache after I deploy to firebase? Clients still getting old code when they refresh the page

screenshot

Upvotes: 1

Views: 1849

Answers (1)

pate
pate

Reputation: 5253

I believe your problem is that the service worker has cached your page's index.html and the client is being served with that from the cache when there's actually a fresh one available in the background. The client always gets the old index.html which points to the old JS, CSS etc.

You have basically two options:

If your service-worker.js lists all files that should be cached, just simply update your service-worker.js and the cache name. The browsers automatically update the SW when they see the change. They automatically check to see if there's even a single-byte change.

If your SW dynamically caches files and returns them from the cache the next time they are requested, you should programmatically check for updates every time you serve something from the cache. You can find code examples here: https://serviceworke.rs/strategy-cache-and-update_service-worker_doc.html

Upvotes: 1

Related Questions