Repository navigation
Expand file tree
/
Copy pathsw.js
More file actions
67 lines (60 loc) · 2.02 KB
/
Copy pathsw.js
File metadata and controls
67 lines (60 loc) · 2.02 KB
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
const STATIC_CACHE = 'static-cache-v2'
const DYNAMIC_CACHE = 'dynamic-cache-v1'
const STATIC_FILES_TO_CHACHE = [
'index.html',
'app.js',
'common.css',
'index.css',
'mainBg.png',
'appIcon.png',
'splash.png',
'manifest.json'
]
self.addEventListener('install', function (event) {
event.waitUntil(
caches.open(STATIC_CACHE)
.then(function (cache) {
console.log('[SW]: Static Caching Started')
cache.addAll(STATIC_FILES_TO_CHACHE)
})
)
})
self.addEventListener('activate', function (event) {
console.log('[SW]: Activated')
const allCaches = [STATIC_CACHE, DYNAMIC_CACHE]
event.waitUntil(
caches.keys()
.then(function (cachedFileNames) {
// Returns the list of old cached file names
return cachedFileNames.filter(cachedFileName => !allCaches.includes(cachedFileName))
})
.then(function (cachedFileNamesToDelete) {
return Promise.all(cachedFileNamesToDelete.map(cachedFileNameToDelete => {
return caches.delete(cachedFileNameToDelete)
}))
})
.then(() => self.clients.claim())
)
})
// CACHE ONLY
self.addEventListener('fetch', function (event) {
event.respondWith(
caches.match(event.request)
.then(function (cachedResponse) {
if (cachedResponse) {
return cachedResponse
}
return caches.open(DYNAMIC_CACHE).then(cache => {
return fetch(event.request)
.then(response => {
// Put a copy of the response in the runtime cache.
return cache.put(event.request, response.clone())
.then(() => { return response });
});
});
})
.catch(function (error) {
console.log('[SW]: ', error)
})
)
})