Skip to content

Kirby 3.6.6

Quicktip: Fine-tuning cache settings

Cache options are set via the config.php file. To prevent caching for certain pages, we can use the ignore option and either provide a list of page IDs to be ignored, or a callback function that returns a boolean for more control.

However, we can use this callback to make excluding pages from the cache much more dynamic without having to return to the config file every time we made some changes that require the cache settings to be adapted.

Using a blueprint option

As already mentioned in our the subpage builder recipe, we can use custom options in our blueprints, which we can then query via the $page->blueprint() method.

Page blueprint

In our blueprint, we add a cache option like this:

title: Blueprint title

options:
  cache: false

In config.php

In our config file, we can now check if the cache option is set or not and return the negated value or default to false.

/site/config/config.php
<?php

return [
    //...other settings
    'cache' => [
        'pages' => [
            'active' => true,
            'ignore' => function ($page) {
                $options = $page->blueprint()->options();
                return isset($options['cache']) ? !$options['cache'] : false;
            }
        ]
    ]
];

Using a field

While the above solution provides flexibility for developers, this is usually out of the control of editors and does not apply to single pages but to entire page types (i.e. pages sharing the same blueprint).

However, using a similar concept, we can leave it to the editor to decide whether or not to cache a page by adding a toggle (or checkbox field) to the page blueprint.

Page blueprint

title: Blueprint title

fields:
  cache:
    type: toggle
    default: false
    text: Exclude from cache?

In config.php

In our config.php, we now query the field value:

/site/config/config.php
<?php

return [
    //...other settings
    'cache' => [
        'pages' => [
            'active' => true,
            'ignore' => function ($page) {
                return $page->cache()->toBool();
            }
        ]
    ]
];

These two approaches can also be combined to allow editors to override default blueprint settings, for example if they add a form to a page that then needs to be excluded from the cache.

Thanks to Markus Denhoff for sharing the idea with the blueprint option.