Show / hide notice on a certain date

Hi! I want to build a notification banner that gets shown at the top of a page. Like “we’re closed on Christmas!”

I was thinking of adding a start date and an end date for showing the notice.

How can I make sure that the notice gets shown and removed correctly? I guess I’d have to set up cache invalidation via a cronjob. Are there any good recipes for that?

1 Like

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.

Thanks! I installed the Kirby CLI on my server and added it to my PATH. What is the part at the end of your Kirby CLI command? pages > /dev/null 2>&1 .

Right now I can type kirby and get a list of the available commands. So I guess to clear the cache the command for the cronjob would be kirby clear:cache, right?

> /dev/null 2>&1 will redirect all output from the command to the null-device, effectivley swallowing all command output. Otherwise cron might notify you after each time the job has run or errored.

You can still implement your own logging or notification system if necessary.

A better short explanation can be found on stackexchange.
A few more words in this medium article.
And a dry one just about content redirection.