-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathservice-worker.js
More file actions
36 lines (36 loc) · 876 Bytes
/
service-worker.js
File metadata and controls
36 lines (36 loc) · 876 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
27
28
29
30
31
32
33
34
35
36
self.addEventListener("install", function(event) {
event.waitUntil(
caches.open("images").then(function(cache) {
return cache.addAll(
[
"ukiyo-e.png",
"car.jpg",
"nishida_2.jpeg"
]
);
})
);
});
self.addEventListener("fetch", function(event) {
if (event.request.url.toLowerCase().match(/\.(jpg|png)/g)) {
// This is an image
// console.log("this is an image request");
event.respondWith(
caches.match(event.request).then(function(response) {
if (response) {
// Found response in cache
// console.log("Found response in cache");
return response;
}
// Image not found in cache
// console.log("Image not found in cache");
return fetch(event.request).then(function(response) {
return response;
}).catch(function(error) {
// console.log(error);
throw error;
});
})
);
}
});