Speeding up Word Press Part 2 Caching and Compression

Today’s post will cover improving this sites performance with Browser cache and Compression. After the last post, we ended up with a score of 88/100 which is good, but we can do better.

Browser caching is very easy to enable in WordPress and should give some decent gains to visitors viewing multiple pages and post. Because I had the W3 Total Cache plug in installed, it only took a couple mouse clicks to setup. From the admin page I selected Performance >> Browser Cache. From there I checked “

Page Speed 1.12 Score: 99/100*

After enabling browser caching you can see the score jumped to an amazing 99/100. That surpassed my original goal but I haven’t enabled compression yet.

Browser compression can be done in a couple different ways. It can be done with  a WordPress plug in which would use PHP to send the gzip header, or you can do it with the web server. I prefer to have the web server do the work when ever possible. Because this site is hosted with Bluehost, I don’t have access to edit the Apache configs, so I need to make the changes using a .htaccess file.

I would like to point out that enabling browser compression has some trade-offs. It could slow things down. This is a shared server and gzip does make the server do more work compressing the content before its sent to the requesting browser. But the compressed content could make the site load quicker for visitors with slower internet connections. Browser compression also adds some over head to the visitors computer, because the browser needs to decompress the content before it can be displayed. In the past it might have been a problem, but with today’s hardware I don’t see that being a issue. Even cell phones have multiple cores and a gig of memory now.

This is what’s needed in the .htaccess file:

<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
</ifmodule>

Basically it says if Apache has the deflate module loaded then compress test, html, xml, css and javascript files. It doesn’t say only when the clients browser asks for compressed content, but Apache mod_deflate is smart enough to know.

After looking at my .htaccess I notices that I already had a section with mod_deflate information.

<IfModule mod_deflate.c>
<IfModule mod_setenvif.c>
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
</IfModule>
<IfModule mod_headers.c>
Header append Vary User-Agent env=!dont-vary
</IfModule>
<IfModule mod_filter.c>
AddOutputFilterByType DEFLATE text/css application/x-javascript text/x-component text/html text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon
</IfModule>

</IfModule>

 

So I ran a quick test with curl which told me something was wrong.

$ curl –head -H “Accept-Encoding: gzip” http://www.nathanboyce.com/wp-content/themes/tweaker4/ie.css

HTTP/1.1 200 OK
Date: Thu, 27 Sep 2012 05:54:37 GMT
Server: Apache
Vary: Accept-Encoding,User-Agent
Last-Modified: Wed, 26 Sep 2012 03:41:13 GMT
Accept-Ranges: bytes
Content-Length: 1159
Cache-Control: max-age=31536000, public, must-revalidate, proxy-revalidate
Expires: Fri, 27 Sep 2013 05:54:37 GMT
Pragma: public
X-Powered-By: W3 Total Cache/0.9.2.4
Content-Type: text/css

If this was a server I managed I would start by checking that the module was loaded and trouble shoot from there. Because its not a server I manage, I did a quick Google search and found this. Basically it says they wrote a cool way to un-deflate when the shared server is busy. I guess that’s a trade off with using a shared server. But I’m not giving up that easy. Remember I pointed out earlier  that we have a couple different ways to enable deflate in WordPress. I could enable it with a WordPress plug in like WordPress Gzip Compression, but if the server is already not compressing because of load that might make the site slower.  Another option that I plan on exploring in a later post is using a Content Delivery Network or CDN to do the compression.

 

If you dont like the idea of bluehost un-deflating your your content when the server is busy (because it happens a lot) look into Host Gator

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*