forked from victrme/Bonjourr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice-worker.js
86 lines (74 loc) · 1.87 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
//
const version = '1.10.0b'
const bonjourrCache = 'bonjourr-v' + version
const filesToChache = [
'/',
'/settings.html',
'/manifest.webmanifest',
'/src/scripts/main.js',
'/src/styles/style.css',
'/src/assets/favicon-128x128.png',
'/src/assets/favicon-512x512.png',
'/src/assets/apple-touch-icon.png',
'/src/assets/interface/gear.svg',
'/src/assets/interface/loading.gif',
]
const weatherList = [
'snow',
'mist',
'clearsky',
'fewclouds',
'lightrain',
'showerrain',
'thunderstorm',
'brokenclouds',
'lightdrizzle',
'showerdrizzle',
'overcastclouds',
]
const addWeatherIcons = (time) => weatherList.forEach((elem) => filesToChache.push(`/src/assets/weather/${time}/${elem}.png`))
addWeatherIcons('day')
addWeatherIcons('night')
//
//
// EVENTS
//
//
self.addEventListener('install', (event) =>
event.waitUntil(caches.open(bonjourrCache).then((cache) => cache.addAll(filesToChache)))
)
self.addEventListener('activate', (e) => {
e.waitUntil(
caches.keys().then((keyList) => {
return Promise.all(
keyList.map((key) => {
if (bonjourrCache.indexOf(key) === -1) {
return caches.delete(key)
}
})
)
})
)
})
self.addEventListener('fetch', function (event) {
event.respondWith(
caches.match(event.request).then(function (response) {
//
if (response) return response
else {
const fetchRequest = event.request.clone()
return fetch(fetchRequest).then(function (response) {
if (!response || response.status !== 200 || response.type !== 'basic') return response
const responseToCache = response.clone()
// Don't save APIs
// Todo: save latest unsplash image
if (event.request.url.includes('unsplash.com') && event.request.url.includes('api.openweathermap.org'))
caches.open(bonjourrCache).then(function (cache) {
cache.put(event.request, responseToCache)
})
return response
})
}
})
)
})