-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
66 lines (54 loc) · 2.25 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
// Name of the cache
const CACHE_NAME = "cache";
// Caching duration is 30 seconds for demo purpose
const DEFAULT_CACHING_DURATION = 30;
self.addEventListener('fetch', (event) => {
const { request } = event;
const CACHING_DURATION = Number(request.headers.get("sw-cache-for-seconds")) || DEFAULT_CACHING_DURATION
event.respondWith(caches.open(`${CACHE_NAME}-tiles`).then(
cache => cache.match(request).then(
(response) => {
// If there is a match from the cache
if (response) {
const expirationDate = Date.parse(response.headers.get('sw-cache-expires'));
const now = new Date();
if (expirationDate > now) {
return response;
}
}
// Otherwise, let's fetch it from the network
return fetch(request.url).then((liveResponse) => {
// let's only store in cache if the content-type is
// JSON or something non-binary
const ct = liveResponse.headers.get('Content-Type');
if (ct && (ct.match(/application\/json/i))) {
// Compute expires date from caching duration
const expires = new Date();
expires.setSeconds(expires.getSeconds() + CACHING_DURATION);
// Recreate a Response object from scratch to put
// it in the cache, with the extra header for
// managing cache expiration.
const cachedResponseFields = {
status: liveResponse.status,
statusText: liveResponse.statusText,
headers: { 'SW-Cache-Expires': expires },
};
liveResponse.headers.forEach((v, k) => {
cachedResponseFields.headers[k] = v;
});
// We will consume body of the live response, so
// clone it before to be able to return it
// afterwards.
const returnedResponse = liveResponse.clone();
return liveResponse.blob().then((body) => {
// Put the duplicated Response in the cache
cache.put(request, new Response(body, cachedResponseFields));
// Return the live response from the network
return returnedResponse;
});
}
return liveResponse;
});
})
));
});