-
Notifications
You must be signed in to change notification settings - Fork 59
/
sw.js
123 lines (99 loc) · 2.96 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
importScripts('serviceworker-cache-polyfill.js');
var CACHE_NAME = 'simple-pwa-v2';
// File want to cache
var urlsToCache = [
'./',
'./index.html',
'./manifest.json',
'./serviceworker-cache-polyfill.js',
'./src/assets/img/blank-thumbnail.png',
'./src/assets/img/favicon.png',
'./src/assets/img/icon-48.png',
'./src/assets/img/icon-96.png',
'./src/assets/img/icon-128.png',
'./src/assets/img/icon-144.png',
'./src/assets/img/icon-152.png',
'./src/assets/img/icon-196.png',
'./src/assets/img/icon-384.png',
'./src/vendor/bootstrap/css/bootstrap.min.css',
'./src/vendor/ionicons/css/ionicons.min.css',
'./src/vendor/ionicons/fonts/ionicons.ttf',
'./src/vendor/ionicons/fonts/ionicons.woff',
'./build/build.js',
// './build.js',
];
// Set the callback for the install step
self.oninstall = function (e) {
console.log('[serviceWorker]: Installing...');
// perform install steps
e.waitUntil(
caches.open(CACHE_NAME)
.then(function (cache) {
console.log('[serviceWorker]: Cache All');
return cache.addAll(urlsToCache);
})
.then(function () {
console.log('[serviceWorker]: Intalled And Skip Waiting on Install');
return self.skipWaiting();
})
);
};
self.onfetch = function (e) {
console.log('[serviceWorker]: Fetching ' + e.request.url);
var raceUrl = 'API/';
if(e.request.url.indexOf(raceUrl) > -1){
e.respondWith(
caches.open(CACHE_NAME).then(function (cache) {
return fetch(e.request).then(function (res) {
cache.put(e.request.url, res.clone());
return res;
}).catch(err => {
console.log('[serviceWorker]: Fetch Error ' + err);
});
})
);
}
else if (e.request.url.indexOf('src/assets/img-content') > -1) {
e.respondWith(
caches.match(e.request).then(function (res) {
if(res) return res
return fetch(e.request.clone(), { mode: 'no-cors' }).then(function (newRes) {
if(!newRes || newRes.status !== 200 || newRes.type !== 'basic') {
return newRes;
}
caches.open(CACHE_NAME).then(function (cache) {
cache.put(e.request, newRes.clone());
}).catch(err => {
console.log('[serviceWorker]: Fetch Error ' + err);
});
return newRes;
});
})
);
}
else {
e.respondWith(
caches.match(e.request).then(function (res) {
return res || fetch(e.request)
})
);
}
};
self.onactivate = function (e) {
console.log('[serviceWorker]: Actived');
var whiteList = ['simple-pwa-v2'];
e.waitUntil(
caches.keys().then(function (cacheNames) {
return Promise.all(
cacheNames.map(function (cacheName) {
if (whiteList.indexOf(cacheName) === -1) {
return caches.delete(cacheName);
}
})
)
}).then(function () {
console.log('[serviceWorker]: Clients Claims');
return self.clients.claim();
})
);
};