-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappService.js
175 lines (158 loc) · 4.12 KB
/
appService.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
const { assets, buildId, staticFiles } = serviceWorkerOption;
const CACHE_PREFIX = "mynameisviii.com";
const cacheNames = {
api: `${CACHE_PREFIX}-api`,
cdn: `${CACHE_PREFIX}-cdn`,
googleApis: `${CACHE_PREFIX}-googleApis`,
local: `${CACHE_PREFIX}-local-${buildId}`,
precache: `${CACHE_PREFIX}-precache-${buildId}`,
};
const precacheUrls = [].concat(assets, staticFiles);
const isCacheValid = (key) => {
for (const name in cacheNames) {
if (cacheNames[name] === key) {
return true;
}
}
return false;
};
importScripts(
"https://storage.googleapis.com/workbox-cdn/releases/3.4.1/workbox-sw.js"
);
workbox.core.setCacheNameDetails({
prefix: CACHE_PREFIX,
suffix: buildId,
});
workbox.skipWaiting();
workbox.clientsClaim();
if (buildId !== "development") {
workbox.precaching.precacheAndRoute(
precacheUrls.map((item) => ({
revision: buildId,
url: item,
})),
{
cleanUrls: false,
}
);
// Local cache-first requests
workbox.routing.registerRoute(
(request) =>
request.url.host === self.location.host &&
!precacheUrls.includes(request.url.pathname),
workbox.strategies.cacheFirst({
cacheName: cacheNames.local,
plugins: [
new workbox.cacheableResponse.Plugin({
statuses: [0, 200],
}),
new workbox.expiration.Plugin({
maxAgeSeconds: 2 * 60 * 60,
}),
],
})
);
}
workbox.googleAnalytics.initialize({
cacheName: cacheNames.googleApis,
});
// Static content requests from Google APIs
workbox.routing.registerRoute(
new RegExp("https://(?:fonts).(?:googleapis|gstatic).com/(.*)"),
workbox.strategies.cacheFirst({
cacheName: cacheNames.googleApis,
plugins: [
new workbox.cacheableResponse.Plugin({
statuses: [0, 200],
}),
],
})
);
// Dynamic content requests from Google APIs
workbox.routing.registerRoute(
new RegExp("https://(?:maps).(?:googleapis|gstatic).com/(.*)"),
workbox.strategies.networkOnly({
cacheName: cacheNames.googleApis,
plugins: [
new workbox.cacheableResponse.Plugin({
statuses: [0, 200],
}),
],
})
);
// App CDN requests
workbox.routing.registerRoute(
new RegExp("https://s3.amazonaws.com/mynameisviii-static/(.*)"),
workbox.strategies.cacheFirst({
cacheName: cacheNames.cdn,
plugins: [
new workbox.cacheableResponse.Plugin({
statuses: [0, 200],
}),
],
})
);
// App API requests
// Listings - cache for 2 hours
workbox.routing.registerRoute(
new RegExp("https://api.mynameisviii.com/(.*)/find(.*)"),
workbox.strategies.cacheFirst({
cacheName: cacheNames.api,
plugins: [
new workbox.cacheableResponse.Plugin({
statuses: [0, 200],
}),
new workbox.expiration.Plugin({
maxAgeSeconds: 2 * 60 * 60,
}),
],
})
);
// Entities - cache for a week
workbox.routing.registerRoute(
new RegExp("https://api.mynameisviii.com/(.*)/[^find](.*)"),
workbox.strategies.cacheFirst({
cacheName: cacheNames.api,
plugins: [
new workbox.cacheableResponse.Plugin({
statuses: [0, 200],
}),
new workbox.expiration.Plugin({
maxAgeSeconds: 7 * 24 * 60 * 60,
}),
],
})
);
self.addEventListener("activate", (event) => {
// Cleanup redundant caches
event.waitUntil(
caches.keys().then((keys) => {
let hasDeletedCaches = false;
keys.forEach((key) => {
if (!isCacheValid(key)) {
caches.delete(key);
indexedDB.deleteDatabase(key);
hasDeletedCaches = true;
}
});
// Notify client that a new version is available, when an old version exists
if (hasDeletedCaches) {
self.clients.matchAll().then((clients) => {
clients.forEach((client) => {
client.postMessage({
type: "serviceWorker.activate",
});
});
});
}
})
);
});
// Handle postMessage requests from the application
self.onmessage = (message) => {
if (message.data.type === "changeRoute") {
return caches
.open(cacheNames.local)
.then((cache) => cache.add(message.data.payload));
}
};