Skip to content

Commit

Permalink
Merge pull request #61 from craftcms/feature/esi
Browse files Browse the repository at this point in the history
Send ESI headers
  • Loading branch information
timkelty authored Dec 27, 2024
2 parents 6a3a4d6 + 6b1b862 commit f9f0299
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/HeaderEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
21 changes: 18 additions & 3 deletions src/StaticCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f9f0299

Please sign in to comment.