Set Edge cache control header

When caching a request in a CF Worker, I need to instruct the edge server to cache the response from origin for X seconds. Is using the ‘Edge-Cache-Control’ header appropriate for this? Keep in mind, it must not instruct the browser to cache the response.

Hi,

When caching a request in a Cloudflare Worker, you can control how the edge server caches the response from the origin by setting the cache time-to-live (TTL) using the cf object. To ensure that the browser does not cache the response, you should not use the Edge-Cache-Control header. Instead, you can set the cache TTL using the cf object in your Cloudflare Worker script. By specifying the cacheTtl value in seconds, you can instruct Cloudflare on how long to cache the response at the edge server without affecting browser caching. You can get more info regarding this here how the cache works

This approach allows you to manage caching behavior at the edge server level while keeping browser caching separate.

I hope this helps. Please let us know if you need further assistance.

Hi @g.andghuladze, your topic has a solution here.

Let us know what you think of the solution by logging in and give it a :+1: or :-1:.


Solutions help the person that asked the question and anyone else that sees the answer later. Login to tell us what you think of the solution with a :+1: or :-1:.

The only thing that document mentions about the cf object is this sentence " * Setting Cloudflare cache rules (that is, operating on the cf object of a request". And that is vague. Can you show me an example of how would I instruct CF worker to cache the response from origin for X seconds?

I hope these can help you:
https://developers.cloudflare.com/workers/examples/cache-using-fetch/

https://developers.cloudflare.com/workers/runtime-apis/request/#the-cf-property-requestinitcfproperties

Sorry, I should have been more specific. I am manually caching origin responses in a CF worker like so:

const url = new URL(request.url);
const cacheKey = new Request(url.toString(), request);

const originResponse = await fetch(request);
const cacheHeader = originResponse.headers.get('Some-Header-that-instructs-this-CF-worker-to-cache-this-response');

if (cacheFastHeader) {
    await cache.put(cacheKey, originResponse.clone());
}

My question is, how do I force the cache.put() to store the originResponse in the cache for X seconds based on the values provided by headers set at the origin and not by the request.

typo - cacheFastHeader => cacheHeader

The idea is to control the cache time at the origin server, not at the worker level.

For this purpose I thought of using:

Edge-Cache-Control: max-age=<seconds> : Specifies the maximum amount of time, in seconds, that the response should be cached at Cloudflare’s edge servers.

Why would this header not work?

That’s the default behaviour.

However, you can’t force Cloudflare to actually cache it for the full duration. It is still subject to the normal Edge Cache eviction process to my knowledge.

Where did you find that header? I don’t see it mentioned in the docs:

What I am trying to do is make sure the browser does not cache the response that was sent to it, but rather it is only cached for X seconds at the CF worker cache level. Which header can I use for that purpose?

That’s really explained in the link from my previous post.

1 Like

ChatGPT, probably that is the old name of the header, which evolved to Cloudflare-CDN-Cache-Control as I can see from the link you provided.

Yes, that was the documentation I needed. Thank you for your help.

2 Likes

Hi @g.andghuladze, your topic has a solution here.

Let us know what you think of the solution by logging in and give it a :+1: or :-1:.


Solutions help the person that asked the question and anyone else that sees the answer later. Login to tell us what you think of the solution with a :+1: or :-1:.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.