I have some pages functions that generate html response, which is a fairly expensive operation. I therefore want to cache the html response, and I’ve set up a middleware for the function that uses the cache api:
export const onRequest: PagesFunction = async ({
request,
next,
waitUntil,
}) => {
if (request.method !== 'GET') {
return await next(request);
}
const cacheUrl = new URL(request.url);
const cacheKey = new Request(cacheUrl.toString(), request);
const cache = caches.default;
let response = await cache.match(cacheKey);
if (!response) {
console.log(
`Response for request url: ${request.url} not present in cache. Fetching and caching request.`
);
response = await next(request);
response = new Response(response.body, response);
waitUntil(cache.put(cacheKey, response.clone()));
} else {
console.log(`Cache hit for: ${request.url}.`);
}
return response;
};
What I would like is for this midleware to cache the html response, but not have anything else between Cloudflare and the browser cache the result. If I try to set Cache-Control: public, max-age=3600
then the response will be cached in the users browser, which I don’t want. If I use Cache-Control: s-maxage=3600
it will not be cached in the users browser but it will potentially be stored in some other cache between Cloudflare and the user. I only want it to be stored in Cloudflare so that I can purge the cache if needed. I have tried to set Cloudflare-CDN-Cache-Control: public, max-age=3600
but it does not seem to be used.
Is there any way to get this to work?