Skip to content

Add Craft Cloud static caching on Craft's HTTP cache layer - #217

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

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

Conversation

@timkelty

@timkelty timkelty commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

This PR adds static page caching on Craft Cloud for Craft 6. It builds on Craft's CacheInfo (craftcms/cms#21032) and the craftcms/http-cache package.

Craft collects cache tags and element expiry. The package provides the cache profile, the tag header middleware and the invalidation flow. Cloud adds three things:

Piece What it does
CacheResponse Global middleware, first in the stack, that sets CDN-Cache-Control. It uses the page's own lifetime, or Cloud's default of max-age=1y, stale-while-revalidate=1h, capped at CacheInfo::getExpiryDate().
CacheTagHeaderFormatter and CacheTagEncoder Write Cache-Tag as {shortEnvironmentId}{crc32} hashes, within Cloudflare's 1,000-tag and 16 KiB limits. They add {environmentId}:overflow when tags are dropped. Purges use the same encoder.
Gateway The cloud http-cache client, and the default one. It sends signed tag, URL, prefix and full purges to the gateway API. With 'clients' => ['cloud' => ['soft_purge' => true]], tag purges go to cache/invalidate.

On Cloud, CloudServiceProvider registers StaticCacheServiceProvider. Off Cloud, nothing changes.

Behavior

  • Responses that CacheProfile::isResponseCacheable() rejects get no CDN-Cache-Control, even one the page set. That covers no-store, no-cache, private and responses that set cookies. Cloudflare then follows their Cache-Control.
  • Core's RestrictCacheControl sends no-cache headers for sessions and token requests, so Cloud doesn't check them itself.
  • Action responses and responses to Authorization requests don't get the default lifetime. An action that sets its own lifetime keeps it.
  • The package invalidates element tags, skipping drafts and revisions, and flushes after each request, command and queue job. Gateway failures are logged, not thrown. Cloud adds URL purges for saved and deleted elements.
  • The Caches utility has a "Craft Cloud static cache" option, and cloud:static-cache:purge-{all,origin,cdn,tags,prefixes} purge directly.

Important

Revert the composer pins once craftcms/cms#21032 merges and craftcms/http-cache is on Packagist. craftcms/cms is at dev-timkelty/http-cache, and craftcms/http-cache is at dev-main through a VCS repository. craftcms/cms-assets is aliased (6.x-dev as dev-timkelty/http-cache) in require-dev, because the core branch requires it at self.version.

Follow-ups

These were in the closed #214:

Related issues

@linear-code

linear-code Bot commented Nov 23, 2026

Copy link
Copy Markdown

CLOUD-774

Rebuilds #217's 26 commits (previously 50c1b6d) on 5.x: the CraftCms\Cloud
namespace, the slimmed CloudServiceProvider, Pint and PHPStan, and Pest on
Testbench. Temporary pins: craftcms/cms at craftcms/cms#21032 and
craftcms/http-cache at dev-main.

Amp-Thread-ID: https://ampcode.com/threads/T-67f4021b-dc1c-4486-bc57-f39fdb7dabcf
Packagist already lists dev-timkelty/http-cache, so Composer no longer
walks every cms tag and branch through the GitHub API.

Amp-Thread-ID: https://ampcode.com/threads/T-67f4021b-dc1c-4486-bc57-f39fdb7dabcf
@timkelty timkelty changed the title Port Cloud static caching to Craft's HTTP cache layer Add Craft Cloud static caching on Craft's HTTP cache layer Dec 6, 2026
timkelty and others added 3 commits December 6, 2026 03:39
Craft split CacheInfo into CacheTags, for adding tags, and ResponseCacheInfo, for reading the response's tags and expiry date.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-01a1fc17-c592-763a-a600-e76afae5cd1f
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