-
Notifications
You must be signed in to change notification settings - Fork 104
/
sw.js
44 lines (40 loc) · 1.13 KB
/
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
41
42
43
44
self.addEventListener('install', function (e) {
e.waitUntil(
caches.open('comoayudar-v1').then(function (cache) {
return cache.addAll([
'./'
]).then(function () {
self.skipWaiting()
})
})
)
})
self.addEventListener('fetch', function (event) {
if (event.request.method !== 'GET' && event.request.url.substring(0, 4) !== 'http') {
return
}
event.respondWith(
caches.match(event.request).then(function (cached) {
var networked = fetch(event.request)
.then(fetchedFromNetwork, unableToResolve)
.catch(unableToResolve)
return cached || networked
function fetchedFromNetwork (response) {
var cacheCopy = response.clone()
caches.open('comoayudar-v1').then(function add (cache) {
cache.put(event.request, cacheCopy)
})
return response
}
function unableToResolve () {
return new Response('<h1>Service Unavailable</h1>', {
status: 503,
statusText: 'Service Unavailable',
headers: new Headers({
'Content-Type': 'text/html'
})
})
}
})
)
})