Pages Caching

Does cloudflare pages respect Cache-Control headers? If so, how can I validate that? When i use Cache API directly i get a Cache Status HIT response header, however id like to avoid this if possible and would ideally use Cache Rules instead. Ive set it to respect the Cache-Control header and they are set correctly, however cannot see the Cache Status HIT header anymore. Is there another way to validate this working? or does it require the use of the Cache API directly?
14 Replies
Hello, I’m Allie!
By default, Pages uses a custom cache system that doesn’t return Cache Status headers
GATES
GATESOP9mo ago
Ok, and will it respect the cache headers my UI sets in Cache-Control? i noticed it says it comes out the box with caching defaults but id like to customise these in some cases
Hello, I’m Allie!
No. Pages will cache assets indefinitely, though that is fine because each deployment uses a separate cache/assets are cached on their hash Though Browsers connecting to your Pages Project should respect the Cache-Control headers
GATES
GATESOP9mo ago
Im happy for the assets to automatically be cached indefinitely, however im thinking mainly about the initial page request and wondering how that could be cached with this
Hello, I’m Allie!
That would be cached too? If that is what you mean
GATES
GATESOP9mo ago
Like this first request for example. So the assets are fine to indefiniely cache, however depending on page we require to cache differently. Some have authorisation/user logins, some are highly static so can be cached aggressively, and some are user/device specific so we would require to control each page independently, preferably through the Cache-Control headers, so wondering if cloudflare would automatically listen to those for that initial page request, if that makes sense?
Hello, I’m Allie!
Oh, you mean SSR Pages? Those aren’t cached by default Slap the Cache API into whatever you use for SSR, and then cache as you want it to
GATES
GATESOP9mo ago
Ah yes sorry that’s right 👍 ah ok so we are required to use Cache api in that case? Thanks for the help
Hello, I’m Allie!
Yeah. The great part with that is, if you don't want something to be cached, you don't have to futz with the headers, you just don't call the Cache API at all
GATES
GATESOP9mo ago
Ok cool I’ll go for that then. Cheers 👌
pepe-le-moko
pepe-le-moko2mo ago
@Hello, I’m Allie! I don't want to open another issue because I've asked myself the same question, so I'll piggyback on this one. First of all, thanks for clarifying that SSR pages aren't cached, I'm assuming SSR pages are what runs behind functions/? If so, would the way to deal with it is: 1. my route would return a Cache-Control: public, s-maxage=300 header 2. then have a function inside my functions/_middleware.js to deal with Cache API (like this https://gist.github.com/boertel/ec56319f5f4dbec28b750cbda3fe0027) then request will be cache for 5 minutes, and is that correct that the CF-Cache-Status header is not set when calling the Cache API?
Hello, I’m Allie!
Sure, and yes, though note that this would still incur a request. Also, for next time, please do open a new thread, and reference the old one
pepe-le-moko
pepe-le-moko2mo ago
duly noted! is there a way to cache the response without incurring a request?
Hello, I’m Allie!
Other than creating an entirely new deployment? No

Did you find this page helpful?