forked from emydame/Restaurant_Reviews
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsworker.js
More file actions
91 lines (71 loc) · 2.3 KB
/
sworker.js
File metadata and controls
91 lines (71 loc) · 2.3 KB
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
/**
* sworker.js for restaurant review
* Implementation of service worker was guided from udacity instructor video
* cache-age values obtained from "https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control"
*/
const idPromise=null;
const restCACHE = "restaurant-cache-v1";
const urlsToCache = [
'/', // include the root
/* Caching static assets */
'/index.html',
'/manifest.json',
'/restaurant.html',
'../css/styles.css',
'../data/restaurants.json',
'../js/dbhelper.js',
'../js/main.js',
'../js/restaurant_info.js',
/* Caching map assets */
'https://unpkg.com/leaflet@1.3.1/dist/leaflet.css',
'https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.3.4/leaflet.js'
];
self.addEventListener('install', function(event) {
event.waitUntil(
caches.open(restCACHE)
.then(function(cache) {
console.log('Opened cache');
return cache.addAll(urlsToCache);
}).then(function() {
console.log('WORKER: install completed');
}).catch(error => {
console.log('Error', error);
})
);
});
/* check if existing cache contains the newly created cache. If
it does, delete old cache, activate and give control to it */
self.addEventListener('activate', function(event) {
event.waitUntil(
caches.keys().then(function(cacheNames) {
return Promise.all(
cacheNames.filter(function(cacheName) {
return cacheName.startsWith('restaurant-')&&
cacheName !=restCACHE;
}).map(function(cacheName) {
return cache.delete(cacheName);
})
);
})
);
fetchRestaurants();
return self.clients.claim();
});
function fetchRestaurants()
{
}
self.addEventListener('fetch', function(event) {
event.respondWith(
caches.match(event.request).then(resp => {
return resp || fetch(event.request).then(response => {
let responseClone = response.clone();
caches.open(restCACHE).then(cache => {
responseClone['Cache-Control'] = 'public,max-age=86400';
if(!event.request.method=="POST"){
cache.put(event.request, responseClone);}
});
return response;
});
})
);
});