-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathservice-worker.js
46 lines (43 loc) · 1.54 KB
/
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
41
42
43
44
45
46
const CACHE_NAME = 'weather-cache-1';
const MAIN_URLS = ['/', '/css/styles.css', '/src/main.js'];
self.addEventListener('install', event => {
// waitUntil() ensures installing isn't complete until the following promise resolves
event.waitUntil(
caches.open(CACHE_NAME).then(cache => {
return fetch('/json/icons.json')
.then(response => response.json())
.then(icons => {
const iconURLs = icons.map(icon => icon.src);
const allURLs = MAIN_URLS.concat(iconURLs);
// Add all fetched URLs to the cache
return cache.addAll(allURLs);
});
})
);
});
// Fetch event fired any time resources from CACHE_URLS is fetched
self.addEventListener('fetch', event => {
event.respondWith(
caches.match(event.request).then(response => {
// If cache is found, return - otherwise fetch from network
return (
response ||
fetch(event.request).then(fetchResponse => {
// If bad response, don't cache & return
if (!fetchResponse || !fetchResponse.ok) {
return fetchResponse;
}
// Response is a stream and can only be consumed once - to use twice, it has to be cloned (one to send to webpage, one to store in cache)
const cachedResponse = fetchResponse.clone();
console.log(fetchResponse, event.request);
// Add cloned response to cache (request as key, response as value)
caches.open(CACHE_NAME).then(cache => {
cache.put(event.request, cachedResponse);
});
// Return the fetched response of the resource to render on page
return fetchResponse;
})
);
})
);
});