-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsw.js
51 lines (46 loc) · 1.47 KB
/
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// This is the service worker
var urls = ["/", "/assets/css/app.css", "/assets/js/all.js"];
// Pre-caching assets in a Service Worker
self.addEventListener("install", function(event) {
console.log("The SW is now installed");
event.waitUntil(
caches.open("PWACache")
.then(function(cache) {
return cache.addAll(urls);
})
.then(function() {
return self.skipWaiting(); // Optional
})
);
});
// Serving from Cache First
self.addEventListener("fetch", function(event) {
event.respondWith(caches.match(event.request) //do you have this request in the cache?
.then(function(response) {
if (response) {
// The request is in the cache
return response;
} else {
// We need to go to the network
return fetch(event.request);
}
})
)
});
// Serving from Cache and Update resources
// self.addEventListener("fetch", function(event) {
// event.respondWith(
// caches.match(event.request)
// .then(function(response) {
// // Even if the response is in the cache, we fetch it
// // and update the cache for future usage
// var fetchPromise = fetch(event.request).then(
// function(networkResponse) {
// caches.put(event.request, networkResponse.clone());
// return networkResponse;
// });
// // We use the currently cached version if it's there
// return response || fetchPromise;
// })
// );
// });