-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
52 lines (48 loc) · 1.15 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
45
46
47
48
49
50
51
var cacheAll = false;
var CACHE_NAME = 'webapk-cache';
var urlsToCache = [
'/',
'/author',
'/about',
];
var urlsNotToCache = [
];
// Install Event
self.addEventListener('install', function(event) {
console.log("[SW] install event: ",event);
// Perform install steps
event.waitUntil(
caches.open(CACHE_NAME).then(
function(cache) {
console.log('[SW] Opened cache: ',cache);
return cache.addAll(urlsToCache);
}
)
);
});
// Fetch Event
self.addEventListener('fetch', function(event) {
console.log("[SW] fetch event: ",event);
event.respondWith(
caches.match(event.request).then(
function(response) {
if (response) return response;
else if (!cacheAll || urlsNotToCache.indexOf(event.request) !== -1) return fetch(event.request);
else {
fetch(event.request).then(
function(response) {
if(!response || response.status !== 200 || response.type !== 'basic') return response;
var responseToCache = response.clone();
caches.open(CACHE_NAME).then(
function(cache) {
cache.put(event.request, responseToCache);
}
);
return response;
}
);
}
}
)
);
});