Skip to content

Commit

Permalink
feat: do not cache if the HTTP method is not cacheable
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Dec 24, 2023
1 parent c6a9112 commit d182491
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions system/Filters/PageCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use CodeIgniter\HTTP\CLIRequest;
use CodeIgniter\HTTP\DownloadResponse;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\Method;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
Expand Down Expand Up @@ -65,6 +66,10 @@ public function after(RequestInterface $request, ResponseInterface $response, $a
{
assert($request instanceof CLIRequest || $request instanceof IncomingRequest);

if ($this->isNotCacheableMethod($request)) {
return;
}

if (
! $response instanceof DownloadResponse
&& ! $response instanceof RedirectResponse
Expand All @@ -75,4 +80,19 @@ public function after(RequestInterface $request, ResponseInterface $response, $a
$this->pageCache->make($request, $response);
}
}

private function isNotCacheableMethod(RequestInterface $request): bool
{
return in_array(
$request->getMethod(),
[
Method::CONNECT,
Method::DELETE,
Method::OPTIONS,
Method::PUT,
Method::TRACE,
],
true
);
}
}

0 comments on commit d182491

Please sign in to comment.