This repository was archived by the owner on Mar 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 188
/
Copy pathsw.js
121 lines (105 loc) · 3.69 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
/*
* @license
* Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
* This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
* The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
* The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
* Code distributed by Google as part of the polymer project is also
* subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
*/
"use strict";
var log = console.log.bind(console);
var err = console.error.bind(console);
this.onerror = err;
// Moves the contents of one named cached into another.
var cacheCopy = function(source, destination) {
return caches.delete(destination).then(function() {
return Promise.all([
caches.open(source),
caches.open(destination)
]).then(function(results) {
var sourceCache = results[0];
var destCache = results[1];
return sourceCache.keys().then(function(requests) {
return Promise.all(requests.map(function(request) {
return sourceCache.match(request).then(function(response) {
return destCache.put(request, response);
});
}));
});
});
});
}
var fetchAndCache = function(request, cache) {
if (!(request instanceof Request)) {
request = new Request(request);
}
return fetch(request.clone()).then(function(response) {
cache.put(request, response.clone());
return response;
});
};
var baseUrl = (new URL("./", this.location.href) + "");
// TODO: This is necessary to handle different implementations in the wild
// The spec defines self.registration, but it was not implemented in Chrome 40.
var scope;
if (self.registration) {
scope = self.registration.scope;
} else {
scope = self.scope || baseUrl;
}
this.addEventListener("install", function(e) {
// Put updated resources in a new cache, so that currently running pages
// get the current versions.
e.waitUntil(caches.delete("core-waiting").then(function() {
return caches.open("core-waiting").then(function(core) {
var resourceUrls = [
"",
"?offline",
"build.js",
"components/topeka-elements/categories.json",
"components/topeka-elements/images/splash.svg",
"favicon.ico",
"icons/icon-196.png",
"images/splash.svg",
"leaderboard.html",
"polyfills/fonts/fonts.css",
"polyfills/fonts/roboto.html",
"polyfills/fonts/RobotoDraft-Black.woff2",
"polyfills/fonts/RobotoDraft-Bold.woff2",
"polyfills/fonts/RobotoDraft-BoldItalic.woff2",
"polyfills/fonts/RobotoDraft-Light.woff2",
"polyfills/fonts/RobotoDraft-Medium.woff2",
"polyfills/fonts/RobotoDraft-Regular.woff2",
"polyfills/fonts/RobotoDraft-Thin.woff2",
"theme.css",
];
return Promise.all(resourceUrls.map(function(relativeUrl) {
return fetchAndCache(baseUrl + relativeUrl, core);
}));
});
}));
});
this.addEventListener("activate", function(e) {
// Copy the newly installed cache to the active cache
e.waitUntil(cacheCopy("core-waiting", "core"));
});
this.addEventListener("fetch", function(e) {
var request = e.request;
if (request.url.indexOf(scope) === -1) {
return;
}
// Basic read-through caching.
e.respondWith(
caches.open("core").then(function(core) {
return core.match(request).then(function(response) {
if (response) {
return response;
}
// we didn't have it in the cache, so add it to the cache and return it
log("runtime caching:", request.url);
return fetchAndCache(request, core);
});
})
);
});