-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
105 lines (96 loc) · 3.13 KB
/
Copy pathsw.js
File metadata and controls
105 lines (96 loc) · 3.13 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const CACHE_NAME = 'qq-tools-v5';
const MAX_CACHE_ITEMS = 100;
let cachePromise = null;
function getCache() {
if (!cachePromise) {
cachePromise = caches.open(CACHE_NAME);
}
return cachePromise;
}
function limitCacheSize(cache) {
return cache.keys().then((keys) => {
if (keys.length > MAX_CACHE_ITEMS) {
const toDelete = keys.slice(0, keys.length - MAX_CACHE_ITEMS);
return Promise.all(toDelete.map((key) => cache.delete(key)));
}
});
}
const PRECACHE_ASSETS = [
'/',
'/index.html',
'/projects.html',
'/blog/index.html'
];
self.addEventListener('install', (event) => {
event.waitUntil(
getCache().then((cache) => {
return Promise.allSettled(
PRECACHE_ASSETS.map((url) =>
fetch(url).then((response) => {
if (response.ok && response.type === 'basic') {
return cache.put(url, response);
}
}).catch(() => {})
)
);
}).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())
);
});
self.addEventListener('fetch', (event) => {
if (event.request.method !== 'GET') return;
const isHTML = event.request.mode === 'navigate' || event.request.destination === 'document';
if (isHTML) {
// Network-first for HTML pages, but fallback to cache when offline
event.respondWith(
fetch(event.request).then((networkResponse) => {
// Prevent cache poisoning from redirects
if (networkResponse && networkResponse.status === 200 && networkResponse.type === 'basic') {
const responseClone = networkResponse.clone();
getCache().then((cache) => {
cache.put(event.request, responseClone);
limitCacheSize(cache);
});
}
return networkResponse;
}).catch(() => {
// Network failed - return cached version or offline fallback
return caches.match(event.request).then((cachedResponse) => {
if (cachedResponse) return cachedResponse;
if (event.request.mode === 'navigate') {
return caches.match('/index.html');
}
return new Response('Not found', { status: 404 });
});
})
);
} else {
// Cache-first for assets (images, CSS, JS)
event.respondWith(
caches.match(event.request).then((cachedResponse) => {
const fetchPromise = fetch(event.request).then((networkResponse) => {
// Only cache basic responses (not opaque/redirects)
if (networkResponse && networkResponse.status === 200 && networkResponse.type === 'basic') {
const responseClone = networkResponse.clone();
getCache().then((cache) => {
cache.put(event.request, responseClone);
limitCacheSize(cache);
});
}
return networkResponse;
}).catch(() => cachedResponse);
return cachedResponse || fetchPromise;
})
);
}
});