-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
93 lines (79 loc) · 2.55 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
const VERSION = 'v1';
const CACHENAME = 'my-test-app';
self.addEventListener('install', event => event.waitUntil(installServiceWorker()));
self.addEventListener('activate', event => event.waitUntil(activateSW()));
self.addEventListener('fetch', event => event.respondWith(cacheThenNetwork(event)));
// importScripts('./assets/js/indexdb.js');
async function installServiceWorker() {
const cache = await caches.open(CACHENAME);
return cache.addAll([
'/index.html',
'/css/bootstrap.min.css',
'/js/bootstrap.min.js',
'/js/jquery.min.js',
'/js/main.js'
]);
}
async function activateSW() {
const cacheKeys = await caches.keys();
cacheKeys.forEach(cacheKey => {
if (cacheKey !== CACHENAME ) {
caches.delete(cacheKey);
}
});
}
async function cacheThenNetwork(event) {
const normalizedUrl = new URL(event.request.url);
normalizedUrl.search = '';
const cachedResponse = await getCache(normalizedUrl);
if (cachedResponse) {
fetchLiveResponse(event, normalizedUrl);
return cachedResponse;
}
const networkResponse = await fetchLiveResponse(event, normalizedUrl);
return networkResponse;
}
async function fetchLiveResponse(event, cacheKey) {
try {
const interceptUrl = await pixelInterCeptor(event.request.url);
const networkResponse = await fetch(interceptUrl);
if (interceptUrl.indexOf('pixel.gif') > -1) {
await setCache(event, cacheKey, networkResponse.clone());
}
return networkResponse;
} catch (error) {
// Offline event tracking
// await savePixel(event.request.url);
return {};
}
}
async function pixelInterCeptor(url) {
try {
const pixelServerObj = {
event: "interaction",
customer: "client",
operating_system_name: "os_name",
utm_source: "x1",
utm_medium: "x2",
utm_campaign: "x3",
campaign_url: "landing_url"
};
for (const key of Object.keys(pixelServerObj)) {
url = url.replace(pixelServerObj[key], key);
}
return url;
} catch (error) {
return url;
}
}
async function getCache(cacheName) {
const cache = await caches.open(CACHENAME);
const cachedResponse = await cache.match(cacheName);
return cachedResponse;
}
async function setCache(event, cacheName, data) {
event.waitUntil(async function() {
const cache = await caches.open(CACHENAME);
await cache.put(cacheName, data);
}());
}