请问如何从代码更新缓存而不是使用开发工具

Please how can I update the cache from the code rather than using dev tools

请问,我该怎么做才能清除缓存并将其自身更新为新文件?可以从我的代码中完成而不是使用开发工具删除现有缓存吗

我一路上发现了一种在激活时使用服务工作者实现的方法

  self.addEventListener('activate', async event => {
  const keys = await caches.keys();
  keys.map(key =>key!=[the name of your current cache]? caches.delete(key):'');
})

然后在安装事件代码中添加self.skipWaiting(),以便尽快开始使用新缓存