For a simple website you can clear the pages cache via cronjob everyday at midnight. So that the first request on the new day will not hit the stale cache and gets served a fresh version which is then written to cache.
You can clear Kirbys pages cache either via the Kirby CLI or, if it’s the default file based cache, just by deleting the pages cache folder.
Setting up the cron job will depend on what your website provider offers.
PHP Script
If you do not have access to crontab, some provider offer a web-based cron. Most of the time this works by pinging a given URL at a time and interval specified by you.
You can create a custom route for this, with the advantage that Kirby is booted, get the pages cache object and flush it. See below for a very simplified example. This route should be protected, so not any visitor can clear the cache just b pinging the url.
[
'pattern' => '/clear-pages-cache',
'action' => function () {
// Check for authentication before running the command.
$result = kirby()→cache(‘pages’)?→flush();
// Note: this will also return a 500 when no pages cache is set.
return $result
? Response::json(body: [ 'status' => 'ok' ], code: 200)
: Response::json(body: [ 'status' => 'error' ], code: 500)
},
]
Crontab
If you’ve ssh access to your host and the crontab terminal utility you could set it up like this via crontab -e
0 0 * * * rm -rf /absolute/path/to/pages/cache/folder > /dev/null 2>&1
This would delete the caches folder on disk. Be careful with the -rf flag, it will delete everything at the given path and not ask if you really want to delete everything : )
Crontab + Kirby CLI
Using Kirby CLI can be a bit more involved. You will need Kirby CLI installed on your host system and should use the same PHP version your website runs on to boot it.
0 0 * * * cd /absolute/path/of/kirby && /path/to/php-bin /path/to/kirby-cli clear:cache pages > /dev/null 2>&1
A quick way to find the path to php binaries available on the host it to run realpath $(which php) on via ssh. This should output something like /usr/opt/bin/php84. Which is the path to the PHP 8.4 binary. PHP 8.3 might be located at /usr/opt/bin/php83. This does not always work, in which case you will have to check the provider documentation or ask support.