-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserviceworker.template
83 lines (77 loc) · 2.77 KB
/
serviceworker.template
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
// PWABoilerplate service worker file
const cacheName = 'pwacalc_v$VERSION'; // name of cache to use.
const oldCacheName = 'pwacalc_v2'; // name of cache from previous version of app. used to remove old cache.
const offlinePage = 'offline.html'; // page to load to show an offline status.
const appjs = 'app_v$VERSION.js'; // the application
// attach listener to 'beforeinstallprompt' event
self.addEventListener('beforeinstallprompt', function(event) {
// NOTE: use the following 2 lines to cancel the Add to Home Screen prompt altogether
/*event.preventDefault();
return false;*/
event.userChoice.then(
function(result) {
console.log(result.outcome);
if(result.outcome == 'dismissed') { /* prompt was dismissed */ }
else { /* prompt was accepted */ }
}
);
});
// attach listener to 'install' event
self.addEventListener('install', function(event) {
event.waitUntil(self.skipWaiting()); // trigger activate event to start immediately instead of waiting for a page reload
event.waitUntil(
caches.open(cacheName).then(
cache => cache.addAll([ '/', 'index.html', 'w3.css', appjs, offlinePage ])
)
);
});
// attach listener to 'activate' event
self.addEventListener('activate', function(event) {
event.waitUntil(self.clients.claim()); // claim all current clients to activate immediately instead of waiting for a page reload
event.waitUntil(
caches.keys().then(
function(cacheNames) {
return Promise.all(cacheNames.filter(
function(cacheName) {
// Return true to remove this cache
if(cacheName === oldCacheName)
return true;
return false;
}).map(function(cacheName) {
return caches.delete(cacheName);
})
);
}
)
);
});
// attach listener to 'fetch' event
self.addEventListener('fetch', function(event) {
event.respondWith(
// first, check if the requested document is in the cache
caches.match(event.request /* , { ignoreSearch: true } */).then( // ignoreSearch: set to true to ignore querystring
function(response) {
// if found in the cache, return it, else fetch from server
if(response) return response;
var requestForCache = event.request.clone()
return fetch(requestForCache).then(
function(response) {
if(!response || response.status !== 200) return reponse;
var responseForCache = response.clone();
// cache the fetched result
caches.open(cacheName).then(
function(cache) {
cache.put(requestForCache, responseForCache);
}
);
return response;
}
).catch(error => {
// If fetch fails and trying to navigate to an html page, return offline page
if (event.request.method === 'GET' && event.request.headers.get('accept').includes('text/html'))
return caches.match(PWAOffline);
});
}
)
);
});