-
Notifications
You must be signed in to change notification settings - Fork 5
/
sw.js
33 lines (30 loc) · 983 Bytes
/
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
this.addEventListener('install', e => {
e.waitUntil(
caches.open('precache').then(cache => cache.add('/broken.png'))
);
});
function isImage(fetchRequest) {
return (
fetchRequest.method === 'GET' && fetchRequest.destination === 'image'
);
}
this.addEventListener('fetch', e => {
e.respondWith(
fetch(e.request)
.then(response => {
if (response.ok) return response;
// User is online, but response was not ok
if (isImage(e.request)) {
// Get broken image placeholder from cache
return caches.match('/broken.png');
}
})
.catch(err => {
// User is probably offline
if (isImage(e.request)) {
// Get broken image placeholder from cache
return caches.match('/broken.png');
}
}) // end fetch
);
});