Skip to content

Commit

Permalink
ttl config
Browse files Browse the repository at this point in the history
  • Loading branch information
emredipi committed Dec 23, 2023
1 parent 5c5ba7d commit d1f28b1
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/CloudflarePagesMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function handle(Request $request, Closure $next): Response
/** @var Response $response */
$response = $next($request);
if ($this->shouldCacheResponse($request, $response)) {
$ttl = $request->attributes->get(CloudflareCache::TTL_ATTR) ?? config('cloudflare_cache.cache_ttl', 600);
$ttl = $this->getCacheTTL($request);
$response->headers->add(['Cache-Control' => "max-age=$ttl, public"]);
$response->headers->remove('set-cookie');

Expand All @@ -40,6 +40,15 @@ protected function getCacheTags(Request $request): array
return array_merge(array_unique($request->attributes->get(CloudflareCache::TAGS_ATTR, [])));
}

protected function getCacheTTL(Request $request): int
{
if ($request->attributes->has(CloudflareCache::TTL_ATTR)) {
return $request->attributes->get(CloudflareCache::TTL_ATTR);
}

return config('cloudflare_cache.cache_ttl', 600);
}

public function shouldCacheResponse(Request $request, Response $response): bool
{
if (! $request->isMethod('GET')) {
Expand Down

0 comments on commit d1f28b1

Please sign in to comment.