ostark / upper

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

Cache tags not getting added to responses when content is within {% cache %} twig tags #61

Open RedLucas opened 2 years ago

RedLucas commented 2 years ago

I've noticed that only get generated and added to the response headers (in my case Cloudflare Cache-Tags) when the Response is a cold cache.

So on first load, the response has all necessary CacheTags headers, but it's possible that the cloudflare cache will be cleared for reasons.

Since Craft's template cache is caching content, any entries that were loaded within the {% cache %} tags no longer get added to the response headers.

I'm seeing this on CraftCMS 3.7.32 and Upper 1.9.1.

I'm not sure that this was always a problem, but perhaps it's a new problem from the 3.7 update?

ostark commented 2 years ago

I guess it was always like this. 🙉 To me, it makes sense to use the Craft/twig template cache and the full-page-cache Upper provides.

I need to investigate the behavior. Hopefully, there is an easy way to deliver cache tags even if you use the {% cache %} tag.

ostark commented 2 years ago

It's very possible!

khalwat commented 2 years ago

You can use Craft's new-ish (as of 3.5) built-in functionality for collecting element cache tags via Elements::startCollectingCacheTags() / Elements::stopCollectingCacheTags() to get a list of tag dependencies for a rendered template

khalwat commented 2 years ago

Related PR: https://github.com/craftcms/cms/pull/11617