forked from udacity/mws-restaurant-stage-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserviceworker.js
137 lines (128 loc) · 5.17 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
importScripts('/js/dbhelper.js');
const CACHE_NAME = 'mws-restaurant-v14';
const CACHE_GOOGLE_MAPS = 'GOOGLE_MAPS_CACHE';
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => {
return cache.addAll([
'/index.html',
'/restaurant.html',
'/css/styles.css',
'/js/dbhelper.js',
'/js/main.js',
'/js/restaurant_info.js',
]);
}).then(() => {
DBHelper.populateRestaurants();
}).then(() => {
return self.skipWaiting();
})
);
});
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames.map((cacheName) => {
if (CACHE_NAME !== cacheName &&
cacheName.startsWith('mws-restaurant') ||
CACHE_GOOGLE_MAPS === cacheName) {
return caches.delete(cacheName);
}
})
);
})
);
return self.clients.claim();
});
const cachedResponse = (cache, request) => {
return cache.match(request).then((cachedResponse) => {
return cachedResponse || fetch(request).then((networkResponse) => {
cache.put(request, networkResponse.clone());
return networkResponse;
});
});
};
self.addEventListener('fetch', (event) => {
// Let the browser do its default thing
// for non-GET requests.
if (event.request.method != 'GET') return;
const requestURL = new URL(event.request.url);
const indexHtml = '/index.html';
if (requestURL.pathname === '/' || requestURL.pathname === indexHtml) {
event.respondWith(caches.open(CACHE_NAME).then((cache) => {
return cachedResponse(cache, indexHtml);
})
);
// Google maps API request caching
} else if (requestURL.href.match(String.raw`^https://(maps|fonts)\.(googleapis|gstatic)\.com`)) {
event.respondWith(caches.open(CACHE_GOOGLE_MAPS).then((cache) => {
return cachedResponse(cache, event.request);
}).catch(() => {
console.log('Failed to load google maps URL:', requestURL);
return new Response();
})
);
} else if (requestURL.href.match(String.raw`^http[s]?://.*/(restaurants|reviews)/?(\d+)?`)) {
return;
} else {
event.respondWith(
caches.open(CACHE_NAME).then((cache) => {
return cache.match(event.request, { ignoreSearch: true }).then((cachedResponse) => {
return cachedResponse || fetch(event.request).then((networkResponse) => {
cache.put(event.request, networkResponse.clone());
return networkResponse;
});
}).catch(() => {
console.log('Error on fetch', event.request);
return fetch(event.request).then((networkResponse) => {
cache.put(event.request, networkResponse.clone());
return networkResponse;
});
});
})
);
}
});
self.addEventListener('sync', (event) => {
console.log('sync event-tag: ', event.tag);
event.waitUntil(processDispatchQueue());
});
/**
* Sequential processing of all queued syncronisation commands.
*/
function processDispatchQueue() {
DBHelper.openDatabase().then((db) => {
let store = DBHelper.openObjectStore(db, 'dispatch-queue');
store.getAllKeys().onsuccess = (event) => {
for (const id of event.target.result) {
store.get(id).onsuccess = (e) => {
const queueId = id;
const {action, url, restaurant_id} = e.target.result;
console.log('Queue item', queueId, action, url, restaurant_id);
if (action === 'toggle-favorite') {
DBHelper.toggleFavorite(url, queueId, db).then(() => {
console.log('Sent toggle-favorite and delete from store dispatch-queue id: ', queueId);
DBHelper.deleteFromQueue(queueId);
});
} else if (action === 'add-review') {
const {
restaurant_id,
name,
rating,
comments} = e.target.result;
const data = {restaurant_id, name, rating, comments};
DBHelper.postRestaurantReview(data).then((review) => {
DBHelper.addToReviewsStore(review).then(() => {
DBHelper.deleteFromQueue(queueId);
});
console.log('SW - Inform users of success posting reivew', review);
});
} else {
console.warn(`Unkown dispatch-queue action: ${action}, id: ${queueId}`);
}
};
}
};
});
}