-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
56 lines (51 loc) · 1.56 KB
/
sw.js
File metadata and controls
56 lines (51 loc) · 1.56 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
const CACHE = 'genesis-monaco-editor-v3';
const MONACO_BASE = 'https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.45.0/min/vs';
// Files to cache on install
const PRECACHE = [
'./',
'./index.html',
`${MONACO_BASE}/loader.min.js`,
`${MONACO_BASE}/editor/editor.main.js`,
`${MONACO_BASE}/editor/editor.main.css`,
`${MONACO_BASE}/editor/editor.main.nls.js`,
`${MONACO_BASE}/base/worker/workerMain.js`,
];
self.addEventListener('install', e => {
e.waitUntil(
caches.open(CACHE).then(c => c.addAll(PRECACHE.map(url => new Request(url, { cache: 'reload' }))))
.catch(() => {}) // don't fail install if CDN unreachable
);
self.skipWaiting();
});
self.addEventListener('activate', e => {
e.waitUntil(
caches.keys().then(keys =>
Promise.all(keys.filter(k => k !== CACHE).map(k => caches.delete(k)))
)
);
self.clients.claim();
});
self.addEventListener('fetch', e => {
// Cache-first for Monaco CDN assets
if (e.request.url.includes('cdnjs.cloudflare.com')) {
e.respondWith(
caches.match(e.request).then(cached => {
if (cached) return cached;
return fetch(e.request).then(res => {
const clone = res.clone();
caches.open(CACHE).then(c => c.put(e.request, clone));
return res;
}).catch(() => cached);
})
);
return;
}
// Network-first for app shell
e.respondWith(
fetch(e.request).then(res => {
const clone = res.clone();
caches.open(CACHE).then(c => c.put(e.request, clone));
return res;
}).catch(() => caches.match(e.request))
);
});