-
Notifications
You must be signed in to change notification settings - Fork 9
/
offline-worker.js
205 lines (176 loc) · 8.61 KB
/
offline-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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
* Copyright 2015 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// This generated service worker JavaScript will precache your site's resources.
// The code needs to be saved in a .js file at the top-level of your site, and registered
// from your pages in order to be used. See
// https://github.com/googlechrome/sw-precache/blob/master/demo/app/js/service-worker-registration.js
// for an example of how you can register this script and handle various service worker events.
'use strict';
var PrecacheConfig = [["brick/OpenSans-SemiBold.ttf","33f225b8f5f7d6b34a0926f58f96c1e9"],["brick/brick-1.0.1.css","b395109cd3a3cd91d96f952c158d4e46"],["brick/brick-1.0.1.js","0e4221d8d559277f886e3aa8980929e1"],["brick/brick-1.0.1.min.css","503fcc6b71b1bb10d5320d8d1cfb336f"],["brick/brick-1.0.1.min.js","7fd5eeddf8f50962e094930ba6d7cf20"],["css/app.css","4abbdb00059b423993d4af967d69cc23"],["favicon.ico","30c0af319e8ee35a9f2d65f9f470103c"],["img/icons/token-120.png","d6716f15340b4d288e428d5c9d8fa23d"],["img/icons/token-128.png","7b1f1ece9aff977735e2ca2b3634246c"],["img/icons/token-16.png","6679c34a93e88d94a42c2b784e63b8f5"],["img/icons/token-256.png","de4c13222da113dcd80e3686910b1a29"],["img/icons/token-32.png","71cab4fec9f06e08b60697e45a9cc90f"],["img/icons/token-60.png","97efa52dcc7d42ad1a93bfb6e1575425"],["img/icons/token-90.png","34dea521144c778854c06488bf9cffb0"],["img/icons/token.png","4b259d146bdb52edff7290740fa1b8bc"],["index.html","b7649793a7dc52fadbd3976f8f55d13b"],["js/app.js","8f0a851302577668ab060efef022237d"],["js/lib/base32.js","1f5ea699e913c7dc67d9c7ae47b10b9f"],["js/lib/localforage.js","9f6b53bc9d4bf0c6a433c6152510f4f4"],["js/lib/pointer.min.js","9ba9814575d18d4cbc76f8e4ea61c4a6"],["js/lib/sha1.js","d2c987769d6b987f41307f729225c5c4"],["js/offline-manager.js","28bd78ca4fcf8ae2b562c51a1cd2f5d3"]];
var CacheNamePrefix = 'sw-precache-v1--' + (self.registration ? self.registration.scope : '') + '-';
var IgnoreUrlParametersMatching = [/^utm_/];
var addDirectoryIndex = function (originalUrl, index) {
var url = new URL(originalUrl);
if (url.pathname.slice(-1) === '/') {
url.pathname += index;
}
return url.toString();
};
var populateCurrentCacheNames = function (precacheConfig, cacheNamePrefix, baseUrl) {
var absoluteUrlToCacheName = {};
var currentCacheNamesToAbsoluteUrl = {};
precacheConfig.forEach(function(cacheOption) {
var absoluteUrl = new URL(cacheOption[0], baseUrl).toString();
var cacheName = cacheNamePrefix + absoluteUrl + '-' + cacheOption[1];
currentCacheNamesToAbsoluteUrl[cacheName] = absoluteUrl;
absoluteUrlToCacheName[absoluteUrl] = cacheName;
});
return {
absoluteUrlToCacheName: absoluteUrlToCacheName,
currentCacheNamesToAbsoluteUrl: currentCacheNamesToAbsoluteUrl
};
};
var stripIgnoredUrlParameters = function (originalUrl, ignoreUrlParametersMatching) {
var url = new URL(originalUrl);
url.search = url.search.slice(1) // Exclude initial '?'
.split('&') // Split into an array of 'key=value' strings
.map(function(kv) {
return kv.split('='); // Split each 'key=value' string into a [key, value] array
})
.filter(function(kv) {
return ignoreUrlParametersMatching.every(function(ignoredRegex) {
return !ignoredRegex.test(kv[0]); // Return true iff the key doesn't match any of the regexes.
});
})
.map(function(kv) {
return kv.join('='); // Join each [key, value] array into a 'key=value' string
})
.join('&'); // Join the array of 'key=value' strings into a string with '&' in between each
return url.toString();
};
var mappings = populateCurrentCacheNames(PrecacheConfig, CacheNamePrefix, self.location);
var AbsoluteUrlToCacheName = mappings.absoluteUrlToCacheName;
var CurrentCacheNamesToAbsoluteUrl = mappings.currentCacheNamesToAbsoluteUrl;
function deleteAllCaches() {
return caches.keys().then(function(cacheNames) {
return Promise.all(
cacheNames.map(function(cacheName) {
return caches.delete(cacheName);
})
);
});
}
self.addEventListener('install', function(event) {
var now = Date.now();
event.waitUntil(
caches.keys().then(function(allCacheNames) {
return Promise.all(
Object.keys(CurrentCacheNamesToAbsoluteUrl).filter(function(cacheName) {
return allCacheNames.indexOf(cacheName) == -1;
}).map(function(cacheName) {
var url = new URL(CurrentCacheNamesToAbsoluteUrl[cacheName]);
// Put in a cache-busting parameter to ensure we're caching a fresh response.
if (url.search) {
url.search += '&';
}
url.search += 'sw-precache=' + now;
var urlWithCacheBusting = url.toString();
console.log('Adding URL "%s" to cache named "%s"', urlWithCacheBusting, cacheName);
return caches.open(cacheName).then(function(cache) {
var request = new Request(urlWithCacheBusting, {credentials: 'same-origin'});
return fetch(request.clone()).then(function(response) {
if (response.status == 200) {
return cache.put(request, response);
} else {
console.error('Request for %s returned a response with status %d, so not attempting to cache it.',
urlWithCacheBusting, response.status);
// Get rid of the empty cache if we can't add a successful response to it.
return caches.delete(cacheName);
}
});
});
})
).then(function() {
return Promise.all(
allCacheNames.filter(function(cacheName) {
return cacheName.indexOf(CacheNamePrefix) == 0 &&
!(cacheName in CurrentCacheNamesToAbsoluteUrl);
}).map(function(cacheName) {
console.log('Deleting out-of-date cache "%s"', cacheName);
return caches.delete(cacheName);
})
)
});
}).then(function() {
if (typeof self.skipWaiting == 'function') {
// Force the SW to transition from installing -> active state
self.skipWaiting();
}
})
);
});
if (self.clients && (typeof self.clients.claim == 'function')) {
self.addEventListener('activate', function(event) {
event.waitUntil(self.clients.claim());
});
}
self.addEventListener('message', function(event) {
if (event.data.command == 'delete_all') {
console.log('About to delete all caches...');
deleteAllCaches().then(function() {
console.log('Caches deleted.');
event.ports[0].postMessage({
error: null
});
}).catch(function(error) {
console.log('Caches not deleted:', error);
event.ports[0].postMessage({
error: error
});
});
}
});
self.addEventListener('fetch', function(event) {
if (event.request.method == 'GET') {
var urlWithoutIgnoredParameters = stripIgnoredUrlParameters(event.request.url,
IgnoreUrlParametersMatching);
var cacheName = AbsoluteUrlToCacheName[urlWithoutIgnoredParameters];
var directoryIndex = 'index.html';
if (!cacheName && directoryIndex) {
urlWithoutIgnoredParameters = addDirectoryIndex(urlWithoutIgnoredParameters, directoryIndex);
cacheName = AbsoluteUrlToCacheName[urlWithoutIgnoredParameters];
}
if (cacheName) {
event.respondWith(
// We can't call cache.match(event.request) since the entry in the cache will contain the
// cache-busting parameter. Instead, rely on the fact that each cache should only have one
// entry, and return that.
caches.open(cacheName).then(function(cache) {
return cache.keys().then(function(keys) {
return cache.match(keys[0]).then(function(response) {
return response || fetch(event.request).catch(function(e) {
console.error('Fetch for "%s" failed: %O', urlWithoutIgnoredParameters, e);
});
});
});
}).catch(function(e) {
console.error('Couldn\'t serve response for "%s" from cache: %O', urlWithoutIgnoredParameters, e);
return fetch(event.request);
})
);
}
}
});