Static Caching
Using the static file cache can bring your page load time down to <100ms, but you can not use escaping or anything that requires ExpressionEngine processing on each request.
It does this by caching the entire HTTP response just before it is sent to the browser.
This response is then built into a PHP template that includes all the code for setting HTTP headers and checking the expiry time.
When using Static caching if the speedy_query_cache_allowlist
config value is changed you will need to clear your site cache to ensure new configuration is used.
When using Redis with static enabled if the speedy_query_cache_allowlist
config value is changed or connection settings to the Redis server is changed you will need to clear your site cache to ensure new configuration is used. You will also need to delete the index_redis_default_site.php
file in the root of your site, then click the prompt below in your control panel to re-generate the index file.
Static caching works exactly the same when using Coilpack as it does when using the native template language.
Installation
To enable the static file cache driver:
Create a new directory in your document root (where
index.php
is) namedstatic
.Make sure
speedy_static_enabled
is set toyes
in yourconfig.php
file.Add the following code to the top of your
.htaccess
file (or create one if it does not exist).Complete the installation in the Speedy control panel page.
At minimum, your config.php file should have the following for static caching to work:
If you are using Redis as a static caching option, you must change speedy_driver
to redis
, and enable it in Redis driver settings page in the control panel, or add the following to your config.php file:
Apache and Nginx configuration is not included with Speedy's support.
The following examples work as provided based on Speedy's default settings. Due to the wide variety of issues that can occur with server configurations and other re-write rules a site may require, debugging and supporting the following configurations on is not included with Speedy's support. These are only provided as a starting point. You may need to modify the rules if they do not work for you as is.
Apache .htaccess rewrite rule:
If using Nginx:
Once static caching is enabled and configured, simply add the following tag to the pages you want to be cached. This tag can be anywhere in your template, and does not need a closing tag pair.
{exp:speedy:static}
accepts the following parameters:
ttl
The number of seconds before this cache item with expire. If left blank the default TTL (1 hour) will be used. Usettl="0"
to cache this item until the cache it broken. (Default:""
)tags
A pipe-separated list of tags to apply to this item. Tags can be used in the cache breaking settings. (Default:""
)url_override
Override the url used as part of the key for non-global items. (Default:""
)url_prefix
Add a prefix to the key. Useful for multi-lingual sites, e.g.url_prefix="{publisher:current_language_code}"
(Default:""
)If using Publisher, and you are not displaying the default language segment in the URL, you may need a conditional similar to
url_prefix="{if publisher:current_language_id != publisher:default_language_id}{publisher:current_language_code}{/if}"
When using Publisher
If you're using the Publisher add-on with Speedy's static caching and need to view a draft on the front-end of your site you may need to include the following (or similar) to bypass the cached file and directly request a fresh page render from ExpressionEngine.
Advanced Customization
If you want to customise the path to the static file directory, you can set the speedy_static_path
option in your config.php
. If you do this, you must update your .htaccess
file.
If you are using the MSM module, or have a different site short name for whatever reason you must update your .htaccess
file.
Replace STATIC_PATH
and SITE_NAME
in the following template:
Apache .htaccess rewrite rule:
If using Nginx:
Query String Caching
Query string caching will only work when using the static driver if the requested page is cached without the query string. For example, if the url /products/shirt?size=small
is requested by the user and cached, it will not display the cached contents to any user until /products/shirt
is also cached.
Note this is not the case when using the Redis cache driver with static enabled.
If the speedy_query_cache_allowlist
config value is defined and contains an array of values, query string caching will be enabled. Speedy will only cache unique pages that contain query strings keys defined in speedy_query_cache_allowlist
.
Redis as Static driver
If you are using Redis with static caching, your .htaccess re-write rules will look a little different. The index_redis_default_site.php
file will be auto-generated by Speedy during the setup process. If this file does not exist you will be prompted in Speedy's control panel page to generate the file.
CSRF tokens
When static caching is enabled, the CSRF tokens in forms will be invalid. Each user gets a CSRF token, thus if every user gets the same cached page forms will not submit due to an invalid token. Speedy will update the CSRF token field in any form that is generated by ExpressionEngine. If you are building your own forms, make sure your csrf_token
field matches the following format.
A csrf_token
field updated by Speedy will have the data-updated
attribute added t it. If you do not see this attribute on the csrf_token
fields in cached output, then Speedy was not able to update the value for the current user.
Debugging
You can put Speedy into debug mode, which will output some additional information as html comments in your cached output. To enable debugging, just add ?speedy_debug=1
to any page request. This currently only works with Static caching.
Last updated