-
Notifications
You must be signed in to change notification settings - Fork 4
/
serviceWorker.js
51 lines (46 loc) · 1.5 KB
/
serviceWorker.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
importScripts('https://storage.googleapis.com/workbox-cdn/releases/5.1.2/workbox-sw.js');
const appVersion = "v24.12.0";
workbox.setConfig({debug: false});
const { registerRoute } = workbox.routing;
const { CacheFirst, NetworkFirst } = workbox.strategies;
const { ExpirationPlugin } = workbox.expiration;
const googleAnalytics = workbox.googleAnalytics;
const cacheNameMapper = {
"static-cache": `static-cache-${appVersion}`,
"images-cache": `images-cache-${appVersion}`,
};
const currCacheNameArray = Object.values(cacheNameMapper);
googleAnalytics.initialize();
registerRoute(/\.(?:js|css|html)$/, new NetworkFirst({ cacheName: cacheNameMapper["static-cache"] }));
registerRoute(/\.(?:png|jpg|jpeg|svg|gif|ico)$/,
new CacheFirst({
cacheName: cacheNameMapper["images-cache"],
plugins: [
new ExpirationPlugin({
maxEntries: 30,
maxAgeSeconds: 7 * 24 * 60 * 60,
})
]
})
);
self.addEventListener("install", () => {
self.skipWaiting();
});
self.addEventListener("activate", (event) => {
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames.map((cacheName) => {
if (!currCacheNameArray.includes(cacheName)) {
return caches.delete(cacheName);
}
})
);
})
);
});
self.addEventListener("message", (event) => {
if (event.data.action === "getAppVersion") {
event.source.postMessage({ action: "sendAppVersion", payload: appVersion });
}
});