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