( 5 ) :
: Cloudflare (, , - ).
: "I just want to add.json to the list of static extensions". , , JSON - - . , , - 60 5 , , , , .
, .json .json:
addEventListener('fetch', event => {
event.respondWith(handleRequest(event));
});
async function handleRequest(event)
{
let request = event.request;
let ttl = undefined;
let cache = caches.default;
let url = new URL(event.request.url);
let shouldCache = false;
if (url.pathname.endsWith('.json'))
{
shouldCache = true;
ttl = 60;
}
let response = await cache.match(request);
if (!response)
{
response = await fetch(request);
if (shouldCache)
{
response = new Response(response.body, response);
if (ttl)
{
response.headers.append('Cache-Control', 'max-age=' + ttl);
}
event.waitUntil(cache.put(request, response.clone()));
}
return response;
}
else {
return response;
}
}
MIME- , , , , API.
, - . products-1.json/products-2.json max-age .