forked from udacity/mws-restaurant-stage-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serviceworker.js
102 lines (94 loc) · 3.06 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
const cacheName = 'mws-restaurant-v1';
const imageCacheName = 'mws-restaurant-image-v1';
const urlsToCache = [
"./",
"./css/styles.css",
"./js/main.js",
"./js/restaurant_info.js",
"./js/dbhelper.js",
"./js/idb.js",
"./restaurant.html",
"./404.html",
"https://fonts.googleapis.com/css?family=Lato|Open+Sans",
"https://fonts.gstatic.com/s/lato/v14/S6uyw4BMUTPHjx4wXiWtFCc.woff2",
"https://fonts.gstatic.com/s/opensans/v15/mem8YaGs126MiZpBA-UFVZ0bf8pkAg.woff2",
'https://unpkg.com/[email protected]/dist/leaflet.css',
'https://unpkg.com/[email protected]/dist/leaflet.js',
'https://unpkg.com/[email protected]/dist/images/marker-icon.png',
'https://unpkg.com/[email protected]/dist/images/marker-icon-2x.png',
'https://unpkg.com/[email protected]/dist/images/marker-shadow.png'
];
// install service worker
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(cacheName).then(cache => {
return cache.addAll(urlsToCache);
})
);
});
// listen for fetch event
self.addEventListener('fetch', (event) => {
if (event.request.destination === 'image') {
event.respondWith(serveCachedImage(event));
} else {
event.respondWith(serveCachedData(event));
}
});
self.addEventListener('activate', (event) => {
const currentCaches = [cacheName, imageCacheName];
event.waitUntil(
caches.keys().then(cacheList => {
cacheList.filter(cache => {
return cache.startsWith('mws-restaurant') && !currentCaches.includes(cache);
}).map(staleCache => {
return caches.delete(staleCache);
});
})
)
});
const serveCachedData = (event) => {
const { pathname } = getRequestPath(event);
let storageUrl;
if (pathname === "/") {
storageUrl = "/";
} else {
storageUrl = pathname.slice(1, pathname.length);
}
return caches.open(cacheName).then(cache => {
return cache.match(storageUrl, { ignoreSearch: true }).then(response => {
return response || fetch(event.request).then(networkResponse => {
if (storageUrl === 'restaurants') return networkResponse;
if (storageUrl === 'reviews/') return networkResponse;
return caches.open(cacheName).then(cache => {
cache.put(storageUrl, networkResponse.clone());
return networkResponse;
});
// .catch
});
// .catch
})
.catch(() => {
if (storageUrl.includes('restaurant.html')) {
caches.match('./404.html')
}
});
});
};
serveCachedImage = (event) => {
const { pathname } = getRequestPath(event);
return caches.open(imageCacheName).then(cache => {
return cache.match(pathname).then(response => {
return response || fetch(event.request).then(networkResponse => {
return caches.open(imageCacheName).then(cache => {
cache.put(pathname, networkResponse.clone());
return networkResponse;
});
});
// possible catch for network failure
});
// possible .catch for network and cache failure
}).catch(error => {
// serve offline image
})
};
const getRequestPath = (event) => new URL(event.request.url);