feat: 更新 Service Worker 版本管理,优化缓存策略,增加未处理更新的提示
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
const CACHE_NAME = 'emailbill-v1';
|
||||
const VERSION = '1.0.0'; // Build Time: 2026-01-07 15:59:36
|
||||
const CACHE_NAME = `emailbill-${VERSION}`;
|
||||
const urlsToCache = [
|
||||
'/',
|
||||
'/index.html',
|
||||
@@ -57,11 +58,13 @@ self.addEventListener('fetch', (event) => {
|
||||
event.respondWith(
|
||||
fetch(request)
|
||||
.then((response) => {
|
||||
// 克隆响应以便缓存
|
||||
const responseClone = response.clone();
|
||||
caches.open(CACHE_NAME).then((cache) => {
|
||||
cache.put(request, responseClone);
|
||||
});
|
||||
// 只针对成功的GET请求进行缓存
|
||||
if (request.method === 'GET' && response.status === 200) {
|
||||
const responseClone = response.clone();
|
||||
caches.open(CACHE_NAME).then((cache) => {
|
||||
cache.put(request, responseClone);
|
||||
});
|
||||
}
|
||||
return response;
|
||||
})
|
||||
.catch(() => {
|
||||
@@ -72,7 +75,25 @@ self.addEventListener('fetch', (event) => {
|
||||
return;
|
||||
}
|
||||
|
||||
// 静态资源使用缓存优先策略
|
||||
// 页面请求使用网络优先策略,确保能获取到最新的 index.html
|
||||
if (request.mode === 'navigate') {
|
||||
event.respondWith(
|
||||
fetch(request)
|
||||
.then((response) => {
|
||||
const responseClone = response.clone();
|
||||
caches.open(CACHE_NAME).then((cache) => {
|
||||
cache.put(request, responseClone);
|
||||
});
|
||||
return response;
|
||||
})
|
||||
.catch(() => {
|
||||
return caches.match('/index.html') || caches.match(request);
|
||||
})
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// 其他静态资源使用缓存优先策略
|
||||
event.respondWith(
|
||||
caches.match(request)
|
||||
.then((response) => {
|
||||
|
||||
Reference in New Issue
Block a user