-
Notifications
You must be signed in to change notification settings - Fork 15
/
sw.js
173 lines (145 loc) · 3.81 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
'use strict';
const version = 'v20160443';
const __DEVELOPMENT__ = false;
const __DEBUG__ = false;
const offlineResources = [
'/',
'/offline.html',
'/offline.svg'
];
const ignoreFetch = [
/https?:\/\/cdnjs.loli.net\//,
/https?:\/\/fonts.loli.net\//,
/https?:\/\/wx2.sinaimg.cn\//,
];
//////////
// Install
//////////
function onInstall(event) {
log('install event in progress.');
event.waitUntil(updateStaticCache());
}
function updateStaticCache() {
return caches
.open(cacheKey('offline'))
.then((cache) => {
return cache.addAll(offlineResources);
})
.then(() => {
log('installation complete!');
});
}
////////
// Fetch
////////
function onFetch(event) {
const request = event.request;
if (shouldAlwaysFetch(request)) {
event.respondWith(networkedOrOffline(request));
return;
}
if (shouldFetchAndCache(request)) {
event.respondWith(networkedOrCached(request));
return;
}
event.respondWith(cachedOrNetworked(request));
}
function networkedOrCached(request) {
return networkedAndCache(request)
.catch(() => { return cachedOrOffline(request) });
}
// Stash response in cache as side-effect of network request
function networkedAndCache(request) {
return fetch(request)
.then((response) => {
var copy = response.clone();
caches.open(cacheKey('resources'))
.then((cache) => {
cache.put(request, copy);
});
log("(network: cache write)", request.method, request.url);
return response;
});
}
function cachedOrNetworked(request) {
return caches.match(request)
.then((response) => {
log(response ? '(cached)' : '(network: cache miss)', request.method, request.url);
return response ||
networkedAndCache(request)
.catch(() => { return offlineResponse(request) });
});
}
function networkedOrOffline(request) {
return fetch(request)
.then((response) => {
log('(network)', request.method, request.url);
return response;
})
.catch(() => {
return offlineResponse(request);
});
}
function cachedOrOffline(request) {
return caches
.match(request)
.then((response) => {
return response || offlineResponse(request);
});
}
function offlineResponse(request) {
log('(offline)', request.method, request.url);
if (request.url.match(/\.(jpg|png|gif|svg|jpeg)(\?.*)?$/)) {
return caches.match('/offline.svg');
} else {
return caches.match('/offline.html');
}
}
///////////
// Activate
///////////
function onActivate(event) {
log('activate event in progress.');
event.waitUntil(removeOldCache());
}
function removeOldCache() {
return caches
.keys()
.then((keys) => {
return Promise.all( // We return a promise that settles when all outdated caches are deleted.
keys
.filter((key) => {
return !key.startsWith(version); // Filter by keys that don't start with the latest version prefix.
})
.map((key) => {
return caches.delete(key); // Return a promise that's fulfilled when each outdated cache is deleted.
})
);
})
.then(() => {
log('removeOldCache completed.');
});
}
function cacheKey() {
return [version, ...arguments].join(':');
}
function log() {
if (developmentMode()) {
console.log("SW:", ...arguments);
}
}
function shouldAlwaysFetch(request) {
return __DEVELOPMENT__ ||
request.method !== 'GET' ||
ignoreFetch.some(regex => request.url.match(regex));
}
function shouldFetchAndCache(request) {
return ~request.headers.get('Accept').indexOf('text/html');
}
function developmentMode() {
return __DEVELOPMENT__ || __DEBUG__;
}
log("Hello from ServiceWorker land!", version);
self.addEventListener('install', onInstall);
self.addEventListener('fetch', onFetch);
self.addEventListener("activate", onActivate);