How to clear cache of service worker?

前端 未结 4 870
伪装坚强ぢ
伪装坚强ぢ 2020-11-28 05:43

So, I have an HTML page with service worker, the service worker cache the index.html and my JS files.

The problem is when I change the JS, the change doesn\'t show u

4条回答
  •  春和景丽
    2020-11-28 05:54

    This is the only code that worked for me. It is my adaptation of Mozilla documentation :

    //Delete all caches and keep only one
    const cachNameToKeep = 'myCache';
    
    //Deletion should only occur at the activate event
    self.addEventListener('activate', event => {
        var cacheKeeplist = [cacheName];
        event.waitUntil(
            caches.keys().then( keyList => {
                return Promise.all(keyList.map( key => {
                    if (cacheKeeplist.indexOf(key) === -1) {
                        return caches.delete(key);
                    }
                }));
            })
    .then(self.clients.claim())); //this line is important in some contexts
    });
    

提交回复
热议问题