With the release of Yahoo’s YSlow plugin for Firefox, web developers are better able to see how fast their web sites are loading, and improve this load time with some nifty server-side adjustments and on-page changes.
In a series of posts I will be discussing some of these elements, starting this week with caching page elements to reduce server load and make pages load faster.
Cache Pages
One of the problems with page caching is over-caching, where virtually every element on the page is cached meaning the user will see an outdated version of a page until the cache is refreshed or they delete their browser cache.
This cache problem isn’t the case as long as the name of images, elements and files are changed each time a new version is uploaded to the server as the browser will then cache the new version and ignore the old.
The image shows YSlow integrated with Firebug. The line outlined in red is what we are interested in today for caching purposes.
In this example I had an F rating because of off-server elements, I.e. Google Adsense code.
We have little control over the caching ability of such thing, however we can control elements of our own web sites.
On Apache the caching is carried out server-side, which means we need to create an .htaccess file. The .htaccess file is simply a text file.
This can be written in notepad or equivalent. It is important to save the file as .htaccess, not .htaccess.txt.
To do this in notepad
1. Load a new page
2. Enter the code:
<ifmodule mod_expires.c>
<filesmatch “\.(jpg|jpeg|gif|png|css|js)$”>
ExpiresActive on
ExpiresDefault “access plus 1 month”
</filesmatch>
</ifmodule>
3. File > Save As
4. In File name: field type .htaccess
5. Select All Files in Save as type: field
6. Click Save button
In the above example I have chosen to compress images (jpeg, jpg, gif and png, as well as external CSS and JavaScript files.
Caching of images is ideal because your basic web site template likely uses the same images across the whole site - these elements can be cached making page switching faster.
The server only needs to get the page text, and any additional on-page images, rather than a total refresh.
It isn’t advisable to add HTML or PHP to this, as the index.html or index.php file will be cached, even after you have refreshed it.
With this method I still have to rename my external CSS and JavaScript files if I make changes, however, this is unlikely/rare so for my needs caching these files is another way to make my pages load faster.
It is possible to choose the time period in the above code, ExpiresDefault “access plus 1 month”, this can be changed to 1 hour, 1 week, 1 year etc. Choose whichever is best for your needs.
Apart from 1 hour caching, the file name will need to be changed otherwise users may see an outdated version of your web site.




16 Responses
Mark @ TheLocoMono
February 1st, 2008 at 4:18 pm
1Terrific job. Easy to understand. Wow. I definitely will be implementing this. Do you have a post that explains in simple layman terms what caching does? I mean I understand it stores the image but how does that help me?
Does the code boost the performance of the built in WP cache or the SuperCache plugin? Like caching on steroids?
Low
February 2nd, 2008 at 5:09 pm
2I tried this and uploaded it to my websites but then it lead to 500 internal server error. Do i miss something?
Rob
February 5th, 2008 at 9:55 am
3@ Mark: By storing the images on the host computer, the images are loaded using the PCs internal memory, rather than having to request the images and download them remotely.
It helps reduce http lookups and makes the page appear much quicker in my experience.
@ Low: It may well be the way Apache is setup by your host. First check if you are allowed to use htaccess files on your hosting package.
They may have certain restrictions on htaccess usage.
Mark @ TheLocoMono
February 18th, 2008 at 10:39 pm
4The light bulb is starting to get a little brighter. I was studying how the WP Super Cache works and wondering about caching certain posts that have been set in stone (meaning it is over and done with, perfect as perfect can be).
So I came back to read this again one more time to think about what I read here the first time.
Basically, what you are saying if I write the code into my htaccess, you are saying I will be storing images on the user’s hard drive which will increase page load times.
Two questions and pardon if I sound like a non-techie.
1) Do you use the WP Super Cache plugin?
2) If I write the code you gave us,
ExpiresActive on
ExpiresDefault “access plus 1 month”
would it interfee with the WP Cache or WP Super Cache?
3) Actually three questions. Would it help if the perfect posts that are good as done, let’s say http://www.yourwebsite.com/dreamon/ which is basically a quote and a photo is cached? or should it just be limited to the image that is cached?
Rob
February 19th, 2008 at 9:37 am
5@ Mark
1) I don’t use WP Super Cache, I did try it but it had lots of problems so I uninstalled it.
2) I don’t know if it would interfere with your other cache plugins. I would suggest testing it, and checking your page load times with and without the caching function installed. To see if the images and scripts are caching correctly you need Live HTTP Headers plugin for Firefox. When the pages load it will display an expiry date for elements that have been cached. If you use 1 month, then it will be exactly one month from when the page was first loaded.
3) Personally I only cache images, CSS and JavaScript files. Usually the text content is the smallest part of the page, therefore no need to cache the text. The images and scripts can be pretty large, and are worth caching universally. That way elements can be reused across the website by pulling the cached information from the users browser temporary files. Remember you’d need to change the name of your scripts/CSS files if you wanted make adjustments. I.e. give them an incremental number for each change. However, I don’t find it necessary unless I make large changes.
Mark @ TheLocoMono
February 22nd, 2008 at 4:57 pm
6I am currently trying to find out from my host server if the code will interfere with their system in any way.
In the meantime, I am wondering about the javascript part, would that mean if a plugin is written in javascript, it would be cached too? Would that include AJAX?
Rob
February 25th, 2008 at 9:48 am
7If you use external JavaScript files (which is advisable), the above code will do this for you by picking up the file extension (.js).
Ajax is JavaScript so it should work for that to. You can compress any xml as well by including the (.xml) extension to the htaccess file.
As with all these things, test and test again. If things don’t work correctly tweak the file by adding/removing extensions until you are happy.
Remember for testing purposes, when you refresh to clear the cache so that your browser isn’t loading the cached version of the page. In Firefox Shift + Refresh button.
Mark @ TheLocoMono
February 26th, 2008 at 10:40 pm
8I just found out from my host server that
“That module doesn’t exist on our servers, so… it would be pointless to have it.”
Is there an alternative module? I am trying to find out if my host server has more information available as to what module is available.
Rob
February 27th, 2008 at 9:29 am
9If your virtual host doesn’t support mod_expires I’d suggest sticking with a Wordpress plug-in. That is the only problem with virtual hosting. It’s worth finding out what’s included in the package beforehand.
My host allows me to pick modules and install them for use on my site. flexibility for those who want/need extra customisation.
moserw
April 12th, 2008 at 2:40 am
10Thanks for sharing. Its really useful if it works long time. Have tried caching before but kept getting 500 error. Hope this works. Much appreciated.
Rob
April 14th, 2008 at 9:07 am
11I hope it works for you Moserw, it does really depend on how your server is setup. But it is definitely worth trying it out.
moserw
April 15th, 2008 at 5:28 pm
12Rob,
Have started using it and have not experienced any problems till now. Will keep you in the loop if any trouble turns up. Thanks for replying, much appreciated.
moserw
Rob
April 16th, 2008 at 8:30 am
13Excellent moserw, I’m glad it works for you. I think most decent Apache hosted packages are capable of using this technique. It’s just some hosts seem to disable some of these types of functions.
Faragon
April 17th, 2008 at 7:19 pm
14Awesome post Rob,
Thanks for sharing this information. Very soon I will try to implement this technique.
When the “expire” time period is set, do you necessarily need to change the name of the files once you update them or is there a way for it to do a date check and update automatically?
thanks.
Rob
April 18th, 2008 at 8:19 am
15Thanks Faragon,
As far as I know there isn’t a way to automatically check the date of the file updates.
The reason you change the file name, is that if a user visits your site they will continue to get an out-dated JavaScript or CSS file because the filename is the same and the server wont download a new version.
By using incremental number changes, this forces the server to download any updated files, if the version in the users cache is older.
I know it can be a bit annoying having to make such sitewide changes, but using Find/Replace in a text editor or Dreamweaver enables you to do it fairly quickly.
A little tip for testing changes on your own PC. Hold down Shift and press the Refresh icon on your browser and the browser will request all the files from the server again rather than caching any.
Compress jQuery Even Further | brightscape.net
June 2nd, 2008 at 11:15 am
16[...] One recommendation when using caching for extended periods (I.e. longer than a few hours), it is recommended to use numbered file names. In the case of jQuery, it is automatically given a unique file name when downloaded. It is recommended to maintain this structure for when the script is upgraded on the site. Please read our previous article for more detailed instructions on .htaccess/page caching. [...]
RSS feed for comments on this post · TrackBack URI
Leave a reply
Categories
Popular Posts
Links
Meta
Powered by WordPress | BloggingPro | Design Disease