forked from rtCamp/wp-decoupled
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service-worker.js
98 lines (81 loc) · 2.7 KB
/
service-worker.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
/**
* Service worker scripts.
*/
importScripts('https://cdn.jsdelivr.net/npm/idb-keyval@3/dist/idb-keyval-iife.min.js');
const store = new idbKeyval.Store('GraphQL-Cache', 'PostResponses');
workbox.core.skipWaiting();
workbox.core.clientsClaim();
self.addEventListener('fetch', function (event) {
if (event.request.method === 'POST') {
event.waitUntil(event.respondWith(staleWhileRevalidate(event)));
}
});
workbox.routing.registerRoute(
new RegExp('https://tekskools.com'),
new workbox.strategies.StaleWhileRevalidate()
);
// @todo Temporary, needs to work on routes.
workbox.routing.registerRoute(
new RegExp(/\/product.+/),
new workbox.strategies.StaleWhileRevalidate()
);
const preCacheFiles = self.__precacheManifest || [];
preCacheFiles.push({
url: '/'
});
workbox.precaching.precacheAndRoute(preCacheFiles);
async function staleWhileRevalidate(event) {
let cachedResponse = await getCache(event.request.clone());
let fetchPromise = fetch(event.request.clone())
.then((response) => {
setCache(event.request.clone(), response.clone());
return response;
})
.catch((err) => {
// Handle error
});
return cachedResponse ? Promise.resolve(cachedResponse) : fetchPromise;
}
async function serializeResponse(response) {
let serializedHeaders = {};
for (var entry of response.headers.entries()) {
serializedHeaders[entry[0]] = entry[1];
}
let serialized = {
headers: serializedHeaders,
status: response.status,
statusText: response.statusText
};
serialized.body = await response.json();
return serialized;
}
async function setCache(request, response) {
let body = await request.json();
let id = body.query.toString() + JSON.stringify(body.variables);
var entry = {
query: body.query,
response: await serializeResponse(response),
timestamp: Date.now()
};
idbKeyval.set(id, entry, store);
}
async function getCache(request) {
let data;
try {
let body = await request.json();
let id = body.query.toString() + JSON.stringify(body.variables);
data = await idbKeyval.get(id, store);
if (!data) return null;
// Check cache max age.
let cacheControl = request.headers.get('Cache-Control');
let maxAge = cacheControl ? parseInt(cacheControl.split('=')[1]) : 3600;
if (Date.now() - data.timestamp > maxAge * 1000) {
// Cache expired. Load from API endpoint
return null;
}
// Load response from cache.
return new Response(JSON.stringify(data.response.body), data.response);
} catch (err) {
return null;
}
}