javascript – 服务工作者也被缓存在Chrome中?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 服务工作者也被缓存在Chrome中?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2087字,纯文字阅读大概需要3分钟。
内容图文
我构建了一个Progressive Web App,https://www.tavest.com.
我不明白为什么我的服务工作者也被缓存在Chrome中? https://www.tavest.com/service-worker-tavest.js因此,当我更改服务工作者时,chrome不会检测到更改,因此服务工作者不会更新.
尽管我多次刷新页面,它仍然是相同的.但是,在Mozilla中,它的工作正常.
这是我安装服务工作者的代码
if ('serviceWorker' in navigator && (window.location.protocol === 'https:')) {
navigator.serviceWorker.register('/service-worker-tavest.js')
.then(function(registration) {
// updatefound is fired if service-worker.js changes.
registration.onupdatefound = function() {
// updatefound is also fired the very first time the SW is installed,
// and there's no need to prompt for a reload at that point.
// So check here to see if the page is already controlled,
// i.e. whether there's an existing service worker.
if (navigator.serviceWorker.controller) {
// The updatefound event implies that registration.installing is set:
// https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html#service-worker-container-updatefound-event
var installingWorker = registration.installing;
installingWorker.onstatechange = function() {
switch (installingWorker.state) {
case 'installed':
// At this point, the old content will have been purged and the
// fresh content will have been added to the cache.
// It's the perfect time to display a "New content is
// available; please refresh." message in the page's interface.
console.warn('New content is available, please refresh the page');
break;
case 'redundant':
throw new Error('The installing ' +
'service worker became redundant.');
default:
// Ignore
}
};
}
};
}).catch(function(e) {
console.error('Error during service worker registration:', e);
});
}
谢谢您的帮助
温暖的问候,
解决方法:
这是因为服务工作者只是一个普通的JS文件,它将被浏览器缓存.
您可以将no-cache标头设置为/service-worker-tavest.js,以便浏览器停止缓存服务工作文件.这样,您可以在上载新文件时立即更新服务工作者.
以下是在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";
}
内容总结
以上是互联网集市为您收集整理的javascript – 服务工作者也被缓存在Chrome中?全部内容,希望文章能够帮你解决javascript – 服务工作者也被缓存在Chrome中?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。