-
Notifications
You must be signed in to change notification settings - Fork 0
/
resSW.js
44 lines (42 loc) · 1.35 KB
/
resSW.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
// Chrome's currently missing some useful cache methods,
// this polyfill adds them.
importScripts('serviceworker-cache-polyfill.js');
// install event!
self.addEventListener('install', function(event) {
// We pass a promise to event.waitUntil to signal how
// long install takes, and if it failed
event.waitUntil(
// We open a cache…
caches.open('res-sw-1').then(function(cache) {
// And add resources to it
return cache.addAll([
'./',
'index.html',
'restaurant.html',
'css/styles.css',
'js/dbhelper.js',
'js/main.js',
'js/restaurant_info.js',
'data/restaurants.json'
]);
})
);
});
// The fetch event happens for the page request with the
// ServiceWorker's scope, and any request made within that
// page
self.addEventListener('fetch', function(event) {
// Calling event.respondWith means we're in charge
// of providing the response. We pass in a promise
// that resolves with a response object
event.respondWith(
// First we look for something in the caches that
// matches the request
caches.match(event.request).then(function(response) {
// If we get something, we return it, otherwise
// it's null, and we'll pass the request to
// fetch, which will use the network.
return response || fetch(event.request);
})
);
});