-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserviceworker.js
72 lines (65 loc) · 2.35 KB
/
serviceworker.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
66
67
68
69
70
71
72
//set up cache name and files to add to it
// only change from here down
const CACHE_NAME = 'My personal website';
const CACHE_URLS = ['index.html',
'quals.html',
'skills.html',
'wordle.html',
'css1.html',
'css2.html',
'scripts/manifest.json',
'styles/styles.css',
'styles/styles2.css',
'assets/images/me.png',
'assets/images/code.jpg',
'scripts/wordle.js',
'scripts/wordlist.js',
'scripts/main.js',
'assets/icons/icon-512x512.png'];
//DO NOT change any of the code below
//...
//add all URLs to cache when installed
self.addEventListener("install", function(event){
console.log("Service worker installed");
event.waitUntil(
//create and open cache
caches.open(CACHE_NAME)
.then(function(cache){
console.log("Cache opened");
//add all URLs to cache
return cache.addAll(CACHE_URLS);
})
);
});
//On activate update the cache with the new version and clean out old caches
self.addEventListener('activate', function(event) {
event.waitUntil(
caches.keys().then(function(cacheNames) {
return Promise.all(
cacheNames.map(function(cacheName) {
if (cacheName.startsWith('my-site-') && CACHE_NAME !== cacheName) {
return caches.delete(cacheName);
}
})
);
})
);
});
//add all URLs to cache when installed
//...
//user has navigated to page - fetch required assets
self.addEventListener("fetch", function(event){
event.respondWith(
caches.match(event.request).then(function(response){
//check whether asset is in cache
if(response){
//asset in cache, so return it
console.log(`Return ${event.request.url} from cache`);
return response;
}
//asset not in cache so fetch asset from network
console.log(`Fetch ${event.request.url} from network`);
return fetch(event.request);
})
);
});