-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
53 lines (48 loc) · 1.66 KB
/
sw.js
File metadata and controls
53 lines (48 loc) · 1.66 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
/**
* BizManga Service Worker — 表紙画像キャッシュ
* 戦略: Network First + Cache Fallback(画像)
* - 初回: ネットワークから取得 → キャッシュに保存
* - 2回目以降: キャッシュから即座に返し、バックグラウンドで更新
*/
var CACHE_NAME = 'bm-covers-v1';
// キャッシュ対象: manga表紙 + material画像
function shouldCache(url) {
return url.includes('/material/manga/') ||
url.includes('/material/images/') ||
url.includes('/wp-content/uploads/');
}
// Install: 即座にactivate
self.addEventListener('install', function(e) {
self.skipWaiting();
});
// Activate: 古いキャッシュ削除
self.addEventListener('activate', function(e) {
e.waitUntil(
caches.keys().then(function(names) {
return Promise.all(
names.filter(function(n) { return n !== CACHE_NAME; })
.map(function(n) { return caches.delete(n); })
);
}).then(function() { return self.clients.claim(); })
);
});
// Fetch: Stale-While-Revalidate for images
self.addEventListener('fetch', function(e) {
if (e.request.method !== 'GET') return;
if (!shouldCache(e.request.url)) return;
e.respondWith(
caches.open(CACHE_NAME).then(function(cache) {
return cache.match(e.request).then(function(cached) {
var fetchPromise = fetch(e.request).then(function(response) {
if (response && response.ok) {
cache.put(e.request, response.clone());
}
return response;
}).catch(function() {
return cached; // オフライン時はキャッシュ
});
return cached || fetchPromise;
});
})
);
});