ostark / upper

Integrates Edge Caches like Fastly, KeyCDN, Cloudflare and Varnish with Craft.
MIT License
102 stars 22 forks source link

Cloudflare won’t cache without Edge Cache TTL #24

Closed joshuabaker closed 3 years ago

joshuabaker commented 5 years ago

I’m trying to get Upper (1.4.2) working with Cloudflare.

Despite seeing cache-control: public, s-maxage=604800 and x-upper-cache headers I’m also getting cf-cache-status: MISS.

If I add ‘Edge Cache TTL’ in the Page Rule, alongside ‘Cache Level: Cache Everything’, pages start to cache on the CDN, however, it also means that cache-control: private, no-cache is ignored, which messes up the control panel and other dynamic pages.

What might I be missing here?

joshuabaker commented 5 years ago

Reading Cloudflare’s article on this rule, there’s actually mention of the Expires header.

Cache-Control headers sending "private","no-cache", or "max-age=0" or Expires header with a date in the past will override the rule and cause Cloudflare to NOT cache the resource. To override these you'd need to add another setting called "Edge Cache TTL" and set it to however long you'd like Cloudflare to cache that resource on our servers.

It looks like setting the Expires header manually gets a cf-cache-status: HIT.

joshuabaker commented 5 years ago

Yep. The Expires header looks to be culprit here.

Not sure how best to approach this from the plugin perspective. Should Upper set this in EventRegistrar.php? Considering that Expires is ignored by everyone conforming to the RFC it shouldn’t be an issue, right?

ostark commented 5 years ago

@joshuabaker Nah, Cache-Control works perfectly. Can you provide an example? Via private channel, if you like ...

ostark commented 5 years ago
HTTP/1.1 200 OK
CF-Cache-Status: HIT
CF-RAY: 4ad38f25b9d56373-FRA
Cache-Control: public, s-maxage=604800
Connection: keep-alive
Content-Encoding: gzip
Content-Type: text/html; charset=UTF-8
Date: Fri, 22 Feb 2019 18:36:36 GMT
Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
Server: cloudflare
Set-Cookie: __cfduid=d1cedc6726af4441238cfc03d79d2ada51550860596; expires=Sat, 22-Feb-20 18:36:36 GMT; path=/; domain=.env.name; HttpOnly
Transfer-Encoding: chunked
Vary: Accept-Encoding
X-Upper-Cache: CACHED: 2019-02-22T18:35:01+0000
ostark commented 5 years ago

@joshuabaker Doublecheck your "Cache Level" Page Rule https://github.com/ostark/upper/tree/develop#cloudflare

ostark commented 5 years ago

ME: requires further research