Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,34 @@ The Cloud package runs Craft CMS 6 on [Craft Cloud](https://craftcms.com/cloud).
composer require craftcms/cloud
```

## Static cache

On Craft Cloud, site pages are cached by the gateway and CDN. Cloud builds on the `craftcms/http-cache` package and Craft's `ResponseCacheInfo`: it registers a `cloud` HTTP cache client and makes it the default, and sets its `Cache-Tag` formatter.

Craft's global `CollectResponseCacheInfo` middleware tags GET and HEAD responses with the elements they query, plus any tags added with `CacheTags::add()`. Its global `RestrictCacheControl` middleware sends no-cache headers for responses no cache may store, and caps the lifetimes of other responses on Craft's routes at the earliest element expiry. Cloud's `CacheResponse` middleware runs first, so it sees the final response, and sets `CDN-Cache-Control`:

- a `CDN-Cache-Control` header the page set itself, or
- the page's `s-maxage`, `max-age` or `Expires` lifetime (including `{% expires %}` and `ResponseHeaders::setCache()`), with `stale-while-revalidate` and `stale-if-error`, or
- for pages that set no cache policy of their own, Cloud's default of one year with an hour of `stale-while-revalidate`.

The lifetime is capped at the earliest element expiry. The page's own `Cache-Control` header isn't changed.

Responses that Craft's `CacheProfile` won't let a cache store (`no-store`, `no-cache`, `private`, responses that set cookies) get no `CDN-Cache-Control`. Craft sends no-cache headers for visitors with a session, saved sessions, and token requests.

`Cache-Tag` holds each tag hashed to `{shortEnvironmentId}{crc32}`, limited to Cloudflare's 1,000 tags and 16 KiB, with `{environmentId}:overflow` added when tags are dropped. Purges hash tags the same way.

The `craftcms/http-cache` package invalidates element tags (other than for drafts and revisions) on the default client, which is `cloud`, and Cloud purges saved and deleted elements by URL. Purges are sent through the signed gateway API once the request, command or queue job finishes, and failures are reported without failing it. To mark tagged pages stale instead of removing them, set `'clients' => ['cloud' => ['soft_purge' => true]]` in `config/http-cache.php`.

The Caches utility has a “Craft Cloud static cache” option, and these commands purge directly:

- `php craft cloud:static-cache:purge-all`
- `php craft cloud:static-cache:purge-origin`
- `php craft cloud:static-cache:purge-cdn`
- `php craft cloud:static-cache:purge-tags {tags*}`
- `php craft cloud:static-cache:purge-prefixes {prefixes*}`

Cloud reads `CRAFT_CLOUD_ENVIRONMENT_ID` and `CRAFT_CLOUD_SIGNING_KEY`. `config/cloud.php` can set `environment_id`, `signing_key`, `gateway_base_url`, `static_cache_duration` and `static_cache_stale_while_revalidate_duration`.

## Development

```bash
Expand Down
14 changes: 12 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
"php": "^8.5",
"bref/bref": "^3",
"bref/laravel-bridge": "^3.1.7",
"craftcms/cms": "^6.0.0-beta.3"
"craftcms/cms": "dev-timkelty/http-cache",
"craftcms/http-cache": "dev-main",
"craftcms/http-message-signatures": "^1.0"
},
"require-dev": {
"craftcms/cms-assets": "6.x-dev as dev-timkelty/http-cache",
"larastan/larastan": "^3.13",
"laravel/pint": "^1.22",
"orchestra/testbench": "^11.0",
Expand Down Expand Up @@ -75,5 +78,12 @@
"bref/laravel-health-check"
]
}
}
},
"repositories": [
{
"name": "craftcms-http-cache",
"type": "vcs",
"url": "https://github.com/craftcms/http-cache"
}
]
}
Loading