-
Notifications
You must be signed in to change notification settings - Fork 3
/
sw.js
68 lines (61 loc) · 2.02 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
const CACHE_NAME = 'v1_cache_PWA_QR_NFC',
urlsToCache = [
'./',
'script.js',
'sqr/build.js',
'./img/favicon.png',
'sqr/js/adapter.js',
'sqr/data/zbar.wasm',
'sqr/js/camera-test.js',
'assets/puritym/css/style.css',
'assets/dropdown-menu/style.light.css',
'https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"',
'https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"'
]
/* During the installation phase, static assets are usually cached. */
self.addEventListener('install', e => {
e.waitUntil(
caches.open(CACHE_NAME)
.then(cache => {
return cache.addAll(urlsToCache)
.then(() => self.skipWaiting())
})
.catch(err => console.log('Falló registro de cache', err))
)
})
/* Once the Service Worker (SW) is installed, it is activated and searches
** for resources to make it work offline.
*/
self.addEventListener('activate', e => {
const cacheWhitelist = [CACHE_NAME]
e.waitUntil(
caches.keys()
.then(cacheNames => {
return Promise.all(
cacheNames.map(cacheName => {
// What is no longer needed in cache is deleted.
if (cacheWhitelist.indexOf(cacheName) === -1) {
return caches.delete(cacheName)
}
})
)
})
// The SW is instructed to activate the current cache
.then(() => self.clients.claim())
)
})
/* When the browser retrieves a url */
self.addEventListener('fetch', e => {
// Retrieve either with the object in cache or continue and search for the real url
e.respondWith(
caches.match(e.request)
.then(res => {
if (res) {
// Retrive in cache
return res
}
// Retrieve with the original url
return fetch(e.request)
})
)
})