diff --git a/composer.json b/composer.json index 9325904..d8e4415 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,6 @@ "name": "craftcms/cloud", "type": "yii2-extension", "require": { - "php": "^8.1", "bref/bref": "2.1.21", "bref/extra-php-extensions": "1.3.2", "craftcms/cms": "^4.6", @@ -37,9 +36,6 @@ }, "prefer-stable": true, "config": { - "platform": { - "php": "8.1" - }, "allow-plugins": { "yiisoft/yii2-composer": true, "craftcms/plugin-installer": true diff --git a/src/HeaderEnum.php b/src/HeaderEnum.php index e67e18a..6aef573 100644 --- a/src/HeaderEnum.php +++ b/src/HeaderEnum.php @@ -8,6 +8,8 @@ enum HeaderEnum: string case CACHE_PURGE_TAG = 'Cache-Purge-Tag'; case CACHE_PURGE_PREFIX = 'Cache-Purge-Prefix'; case CACHE_CONTROL = 'Cache-Control'; + case CDN_CACHE_CONTROL = 'CDN-Cache-Control'; + case SURROGATE_CONTROL = 'Surrogate-Control'; case AUTHORIZATION = 'Authorization'; case DEV_MODE = 'Dev-Mode'; case REQUEST_TYPE = 'Request-Type'; diff --git a/src/StaticCache.php b/src/StaticCache.php index 5bff051..ddcde8f 100644 --- a/src/StaticCache.php +++ b/src/StaticCache.php @@ -219,10 +219,25 @@ private function addCacheHeadersToWebResponse(): void 'duration' => $this->cacheDuration, ])); - // Cache in proxy, not in browser + // Copy the cache-control header to the cdn-cache-control header + $cacheControl = Craft::$app->getResponse()->getHeaders()->get( + HeaderEnum::CACHE_CONTROL->value + ); + + // Cache in CDN, not in browser + if ($cacheControl) { + Craft::$app->getResponse()->getHeaders()->setDefault( + HeaderEnum::CDN_CACHE_CONTROL->value, + $cacheControl, + ); + } + + // Enable ESI processing + // Note: The Surrogate-Control header will cause Cloudflare to ignore + // the Cache-Control header: https://developers.cloudflare.com/cache/concepts/cdn-cache-control/#header-precedence Craft::$app->getResponse()->getHeaders()->setDefault( - HeaderEnum::CACHE_CONTROL->value, - "public, s-maxage=$this->cacheDuration, max-age=0", + HeaderEnum::SURROGATE_CONTROL->value, + 'content="ESI/1.0"', ); // Capture, remove any existing headers so we can prepare them