-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
35 lines (30 loc) · 1.35 KB
/
Copy pathsw.js
File metadata and controls
35 lines (30 loc) · 1.35 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
const CACHE_NAME = 'scryfall-researcher-static-v2';
const STATIC_URLS = ['./', './index.html', './manifest.json'];
self.addEventListener('install', event => {
event.waitUntil(caches.open(CACHE_NAME).then(cache => cache.addAll(STATIC_URLS)).then(() => self.skipWaiting()));
});
self.addEventListener('activate', event => {
event.waitUntil(caches.keys().then(keys => Promise.all(keys
.filter(key => key.startsWith('scryfall-researcher-static-') && key !== CACHE_NAME)
.map(key => caches.delete(key)))).then(() => self.clients.claim()));
});
self.addEventListener('fetch', event => {
if (event.request.method !== 'GET') return;
const url = new URL(event.request.url);
if (url.origin !== self.location.origin || url.pathname.includes('/api.scryfall.com/')) return;
if (event.request.mode === 'navigate') {
event.respondWith(fetch(event.request).then(response => {
const copy = response.clone();
caches.open(CACHE_NAME).then(cache => cache.put('./index.html', copy));
return response;
}).catch(() => caches.match('./index.html')));
return;
}
event.respondWith(caches.match(event.request).then(cached => cached || fetch(event.request).then(response => {
if (response.ok) {
const copy = response.clone();
caches.open(CACHE_NAME).then(cache => cache.put(event.request, copy));
}
return response;
})));
});