-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
cache.ts
55 lines (43 loc) · 1.58 KB
/
cache.ts
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
import type { FetchHandler } from 'worktop';
import type { ResponseHandler } from 'worktop/cache';
export const Cache: Cache = /*#__PURE__*/ (caches as any).default;
export async function lookup(event: FetchEvent, request?: Request | string) {
let req = request || event.request;
let isHEAD = typeof req !== 'string' && req.method === 'HEAD';
if (isHEAD) req = new Request(req, { method: 'GET' });
let res = await Cache.match(req);
if (isHEAD && res) res = new Response(null, res);
return res;
}
export function save(event: FetchEvent, res: Response, request?: Request | string) {
const req = request || event.request;
const isGET = typeof req === 'string' || req.method === 'GET';
if (isGET && isCacheable(res)) {
if (res.headers.has('Set-Cookie')) {
res = new Response(res.body, res);
res.headers.append('Cache-Control', 'private=Set-Cookie');
}
event.waitUntil(Cache.put(req, res.clone()));
}
return res;
}
export function isCacheable(res: Response): boolean {
if (res.status === 206) return false;
const vary = res.headers.get('Vary') || '';
if (!!~vary.indexOf('*')) return false;
const ccontrol = res.headers.get('Cache-Control') || '';
if (/(private|no-cache|no-store)/i.test(ccontrol)) return false;
return true;
}
export function reply(handler: ResponseHandler): FetchHandler {
return (event, request) => event.respondWith(
lookup(event, request).then(prev => {
return prev || handler(event).then(res => {
return save(event, res, request);
});
})
);
}
export function listen(handler: ResponseHandler): void {
addEventListener('fetch', reply(handler));
}