-
Notifications
You must be signed in to change notification settings - Fork 3
/
serviceWorker.js
120 lines (108 loc) · 3.16 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
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
importScripts('https://storage.googleapis.com/workbox-cdn/releases/5.1.2/workbox-sw.js');
const appVersion = "v24.10.0";
workbox.setConfig({debug: false});
const { registerRoute } = workbox.routing;
const { CacheFirst, NetworkFirst, StaleWhileRevalidate, NetworkOnly } = workbox.strategies;
const { CacheableResponse, CacheableResponsePlugin } = workbox.cacheableResponse;
const { ExpirationPlugin } = workbox.expiration;
const { BackgroundSyncPlugin } = workbox.backgroundSync
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|pdf)$/, 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,
})
]
})
);
registerRoute(
new RegExp('https://us-central1-nih-nci-dceg-episphere-dev.cloudfunctions.net/.+'),
new NetworkFirst({
cacheName: 'api-cache',
plugins: [
new CacheableResponsePlugin({
statuses: [200],
})
]
}),
'GET'
);
registerRoute(
new RegExp('https://api-myconnect-stage.cancer.gov/.+'),
new NetworkFirst({
cacheName: 'api-cache',
plugins: [
new CacheableResponsePlugin({
statuses: [200],
})
]
}),
'GET'
);
registerRoute(
new RegExp('https://api-myconnect.cancer.gov/.+'),
new NetworkFirst({
cacheName: 'api-cache',
plugins: [
new CacheableResponsePlugin({
statuses: [200],
})
]
}),
'GET'
);
const bgSyncPlugin = new BackgroundSyncPlugin('ConnectAppBgSync', {
maxRetentionTime: 7 * 24 * 60 // Retry for max of 24 Hours (specified in minutes)
});
registerRoute(
new RegExp('https://us-central1-nih-nci-dceg-episphere-dev.cloudfunctions.net/.+'),
new NetworkOnly({
plugins: [bgSyncPlugin]
}),
'POST'
);
registerRoute(
new RegExp('https://api-myconnect-stage.cancer.gov/.+'),
new NetworkOnly({
plugins: [bgSyncPlugin]
}),
'POST'
);
registerRoute(
new RegExp('https://api-myconnect.cancer.gov/.+'),
new NetworkOnly({
plugins: [bgSyncPlugin]
}),
'POST'
);
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 });
}
});