fbpx

Wordpress

Conditional HTTP GET for WordPress

by ,


We have by far the largest RPM repository with NGINX module packages and VMODs for Varnish. If you want to install NGINX, Varnish, and lots of useful performance/security software with smooth yum upgrades for production use, this is the repository for you.
Active subscription is required.

Conditional HTTP GET is a feature of HTTP protocol that can help with both the performance and even saving bandwidth to your users.
There is plugin for the purpose: If Modified Since.

The plugin doesn’t really support latest WordPress, but we have the fix for it. Edit the main plugin PHP file and replace:

if ( function_exists( 'http_response_code' ) ) {
    http_response_code( 304 );
} else {
    header( $this->get_http_protocol() . ' 304 Not Modified' ); 
}

with:

status_header( 304 );

Let’s confirm things are working well.

Verify that conditional HTTP GET is working with your WordPress

We have example commands to test it with this page.

First, check that Last-Modified header is being returned by plugin:

curl -Is "https://www.getpagespeed.com/web-apps/wordpress/wordpress-performance-checklist" | grep Last-Modified

If there is “Last-Modified: Thu, 28 Jul 2016 10:09:35 GMT” returned (or similar), the plugin successfully sends the header in the response. If the output is empty, something is wrong.

Next, confirm that conditional GET request will return empty response with 304 Not Modified status:

curl -Is "https://www.getpagespeed.com/web-apps/wordpress/wordpress-performance-checklist" -H 'If-Modified-Since: Thu, 28 Jul 2016 10:24:45 GMT' | head -n1

Will output “HTTP/1.1 304 Not Modified”

But wait, we have Varnish installed, and since the page is likely to be cached, it’s Varnish that handles conditional GET. We want to make sure that our uncached page still supports conditional requests. Let’s bypass Varnish by prepending a cookie:

curl -Is "https://www.getpagespeed.com/web-apps/wordpress/wordpress-performance-checklist" -H 'If-Modified-Since: Thu, 28 Jul 2016 10:24:45 GMT' -H 'Cookie: wordpress_test=1' | egrep 'HTTP|x-Cache'

Will give:

HTTP/1.1 304 Not Modified
x-Cache: uncached

This means that the request is not returned by Varnish cache and our PHP backend (WordPress) properly handles the conditional request.

Note: if things don’t work for you using this page as a test, make sure to adjust the date you’re checking with since this post will likely be updated later on and thus change its modification time.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: