Skip to content

Things read, heard, watched and found. Now and then a thought of my own.

Timeline

Post 1 min read

Setup Statamic Static Caching

Setting up Static Caching in Statamic isn’t too hard and brings a huge performance boost. Here are the steps I had to take to enable it both locally with Laravel Valet and on a production server managed through Laravel Forge.

Enable and configure Nginx

  • Enable Static caching by setting STATAMIC_STATIC_CACHING_STRATEGY=full in .env
  • Add the following directive to Nginx config
location / { try_files /static${uri}_${args}.html $uri /index.php?$args; }

Clear and warm the static caches regularly

For sites that have dynamic content such as scheduled publishing or filtering by date, it makes sense to rebuild the static cache frequently.

The commands to do so are

  1. php please static:clear
  2. php please static:warm

We can add those to Laravel’s scheduler (related: Setting Up Job Scheduling for Statamic )by adding the following to the Kernel class in app/Console/Kernel.php:

/** * Define the application's command schedule. * * @param \\Illuminate\\Console\\Scheduling\\Schedule $schedule * @return void */ protected function schedule(Schedule $schedule) { // $schedule->command('cache:clear')->daily(); // $schedule->command('config:cache')->daily(); // $schedule->command('route:cache')->daily(); // $schedule->command('statamic:stache:warm')->daily(); $schedule->command('statamic:static:clear')->dailyAt('03:00'); $schedule->command('statamic:static:warm')->dailyAt('03:00'); }

daily runs at midnight, while dailyAt can be given any time of the day.

All that is left to do is to set up a Cronjob that calls php /home/forge/mysite.tld/artisan schedule:run every minute:

Screenshot of managing a cron job in Laravel Forge

Originally published at https://wentsch.me.

A letter every Monday

The week’s reading, highlights and the odd thought of my own, in one email.

One email a week. Unsubscribe with one click. See a sample