-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
55 lines (50 loc) · 1.46 KB
/
Copy pathsw.js
File metadata and controls
55 lines (50 loc) · 1.46 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
/*
* Service worker: caches the app shell so Go works offline and can be
* installed as a PWA. Cache-first since every asset is static; bump CACHE to
* ship updated files.
*/
const CACHE = 'go-v2'
// Paths are relative to this file, so they resolve correctly when the site is
// served from a subpath (e.g. GitHub Pages at /go/).
const ASSETS = [
'./',
'index.html',
'css/main.css',
'js/jquery.js',
'js/main.js',
'manifest.webmanifest',
'assets/favicon-16.png',
'assets/favicon-32.png',
'assets/apple-touch-icon.png',
'assets/icon-192.png',
'assets/icon-512.png',
'assets/icon-maskable-512.png',
]
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE)
.then(cache => cache.addAll(ASSETS))
.then(() => self.skipWaiting())
)
})
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys()
.then(keys => Promise.all(keys.filter(key => key !== CACHE).map(key => caches.delete(key))))
.then(() => self.clients.claim())
)
})
self.addEventListener('fetch', (event) => {
const request = event.request
if (request.method !== 'GET') return
event.respondWith(
caches.match(request).then(cached => {
if (cached) return cached
return fetch(request).catch(() => {
// Offline and uncached: fall back to the app shell for navigations.
if (request.mode === 'navigate') return caches.match('index.html')
return Response.error()
})
})
)
})