forked from google/santa-tracker-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
347 lines (303 loc) · 11.6 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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
* Copyright 2016 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.
*/
/**
* @fileoverview Service Worker for Santa Tracker.
*/
const VERSION = '<STATIC_VERSION>';
const STATIC_HOST = '<STATIC_HOST>';
const IS_STAGING = location.hostname !== 'santatracker.google.com';
if (VERSION.startsWith('<STATIC')) { // don't use whole, prevent replacing
// Don't enable the SW on environments without versions.
throw new Error('sw disabled without build');
}
const MANIFEST = `${STATIC_HOST}${VERSION}/contents.json`;
const LANGUAGE = new URL(self.location).searchParams.lang || 'en';
const STATIC_DOMAINS = ['maps.gstatic.com', 'fonts.googleapis.com', 'fonts.gstatic.com'];
const MATCH_INTL_PATH = new RegExp(/^\/intl\/([^/]+)(?:\/(.*)|)$/);
const PRECACHE = (function() {
const prod = (self.location.hostname === 'santatracker.google.com');
const prefix = (function() {
if (!prod && LANGUAGE == 'en') {
// In staging, /intl/en_ALL/ won't return anything. Just use top-level.
return '/';
}
return '/intl/' + LANGUAGE + (prod ? '_ALL' : '');
}());
const out = {};
// Cache the "intl" files as top-level files. While the user keeps making requests to this
// language, we'll keep serving them. Otherwise, they'll get real files anyway.
out['/'] = `${prefix}/somebodypleasethinkofthechildren.html`;
out['/error.html'] = `${prefix}/error.html`;
out['/manifest.json'] = `${prefix}/manifest.json`;
// cache images from Web App Manifest, top-level is fine
const sizes = [16, 32, 76, 120, 152, 192, 256];
sizes.forEach(size => {
const url = `/images/${size <= 32 ? 'fav' : ''}icon-${size}.png`;
out[url] = url;
});
return out;
}());
/**
* Fetches a url and adds it to the cache.
* @param {string|Request} url
* @param {string=} opt_cacheUrl the optional target cache URL
* @param {boolean=} opt_withCredentials
* @returns {Promise<Response>}
*/
function fetchAndCache(url, opt_cacheUrl, opt_withCredentials) {
const cacheUrl = opt_cacheUrl || url;
const opts = {};
if (opt_withCredentials) {
opts.mode = 'cors';
opts.credentials = 'include';
}
return fetch(url, opts).then(response => {
if (response.status === 200) {
if (cacheUrl !== url) {
// console.debug(`Adding ${url} to cache, as ${cacheUrl}`);
} else {
// console.debug(`Adding ${url} to cache.`);
}
caches.open(VERSION).then(cache => cache.put(new Request(cacheUrl), response));
}
return response.clone();
});
}
/**
* Loads a url from cache, or adds it if it wasn't previously cached.
* @param {string|Request} url
* @param {string=} opt_cacheUrl the optional target cache URL
* @param {boolean=} opt_withCredentials
* @returns {Promise<Response>}
*/
function loadFromCache(url, opt_cacheUrl, opt_withCredentials) {
return caches.open(VERSION)
.then(cache => cache.match(opt_cacheUrl || url))
.then(response => response || fetchAndCache(url, opt_cacheUrl, opt_withCredentials));
}
/**
* Returns the manifest corresponding to the currently active service worker.
* If there's no current manifest, returns an empty Object.
* @returns {Promise<Object>}
*/
function getOldManifest() {
return caches.open('persistent')
.then(cache => cache.match('/manifest.json'))
.then(response => response ? response.json() : {});
}
/**
* Returns the manifest corresponding to this service worker.
* @returns {Promise<Object>}
*/
function getNewManifest() {
return loadFromCache(MANIFEST).then(response => response.json())
}
/**
* Returns true if a URL should be cached. It excludes internationalised files
* from other languages.
* @param {string} url
* @returns {boolean}
*/
function shouldCache(url) {
return !url.match(/_\w\w(-\w+)?\.html$/) ||
url.match(new RegExp(`_${LANGUAGE}.html$`));
}
/**
* Returns an array of all scene IDs (e.g., dorf, boatload) cached in a specific
* cache instance.
* @param {Cache} cache
* @returns {Promise<Array<string>>}
*/
function getScenes(cache) {
return cache.keys().then(requests => {
const matches = requests.map(r => r.url.match(/\/scenes\/(\w+)\//));
return [...new Set(matches.filter(m => m).map(m => m[1]))];
});
}
function compareScene(oldScene, newScene, oldCache, newCache, oldVersion) {
if (!oldScene || !newScene) { return; }
let oldHashes = {};
for (let path in oldScene) {
oldHashes[oldScene[path]] = `${STATIC_HOST}${oldVersion}/${path}`;
}
return Promise.all(Object.keys(newScene).map(path => {
const url = `${STATIC_HOST}${VERSION}/${path}`;
if (newScene[path] in oldHashes) {
// Copy an existing response from the old cache.
return oldCache.match(oldHashes[newScene[path]]).then(match => {
if (match) { return newCache.put(url, match); }
});
} else if (shouldCache(url)) {
// Add a new response.
return newCache.add(url);
}
}));
}
/**
* Compares the content of two manifests and updates all scenes that were
* previously cached.
* @param oldManifest
* @param newManifest
* @returns {Promise}
*/
function compareManifest(oldManifest, newManifest) {
return Promise.all([caches.open(oldManifest.version), caches.open(VERSION)])
.then(([oldCache, newCache]) => {
return getScenes(oldCache).then(scenes => {
console.debug('Updating cache for', scenes);
const sharedPromise = compareScene(oldManifest.shared,
newManifest.shared, oldCache, newCache, oldManifest.version);
const scenePromises = scenes.map(s => {
return compareScene(oldManifest.scenes[s], newManifest.scenes[s],
oldCache, newCache, oldManifest.version);
});
return Promise.all([sharedPromise, ...scenePromises]);
});
});
}
/**
* Precaches all content in a given scene (for the current language).
* @param scene
* @returns {Promise}
*/
function precacheScene(scene) {
const paths = Object.keys(scene).filter(shouldCache);
return Promise.all(paths.map(path => {
return fetchAndCache(`${STATIC_HOST}${VERSION}/${path}`);
}));
}
self.addEventListener('install', function(event) {
console.debug('sw install', VERSION, LANGUAGE);
const updatePromise = Promise.all([getOldManifest(), getNewManifest()])
.then(function([oldManifest, newManifest]) {
// This is an install event, so always compare manifests, even if they may be the same.
if (oldManifest.version) {
return compareManifest(oldManifest, newManifest);
}
// TODO(samthor): This should cache village (aka dorf).
return precacheScene(newManifest.shared);
});
// nb. We fetch PRECACHE with credentials to support Santa's staging server.
const precacheRequests = Object.keys(PRECACHE).map(cacheUrl => {
const url = PRECACHE[cacheUrl]; // actually fetched URL
return loadFromCache(url, cacheUrl, true);
});
const precachePromise = Promise.all(precacheRequests);
// Wait for an update, and then always skip waiting. Santa Tracker is fairly stateless and
// time-sensitive, so ensure clients get a new version ASAP.
event.waitUntil(Promise.all([updatePromise, precachePromise]).then(() => self.skipWaiting()));
});
self.addEventListener('activate', function(event) {
console.debug('sw activate', VERSION, LANGUAGE);
const activatePromise = Promise.all([getOldManifest(), getNewManifest()])
.then(function([oldManifest, newManifest]) {
// If necessary, remove the old cache (can be asynchronous).
if (oldManifest.version && oldManifest.version !== newManifest.version) {
console.debug('sw deleting cache', oldManifest.version);
caches.delete(oldManifest.version);
}
// Move the current manifest into the persistent cache
return caches.open('persistent').then(persistentCache => {
return loadFromCache(MANIFEST).then(response => {
return persistentCache.put('/manifest.json', response);
});
});
});
// In staging, we activate the service worker immediately and claim all curent
// clients. The main thread listens to this event and refreshes the page.
// Updating and clearing the caches will still happen in the background.
if (IS_STAGING) {
event.waitUntil(activatePromise.then(_ => self.clients.claim()));
} else {
event.waitUntil(activatePromise);
}
});
/**
* Returns the cache URL for the specified prod request. Returns undefined if this should not be
* handled.
* @param {string} pathname
* @param {string} lang
* @return {string|undefined}
*/
function urlForProd(pathname, lang) {
const intl = MATCH_INTL_PATH.exec(pathname);
if (intl) {
lang = intl[1];
pathname = intl[2] || '/'; // can be undefined if "/intl/de"
}
if (lang && lang !== LANGUAGE) {
// If the language doesn't match, then defer to the network. We don't care ¯\_(ツ)_/¯
// But don't worry if there's no language: serve whatever is in cache.
console.debug('ignoring request for lang', lang, 'sw registered for', LANGUAGE);
return;
}
if (pathname === '/' || pathname.endsWith('.html')) {
if (pathname.lastIndexOf('/') > 0) {
return; // could be e.g. "/foo/bar.html", ignore
}
// TODO(samthor): We could limit to actual fanned out routes here.
switch (pathname) {
case '/upgrade.html':
case '/cast.html':
return; // don't cache cast or upgrade
case '/error.html':
return pathname; // serve error properly (used for kill switch)
}
return '/'; // this is a route, return generic index page
}
// Allow other top-level files, or anything under /images.
if (pathname.lastIndexOf('/') === 0 || pathname.startsWith('/images/')) {
return pathname;
}
// Everything else goes through to the network.
}
self.addEventListener('fetch', function(event) {
// Only handle GET requests.
if (event.request.method != 'GET') { return; }
const url = new URL(event.request.url);
// Look for static requests. This can be the same as the prod domain (in staging only), but can
// be longer (STATIC_DOMAINS, used below, can never contain STATIC_HOST).
const staticRequest = event.request.url.startsWith(STATIC_HOST);
if (staticRequest) {
// Strip "?foo" from purely static requests. This compliments a workaround inside `lazy-pages`
// for Polymer: https://github.com/Polymer/polymer/issues/3908
url.search = '';
}
// Intercept static requests. This includes STATIC_DOMAINS, for external resources like fonts,
// maps and etc; but which exludes e.g., santa-api.appspot.com and ssl.google-analytics.com.
const urlIsMatched = staticRequest || STATIC_DOMAINS.includes(url.hostname);
if (urlIsMatched) {
const urlString = url.toString();
event.respondWith(loadFromCache(urlString));
return;
}
// Otherwise, this is probably a prod request: to the domain of the SW itself.
if (url.hostname === self.location.hostname) {
let hlLang = null;
if (url.search) {
const match = /\bhl=([^&]*)\b/;
if (match) {
hlLang = match[1];
}
}
const cacheUrl = urlForProd(url.pathname, hlLang);
if (cacheUrl) {
event.respondWith(loadFromCache(url.pathname, cacheUrl, true));
}
return;
}
// TODO(plegner) Add catch handlers, to provide offline fallback responses.
});