-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsw.js
54 lines (52 loc) · 1.56 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
const version = 2.0;
const cacheName = 'khutbah-cache-v' + version;
self.addEventListener('install', function(e) {
e.waitUntil(
caches.open(cacheName).then(function(cache) {
return cache.addAll([
'/jummah-khutbah/',
'/jummah-khutbah/index.html',
'/jummah-khutbah/myscript.js',
'/jummah-khutbah/sw.js',
'/jummah-khutbah/assets/jquery-3.1.0.js',
'/jummah-khutbah/style.css',
'/jummah-khutbah/manifest.json',
'/jummah-khutbah/assets/icon_192.png',
'/jummah-khutbah/assets/icon_512.png',
'/jummah-khutbah/assets/al-qalam-quran-majeed.ttf',
'/jummah-khutbah/assets/PatuaOne-Regular.ttf'
]);
})
);
// self.skipWaiting();
});
self.addEventListener('fetch', function(e) {
e.respondWith(
caches.match(e.request).then(function(response) {
return response || fetch(e.request).then(response => {
return caches.open(cacheName)
.then(cache => {
cache.put(e.request.url, response.clone());
return response;
});
});
})
.catch(error => {
console.log('Error Fetching Files!', 'Error:', error);
})
);
});
// code source: https://github.com/ahmedazhar05/guess-who/blob/master/service-worker.js
self.onactivate = event => {
const cacheAllowlist = [cacheName];
event.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.map(cacheName => {
if (cacheAllowlist.indexOf(cacheName) === -1)
return caches.delete(cacheName);
})
);
})
);
};