-
Notifications
You must be signed in to change notification settings - Fork 0
/
stws.js
97 lines (91 loc) · 2.9 KB
/
stws.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Stitch Kit service worker file
const cacheName = 'stitchkit_v0.2'; // name of cache to use.
const oldCacheName = 'stitchkit_v0.1'; // 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.
// attach listener to 'beforeinstallprompt' event
self.addEventListener('beforeinstallprompt', function(event) {
event.userChoice.then(
function(result) {
console.log(result.outcome);
if(result.outcome == 'dismissed') { /* prompt was dismissed */ }
else { /* prompt was accepted */ }
}
);
});
// TODO: add images to cache in next version
// 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([ '/',
'/bootstrap.min.css',
'/about.html',
'/cables.html',
'/cables.png',
'/eyeofpartridge.html',
'/eyeofpartridge.png',
'/index.html',
'/kitchener.html',
'/kitchener.png',
'/m1randm1l.html',
'/m1randm1l.png',
'/wrapandturn.html',
'/wrapandturn.png',
'/chevron-circle-left-solid.svg',
'/chevron-circle-right-solid.svg',
'/stitchkit.png',
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).then(
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(offline);
});
}
)
);
});