-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice-worker.js
77 lines (71 loc) · 1.91 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
70
71
72
73
74
75
76
77
/*
*
* LPA
* Author: Ido Green
* Date: March 2016
* Cache strategy:
* + Cache on install, for the static UI and behaviour
* + Fetch from cache, then network, for the Flickr search results (?)
*/
const CACHE_NAME = 'lpa-app-cache';
const SITE_VERSION = '1';
var urlsToCache = [
'/',
'/index.html',
'/index.html?homescreen=1',
'/index-mentor-new.html',
'/js/main-mentor-new.js',
'/js/firebase.js',
'/js/auth.js',
'/css/mentor-new.css',
'/img/city-hd.jpeg'
];
// These we cache separately due to CORS concerns.
var thirdPartyUrls = [
'https://code.getmdl.io/1.2.1/material.blue_grey-orange.min.css',
'https://code.getmdl.io/1.2.1/material.min.js',
'https://www.gstatic.com/firebasejs/3.0.4/firebase.js'
];
self.addEventListener('install', event => {
event.waitUntil(
caches
.open(CACHE_NAME + '-v' + SITE_VERSION)
.then(cache => {
// This may fail silently if promise rejects.
thirdPartyUrls.forEach(url => {
let req = new Request(url, {mode: 'no-cors'});
fetch(req).then(response => cache.put(req, response));
})
return cache.addAll(urlsToCache).then(_ => console.log('caches done'));
})
);
self.skipWaiting();
});
self.addEventListener('activate', event => {
const currentCache = CACHE_NAME + '-v' + SITE_VERSION;
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.map(cacheName => {
if (cacheName.indexOf(CACHE_NAME) === -1) {
return null;
}
if (cacheName !== currentCache) {
return caches.delete(cacheName);
}
return null;
})
);
});
self.clients.claim();
});
self.addEventListener('fetch', event => {
event.respondWith(
caches.match(event.request)
.then(response => {
if (response) {
return response;
}
return fetch(event.request);
})
);
});