Hi,
I am playing with Kirby cache, and need the cache to ignore a page in a couple of scenarios:
I have specified the page shouldn’t be cached in the blueprint
I have a specific block included on the page (a contact form)
I have the below setup:
'cache' => [
'pages' => [
'active' => true,
'ignore' => function ($page) {
return $page->pageBuilder()->toBlocks()->filterBy('type', 'contact-form')->isNotEmpty();
},
'ignore' => function($page) {
$options = $page->blueprint()->options();
return isset($options['cache']) ? !$options['cache'] : false;
}
]
],
Is this correct? It doesn’t seem to be working.
texnixe
December 6, 2022, 12:29pm
2
You can only have one ignore key, so no, this is not correct, and you have to merge your conditions into one.
Thought I would revisit this to post my solution - it could probably be more elegant but does the job. It allows for a cache: false
in blueprint options, and also checks for a contact form block being used in a pageBuilder
field:
'cache' => [
'pages' => [
'active' => true,
'ignore' => function ($page) {
$options = $page->blueprint()->options();
$optionsSet = isset($options['cache']) ? !$options['cache'] : false;
$noCacheBlocks = $page->pageBuilder()->toBlocks()->filterBy('type', 'contact-form')->isNotEmpty();
if($optionsSet == true || $noCacheBlocks == true):
return true;
else:
return false;
endif;
}
]
],
If you want something shorter:
'cache' => [
'pages' => [
'active' => true,
'ignore' => fn ($page) => ($page->blueprint()->options()['cache'] ?? true) === false ||
$page->pageBuilder()->toBlocks()->filterBy('type', 'contact-form')->isNotEmpty()
],
],
That is much more elegant, thank you