Nearly all the big development news on the web over the past few years have been related to the rich experience and interaction users are coming to expect from the web sites. Web sites are no longer just flat presentation brochures that can be paged through like magazines, they are expected to flow and mold with users each click and do so without tremendous page reloads.
AJAX technology has made that possible through dozens of AJAX javascript frameworks. But those frameworks, handling everything from special interface effects to site functionality, come at a hefty price. While there are highly optimized javascript toolkits out there, it is not unusual for people to use several large libraries in their development. Without optimization that means downloading a few hundred KB each time you load the site or reload the page. Maybe not as painful on a desktop with broadband, but imagine a slower or mobile phone/pc connection. Not good news. However, if you are on a server running Apache, you are in luck!
Enter mod_expires
mod_expires is a module that can be built into Apache web server to control the caching (read: expiration) of items downloaded from the site. It is both compiled and enabled in Redhat Enterprise Linux package of Apache and all you have to do is enable it through .htaccess. Here is mine:
<IfModule mod_expires.c> ExpiresActive On ExpiresByType application/x-javascript “access plus 1 month” </IfModule>
That is all there is to it but let’s look closely at this because it is more powerful than it appears. First and last line are there just for the sake of error control, the <IfModule>..</IfModule> checks if mod_expires is available and if it is the code gets executed. This prevents your web application from crashing if it doesn’t have the required module available. The ExpiresByType is very powerful. In my example I am telling the browser to cache all javascript files it downloads for 1 month after the access.
This mechanism is very powerful even in applications beyond AJAX. For example, let’s say you design your site with the layout images in the gif format. Your template, things like bullets, borders, header images, icon art and more are all saved in the gif format. Since those are loaded each time a visitor accesses your site, caching template images can significantly decrease the load time and display your content faster. How is that for a benefit. Publish template files as .gif or .png but publish content inline as a .jpg so dynamic content loads on demand while the .gif or .png are cached because they rarely change.
Does it work?
Quick way to check if the mod expires works is to look at the headers. I use wget:
wget -S -O – http://support.ownwebnow.com/includes/checkform.js | more
Obviously replace the URL with your own. You will get something similar to the following:
HTTP request sent, awaiting response… HTTP/1.1 200 OK Date: Tue, 04 Dec 2007 04:34:34 GMT Server: Apache/2.0.52 (CentOS) Last-Modified: Sun, 08 Jul 2007 19:19:48 GMT ETag: “b8c21a-304a-6960dd00″ Accept-Ranges: bytes Content-Length: 12362 Cache-Control: max-age=2592000 Expires: Thu, 03 Jan 2008 04:34:34 GMT Connection: close Content-Type: application/x-javascript Length: 12,362 (12K) [application/x-javascript]
Notice the expiration date: one month in the future. If you don’t see the Expires in the http headers something went wrong.
Handling code revisions
Obviously this works great for caching code that is not modified very often, such as your base application libraries. For example, cache the entire framework but do not cache your specific includes and extensions that you may actively modify. Here is an easy hack to get around that, just place your static code (that doesn’t change often) in a different directory.
<LocationMatch /inc> ExpiresActive on ExpiresByType application/x-javascript “access plus 1 month” </LocationMatch>
While my original code will handle the expirations server-wide just by matching the content type (javascript), LocationMatch adds in an extra restriction so that only javascript from a particular directory (/inc) gets cached for 1 month.
In the worst case scenario, your library might need to be updated immediately. It happens. Security patches, urgent code changes, meeting deadlines and other external factors might force you to deploy code before the other libraries have expired. Not to worry, I have a trick for that as well. I keep my includes in a set of include files. If I ever need to roll a new file I just remove the old one and place the new one in with a new filename in a different directory. Makes change management a nightmare, but I have yet to be forced to use it and I don’t forsee a day that I will but its always best to be prepared.
There you go, a little bit of server content expiration trickery to decrease your bandwidth costs and your application load times.
|
|
|
Whats on Vlad's Mind?
|
|
|
|
|
Sponsors: This blog is made possible by
Own Web Now Corp and ExchangeDefender.
If you like this blog and are in the need of products we offer I hope you give us some
consideration.
|
|
|
|
|
|
Get The Newsletter
|
Looking for a more focused, exclusive insight into the world of SMB tech & business? Sign up for my newsletter:
Click here to sign up
|
|
|
|
|
Vladfire Vlog
|
Vladfire is my video blog showcasing successful people and technology in small to medium business.
Below are a few recent episodes, check out the archive for all other films.
|

See more episodes...
|
|
|
SBS Show Podcast
|
SBS Show is a free weekly podcast (Internet for recorded radio show) focusing on small business and technology. More at sbsshow.com but check out our latest episode:
SBS Show #26
Erick Simpson
Managed Services Part 2

Listen to older shows..
|
|
|
|
| |
|
|
Categories
|
|
Archives
|
|
About
|
| Apple, Awesome, Beta, Blogroll, Boss, Cloud, Deals, E12, Events, Exchange, ExchangeDefender, Friends, Gadgets, Gators, Gaypile, Google, GTD, iPhone, IT Business, IT Culture, Legal, Linux, Microsoft, Misc, Mobility, Open Source, OS, OwnWebNow, Pimpin, Podcast, Programming, Rant, SBS Show, Security, Shockey Monkey, SMB, System Admin, Thieving Weasel, Uncategorized, Vista, Vladcast, Vladfire, Vladville, Web 2.0, Windows Home Server, WordPress, Work Ethic, Wrong |
 |
February 2012,
January 2012,
December 2011,
November 2011,
October 2011,
September 2011,
August 2011,
July 2011,
June 2011,
May 2011,
April 2011,
March 2011,
February 2011,
January 2011,
December 2010,
November 2010,
October 2010,
September 2010,
August 2010,
July 2010,
June 2010,
May 2010,
April 2010,
March 2010,
February 2010,
January 2010,
December 2009,
November 2009,
October 2009,
September 2009,
August 2009,
July 2009,
June 2009,
May 2009,
April 2009,
March 2009,
February 2009,
January 2009,
December 2008,
November 2008,
October 2008,
September 2008,
August 2008,
July 2008,
June 2008,
May 2008,
April 2008,
March 2008,
February 2008,
January 2008,
December 2007,
November 2007,
October 2007,
September 2007,
August 2007,
July 2007,
June 2007,
May 2007,
April 2007,
March 2007,
February 2007,
January 2007,
December 2006,
November 2006,
October 2006,
September 2006,
August 2006,
July 2006,
June 2006,
May 2006,
April 2006,
March 2006,
February 2006,
January 2006,
December 2005,
November 2005,
October 2005,
September 2005,
August 2005,
July 2005,
|
 |
Vlad says:
Thanks for checking out my blog. You've officially reached the end of the Internet so take in what you've read and don't look at it as gospel but an invitation to start thinking for yourself.
|
|
|
|
| |
Copyright © 2005-2010 Vlad Media, Inc. All Rights Reserved.
Content is provided AS-IS without warranty of any kind.
Syndicate this blog: 
|
|