-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
41 lines (36 loc) · 1.08 KB
/
sw.js
File metadata and controls
41 lines (36 loc) · 1.08 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
// Service Worker for SnapCode
const CACHE_NAME = 'snapcode-v2';
const CACHE_URLS = [
'/',
'/index.html',
'/static/favicon.ico'
];
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME)
.then((cache) => cache.addAll(CACHE_URLS))
.then(() => self.skipWaiting())
);
});
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys()
.then((names) => Promise.all(
names.filter((n) => n !== CACHE_NAME).map((n) => caches.delete(n))
))
.then(() => self.clients.claim())
);
});
self.addEventListener('fetch', (event) => {
// Skip non-GET and chrome-extension requests
if (event.request.method !== 'GET' || event.request.url.startsWith('chrome-extension')) {
return;
}
// Network-first for external CDN, cache-first for local
const isExternal = !event.request.url.startsWith(self.location.origin);
event.respondWith(
isExternal
? fetch(event.request).catch(() => caches.match(event.request))
: caches.match(event.request).then((r) => r || fetch(event.request))
);
});