Static Caching

In order to get the very best performance from your project, it's worth considering the use of static caching to dramatically accelerate the speed of most requests.

Static caching captures a snapshot of your project's response HTML and stores it in a cache. The next request for the same URL will return the pre-rendered HTML from the cache rather than forwarding the request to PHP for it to build a whole new response.

This strategy can work very well for websites which serve the same content to all users, but can cause issues for projects which rely on user logins or detection of a user's country in order to tailor the content that is displayed. However, the benefits are huge so if you're not sure whether it'll work for you, just give it a try in your staging environment and see how it goes!

Enabling static caching on Servd is easy and can be fully configured via the dashboard.

Enabling/Disabling Static Caching

  1. Navigate the the Environment > Settings page for the environment that you'd like to change

  2. Find the Static Caching section on the page

  3. Toggle the master enable/disable switch

  4. Click the 'Sync' button at the top of the page to deploy your change

Add Static Caching Exceptions

Whenever you use static caching it is very important to also define exceptions to the caching mechanism.

At a minimum the Craft control panel should be excluded in its entirety. You should also add exceptions for any pages which return different content to each user.

Static caching exceptions are based on URL prefixes. Any incoming request which begins with one of these prefixes will entirely bypass Servd's static caching mechanism.

  1. Navigate the the Environment > Settings page for the environment that you'd like to change

  2. Find the Static Caching section on the page

  3. Add, remove or edit any required URL prefixes

  4. Click Save

  5. Click the 'Sync' button at the top of the page to deploy your change

Automated Cache Purging

The Servd plugin is capable of automatically purging the cached copied of pages whenever those pages are updated. You can find out more about the settings which control this functionality in the plugin settings documentation.

Other Static Caching Options

Default Cache TTL

The length of time that cached versions of pages are stored before automatically expiring. If you set this to 'Use Origin Cache Headers' the value of the Cache-Control header will be used.

Logged in users skip the cache

When enabled, users who are logged into Craft (the control panel or otherwise) will never be sent cached responses and the responses they generate will never be cached. This is useful if you need to show admin-only content on the site's front end.

Include GET Params in cache keys

If you do not use GET/Query String parameters in your site's URLs you should disable this in order to greatly increase the number of requests which are served from the cache.

Check Whether a Response Came From the Cache

Once static caching has been enabled Servd will begin adding a response header called X-Cache to indicate whether or not a specific request has been served from the static cache. The header will not be present for requests which skip the cache entirely (CP logged in users or requests which match a cache exception).

You can check this header using the developer tools in your browser. It will be set to one of the following values:

  • MISS - The request was not served from the cache because no cached copy of the page was available
  • HIT - The request was served from the cache

Cache Stampede Protection

Cached pages are stored for a specific length of time, as defined by the Default Cache TTL setting, or the headers returned in your project's responses.

When this time elapses the cached page will be marked as 'expired'. The following request for the same page will return the 'expired' copy, but will also trigger a new version of the page to be created in the background. This ensures that all users are able to benefit from the increased speeds of statically cached content and avoids the danger of cache stampedes.

This mechanism means that even when cached pages are purged, you'll still see cache HITs for subsequent requests. In order to determine if a page has actually been flushed from the cache, check the age header which contains the number of seconds elapsed since the cached page was generated.

ESI & Other Advanced Functionality

The Servd platform is capable of performing other advanced functions for Enterprise projects, including ESI. Please reach out to us if you'd like to make use of this in your project.