-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
77 lines (71 loc) · 2.11 KB
/
sw.js
File metadata and controls
77 lines (71 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const CACHE_NAME = 'dnd-shadow-adventure-v2';
const STATIC_ASSETS = [
"./",
"./index.html",
"./manifest.json",
"./css/main.css",
"./css/character.css",
"./css/combat.css",
"./css/animations.css",
"./css/features.css",
"./js/state-manager.js",
"./js/encounter-engine.js",
"./js/audio-engine.js",
"./js/character.js",
"./js/dice.js",
"./js/storage.js",
"./js/ai-bridge.js",
"./js/combat.js",
"./js/npc.js",
"./js/inventory.js",
"./js/ui.js",
"./js/app.js",
"./assets/icons/icon-192.svg",
"./assets/icons/icon-512.svg",
"./assets/icons/icon-192.png",
"./assets/icons/icon-512.png"
];
const toScopeUrl = (path) => new URL(path, self.registration.scope).toString();
// 安装 Service Worker 并缓存资源
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME)
.then((cache) => {
console.log('[SW] 缓存静态资源');
return cache.addAll(STATIC_ASSETS.map(toScopeUrl));
})
.then(() => self.skipWaiting())
);
});
// 激活并清理旧缓存
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((cacheNames) => Promise.all(
cacheNames
.filter((name) => name !== CACHE_NAME)
.map((name) => caches.delete(name))
)).then(() => self.clients.claim())
);
});
// 拦截请求:优先缓存,导航请求离线时回退到 index.html
self.addEventListener('fetch', (event) => {
const request = event.request;
if (request.method !== 'GET') return;
event.respondWith(
caches.match(request).then((cachedResponse) => {
if (cachedResponse) return cachedResponse;
return fetch(request).then((response) => {
if (response && response.status === 200 && response.type === 'basic') {
const responseToCache = response.clone();
caches.open(CACHE_NAME).then((cache) => cache.put(request, responseToCache));
}
return response;
}).catch(() => {
if (request.mode === 'navigate') {
return caches.match(toScopeUrl('./index.html'));
}
return caches.match(request);
});
})
);
});