forked from udacity/mws-restaurant-stage-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
117 lines (107 loc) · 3.25 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
'use strict';
/* This acts has the cache name as well */
const app = 'mwa-stage1-';
const version = '03'
const appName = `${app + version}`;
const path = (location.hostname === 'triple0t.github.io') ? '/mws-restaurant-stage-1/' : `/`;
/* List of resources to add to cache */
const resources = {
app: [
`${path}`,
`${path}index.html`,
`${path}restaurant.html`,
`${path}data/restaurants.json`,
`${path}js/dbhelper.js`,
`${path}js/main.js`,
`${path}js/restaurant_info.js`,
`${path}js/swreg.js`,
`${path}css/styles.css`,
`${path}css/responsive.css`,
`${path}img/1.jpg`,
`${path}img/2.jpg`,
`${path}img/3.jpg`,
`${path}img/4.jpg`,
`${path}img/5.jpg`,
`${path}img/6.jpg`,
`${path}img/7.jpg`,
`${path}img/8.jpg`,
`${path}img/9.jpg`,
`${path}img/10.jpg`,
'https://unpkg.com/[email protected]/dist/images/marker-icon.png',
'https://unpkg.com/[email protected]/dist/images/marker-shadow.png'
]
};
/**
* On Installation
*
* add all resources to the cache
*/
self.addEventListener('install', event => {
event.waitUntil(
caches.open(appName)
.then(cache => cache.addAll(resources.app) )
.catch(err => {handleError(err)})
.then( () => self.skipWaiting() )
)
});
/**
* On Activation, Perform Clean up on the Caches Storage by Comparing the current CacheName
* againt the one in the database
*/
self.addEventListener('activate', event => {
event.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.filter(cacheName => {
return cacheName.startsWith(app) &&
cacheName != appName;
}).map(cacheName => caches.delete(cacheName) )
);
}).then( () => self.clients.claim() ).catch(err => {handleError(err)})
);
});
/**
* On Fetch Event.
*
* Check the cache storage for match.
*
* if found return cached item else make a request to the network
*/
self.addEventListener('fetch', event => {
// const request = event.request;
const requestUrl = new URL(event.request.url);
// Google Chrome Error
/* if (request.cache === 'only-if-cached' && request.mode !== 'same-origin') {
return;
} */
// check for map images and do NOT add it to cache
if (requestUrl.origin === 'https://api.tiles.mapbox.com') {
// console.log('map not cached');
return;
}
event.respondWith(
caches.match(event.request)
.then(res => {
return res || fetch(event.request).then(newres => {
caches.open(appName)
.then(cache => {
cache.put(event.request, newres.clone())
return newres
})
.catch(err => {handleError(err, 'item not found in cache, error with network request')})
})
})
.catch(Err => {
handleError(Err, 'Error with caches match.');
})
);
});
/**
* Handle Errors
*
* @param {Any} err
* @param {String} message Extra Error Message (Optional)
*/
const handleError = (err, message = '') => {
console.error(`[sw err]: ${message} `, err);
}