|||

Setup Statamic Static Caching for super fast websites

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;
}
  • Laravel Forge provides easy access to a site’s Nginx config. With Valet locally I had to first run valet secure, because to run a site with https it create a config file in ~/.config/valet/Nginx. This can be used to add the try_files rule (under a weird location like location /41c270e4-5535-4daa-b23e-c269744c2f45/. This exists two times, I added it to both, no idea if that’s required). Don’t forget to update APP_URL in .env and flush the config cache
  • ⚠️ Be aware that Statamic Multi Site setups require a different rule that includes the ${host} variable
location / {
  try_files /static/${host}${uri}_${args}.html $uri /index.php?$args;
}

Clear and warm the static caches regularly

For sites that have dynamic like 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 in Statamic by adding the following to the Kernel class in app/Console/Kernel.php:

/**
     * Define the application's command schedule.
     *
     * @param  IlluminateConsoleSchedulingSchedule  $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

forge-scheduler.png

php please static:clear && php please static:warm

Up next Setting Up Job Scheduling in Statamic If a Job implements ShouldQueue the Job will be automatically added to the queue (as do both Shopify Statamic and Statamic Peak|Peak Social Image
Latest posts Noted: UX/UI Opening at geOps Back to MyMind Sending Articles from Feedbin to Readwise Reader In Praise of Sublime App Why I Ended Up Using Todoist Yet Again Pros and Cons of Reflect Notes App A Love Letter to Sublime Internet Lazy.so is an amazing tool. Here's why I don't keep it Carl Sagan has shown the absurdity of not acting against climate change back in 1990 A success message that communicates the opposite of success Epomaker TH80 Pro Polyamor, aber mit PKM apps 🫣 Ironie, die Fabi Rommel Helft der Natur Tiny Garden Paradise Schwiegerfuchs Same Vibe Tailwind and Web Craftsmanship Buttons with CSS Expand Basic Markdown Formatting in Apple Notes Cannot Divide by Zero in Peak Picture Partial Applying a Growth Mindset (Doodle) Using the AVIF format for CSS background images Workaround for Email Validation Issue in Statamic A First Quick Look at Cosmos.so – and Why I'll Stick with MyMind Install a Starter Kit from local repo to get the latest Peak working in Statamic 4 beta Set sensible validations for Statamic image uploads Render first heading inside Statamic Page Builder as `h1` My PKM App Stack for 2023 Favorite apps in recent weeks: mymind & Craft