Skip to content

Port Cloud static caching to Craft's HTTP cache layer - #217

Draft
timkelty wants to merge 14 commits into
5.xfrom
http-cache-port
Draft

timkelty wants to merge 14 commits into
5.xfrom
http-cache-port

Conversation

@timkelty

@timkelty timkelty commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Ports Cloud's Craft 6 static caching (from cache-control-model-port, fdf8c71) onto Craft's ResponseCacheInfo (craftcms/cms#21032) and the craftcms/http-cache package. Craft collects the tags and caps lifetimes at element expiry, the package provides the cache policy, invalidation clients and tag header, and Cloud only adds its tag wire format, its CDN header and its purge client.

Piece Before (fdf8c71) Now
Tags and expiry Cloud's CacheResponse collected with ElementCaches Core's global CollectCacheInfo + ResponseCacheInfo (getTags(), getExpiryDate())
Tag hashing CacheTags CacheTagEncoder, used by the formatter for the header and by the client for purges
Cache-Tag header and limits CacheTags::header() CacheTagHeaderFormatter (FOS TagHeaderFormatter, hashes the raw tags from ResponseCacheInfo), set as http-cache.tags.formatter
CDN-Cache-Control CdnCacheControl, always set, no-store for personal responses CacheResponse (global, prepended): the page's own lifetime, or Cloud's default public, max-age=1y, stale-while-revalidate=1h, capped at ResponseCacheInfo::getExpiryDate(). None when CacheProfile::isResponseCacheable() rejects the response, including no-cache
Purges Gateway + StaticCache buffers Gateway is a FOS ProxyClient (Tag/Purge/Refresh/Prefix/ClearCapable) registered as the cloud driver and default client; callers purge raw tags and the client encodes them. 'clients' => ['cloud' => ['soft_purge' => true]] sends tags to cache/invalidate

The package invalidates element tags on the default client (cloud) and every other tag-capable client in http-cache.clients, and sends invalidations after each request, command and job. Cloud only adds URL purges for saved and deleted elements. The package reports gateway failures instead of failing termination.

Important

Revert the composer pins once craftcms/cms#21032 lands and craftcms/http-cache is released. craftcms/cms points at dev-timkelty/http-cache and craftcms/http-cache at dev-main, both through VCS repositories. craftcms/http-cache is private, so CI needs a token that can read it. craftcms/cms-assets is aliased (6.x-dev as dev-timkelty/http-cache) because the branch requires it at self.version. The alias is in require-dev so composer validate --strict passes; root aliases only apply to this repo's own installs either way.

Behavior changes from fdf8c71

  • Responses Cloud won't cache get no CDN-Cache-Control instead of no-store, per the design ("Core speaks only standard Cache-Control"). Cloudflare then follows their Cache-Control.
  • Cloud no longer checks the session cookie, route and site tokens itself; it relies on the no-cache headers core sends for them ([6.x] HTTP cache header improvements cms#20953). Requests with Authorization don't get Cloud's default.
  • An Expires-only response (no public) isn't cached: Symfony computes private, must-revalidate for it.
  • cloud:static-cache:purge-tags hashes its tags like every other tag purge.
  • A CDN-Cache-Control header the page sets is kept, but only if the page's Cache-Control and the CDN header itself pass the same checks.

Evidence

composer validate --strict   # valid
composer lint                # mago lint + format: clean
composer test                # Tests: 38 passed (75 assertions)

Contract tests in tests/Feature/StaticCacheTest.php run requests through core's global CollectCacheInfo and the route groups:

  • hashes tags the same way in the Cache-Tag header and in purges: element and ResponseCacheInfo tags in the header equal the tags POSTed to /cache/purge.
  • gives pages without a cache policy Cloud's default, capped at element expiry: CDN-Cache-Control: max-age=90, … with an element expiring in 90s, while the page's Cache-Control stays no-cache, private. Dropping the expiry cap fails it.
  • sends no CDN-Cache-Control for responses a cache mustn't store: noCache(), no-store, no-cache, private, Set-Cookie, queued cookies, and Set-Cookie with a lifetime.

Related issues

timkelty and others added 3 commits November 10, 2026 04:57
Consume the CMS checkpoint for browser/CDN policy and dependency tags, apply Cloud defaults before expiry caps, and preserve gateway tag invalidation.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1c3c7-9d57-7547-959b-ebaf6c0cd15c
Co-authored-by: Amp <amp@ampcode.com>
craftcms/cms#20545 was closed without merging, so drop the CacheControl
service, middleware and event dependency. Collect element cache tags and
expiry with ElementCaches, derive CDN-Cache-Control from the final
response headers, purge on ElementCachesInvalidated and element
save/delete through the signed gateway, and add the Clear Caches option
and purge commands from 3.x. Lock craftcms/cms 6.x at 8a13417.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-6551416a-ce7c-4e90-adac-8c2edc72248c
Build on craftcms/cms#21032 instead of collecting element tags and
expiry in Cloud's own middleware:

- Hash tags with a TagEncoder and write Cache-Tag with a FOSHttpCache
  tag header formatter, so the header and purges match.
- Make the signed gateway a FOSHttpCache ProxyClient (`cloud` driver),
  with soft purge as a client option, and purge through
  HttpCache::invalidator('cloud').
- Set Cloud's default lifetime as s-maxage inside TagResponse, so it's
  capped at element expiry, and copy the final lifetime to
  CDN-Cache-Control outside it, skipping responses the CachePolicy
  won't store and no-cache responses.

Require craftcms/cms's timkelty/http-cache branch until #21032 merges.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-4e830087-a857-4596-b578-0893eaf5a9db
@linear-code

linear-code Bot commented Nov 23, 2026

Copy link
Copy Markdown

CLOUD-774

timkelty and others added 11 commits November 23, 2026 21:43
craftcms/cms#21032 now gives ResponseTagger the earliest element expiry
as it's collected, so Cloud's default lifetime no longer has to be set
inside TagResponse and restored afterward. CacheResponse sets
CDN-Cache-Control to the page's own lifetime, or Cloud's default, capped
at ResponseTagger::getExpiryDate(), and judges a response with its own
CDN-Cache-Control header by that header. The `cloud` client needs no
config entry now. Update craftcms/cms to 7283599.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-4e830087-a857-4596-b578-0893eaf5a9db
craftcms/cms#21032 now rejects no-cache responses in DefaultCachePolicy
and defaults a client's driver to its name when an extend() creator
exists, so drop Cloud's own no-cache check and the `driver` key from the
soft_purge config. Update craftcms/cms to c16d739.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-4e830087-a857-4596-b578-0893eaf5a9db
craftcms/cms#21032 removed TagEncoder, so ResponseTagger hands the
formatter raw tags. CacheTagHeaderFormatter now hashes them with
CacheTagEncoder, a plain Cloud class, before truncating, and Gateway
keeps hashing purges with the same class. Update craftcms/cms to
5cecafe.

Amp-Thread-ID: https://ampcode.com/threads/T-4e830087-a857-4596-b578-0893eaf5a9db
Co-authored-by: Amp <amp@ampcode.com>
craftcms/cms#21032 now keeps only ResponseTagger, element tag collection
and the expiry cap. CachePolicy, HttpCacheManager and the tag header
moved to craftcms/http-cache, configured under `http-cache.*`. Require
its main branch through a VCS repository and update imports and config
keys. Update craftcms/cms to 157ef55.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-4e830087-a857-4596-b578-0893eaf5a9db
Update craftcms/cms to e147b17.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-4e830087-a857-4596-b578-0893eaf5a9db
Update to craftcms/cms 30bdbb8 (ResponseTagger is now
Http\ResponseCacheInfo) and craftcms/http-cache ccc94d8 (CachePolicy is
now CacheProfile). The package now invalidates element tags on the
default client and sends invalidations after each request, command and
job, so drop Cloud's own ElementCachesInvalidated listener and flush
hooks. Gateway::flush() now throws FOSHttpCache's ExceptionCollection,
so the package reports gateway failures instead of failing termination.

Amp-Thread-ID: https://ampcode.com/threads/T-4e830087-a857-4596-b578-0893eaf5a9db
Co-authored-by: Amp <amp@ampcode.com>
craftcms/http-cache 1633ff5 reports any exception a client throws, so
Gateway::flush() throws Laravel's exceptions as they are again. The
package now invalidates element tags only on clients listed in
`http-cache.clients`, so Cloud adds a `cloud` entry.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-4e830087-a857-4596-b578-0893eaf5a9db
craftcms/http-cache 79f089b invalidates element tags on the default
client even without a config entry.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-4e830087-a857-4596-b578-0893eaf5a9db
Update craftcms/cms and craftcms/http-cache. Craft now flushes cache
invalidations after database transactions commit, so the static cache
tests register the database provider.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1fc17-c592-763a-a600-e76afae5cd1f
Co-authored-by: Amp <amp@ampcode.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant