-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
98 lines (85 loc) · 3.41 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
/* global self workbox */
"use strict";
var coreFiles = [
'index.html',
'xlsx-encoder.html',
'setup.js',
'checkin.js',
'xlsx-encoder.js',
'https://cdn.jsdelivr.net/npm/[email protected]/dist/turretcss.min.css',
'https://cdn.jsdelivr.net/npm/[email protected]/surpass.css',
'https://cdn.jsdelivr.net/npm/[email protected]/surpass.js',
'https://cdn.jsdelivr.net/npm/[email protected]/nacl-fast.min.js',
'https://cdn.jsdelivr.net/npm/[email protected]/dist/fuse.min.js',
'https://cdn.jsdelivr.net/npm/[email protected]/dist/localforage.min.js',
'https://cdn.jsdelivr.net/npm/[email protected]/dist/shim.min.js',
'https://cdn.jsdelivr.net/npm/[email protected]/dist/xlsx.full.min.js',
'https://cdn.jsdelivr.net/npm/[email protected]/dist/zxcvbn.js',
'https://cdn.jsdelivr.net/npm/[email protected]/FileSaver.min.js'
];
// vaguely inspired by recipes like https://serviceworke.rs/strategy-cache-update-and-refresh_service-worker_doc.html
function areBodiesEqual(bodies) {
return Promise.all(bodies.map(function(body){return body.arrayBuffer()}))
.then(function(arrayBuffers) {
var i, j, firstValue;
var bufferLength = arrayBuffers[0].length;
for (i = 1; i < arrayBuffers.length; ++i) {
if (arrayBuffers[i] != bufferLength) return false;
}
for (i = 0; i < bufferLength; ++i) {
firstValue = arrayBuffers[0][i];
for (j = 1; j < arrayBuffers.length; ++j) {
if (arrayBuffers[j][i] != firstValue) return false;
}
}
return true;
});
}
function announceUpdate(url) {
return self.clients.matchAll().then(function (clients) {
clients.forEach(function (client) {
client.postMessage({
type: 'update',
url: url
});
});
});
}
function areResponsesEqual(cachedResponse, newResponse) {
var cachedHeader, newHeader;
// if one response is missing, there's no "change" (we're starting here)
if (!cachedResponse || !newResponse) {
return Promise.resolve(false);
}
// if either response is opaque / a network error
// (the latter not being likely to be sent to this function as written)
if (!(cachedResponse.status && newResponse.status)) {
// this is only noteworthy if the new response isn't opaque / an error
return Promise.resolve(!!newResponse.status);
}
// compare ETags or Last-Modified, if present for comparison
cachedHeader = cachedResponse.headers.get('ETag');
newHeader = newResponse.headers.get('ETag');
if (cachedHeader && newHeader) {
return Promise.resolve(cachedHeader == newHeader);
}
cachedHeader = cachedResponse.headers.get('Last-Modified');
newHeader = newResponse.headers.get('Last-Modified');
if (cachedHeader && newHeader) {
return Promise.resolve(cachedHeader == newHeader);
}
// fall back to outright comparing bodies byte by byte
// NOTE: Unless Workbox gives plugins responses from cache,
// it will probably be too late to clone these request bodies
return areBodiesEqual([cachedResponse.clone(), newResponse.clone()]);
}
importScripts("https://storage.googleapis.com/workbox-cdn/releases/3.1.0/workbox-sw.js");
workbox.precaching.precache(coreFiles);
workbox.routing.setDefaultHandler(workbox.strategies.staleWhileRevalidate({
plugins: [{
cacheDidUpdate: function (cacheName, url, cachedResponse, newResponse) {
return areResponsesEqual(cachedResponse, newResponse).then(
function(unchanged) {if (!unchanged) return announceUpdate(url)});
}
}]
}));