-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsw.js
122 lines (113 loc) · 3.14 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
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
114
115
116
117
118
119
120
121
122
const staticCache = 'static-v4';
const dynamicCache = 'dynamic-v4';
const apiCache = 'apiCache';
const assets = [
'./',
'index.html',
'styles.css',
'js/ui.js',
'js/app.js',
'js/controller.js',
'js/material.min.js',
'js/materialize.min.js',
'css/materialize.css',
'https://fonts.googleapis.com/icon?family=Material+Icons',
'https://fonts.googleapis.com/css?family=Poppins&display=swap',
'https://fonts.gstatic.com/s/materialicons/v48/flUhRq6tzZclQEJ-Vdg-IuiaDsNcIhQ8tQ.woff2',
'manifest.json',
'icons/icon144.png',
'images/favicon.ico',
'images/favicon-32x32.png',
'images/place.png',
];
// Cache size limiting functiom
const limitCache = (name, size) => {
caches.open(name).then(cache => {
cache.keys().then(keys => {
if (keys.length > size) {
cache.delete(keys[0]).then(limitCache(name, size));
}
})
})
}
// 'install' event handler
self.addEventListener('install', evt => {
console.log("Service worker installed");
evt.waitUntil(
caches.open(staticCache)
.then(cache => {
cache.addAll(assets)
.then(console.log("Assets Cached")
);
}).catch(err => {
console.log(err);
})
);
});
// 'activate' event handler
self.addEventListener('activate', evt => {
console.log("Service worker activated");
evt.waitUntil(
caches.keys().then(keys => {
return Promise.all(
keys.filter(key => key !== staticCache && key !== dynamicCache)
.map(key => caches.delete(key))
);
})
)
});
// 'fetch' event handler
self.addEventListener('fetch', evt => {
evt.respondWith(
caches.match(evt.request).then(res => {
return res ||
fetch(evt.request).then(async fetchRes => {
const cache = await caches.open(dynamicCache);
cache.put(evt.request.url, fetchRes.clone());
limitCache(dynamicCache, 8);
return fetchRes;
});
}).catch((err) => {
console.log(err);
})
);
if (evt.request.url.includes("openweathermap")) {
evt.waitUntil(
update(evt.request)
.then(data => { return refresh(evt.request.url, data) })
.catch(err => console.log(err))
)
}
});
const update = (request) => {
return new Promise((resolve, reject) => {
try {
fetch(request.url).then(async fetchRes => {
const cache = await caches.open(apiCache);
cache.put(request.url, fetchRes.clone());
limitCache(apiCache, 10);
resolve(fetchRes);
});
} catch (error) {
console.log(error);
reject();
}
})
}
const refresh = (url, response) => {
return new Promise((resolve, reject) => {
response.json() // read and parse JSON response
.then(jsonResponse => {
self.clients.matchAll().then(clients => {
clients.forEach(client => { // report and send new data to client
var type = 'weather';
if (url.includes('forecast'))
type = 'graph'
client.postMessage(JSON.stringify({ type: type, data: jsonResponse }))
})
})
resolve(jsonResponse.data); // resolve promise with new data
})
.catch(err => reject(err));
})
}