1

I made a sw.js file that caches my chat website so users can open in offline mode, however, the Service Worker file caused alot of issues including not being able to see new messages and alot of website crashes so I was forced to delete it. Sadly my none of my current users can delete the cache manually! NOte that I kept the sw.js file but it's now empty so is there any code I can write to delete all of my current user caches?

I don't think this is relevant but my app uses django

4

1 回答 1

1

To delete the cache, you can use inbuild cache API.

caches.keys().then(cacheNames => {
    cacheNames.forEach(value => {
        caches.delete(value);
    });
})

Removing content from your sw.js file is not enough. If there is already a service worker installed and running then I would suggest you to "unRegister" that also. You can do so programmatically using below code.

navigator.serviceWorker.getRegistrations().then(function(registrations) {
    for(let registration of registrations) {
        registration.unregister()
    } 
})

Please note you only need to run this code once in all the user's browser.

于 2020-10-26T04:31:38.527 回答