-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
119 lines (114 loc) · 4.17 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
console.log("Service Worker Waking UP :)");
self.addEventListener('install', function(event) { //when the SW is registered this install function is executed
event.waitUntil(//opens the cache with the name defined on line 34 and caches the list of static files
caches.open(CACHE_NAME)
.then(cache => {
return cache.addAll(cacheurls);
})
);
});
self.addEventListener('activate', function(event) { //handles the activation of the sw (after registering)
console.log("NEW Service Worker Activated :)");
});
//cache first fetch function
self.addEventListener('fetch', event => {//waits till a file is requested
console.log('[Trail - ServiceWorker] Fetch event fired.', event.request.url);
event.respondWith(
caches.match(event.request).then(function(response) {//if the file is in the cache then it retrieves it
if (response) {
console.log('[Trail - ServiceWorker] Retrieving from cache...');
return response;
}
console.log('[Trail - ServiceWorker] Retrieving from URL...');//if it is not in the cache then it uses the network
return fetch(event.request).catch(function (e) {
alert('You appear to be offline, please try again when back online');
});
})
);
});
const CACHE_NAME = 'Trail-cache-v3'; //defines the name of the cache
const cacheurls = [ //an array consistic of the static files to be cached
'./',
'/static/edi.geojson',
'/static/css/bootstrap.css',
'/static/css/main.css',
'/static/css/mdb.css',
'/static/img/favicon.png',
'/static/img/marker.svg',
'/static/js/WriteIt.js',
'/static/js/jquery.js',
'manifest.webmanifest',
'sw.js',
'/static/facts.json',
'/static/templates/footer.html',
'/static/templates/head.html',
'/static/templates/index.html',
'/static/templates/map.html',
'/static/templates/mobnav.html',
'/static/templates/nav.html',
'/static/templates/scan.html',
'/static/templates/scripts.html',
'/static/templates/settings.html',
'/static/templates/tour.html',
'/static/font/coolvetica_rg-webfont.woff2',
'/static/font/coolvetica_rg-webfont.woff',
'/static/fontawesome/css/all.min.css',
'/static/css/bootstrap.min.css',
'/static/js/jquery.min.js',
'/static/js/popper.min.js',
'/static/js/bootstrap.min.js',
'/static/js/mdb.min.js',
'/static/js/WriteIt.min.js',
'/static/js/html5-qrcode.min.js',
'/static/img/icon-144.png',
'/static/fontawesome/webfonts/fa-solid-900.woff2',
'/static/templates/infomodal.html',
'/static/TreesHWU.geojson',
'/static/img/tree1.svg',
'/static/img/tree2.svg',
'/static/img/tree3.svg',
'/static/img/tree4.svg',
'/static/img/tree5.svg',
'/static/img/tree6.svg',
'/static/img/tree7.svg',
'/static/img/tree8.svg',
'/static/img/tree9.svg',
'/static/img/tree10.svg',
'/static/img/tree11.svg',
'/static/img/tree12.svg',
'/static/img/tree13.svg',
'/static/img/tree14.svg',
'/static/img/tree15.svg',
'/static/img/tree16.svg',
'/static/img/tree17.svg',
'/static/img/tree18.svg',
'/static/img/tree19.svg',
'/static/img/tree20.svg',
'/static/img/tree21.svg',
'/static/img/tree22.svg',
'/static/img/tree23.svg',
'/static/img/tree24.svg',
'/static/img/tree25.svg',
'/static/img/tree26.svg',
'/static/img/tree27.svg',
'/static/img/tree28.svg',
'/static/img/tree29.svg',
'/static/img/tree30.svg',
'/static/img/tree31.svg',
'/static/img/tree32.svg',
'/static/img/tree33.svg',
'/static/img/tree34.svg',
'/static/img/tree35.svg',
'/static/img/tree36.svg',
'/static/img/tree37.svg',
'/static/img/tree38.svg',
'/static/img/tree39.svg',
'/static/img/tree40.svg',
'/static/img/tree41.svg',
'/static/img/tree42.svg',
'/static/img/tree43.svg',
'/static/img/tree44.svg',
'/static/img/tree45.svg',
'/static/img/tree46.svg',
'/static/img/tree47.svg'
]