-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudflare.js
32 lines (26 loc) · 857 Bytes
/
cloudflare.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
addEventListener('fetch', evento => {
evento.respondWith(handleRequest(evento.request));
});
async function handleRequest(request) {
let url = new URL(request.url);
url.searchParams.set('width', '800');
url.searchParams.set('height', '600');
let modifiedRequest = new Request(url.toString(), {
method: request.method,
headers: request.headers
});
let cache = caches.default;
let response = await cache.match(modifiedRequest);
if (!response) {
response = await fetch(modifiedRequest);
let cacheHeaders = new Headers(response.headers);
cacheHeaders.set('Cache-Control', 'public, max-age=14400');
response = new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: cacheHeaders
});
evento.waitUntil(cache.put(modifiedRequest, response.clone()));
}
return response;
}