Javascript 服务工作者也被缓存在Chrome中?

Javascript 服务工作者也被缓存在Chrome中?,javascript,google-chrome,progressive-web-apps,Javascript,Google Chrome,Progressive Web Apps,我构建了一个渐进式Web应用程序。 我不明白为什么我的服务人员也被缓存在Chrome中?因此,当我更改服务人员时,chrome没有检测到更改,因此服务人员没有更新 尽管我刷新了很多次页面,它仍然是一样的。然而,在Mozilla中,它工作得很好。 这是我安装服务人员的代码 if('serviceWorker'位于导航器中&&&(window.location.protocol==='https:')){ navigator.serviceWorker.register('/service wor

我构建了一个渐进式Web应用程序。 我不明白为什么我的服务人员也被缓存在Chrome中?因此,当我更改服务人员时,chrome没有检测到更改,因此服务人员没有更新

尽管我刷新了很多次页面,它仍然是一样的。然而,在Mozilla中,它工作得很好。 这是我安装服务人员的代码

if('serviceWorker'位于导航器中&&&(window.location.protocol==='https:')){
navigator.serviceWorker.register('/service worker tavest.js')
.然后(功能(注册){
//如果service-worker.js发生更改,则会激发updatefound。
registration.onupdatefound=函数(){
//updatefound也会在第一次安装软件时触发,
//此时无需提示重新加载。
//因此,请检查此处以查看页面是否已被控制,
//即是否有现有的服务人员。
if(navigator.serviceWorker.controller){
//updatefound事件表示已设置registration.installing:
// https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html#service-工作容器updatefound事件
var installingWorker=registration.installing;
installingWorker.onstatechange=函数(){
开关(installingWorker.state){
“已安装”案例:
//此时,旧内容将被清除,并且
//新内容将被添加到缓存中。
//这是展示“新内容”的最佳时机
//可用;请刷新页面界面中的“消息”。
console.warn('新内容可用,请刷新页面');
打破
“多余”情况:
抛出新错误('安装'+
“服务人员被解雇了。”);
违约:
//忽略
}
};
}
};
}).catch(函数(e){
console.error('服务人员注册期间出错:',e);
});

}
这是因为服务工作者只是一个普通的JS文件,它将被浏览器缓存

您可以将
no cache
头设置为
/service worker tavest.js
,以便浏览器停止缓存您的service worker文件。这样,当上传新文件时,您可以立即让您的服务人员进行更新

以下是如何在Nginx中做到这一点:

location /service-worker-tavest.js {
  # Don't use browser cache for service worker file
  add_header cache-control "no-store, no-cache, must-revalidate";
}

我想我知道这个缓存的答案。 这是因为Chrome中的“服务人员生命周期”

结论:chrome浏览器中的缓存是正常的,因为chrome会自动更新服务人员。

可能重复的