Full page static caching

2 min read Updated 1 day ago

Full page static caching

Statamic's full page static caching generates static HTML files for your pages and serves them directly without running PHP. This dramatically improves response times and reduces server load for content-heavy sites.

When enabled, Statamic saves a static HTML copy of each page the first time it's visited. Subsequent visitors receive the pre-rendered HTML file instead of waiting for PHP to process the request.

How it works on Ploi Cloud

To enable full page static caching, you need to set an environment secret and add a custom nginx configuration. Once configured, your application will serve pre-rendered HTML files from a /static directory for GET requests, while non-GET requests and live preview sessions bypass the cache.

Step 1: Add the secret

  1. Navigate to your application's settings page
  2. Go to the "Environment secrets" section
  3. Add a new secret with key STATAMIC_STATIC_CACHING_STRATEGY and value full
  4. Save your changes

Step 2: Add custom nginx configuration

Add a custom nginx configuration to serve static files for GET requests and bypass the cache for other request types:

  1. Navigate to your application's "Security" tab
  2. Expand the "Custom nginx configuration" section
  3. Paste the following configuration:
set $try_location @static;

if ($request_method != GET) {
    set $try_location @not_static;
}

if ($args ~* "live-preview=(.*)") {
    set $try_location @not_static;
}

location @static {
    try_files /static${uri}_$args.html $uri $uri/ /index.php?$args;
}

location @not_static {
    try_files $uri /index.php?$args;
}
  1. Save your settings

This configuration ensures that:

  • GET requests are served from the static cache when available
  • POST, PUT, DELETE and other requests bypass the cache and go directly to PHP
  • Live preview requests from the Statamic control panel always serve fresh content

Step 3: Configure Statamic

In your Statamic configuration file (config/statamic/static_caching.php), set the strategy to full:

'strategy' => env('STATAMIC_STATIC_CACHING_STRATEGY', null),

You can also configure which URLs should be excluded from caching using the exclude array in the same configuration file.

Step 4: Deploy

Deploy your application to apply the changes. Both the secret and the nginx configuration take effect after deployment.