Introducing Preload Fullpage Cache

Situation #1

Right after creating my first WordPress plugin, Auto Maintenance Mode, I came across a situation on a high-traffic blog where the visitors just rush to the website immediately after a blog post is published and shared (automatically) via social media. It created chaos in the server and the initial visitors didn’t get the cached version of the blog post, because it wasn’t even ready.

Situation #2

In another popular full page cache plugin that I use in certain sites, there is no built-in functionality to preload any posts. Preloading is hard, especially when we had to scale with pre-loading with thousands of posts. So, the plugin (authors) decided to stick with only caching. Of course, it does its job very well.

Situation #3

I use WP Super Cache as the primary caching engine with Batcache as the backup. WP Super Cache plugin offers built-in capability to preload existing posts. However, it lacks the ability to preload the AMP posts. It means when Google comes and checks for AMP version of the post, it is usually prepared and then served by the web server, taking considerable time to serve an AMP version of the post to Google, the most important (bot) visitor of your site!

Situation #4

I always serve stripped down version of my site for mobile visitors. It is easy to do with wp_is_mobile function. But, it means my site needs additional version of a post to serve mobile visitors.

All the above scenarios can be solved by using my latest plugin, Preload Fullpage Cache. Basically, the plugin does the following…

  • Create a cached copy of a post immediately after publishing.
  • Create a desktop version of the cache for the post (that is just published)
  • Create a mobile version of the cache for the post (that is just published)
  • Create an AMP version of the cache  for the post (that is just published)
  • Don’t bother to create AMP version for pages (because the popular AMP plugin doesn’t support AMP for pages, yet)

All of the above may seem like a daunting task to achieve. In reality, it is easy to do. The general idea is to use the built-in WP HTTP API to fetch the post immediately after it is published, effectively storing the cached version of the post. Here’s the snippet from the plugin…

function __construct() {
    add_action( 'save_post', array( $this, 'preload_desktop' ),  900, 3 ); // let's fetch the post very late
    add_action( 'save_post', array( $this, 'preload_mobile' ),   990, 3 ); // let's fetch mobile version even later
    add_action( 'save_post_post', array( $this, 'preload_amp' ), 999, 3 ); // let's fetch AMP version at last; only works on posts

function preload_desktop( $post_ID, $post, $update ) {
    $desktop_url = get_permalink( $post_ID );
    $desktop_url_args = array(
        'httpversion' => '1.1',
        'user-agent'  => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36',
    wp_remote_get( $desktop_url, $desktop_url_args );

function preload_mobile( $post_ID, $post, $update ) {
    $mobile_url = get_permalink( $post_ID );
    $mobile_url_args = array(
        'httpversion' => '1.1',
        'user-agent'  => 'Mozilla/5.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/600.1.3 (KHTML, like Gecko) Version/8.0 Mobile/12A4345d Safari/600.1.4',
    wp_remote_get( $mobile_url, $mobile_url_args );

function preload_amp( $post_ID, $post, $update ) {
    $amp_url = get_permalink( $post_ID ) . 'amp/';
    $amp_url_args = array(
        'httpversion' => '1.1',
        'user-agent'  => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36',
    wp_remote_get( $amp_url, $amp_url_args );

Complete code can be found in Github.

There is so much to do to improve it further.

  • Settings page to select desktop, mobile, and AMP
  • Advanced settings to select the user agent/s
  • Translations
  • Option to keep the plugin options when the plugin is deleted
  • Make it a full-fledged preload plugin similar to what’s offered by WP Super Cache plugin!

Check it out in the official WP repo and all kind of feedbacks are welcome!

Leave a Reply

Your email address will not be published. We use cookies to prevent spam comments. Required fields are marked *