-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
40 lines (34 loc) · 962 Bytes
/
sw.js
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
let cacheName = 'v1'
let assetsToCache = [
'/index.html',
'/main.css',
'/main.js'
]
self.addEventListener('install', function(event) {
console.log('sw installed')
event.waitUntil(
caches.open(cacheName).then(function(cache) {
return cache.addAll(assetsToCache)
}).then(function() {
return self.skipWaiting()
})
)
})
self.addEventListener('activate', function(event) {
return self.clients.claim()
});
self.addEventListener('fetch', function(event) {
console.log('sw proxying a fetch', event.request.url)
if (event.request.method !== 'GET') return
// if (/http:/.test(event.request.url)) return
event.respondWith(
caches.open(cacheName).then(function(cache) {
return fetch(event.request).then(function(networkResponse) {
cache.put(event.request, networkResponse.clone())
return networkResponse;
}).catch(function() {
return cache.match(event.request)
})
})
)
})