-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsw.js
170 lines (152 loc) · 3.92 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
const SW_VERSION = 7;
const APP_PREFIX = "web-skills";
const CACHE_NAME = `${APP_PREFIX}-v${SW_VERSION}`;
const DEBUG = false;
const URLS_TO_CACHE = [
"",
"index.html",
"src/app.js",
"src/config.js",
"src/data.js",
"src/util/icons.js",
"src/util/util.js",
"src/util/open-help.js",
"src/util/open-share.js",
"src/util/show-snackbar.js",
"src/util/measure.js",
"src/util/confetti-helper.js",
"src/molecules/area.js",
"src/molecules/collection.js",
"src/molecules/skill.js",
"src/firebase/init-firebase.js",
"src/firebase/auth.js",
"src/atoms/blur.js",
"src/atoms/button.js",
"src/atoms/description.js",
"src/atoms/icon.js",
"src/atoms/compact-switch.js",
"src/atoms/snackbar.js",
"src/data/accessibility.js",
"src/data/algorithms.js",
"src/data/architecture-and-paradigmes.js",
"src/data/build-tools.js",
"src/data/databases.js",
"src/data/design-and-ux.js",
"src/data/frameworks-and-libraries.js",
"src/data/fundamentals.js",
"src/data/modern.js",
"src/data/pwa.js",
"src/data/team-collaboration.js",
"src/data/testing.js",
"src/data/web-components.js",
"src/styles/shared.js",
"src/styles/global.css",
"web_modules/web-dialog.js"
];
const BLACK_LISTED_REQUEST_PATHS = [
"livereload", // Don't cache the local development server files
"?c=", // Don't cache the config when we use it to find the newest version
"r/collect" // Don't cache anything GA related
];
/**
* Precache when installed.
*/
self.addEventListener("install", e => {
log(`Installing`);
e.waitUntil(precache());
});
/**
* Delete old caches when activated.
*/
self.addEventListener("activate", async e => {
log(`Activate`);
self.clients.claim().then();
e.waitUntil(deleteOldCaches());
});
/**
* Handle fetch requests with a cache then network and cache strategy.
*/
self.addEventListener("fetch", e => {
log(`Fetch`);
// Let the browser do its default thing for non-GET requests.
if (e.request.method !== 'GET') return;
e.respondWith((async () => await cacheOrNetworkAndCache(e))());
});
/**
* Make it possible to skip the waiting.
*/
self.addEventListener("message", async e => {
log(`Message`);
const {action} = e.data;
switch (action) {
case "skipWaiting":
self.skipWaiting();
break;
default:
break;
}
});
/**
* Show debug message.
*/
function log () {
if (DEBUG) {
console.log(`SW (${CACHE_NAME})`, ...arguments);
}
}
/**
* Precache the resources.
* @returns {Promise<void>}
*/
async function precache () {
const cache = await caches.open(CACHE_NAME);
// https://jakearchibald.com/2016/caching-best-practices/
await cache.addAll(URLS_TO_CACHE.map(url => new Request(url, {cache: "no-cache"})));
}
/**
* Deletes old caches.
* @returns {Promise<void>}
*/
async function deleteOldCaches () {
const keys = await caches.keys();
// Delete old caches
await Promise.all(keys.map(key => {
if (key !== CACHE_NAME) {
return caches.delete(key);
}
}));
}
/**
* Determines whether the URL is blacklisted.
* @param url
* @returns {string}
*/
function isBlacklistedUrl (url) {
return BLACK_LISTED_REQUEST_PATHS.find(path => url.includes(path));
}
/**
* Fetches a request using a cache first strategy.
* @param e
* @returns {Promise<Response>}
*/
async function cacheOrNetworkAndCache (e) {
const {request} = e;
// DevTools opening will trigger these o-i-c requests, which we ignore.
// https://stackoverflow.com/a/49719964
if (request.cache === "only-if-cached" && request.mode !== "same-origin") {
return new Response();
}
// Try the cache
const cachedResponse = await caches.match(request);
log("Cache", cachedResponse);
if (cachedResponse != null) return cachedResponse;
// Fallback to network
const response = await fetch(request);
log("Response", response);
// Cache response if necessary
if (response != null && response.ok && response.type === "basic" && !isBlacklistedUrl(response.url)) {
const cache = await caches.open(CACHE_NAME);
cache.put(request, response.clone()).then();
}
return response;
}