Armin Nikdel
Armin Nikdel

Reputation: 335

How to let cloudlfare to cache the content but make browser do not cache? (so browser always fetch from CF on every refresh)

Following code tell browser and CloudFlare to not cache the page. I need CloudFlare to cache the content. But browser not to cache the content.

header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s",time()+1) . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

Use case that I want to achieve is to allow me to purge cache and have results reflect on visitors browsers immediately. (I am purging cache dynamically via API)

Upvotes: 1

Views: 308

Answers (2)

coder_uk
coder_uk

Reputation: 787

I found this answer when looking up the same question.

Handily there is new way to do this which seems even better than s-maxage: you can send separate CDN-Cache-Control and/or Cloudflare-CDN-Cache-Control headers.

The advantage of using these new CDN-specific headers is it allows Cloudflare can be configured separately to any other proxy cache that may along the way (separate to a browser). For example if you set Cloudflare-CDN-Cache-Control Cloudflare respects that and then removes that header from the response. So then your cache-control header would be targeted at the browser (set as no-cache).

https://developers.cloudflare.com/cache/about/cdn-cache-control

Upvotes: 2

Armin Nikdel
Armin Nikdel

Reputation: 335

I found answer to my question from following source: https://support.cloudflare.com/hc/en-us/articles/202775670-How-Do-I-Tell-Cloudflare-What-to-Cache-

It mentions:

if a customer would like to specify a cache timeout in the CDN which is different from the browser we can also use s-maxage:

Cache-Control: s-maxage=200, max-age=60

This will tell Cloudflare to cache the object for 200 seconds and the browser to cache the object for 60 seconds. Basically, s-maxage is intended to be followed ONLY by reverse proxies (so the browser should ignore it) whilst on the other hand, Cloudflare gives priority to s-maxage if present. Note that we will respect whichever value is higher: the browser cache setting in Cloudflare or the max-age header.

Upvotes: 1

Related Questions