-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
111 lines (104 loc) · 3.22 KB
/
Copy pathsw.js
File metadata and controls
111 lines (104 loc) · 3.22 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
106
107
108
109
110
111
// Comic Tracker PWA Service Worker v6
const CACHE_NAME = 'comic-tracker-v6b-cache';
const ASSETS_TO_CACHE = [
'./',
'./index.html',
'./manifest.json',
'./icon-192.png',
'./icon-512.png',
'https://fonts.googleapis.com/css2?family=DM+Sans:ital,opsz,wght@0,9..40,300;0,9..40,400;0,9..40,500;0,9..40,600;0,9..40,700;1,9..40,400&family=Playfair+Display:wght@700;800&display=swap'
];
// Install: cache core assets
self.addEventListener('install', function(event) {
event.waitUntil(
caches.open(CACHE_NAME).then(function(cache) {
return cache.addAll(ASSETS_TO_CACHE);
}).then(function() {
return self.skipWaiting();
})
);
});
// Activate: clean up old caches
self.addEventListener('activate', function(event) {
event.waitUntil(
caches.keys().then(function(cacheNames) {
return Promise.all(
cacheNames.filter(function(name) {
return name !== CACHE_NAME;
}).map(function(name) {
return caches.delete(name);
})
);
}).then(function() {
return self.clients.claim();
})
);
});
// Fetch: network-first for API calls, cache-first for static assets
self.addEventListener('fetch', function(event) {
var url = new URL(event.request.url);
// Network-first for API calls and CORS proxy
if (url.hostname === 'corsproxy.io' || url.hostname === 'comicvine.gamespot.com') {
event.respondWith(
fetch(event.request).catch(function() {
return new Response(JSON.stringify({ error: 'offline', results: [] }), {
headers: { 'Content-Type': 'application/json' }
});
})
);
return;
}
// Cache-first for Google Fonts (font files)
if (url.hostname === 'fonts.gstatic.com') {
event.respondWith(
caches.match(event.request).then(function(cached) {
if (cached) return cached;
return fetch(event.request).then(function(response) {
if (response.ok) {
var clone = response.clone();
caches.open(CACHE_NAME).then(function(cache) {
cache.put(event.request, clone);
});
}
return response;
});
})
);
return;
}
// Stale-while-revalidate for Google Fonts CSS
if (url.hostname === 'fonts.googleapis.com') {
event.respondWith(
caches.match(event.request).then(function(cached) {
var fetchPromise = fetch(event.request).then(function(response) {
if (response.ok) {
var clone = response.clone();
caches.open(CACHE_NAME).then(function(cache) {
cache.put(event.request, clone);
});
}
return response;
}).catch(function() {
return cached;
});
return cached || fetchPromise;
})
);
return;
}
// Cache-first for local static assets
event.respondWith(
caches.match(event.request).then(function(cached) {
if (cached) return cached;
return fetch(event.request).then(function(response) {
if (response.ok && event.request.method === 'GET') {
var clone = response.clone();
caches.open(CACHE_NAME).then(function(cache) {
cache.put(event.request, clone);
});
}
return response;
});
})
);
});