更新serviceworker逻辑

This commit is contained in:
lloydzhou 2024-07-16 16:37:55 +08:00
parent 4b84fb328c
commit eb7c7cdcb6
2 changed files with 9 additions and 1 deletions

View File

@ -7,6 +7,7 @@ self.addEventListener("activate", function (event) {
}); });
self.addEventListener("install", function (event) { self.addEventListener("install", function (event) {
self.skipWaiting(); // 立即启用新的版本
event.waitUntil( event.waitUntil(
caches.open(CHATGPT_NEXT_WEB_CACHE).then(function (cache) { caches.open(CHATGPT_NEXT_WEB_CACHE).then(function (cache) {
return cache.addAll([]); return cache.addAll([]);

View File

@ -2,8 +2,15 @@ if ('serviceWorker' in navigator) {
window.addEventListener('load', function () { window.addEventListener('load', function () {
navigator.serviceWorker.register('/serviceWorker.js').then(function (registration) { navigator.serviceWorker.register('/serviceWorker.js').then(function (registration) {
console.log('ServiceWorker registration successful with scope: ', registration.scope); console.log('ServiceWorker registration successful with scope: ', registration.scope);
registration.update().then(res => {
console.log('ServiceWorker registration update: ', res);
});
}, function (err) { }, function (err) {
console.error('ServiceWorker registration failed: ', err); console.error('ServiceWorker registration failed: ', err);
}); });
navigator.serviceWorker.addEventListener('controllerchange', function() {
console.log('ServiceWorker controllerchange ');
window.location.reload(true);
});
}); });
} }