forked from nightscout/cgm-remote-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice-worker.js
174 lines (162 loc) · 5.29 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
'use strict';
var CACHE = '<%= locals.cachebuster %>';
const CACHE_LIST = [
'/images/launch.png',
'/images/apple-touch-icon-57x57.png',
'/images/apple-touch-icon-60x60.png',
'/images/apple-touch-icon-72x72.png',
'/images/apple-touch-icon-76x76.png',
'/images/apple-touch-icon-114x114.png',
'/images/apple-touch-icon-120x120.png',
'/images/apple-touch-icon-144x144.png',
'/images/apple-touch-icon-152x152.png',
'/images/apple-touch-icon-180x180.png',
'/images/favicon-32x32.png',
'/images/android-chrome-192x192.png',
'/images/favicon-96x96.png',
'/images/favicon-16x16.png',
'/manifest.json',
'/images/favicon.ico',
'/images/mstile-144x144.png',
'/css/ui-darkness/jquery-ui.min.css',
'/css/jquery.tooltips.css',
'/css/ui-darkness/images/ui-icons_ffffff_256x240.png',
'/css/ui-darkness/images/ui-icons_cccccc_256x240.png',
'/css/ui-darkness/images/ui-bg_inset-soft_25_000000_1x100.png',
'/css/ui-darkness/images/ui-bg_gloss-wave_25_333333_500x100.png',
'/css/main.css',
'/bundle/js/bundle.app.js',
'/bundle/js/bundle.clock.js',
'/socket.io/socket.io.js',
'/js/client.js',
'/images/logo2.png'
];
function returnRangeRequest(request) {
return caches
.open(CACHE)
.then((cache) => {
return cache.match(request.url);
})
.then((res) => {
if (!res) {
return fetch(request)
.then(res => {
const clonedRes = res.clone();
return caches
.open(CACHE)
.then(cache => cache.put(request, clonedRes))
.then(() => res);
})
.then(res => {
return res.arrayBuffer();
});
}
return res.arrayBuffer();
})
.then((arrayBuffer) => {
const bytes = /^bytes=(\d+)-(\d+)?$/g.exec(
request.headers.get('range')
);
if (bytes) {
const start = Number(bytes[1]);
const end = Number(bytes[2]) || arrayBuffer.byteLength - 1;
return new Response(arrayBuffer.slice(start, end + 1), {
status: 206,
statusText: 'Partial Content',
headers: [
['Content-Range', `bytes ${start}-${end}/${arrayBuffer.byteLength}`]
]
});
} else {
return new Response(null, {
status: 416,
statusText: 'Range Not Satisfiable',
headers: [['Content-Range', `*/${arrayBuffer.byteLength}`]]
});
}
});
}
// Open a cache and `put()` the assets to the cache.
// Return a promise resolving when all the assets are added.
function precache() {
return caches.open(CACHE)
.then((cache) => {
// if any cache requests fail, don't interrupt other requests in progress
return Promise.allSettled(
CACHE_LIST.map((url) => {
// `no-store` in case of partial content responses and
// because we're making our own cache
let request = new Request(url, { cache: 'no-store' });
return fetch(request).then((response) => {
// console.log('Caching response', url, response);
cache.put(url, response);
}).catch((err) => {
console.log('Could not precache asset', url, err);
});
})
);
});
}
// Try to read the requested resource from cache.
// If the requested resource does not exist in the cache, fetch it from
// network and cache the response.
function fromCache(request) {
return caches.open(CACHE).then((cache) => {
return cache.match(request).then((matching) => {
console.log(matching);
if(matching){
return matching;
}
return fetch(request).then((response) => {
// console.log('Response from network is:', response);
cache.put(request, response.clone());
return response;
}).catch((error) => {
// This catch() will handle exceptions thrown from the fetch() operation.
// Note that a HTTP error response (e.g. 404) will NOT trigger an exception.
// It will return a normal response object that has the appropriate error code set.
console.error('Fetching failed:', error);
throw error;
});
});
});
}
// On install, cache some resources.
self.addEventListener('install', (evt) => {
// console.log('The service worker is being installed.');
self.skipWaiting();
evt.waitUntil(precache());
});
function inCache(request) {
let found = false;
CACHE_LIST.forEach((e) => {
if (request.url.endsWith(e)) {
found = true;
}
});
return found;
}
self.addEventListener('fetch', (evt) => {
if (!evt.request.url.startsWith(self.location.origin) || CACHE === 'developmentMode' || !inCache(evt.request) || evt.request.method !== 'GET') {
//console.log('Skipping cache for ', evt.request.url);
return void evt.respondWith(fetch(evt.request));
}
if (evt.request.headers.get('range')) {
evt.respondWith(returnRangeRequest(evt.request));
} else {
evt.respondWith(fromCache(evt.request));
}
});
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames.map((cacheName) => {
if (cacheName !== CACHE) {
// console.log('Deleting out of date cache:', cacheName);
return caches.delete(cacheName);
}
})
);
}));
});