forked from 9jaswag/speechrec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service-worker.js
69 lines (66 loc) · 2.09 KB
/
service-worker.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
var dataCacheName = 'weatherData-v1';
let cacheName = 'speechrec';
let filesToCache = [
'/',
'/index.html',
'/main.js',
'/style.css',
'/blek pentha.png',
'/chime.mp3',
'/mic-icon.png'
];
self.addEventListener('install', function(event) {
console.log('[ServiceWorker] Install');
event.waitUntil(
caches.open(cacheName).then(function(cache) {
console.log('[ServiceWorker] Caching app shell');
return cache.addAll(filesToCache);
})
);
});
self.addEventListener('activate', function(event) {
console.log('[ServiceWorker] Activate');
event.waitUntil(
caches.keys().then(function(keyList) {
return Promise.all(keyList.map(function(key) {
if (key !== cacheName && key !== dataCacheName) {
console.log('[ServiceWorker] Removing old cache', key);
return caches.delete(key);
}
}));
})
);
return self.clients.claim();
});
self.addEventListener('fetch', function(event) {
console.log('[ServiceWorker] Fetch', event.request.url);
var dataUrl = 'http://api.openweathermap.org/data/2.5/weather';
if (event.request.url.indexOf(dataUrl) > -1) {
/*
* When the request URL contains dataUrl, the app is asking for fresh
* weather data. In this case, the service worker always goes to the
* network and then caches the response. This is called the "Cache then
* network" strategy:
* https://jakearchibald.com/2014/offline-cookbook/#cache-then-network
*/
event.respondWith(
caches.open(dataCacheName).then(function(cache) {
return fetch(event.request).then(function(response){
cache.put(event.request.url, response.clone());
return response;
});
})
);
} else {
/*
* The app is asking for app shell files. In this scenario the app uses the
* "Cache, falling back to the network" offline strategy:
* https://jakearchibald.com/2014/offline-cookbook/#cache-falling-back-to-network
*/
event.respondWith(
caches.match(event.request).then(function(response) {
return response || fetch(event.request);
})
);
}
});