-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
executable file
·65 lines (50 loc) · 1.61 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
const CacheN = 'currCache';
const ImgsCacheN = 'currImgN';
self.addEventListener('install', function(event) {
event.waitUntil(
caches.open(CacheN).then(function(cache) {
return cache.addAll([
'/currency/',
'/currency/index.html',
'/currency/js/main.js',
'/currency/css/style.css'
]);
})
);
});
self.addEventListener('fetch', function(event) {
let requestUrl = new URL(event.request.url);
console.log(event.request);
if (requestUrl.origin === location.origin) {
if (requestUrl.pathname === '/currency/') {
event.respondWith(caches.match('/currency/'));
return;
}
}
if (requestUrl.pathname.startsWith('/currency/images')) {
event.respondWith(servePhoto(event.request));
return;
}
event.respondWith(
caches.match(event.request).then(function(response) {
return response || fetch(event.request);
})
);
});
function servePhoto(request) {
let storageUrl = request.url.replace(/-\d+px\.jpeg$/, '');
return caches.open(ImgsCacheN).then(function(cache) {
return cache.match(storageUrl).then(function(response) {
if (response) return response;
return fetch(request).then(function(networkResponse) {
cache.put(storageUrl, networkResponse.clone());
return networkResponse;
});
});
});
}
self.addEventListener('message', function(event) {
if (event.data.action === 'skipWaiting') {
self.skipWaiting();
}
});