-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
65 lines (54 loc) · 1.89 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
Adding a service worker.
*/
//How to automatically change cache version?
const cacheName = 'v3';
const cachedFiles= [
'/',
'/index.html',
'css/main.css',
'js/main.js',
'img/WorkLife-Hero.png'
];
// cache of items and installing of sw is successful.
self.addEventListener('install', function(event){
console.log("[Step 2a. Service Worker from sw.js] Installed");
event.waitUntil(
caches.open(cacheName).then(
function(cache){
console.log("[Step 2b. Caching files...]")
return cache.addAll(cachedFiles);
})
);
});
//allows old cache to be removed by removing any old files matching the previous cacheName
self.addEventListener('activate', function(event){
console.log("[Step 3a. Service Worker from sw.js] Activated")
event.waitUntil(//loop through anything in the cache and remove anything that doesn't relate to the original cacheName variable
caches.keys().then(function(cacheNames){
return Promise.all(cacheNames.map(function(thiscacheName){
if (thiscacheName !== cacheName){
console.log("[Step 3b. Service Worker] Removing Cache", thiscacheName)
return caches.delete(thiscacheName);
}
}))
})
)//end of waitUntil method
});
// the fetch event intercepts and allows custom responses to network
self.addEventListener('fetch', function(event) {
console.log("[Step 4. Service Worker from sw.js] Fetching", event.request.url);
event.respondWith(
caches.match(event.request).then(//check to see if that url exists first then respond
function(response){
if (response){
console.log("[Service worker] Found in cache", event.request.url); //if this url is in the cache return it
return response;
}
//clone the request and response
let requestClone = event.request.clone();
fetch (requestClone)
return fetch(event.request);
})
);
});