-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
113 lines (97 loc) · 2.66 KB
/
sw.js
File metadata and controls
113 lines (97 loc) · 2.66 KB
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
104
105
106
107
108
109
110
111
112
113
'use strict';
// Static Files as version
const staticCacheVersion = 'v0.1.2';
// Files to cache
const files = [
'./',
'./app.html',
'./index.html',
'./country_selector/css/intlTelInput.min.css',
'./country_selector/img/flags.png',
'./country_selector/img/flags@2x.png',
'./country_selector/js/intlTelInput.min.js',
'./country_selector/js/utils.js',
'./css/app.css',
'./css/toastify.min.css',
'./images/fav.png',
'./images/logo-16.png',
'./images/logo-32.png',
'./images/logo-128.png',
'./images/logo-152.png',
'./images/logo-167.png',
'./images/logo-180.png',
'./images/logo-192.png',
'./images/logo-512.png',
'./images/logo.png',
'./fonts/Nunito/Nunito-300-1.woff2',
'./fonts/Nunito/Nunito-300-2.woff2',
'./fonts/Nunito/Nunito-300-3.woff2',
'./fonts/Nunito/Nunito-300-4.woff2',
'./fonts/Nunito/Nunito-300-5.woff2',
'./fonts/Nunito/Nunito-400-1.woff2',
'./fonts/Nunito/Nunito-400-2.woff2',
'./fonts/Nunito/Nunito-400-3.woff2',
'./fonts/Nunito/Nunito-400-4.woff2',
'./fonts/Nunito/Nunito-400-5.woff2',
'./js/script.js',
'./js/toastify.js',
'./manifest.json',
];
// Install
self.addEventListener('install', e => {
self.skipWaiting();
e.waitUntil(
caches.open(staticCacheVersion).then(cache => {
return cache
.addAll(files)
.then(() => console.log('App Version: ' + staticCacheVersion))
.catch(err => console.error('Error adding files to cache', err));
}),
);
});
// Activate
self.addEventListener('activate', e => {
e.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.map(cache => {
if (cache !== staticCacheVersion) {
console.info('Deleting Old Cache', cache);
return caches.delete(cache);
}
}),
);
}),
);
return self.clients.claim();
});
const cacheFirstOrigins = [location.origin, 'https://ipinfo.io'];
// Fetch
self.addEventListener('fetch', e => {
const req = e.request;
const url = new URL(req.url);
if (cacheFirstOrigins.includes(url.origin)) {
return e.respondWith(cacheFirst(req));
}
return e.respondWith(networkFirst(req));
});
async function cacheFirst(req) {
const cacheRes = await caches.match(req);
if (cacheRes && cacheRes.status === 200) {
return cacheRes;
}
return fetch(req);
}
async function networkFirst(req) {
try {
const dynamicCache = await caches.open('dynamic');
const networkResponse = await fetch(req);
if (req.method !== 'POST') {
dynamicCache.put(req, networkResponse.clone());
}
return networkResponse;
} catch (err) {
const cacheResponse = await caches.match(req);
return cacheResponse;
}
}