-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
103 lines (77 loc) · 2.27 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
// thanks a lot for help from youtube tutorial https://www.youtube.com/watch?v=BfL3pprhnms&t=246s
let cacheName = 'ver1337';
let cacheFiles = [
'/',
'index.html',
'restaurant.html',
'css/styles.css',
'/js/main.js',
'/js/dbhelper.js',
'/js/restaurant_info.js',
'data/restaurants.json',
'/img/1.jpg',
'/img/2.jpg',
'/img/3.jpg',
'/img/4.jpg',
'/img/5.jpg',
'/img/6.jpg',
'/img/7.jpg',
'/img/8.jpg',
'/img/9.jpg',
'/img/10.jpg'
];
self.addEventListener('install', function (e) {
console.log('[ServiceWorker] Installed');
e.waitUntil(
//open cache and add files
caches.open(cacheName).then(function (cache) {
console.log('[ServiceWorker] Caching cacheFiles');
return cache.addAll(cacheFiles);
})
);
});
self.addEventListener('activate', function (e) {
console.log('[ServiceWorker] Activated');
e.waitUntil(
//deletes old cache if there is a new one
caches.keys().then(function (cacheNames) {
return Promise.all(cacheNames.map(function (thisCacheName) {
if (thisCacheName !== cacheName) {
console.log('[ServiceWorker] Removing Cached Files from Cache - ', thisCacheName);
return caches.delete(thisCacheName);
}
}));
})
);
});
self.addEventListener('fetch', function (e) {
console.log('[ServiceWorker] Fetch', e.request.url);
// Check in cache for the request
e.respondWith(
caches.match(e.request)
.then(function (response) {
if (response) {
console.log("[ServiceWorker] Found in Cache", e.request.url, response);
return response;
}
var requestClone = e.request.clone();
fetch(requestClone)
.then(function (response) {
if (!response) {
console.log("[ServiceWorker] No response from fetch ")
return response;
}
var responseClone = response.clone();
caches.open(cacheName).then(function (cache) {
cache.put(e.request, responseClone);
console.log('[ServiceWorker] New Data Cached', e.request.url);
return response;
});
//checking for errors
})
.catch(function (err) {
console.log('[ServiceWorker] Error Fetching & Caching New Data', err);
});
})
);
});