WordPress Litespeed Cache and Shortcodes purging
Litespeed Cache is a really good description of what this plugin does, it’s a caching plugin for sites using Litespeed as a webserver.
At a SaaS I help run we have a couple of WordPress plugins that use shortcodes to show widgets with content, mostly used by news sites, and one of our customers uses Litespeed Cache as the caching solution to keep Google and readers happy.
Edge Side Includes + Litespeed Cache + shortcodes
Luckily the plugin allows an easy ways to update only the content of the shortcode and not the rest of the page. It’s so easy that you can do it just adding one word to the shortcode.
This is the shortcode for our plugin:
[downtack competition="liga-profesional-argentina-2022"]
This is the shortcode using Litespeed’s ESI Blocks:
[esi downtack competition="liga-profesional-argentina-2022"]
It’s too easy to be true. But there’s a catch, you also have to specify how long the Time To Live should be, if you don’t Litespeed Cache is gonna use the TTL specified in the “Default Public Cache TTL” option. But one of the solutions is so easy we didn’t have to modify our plugin’s code.
[esi downtack competition="liga-profesional-argentina-2022" ttl="60"]
There’s also the option to purge the shortcode programmatically, without the need to add the ttl
param.
do_action( 'litespeed_purge', 'esi.downtack' );
method_exists( 'LiteSpeed_Cache_API', 'purge' ) && LiteSpeed_Cache_API::purge( 'esi.downtack' );
This last option is the one we choose as it gives us much more control over the purging of the content and helps the site run smoother as you can purge the cache whenever a change is made.
The shortcode is basically showing a HTML file downloaded from our server so it doesn’t make sense to purge the cache if that file wasn’t updated.
One thing to consider is that if Litespeed is disabled you should update the shortcodes and remove the esi
keyword.