Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send ESI headers #61

Merged
merged 2 commits into from
Dec 27, 2024
Merged
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
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
Loading