-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathservice-worker.js
More file actions
26 lines (23 loc) · 757 Bytes
/
service-worker.js
File metadata and controls
26 lines (23 loc) · 757 Bytes
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
function addToCache(request, networkResponse) {
return caches.open('web-bluetooth')
.then(cache => cache.put(request, networkResponse));
}
function getCacheResponse(request) {
return caches.open('web-bluetooth').then(cache => {
return cache.match(request);
});
}
function getNetworkOrCacheResponse(request) {
return fetch(request).then(networkResponse => {
addToCache(request, networkResponse.clone());
return networkResponse;
}).catch(_ => {
return getCacheResponse(request)
.then(cacheResponse => cacheResponse || Response.error());
});
}
self.addEventListener('fetch', event => {
if (event.request.url.startsWith(self.location.origin)) {
event.respondWith(getNetworkOrCacheResponse(event.request));
}
});