-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserviceworker.js
177 lines (167 loc) · 5.22 KB
/
serviceworker.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
const version = 'V0.02';
const staticCacheName = version + 'staticfiles';
const imageCacheName = 'images';
const pagesCacheName = 'pages';
const cacheList = [ staticCacheName, imageCacheName, pagesCacheName ];
addEventListener( 'install', installEvent => {
skipWaiting();
installEvent.waitUntil(
caches.open(staticCacheName)
.then( staticCache => {
// Nice to have: these items won't block the installation of the Service Worker
// staticCache.addAll([]);
// Must have: these items must be cached for the Service Worker to complete installation
return staticCache.addAll([
'/assets/styles.css',
'/assets/sprites.svg',
'/assets/scripts.js',
'/offline.html',
'/fallback.svg'
]); // end return addAll
}) // end open then
); // end waitUntil
}); // end addEventListner
addEventListener('fetch', fetchEvent => {
const request = fetchEvent.request;
// When the user requests an HTML file
if (request.headers.get('Accept').includes('text/html')) {
// When the requested page is an article
if (/\/articles\/.+/.test(request.url)) {
fetchEvent.respondWith (
// Look in the cache
caches.match(request)
.then( responseFromCache => {
if (responseFromCache) {
// Fetch a fresh version from the network
fetchEvent.waitUntil(
fetch(request)
.then( responseFromFetch => {
// Update the cache
caches.open(pagesCacheName)
.then( pagesCache => {
return pagesCache.put(request, responseFromFetch);
}); // end open then
}) // end fetch then
); // end waitUntil
return responseFromCache;
} // end if
// Otherwise fetch the page from the network
return fetch(request)
.then( responseFromFetch => {
// Put a copy in the cache
const copy = responseFromFetch.clone();
fetchEvent.waitUntil(
caches.open(pagesCacheName)
.then( pagesCache => {
return pagesCache.put(request, copy);
}) // end open then
); // end waitUntil
return responseFromFetch;
}) // end fetch then
.catch( error => {
// Otherwise show the fallback page
return caches.match('/offline.html');
}); // end fetch catch and return
}) // end match then
); // end respondWith
return; // Go no further
} // end if
fetchEvent.respondWith(
// Otherwise fetch the page from the network
fetch(request)
.then( responseFromFetch => {
// Put a copy in the cache
const copy = responseFromFetch.clone();
fetchEvent.waitUntil(
caches.open(pagesCacheName)
.then( pagesCache => {
return pagesCache.put(request, copy);
}) // end open then
); // end waitUntil
return responseFromFetch;
}) // end fetch then
.catch( error => {
// Otherwise look for a cached version of the page
return caches.match(request)
.then( responseFromCache => {
if (responseFromCache) {
return responseFromCache;
} // end if
// Otherwise show the fallback page
return caches.match('/offline.html');
}); // end match then and return
}) // end fetch catch
); // end respondWith
return; // Go no further
} // end if
// When the user requests an image
if (request.headers.get('Accept').includes('image')) {
fetchEvent.respondWith(
// Look for a cached version of the image
caches.match(request)
.then( responseFromCache => {
if (responseFromCache) {
// Fetch a fresh version from the network
fetchEvent.waitUntil(
fetch(request)
.then( responseFromFetch => {
//Update the cache
caches.open(imageCacheName)
.then( imageCache => {
return imageCache.put(request, responseFromFetch);
}); // end open then
}) // end fetch then
); // end waitUntil
return responseFromCache;
} // end if
// Otherwise fetch the image from the network
return fetch(request)
.then( responseFromFetch => {
// Put a copy in the cache
const copy = responseFromFetch.clone();
fetchEvent.waitUntil(
caches.open(imageCacheName)
.then( imageCache => {
return imageCache.put(request, copy);
}) // end open then
); // end waitUntil
return responseFromFetch;
}) // end fetch then and return
.catch( error => {
// Otherwise show a fallback image
return caches.match('fallback.svg');
}); // end fetch catch and return
}) // end match then
); // end respondWith
return; // Go no further
} // end if
// For everything else...
fetchEvent.respondWith(
// Look for a cached copy of the file
caches.match(request)
.then( responseFromCache => {
if (responseFromCache) {
return responseFromCache;
} // end if
// Otherwise fetch the file from the network
return fetch(request);
}) // end match then
); // end respondWith
}); // end addEventListner
addEventListener('activate', activateEvent => {
activateEvent.waitUntil(
caches.keys()
.then( cacheNames => {
return Promise.all(
cacheNames.map( cacheName => {
if (!cacheList.includes(cacheName)) {
return caches.delete(cacheName);
} // end if
}) // end map
); // end return Promise.all
}) // end keys then
.then( () => {
return clients.claim();
}) // end then
); // end waitUntil
}); // end addEventListener