forked from udacity/mws-restaurant-stage-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
131 lines (105 loc) · 3.36 KB
/
sw.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
var staticCacheName = 'mws-restaurant-static-v1';
var dynamicCacheName = 'mws-restaurant-dynamic-v1';
var contentImgsCache = 'mws-restaurant-imgs';
var allCaches = [
staticCacheName,
contentImgsCache,
dynamicCacheName
];
self.addEventListener('install', function(event) {
event.waitUntil(
caches.open(staticCacheName).then(function(cache) {
return cache.addAll([
'favicon.ico',
'manifest.webmanifest',
'js/restaurant_info.js',
'js/main.js',
'css/main6.css',
'css/css1.css',
'css/main1.css',
'css/main2.css',
'css/main3.css',
'css/main4.css',
'css/main5.css',
'css/details.css',
'css/details2.css',
'css/css2.css';
'img/image_not_available.png',
'index.html',
'restaurant.html',
'https://fonts.gstatic.com/s/roboto/v18/KFOmCnqEu92Fr1Mu4mxK.woff2',
'https://fonts.gstatic.com/s/roboto/v18/KFOlCnqEu92Fr1MmEU9fBBc4.woff2'
]);
})
);
});
//delete old caches if any
self.addEventListener('activate', function(event) {
event.waitUntil(
caches.keys().then(function(cacheNames) {
return Promise.all(
cacheNames.filter(function(cacheName) {
return cacheName.startsWith('mws-restaurant-') &&
!allCaches.includes(cacheName);
}).map(function(cacheName) {
return caches.delete(cacheName);
})
);
})
);
});
self.addEventListener('fetch', function(event) {
var requestUrl = new URL(event.request.url);
if (requestUrl.origin === location.origin) {
if (requestUrl.pathname === '/') {
event.respondWith(caches.match('/index.html'));
return;
}
if (requestUrl.pathname.startsWith('/restaurant.html')) {
event.respondWith(caches.match('/restaurant.html'));
return;
}
if (requestUrl.pathname.startsWith('/photos/')) {
event.respondWith(servePhoto(event.request));
return;
}
}
//default - get from cache then fetch and cache if not found
event.respondWith(
caches.match(event.request).then(function(cacheResponse) {
return cacheResponse ||
fetch(event.request).then(function(networkResponse) {
return caches.open(dynamicCacheName).then(function(cache) {
cache.put(event.request, networkResponse.clone());
return networkResponse;
});
});
})
);
});
async function servePhoto(request) {
//cut off the _<number> prefix
var storageUrl = request.url.replace(/_\d+\.webp$/, '');
const cache = await caches.open(contentImgsCache);
const cachedImage = await cache.match(request);
if (cachedImage) return cachedImage;
const cachedAnySizeImagePromise = cache.match(storageUrl);
const networkImagePromise = fetch(request);
const cachedAnySizeImage = await cachedAnySizeImagePromise;
let toReturn = cachedAnySizeImage;
let errorToThrow = null;
try {
const networkImage = await networkImagePromise;
if(!cachedAnySizeImage) {
cache.put(storageUrl, networkImage.clone());
}
//store the requested image in cache
cache.put(request, networkImage.clone());
toReturn = networkImage;
} catch (error) {
console.log('Fetch operation is not defined: ', error.message);
errorToThrow = error;
}
if (toReturn) return toReturn;
throw errorToThrow;
}