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

do no cache when page cache disabled #22

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
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
23 changes: 23 additions & 0 deletions advanced-pagecache.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
class AdvancedPageCachePlugin extends Plugin
{
public const CACHE_ENABLED_KEY = 'cache_enable';

/** @var Config $config */
protected $config;
/** @var string */
Expand Down Expand Up @@ -126,6 +128,10 @@ public function onOutputGenerated(Event $event): void
$page = $this->grav['page'];
$html = $this->grav->output;

if($this->isPageCachingDisabled($page)) {
return;
}

$item = [
'code' => $page->httpResponseCode(),
'headers' => $page->httpHeaders(),
Expand All @@ -144,6 +150,10 @@ public function onOutputRendered(Event $event): void
$page = $event['page'];
$html = $event['output'];

if($this->isPageCachingDisabled($page)) {
return;
}

$item = [
'code' => $page->httpResponseCode(),
'headers' => $page->httpHeaders(),
Expand All @@ -152,4 +162,17 @@ public function onOutputRendered(Event $event): void

$this->grav['cache']->save($this->pagecache_key, $item);
}

private function isPageCachingDisabled(PageInterface $page): bool
{
$header = $page->header();
if (property_exists($header, self::CACHE_ENABLED_KEY)) {
if ($header->{self::CACHE_ENABLED_KEY} === false) {
// do not cache
return true;
}
}

return false;
}
}