如何清除服务工作者的缓存? [英] How to clear cache of service worker?

查看:45
本文介绍了如何清除服务工作者的缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个带有 service worker 的 HTML 页面,Service Worker 缓存 index.html 和我的 JS 文件.

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

问题是当我更改 JS 时,更改不会直接显示在客户端浏览器上.当然,在 chrome dev-tools 中,我可以禁用缓存.但在 Chrome 移动版中,我该怎么做?

The problem is when I change the JS, the change doesn't show up directly on the client browser. Of course in chrome dev-tools, I can disable cache. But in chrome mobile, how do I do that?

我尝试访问站点设置并点击了 CLEAR % RESET 按钮.但它仍然从缓存加载旧页面/加载.我尝试使用其他浏览器或 chrome 隐身模式,但它会加载新页面.

I tried to access the site settings and hit the CLEAR % RESET button. But it still loads the old page/load from cache. I tried to use other browser or chrome incognito and it loads the new page.

然后,我尝试清除我的浏览数据(只是缓存)并且它起作用了.

Then, I try to clear my browsing data (just cache) and it works.

我想这不是它应该如何工作的吧?我的用户不会知道页面是否在不清除 chrome 浏览器缓存的情况下更新.

I guess that's not how it should work right? my user won't know if the page is updated without clearing the chrome browser cache.

推荐答案

用这个来删除过时的缓存:

Use this to delete outdated caches:

self.addEventListener('activate', function(event) {
  event.waitUntil(
    caches.keys().then(function(cacheNames) {
      return Promise.all(
        cacheNames.filter(function(cacheName) {
          // Return true if you want to remove this cache,
          // but remember that caches are shared across
          // the whole origin
        }).map(function(cacheName) {
          return caches.delete(cacheName);
        })
      );
    })
  );
});

这篇关于如何清除服务工作者的缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆