Speed Benefits of Caching AJAX Get Requests Swift Performance
For this example we use WordPress Infinite Scroll – Ajax Load More plugin for lazy loading posts, single posts, pages, comments and more with Ajax powered queries. It is a great plugin, however we already know that it takes between 300-400ms for the site to display the data. That is not the plugins’ fault, it is just the nature of AJAX
Luckily we can cache this AJAX request and get it down to 50ms with Swift Performance. We will assume you already have Swift installed and configured on your site.
Here are some GTMetrix results before and after caching the AJAX requests.
Uncached AJAX Response Time
With Swift Cacheable AJAX Actions feature DISABLED took 431 ms.
Cached AJAX Response Time
With Swift Cacheable AJAX Actions feature ENABLED took 55 ms.
That is a pretty amazing increase in performance by caching the AJAX request with Swift Performance.
How to Cache AJAX GET Requests with Swift Performance Pro.
Cacheable AJAX Actions – With this option you can cache resource-intensive AJAX requests (eg AJAX search). You should specify the action names here. You can also set cache expiry time for AJAX requests in seconds. For example you can use it to cache custom ajax search or cache WP datatables ajax responses. It´s advanced stuff. If you don´t know how to use this feature, it`s better not to use it.
AJAX Cache Expiry Time – Cache expiry time for AJAX requests in seconds.
You find this feature in Settings->Caching->General of Swift Performance. If AJAX calls are resource-intensive you can cache those with Cacheable AJAX Actions.
So, to achieve this, you first need to find the Ajax Action as Ajax always has an action. Load the page in GTMetrix and open the Waterfall tab, look for the admin-ajax.php and open that tab.
Just click on the Params tab and note the action, in this example: alm_get_posts
Then return to your WordPress site backend, Swift Settings and look for Cacheable AJAX Actions in Settings->Caching->General: set alm_get_posts there. That`s all.
Verify if Swift is Caching the AJAX GET Requests
Use Curl or Load the page in GTMetrix and open the Waterfall tab, look for the admin-ajax.php, open that tab Headers. Cache response header should reporting a HIT (swift-performance – HIT).
Now you have successfully cached WordPress admin-ajax GET requests with Swift Performance and your AJAXified content will load faster while PHP and MySQL get a much deserved rest.