admin管理员组文章数量:1421176
I built a Progressive Web App, . I don't understand why my service worker is also being cached in Chrome? .js So when I change the service-worker, the chrome doesn't detect the change thus the service worker is not updating.
Eventough I refresh the page many times, it's still the same. However, in Mozilla it works just fine. Here's my code for installing the 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:
// .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);
});
}
I built a Progressive Web App, https://www.tavest.. I don't understand why my service worker is also being cached in Chrome? https://www.tavest./service-worker-tavest.js So when I change the service-worker, the chrome doesn't detect the change thus the service worker is not updating.
Eventough I refresh the page many times, it's still the same. However, in Mozilla it works just fine. Here's my code for installing the 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);
});
}
Thank you for your help
Warm regards,
Share Improve this question asked Feb 6, 2017 at 2:29 Albert SocijoAlbert Socijo 611 silver badge6 bronze badges 1- Possible duplicate of Service worker JavaScript update frequency (every 24 hours?) – Jeff Posnick Commented Feb 6, 2017 at 15:30
2 Answers
Reset to default 5It's because the service worker is just a normal JS file and it will be browser-cached.
You can set no-cache
header to the /service-worker-tavest.js
so that the browser will stop caching your service worker file. That way you can have your service worker updated immediately when the new file is uploaded.
Here is how to do that in 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";
}
I think I know the answer regarding to this caching. It is because of "Service worker Lifecycle" in Chrome.
https://www.youtube./watch?v=TF4AB75PyIc
Conclusion: The cache in chrome browser is okay because chrome will update the service worker by itself.
本文标签: javascriptService Worker is also Being cached in ChromeStack Overflow
版权声明:本文标题:javascript - Service Worker is also Being cached in Chrome? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745340880a2654255.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论