How to programmatically empty browser cache?

前端 未结 11 979
迷失自我
迷失自我 2020-11-22 17:36

I am looking for a way to programmatically empty the browser cache. I am doing this because the application caches confidential data and I\'d like to remove those when you p

11条回答
  •  萌比男神i
    2020-11-22 17:54

    You can now use Cache.delete()

    Example:

    let id = "your-cache-id";
    // you can find the id by going to 
    // application>storage>cache storage 
    // (minus the page url at the end)
    // in your chrome developer console 
    
    caches.open(id)
    .then(cache => cache.keys()
      .then(keys => {
        for (let key of keys) {
          cache.delete(key)
        }
      }));
    

    Works on Chrome 40+, Firefox 39+, Opera 27+ and Edge.

提交回复
热议问题