gpt4 book ai didi

javascript - Service Worker 也在 Chrome 中缓存?

转载 作者:搜寻专家 更新时间:2023-11-01 05:20:41 24 4
gpt4 key购买 nike

我构建了一个渐进式 Web 应用程序,https://www.tavest.com .我不明白为什么我的 service worker 也被缓存在 Chrome 中? https://www.tavest.com/service-worker-tavest.js因此,当我更改 service-worker 时,chrome 不会检测到更改,因此 service worker 不会更新。

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

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);
});
}

谢谢你的帮助

热烈的问候,

最佳答案

这是因为 service worker 只是一个普通的 JS 文件,它会被浏览器缓存。

您可以将 no-cache header 设置到 /service-worker-tavest.js 以便浏览器停止缓存您的服务 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";
}

关于javascript - Service Worker 也在 Chrome 中缓存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42059593/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com