-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathservice-worker.js
96 lines (87 loc) · 2.58 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* Service worker for PWA
*/
const CACHE = 'samsung-chromium-pwa';
const filesToCache = [
'css/style.css',
'images/btn1.svg',
'images/btn2.svg',
'images/btn3.svg',
'images/btn4.svg',
'images/btn5.svg',
'images/btn6.svg',
'images/btn7.svg',
'images/btn8.svg',
'images/chromiumsamsung.svg',
'images/favicon.png'
];
//Install stage sets up the cache-array to configure pre-cache content
self.addEventListener('install', function(event) {
event.waitUntil(precache().then(function() {
return self.skipWaiting();
}))
});
//allow sw to control of current page
self.addEventListener('activate', function(event) {
caches.delete(CACHE);
return self.clients.claim();
});
self.addEventListener('fetch', function(event) {
if ( !(event.request.url).includes(location.origin) ||
(event.request.url) == (location.origin + '/') ||
(event.request.url) == (location.origin + '/index.html')) {
return;
}
event.respondWith(
caches.match(event.request)
.then(function(response) {
if (response) {
return response;
} else {
return fetch(event.request)
.then(function(res) {
return caches.open(CACHE)
.then(function(cache) {
cache.put(event.request.url, res.clone());
return res;
})
})
.catch(function(err) {
console.log(err);
});
}
})
);
event.waitUntil(update(event.request));
});
function precache() {
return caches.open(CACHE).then(function (cache) {
return cache.addAll(filesToCache.map(function(filesToCache) {
return new Request(filesToCache, {mode:'no-cors'});
})).then(function() {
});
}).catch(function(error){
console.error('Pre-fetching failed:', error);
});
}
function fromCache(request) {
//we pull files from the cache first thing so we can show them fast
return caches.open(CACHE).then(function (cache) {
return cache.match(request).then(function (matching) {
return matching || Promise.reject('no-match');
});
});
}
function update(request) {
//this is where we call the server to get the newest version of the
//file to use the next time we show view
return caches.open(CACHE).then(function (cache) {
return fetch(request).then(function (response) {
return cache.put(request, response);
});
});
}
function fromServer(request){
//this is the fallback if it is not in the cache to go to the server and get it
return fetch(request).then(function(response){ return response});
}