Conditional GET with Transform Rules

I have a server providing responses with a Last-Modified header, but no Cache-Control. To avoid unnecessary traffic while still having fresh responses, I would like Cloudflare to perform a conditional GET request before providing cached responses.

I am using a Page Rule that enables “Cache Everything” and “Origin Cache Control”, but I don’t think that’s the issue.

I’ve tried using a Transform Rule to add the Cache-Control header[1] with a value of no-cache: my understanding of RFC 7234 says that’s sufficient, and the Cache Control document seems to concur assuming the Page Rule works.

I’ve also tried using public, no-cache, must-revalidate, which the document seems to say will work even if Origin Cache Control is off.

Neither option works as expected. Instead of revalidating cached responses then using them, Cloudflare proceeds to serve a single cached response without any revalidation whatsoever[2]. I have tried sending conditional GET requests manually to both with and without traffic proxied through Cloudflare, and the server is returning HTTP 304 responses correctly. So clearly I’m misunderstanding something about Cloudflare here.

If anyone has any advice about this issue, I’d appreciate it. I can provide more specifics if needed.

  1. It is set to “static”, mainly because I’m not clear on what “dynamic” changes based on. ↩︎

  2. CF-Cache-Status shows HIT, when I’d expect it to be REVALIDATED or EXPIRED. ↩︎

Transform Rules can modify the Request headers from the User-Agent, or Response headers to the User-Agent. In order for Cloudflare to read the Cache-Control header it needs to be present on the Response headers from your Origin, which you cannot do in a Transform Rule.

This tells Cloudflare to use the Cloudflare cache even if the file extension is not on the standard list of cacheable file extensions. (So .html is not usually cached, but Cache Everything will make them cacheable).

This tells Cloudflare to honour the Cache-Control directives from your Origin for all cacheable objects (which the Everything rule means every Origin response).

I’m not sure what behaviour is expected when setting Origin Cache Control but then not controlling the Cache with the appropriate header. I suspect the behaviour you want is achieved by setting a Cache Control max-age=0 header on your Origin.

1 Like

That explains everything, thank you!

As I said in the original post, what I want should be described by no-cache alone. I don’t actually control this server, though: if I did, I wouldn’t have tried using Transform Rules in the first place.

Without a Cache-Control directive of any sort, caches have considerable discretion. A common default is assuming the response is fresh for an arbitrary period (a day, for instance) since the Last-Modified time: I suspect that is what is being followed here.

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