forked from indreklasn/pwa-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice-worker.js
40 lines (29 loc) · 940 Bytes
/
service-worker.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
// check if the serviceWorker object exists in the navigator object
if ('serviceWorker' in navigator) {
// attach event listener on page l aod
window.addEventListener('load', function() {
// register serviceWorker withthe [service-worker.js] file
navigator.serviceWorker.register('/service-worker.js').then(registration => {
// Registration was successful
console.log('ServiceWorker registration successful with scope: ', registration.scope);
}, function(err) {
// registration failed :(
console.log('ServiceWorker registration failed: ', err);
});
});
}
// install event for the service worker
self.addEventListener('install', e => {
e.waitUntil(
caches.open('site-static').then(cache => {
cache.addAll(['/', '/index.html'])
})
)
})
self.addEventListener('fetch', e => {
e.respondWith(
caches.match(e.request).then(res => {
return res
})
)
})