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
In our blueprint, we add a
cache option like this:
In our config file, we can now check if the
cache option is set or not and return the negated value or default to
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.
config.php, we now query the field value:
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.