generated from adobe/aem-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlocaldata.js
100 lines (92 loc) · 3.2 KB
/
localdata.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
/* eslint-disable no-restricted-globals */
// eslint-disable-next-line no-console
console.log('running service worker');
function stringifySort(sortKey, value) {
if (value instanceof Object && !(value instanceof Array)) {
return Object.keys(value)
.sort()
.reduce((sorted, key) => {
sorted[key] = value[key];
return sorted;
}, {});
}
return (value instanceof Array) ? value.sort() : value;
}
async function handleResponse(event) {
const { request } = event;
let cacheRequest = request;
if (request.method.toUpperCase() === 'POST') {
// get the body text...
let body = await request.clone().text();
// create a new URL for the purposes of a cache key...
const cacheUrl = new URL(request.url);
// create an augmented URL by appending the body to the original pathname...
body = JSON.stringify(body, stringifySort);
cacheUrl.pathname += body;
// convert the request to a GET to be able to cache it...
cacheRequest = new Request(cacheUrl.toString(), {
/* headers: request.headers, */
method: 'GET',
});
}
// get cache...
const cache = await caches.open('algolia-search');
// check if there is a cached response in the cache based on the cloned
// GET request (for the cache key) NOT the original POST request...
let response = await cache.match(cacheRequest);
// if not, fetch the response using the original POST request...
if (!response) {
response = await fetch(request);
// put the response into the cache using the cloned GET request
// (for the cache key) NOT the original POST request...
event.waitUntil((await cache).put(cacheRequest, response.clone()));
incrementCounterInDB(request, false);
} else {
incrementCounterInDB(request, true);
}
return response;
}
function updateCounterObject(counterObject, isHit) {
if (isHit) {
if (counterObject.hits >= 1) {
counterObject.hits = parseInt(counterObject.hits, 10);
counterObject.hits += 1;
} else {
counterObject.hits = 1;
}
} else if (counterObject.miss >= 1) {
counterObject.miss = parseInt(counterObject.miss, 10);
counterObject.miss += 1;
} else {
counterObject.miss = 1;
}
}
function incrementCounterInDB(request, isHit) {
const idb = indexedDB.open('algoliaCacheUsageStats', 1);
idb.onupgradeneeded = (event) => {
event.target.result.createObjectStore('hitMissStore');
};
// Open a connection to the IndexedDB
idb.onsuccess = (event) => {
const db = event.target.result;
const transaction = db.transaction('hitMissStore', 'readwrite');
const store = transaction.objectStore('hitMissStore');
// Get the current value of the counter for the URL
store.get(request.url).onsuccess = (e) => {
const hitMiss = e.target.result || {};
// check if counter is a number
updateCounterObject(hitMiss, isHit);
// Update the counter in the IndexedDB
store.put(hitMiss, request.url);
};
};
}
self.addEventListener('fetch', (e) => {
if (e.request.url.includes('/adobe/assets/search')) {
e.respondWith((async () => handleResponse(e))());
}
});
self.addEventListener('activate', (ev) => {
ev.waitUntil(self.clients.claim());
});
self.skipWaiting();