-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathservice-worker.js
151 lines (133 loc) · 5.14 KB
/
service-worker.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Service worker that makes the website run offline.
// Author: Jonas Beuchert
// Sources:
// https://web.dev/offline-cookbook/
// https://developers.google.com/web/fundamentals/codelabs/offline
// Cache name
const CACHE = 'snappergps-static-v1';
async function updateCache() {
// Open 'caches' object
caches.open(CACHE).then(function (cache) {
// Populate 'caches' object with list of resources to cache
return cache.addAll([
'/',
'/configure',
'/upload',
'/flash',
'/accelerometer',
'/images/favicon.ico',
'/css/style.css',
'/css/upload.css',
'/js/configure/configureComms.js',
'/js/configure/configureUI.js',
'/js/upload/uploadComms.js',
'/js/upload/uploadUI.js',
'/js/configure/flashUI.js',
'/js/accelerometer/accelerometerUI.js',
'/js/deviceCommunication.js',
'/js/deviceInfo.js',
'/service-worker.js',
'/strftime-min.js',
'/FileSaver.js',
'/jszip.min.js',
'/firmware/SnapperGPS-Basic.bin',
'/firmware/SnapperGPS-Capacitance-Triggered.bin',
'/firmware/SnapperGPS-Accelerometer.bin',
]);
// TODO: Handle fail of addAll operation
})
console.log('Updated all resources in cache.');
}
// Cache page assets
// Mode: on install - as a dependency
self.addEventListener('install', function (event) {
event.waitUntil(
updateCache,
);
});
// Service worker intercepts requests to resources
// Mode: network, falling back to cache
// Trigger whenever request is made
self.addEventListener('fetch', event => {
event.respondWith(
// Try to fetch resource from network
fetch(event.request).catch(() =>
// If resource could not be fetched from network, get it from cache
caches.match(event.request)
).then(response =>
// // If resource could be fetched from network, open cache
// caches.open(CACHE).then(cache =>
// // Put resource from network into cache
// cache.put(event.request, response.clone()).then(() =>
// Return resource from network
response
// )
// )
),
);
});
// // Service worker intercepts requests to cached resources
// // Mode: cache, falling back to network
// // Trigger whenever request is made
// self.addEventListener('fetch', function (event) {
// event.respondWith(
// // Check if requested resource is available in cache
// caches.match(event.request).then(function (response) {
// // If yes, pull from cache, if not, fetch it from network
// return response || fetch(event.request);
// }),
// );
// // Update entry with latest contents from server.
// // waitUntil() to prevent worker to be killed until cache is updated.
// event.waitUntil(
// update(event.request)
// );
// });
// Open cache, perform network request, and store new response data.
// function update(request) {
// return caches.open(CACHE).then(function (cache) {
// // If there was no need to fetch resource from network:
// if (!request.bodyUsed) {
// return fetch(request).then(function (response) {
// // if (!response.ok) {
// // throw new TypeError('Bad response status');
// // }
// return cache.put(request, response.clone()).then(function () {
// return response;
// });
// });
// }
// });
// }
// Register event listener for the 'push' event.
self.addEventListener('push', function (event) {
console.log('Push notification received.');
// Retrieve the textual payload from event.data (a PushMessageData object).
// Other formats are supported (ArrayBuffer, Blob, JSON), check out the documentation
// on https://developer.mozilla.org/en-US/docs/Web/API/PushMessageData.
const uploadID = event.data.text();
// Keep the service worker alive until the notification is created.
event.waitUntil(
// Show a notification with title 'ServiceWorker Cookbook' and use the payload
// as the body.
self.registration.showNotification('SnapperGPS - ' + uploadID, {
// body: 'I have processed your data. You can go to ' +
// 'https://snappergps.info/view?uploadid=' +
// uploadID + ' to view and download your track.',
body: 'I have processed your data. Click here ' +
'to view and download your track.',
icon: 'images/icon-512.png',
// badge: 'images/icon-512.png'
data: uploadID
})
);
});
self.addEventListener('notificationclick', function (event) {
console.log('Notification click received.');
const uploadID = event.notification.data;
event.notification.close();
event.waitUntil(
clients.openWindow('https://snappergps.info/view?uploadid=' +
uploadID)
);
});