Skip to content

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

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

timkelty wants to merge 23 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 CacheInfo (craftcms/cms#21032) and the craftcms/http-cache package. Craft collects the tags and caps lifetimes at element expiry. The package provides the cache profile, invalidation clients and tag header. 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 and CacheInfo (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 (a FOS TagHeaderFormatter that hashes the raw tags from CacheInfo), set as http-cache.tags.formatter
CDN-Cache-Control CdnCacheControl, always set, no-store for personal responses CacheResponse (global, first): the page's own lifetime, or Cloud's default public, max-age=1y, stale-while-revalidate=1h, capped at CacheInfo::getExpiryDate(). None when CacheProfile::isResponseCacheable() rejects the response, including no-cache
Purges Gateway and StaticCache buffers Gateway is a FOS ProxyClient (Tag, Purge, Refresh, Prefix and 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 on Packagist. craftcms/cms points at dev-timkelty/http-cache and craftcms/http-cache at dev-main, both through VCS repositories. craftcms/cms-assets is aliased (6.x-dev as dev-timkelty/http-cache) because the core 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 tokens or site tokens itself. Core's RestrictCacheControl sends no-cache headers for them.
  • Action responses and responses to Authorization requests don't get Cloud's default lifetime. An action that sets its own lifetime keeps it.
  • An Expires-only response (no public) isn't cached, because Symfony computes private, must-revalidate for it.
  • cloud:static-cache:purge-tags hashes its tags like every other tag purge.
  • Cloud keeps a CDN-Cache-Control header the page sets, but only if the page's Cache-Control and the CDN header pass the same checks.

Evidence

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

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

  • hashes tags the same way in the Cache-Tag header and in purges: element and CacheInfo 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 90 seconds, 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 20 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>
Core's global RestrictCacheControl now caps lifetimes, so the README,
the CacheResponse docblock and the test's copy of core's stack name it.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-01a1fc17-c592-763a-a600-e76afae5cd1f
Craft sends no-cache headers again for actions without a lifetime of
their own, so CacheResponse's comment says which requests are covered.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-01a1fc17-c592-763a-a600-e76afae5cd1f
Core no longer sends no-cache headers for actions that set no lifetime, so
CacheResponse skips the default for action requests itself.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1fc17-c592-763a-a600-e76afae5cd1f
Co-authored-by: Amp <amp@ampcode.com>
Core picks up #21032's fix for element expiry when an element's tag was
collected first; http-cache's SendTagHeader now tags action responses.

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