Quality Reads

Saturday, November 11, 2006

Optimizing Page Loading in ColdFusion

I was reading this posting by Aaron Hopkins (a Google Engineer) about optimizing page loading when I started to think about my experimentation with different AJAX implementations. Every time I tried a new implementation, I'd end up with a hodge podge of javascript files on my demo website (some from the previous framework, some from the new). After reading that post, I've come to realize that even though it may be nicer to link to each javascript file individually, this behavior can negatively affect load time. This is due in large part to modern browsers limitations to only allow two outstanding HTTP Requests at any time.
Personally, I still like keeping things organized but I don't want to sacrifice performance for it. So, I've started using the following approach for all my sites:
  • Create two ColdFusion pages (or PHP, Ruby, etc) that change the mime type to "text/js" and "text/css" respectively.

  • Then include all the appropriate js or css files using <cfinclude>.

/* js_compilation.cfm*/
<cfcontent type="text/js">
<cfinclude template="js/core.js">
<cfinclude template="js/prototype.js">
<cfinclude templat="js/rico.js">
  • Lastly, you should simply reference that file in the Head section of the calling page.

<script src="js_compilation.cfm" type="text/js">
Do the same thing for all your assorted CSS files and you're load time should be significantly improved without sacrificing organization. You can keep your file structure simple and efficient.
Cheers,
Todd

PS-I know everyone should be crunching their JS files into one library but we can't all afford to do that. This is a simple step that everyone can handle.

4 comments:

Anonymous said...

hi todd,
i'd like to use your solution to organize our .js-files but it has the drawback that the javascript isn't cached anymore (HTTP/1.1 304 Not Modified) did you find a solution for this problem?

Anonymous said...

nevermind, found a nice solution, working on the network layer:

&#c60;cfheader name="expires" value="now()+43200"&#c62;
&#c60;cfheader name="Cache-Control" value="max-age=43200"&#c62;

gets the js cached for ´10h (one workday) later on we gonna modify our build-process using a version number coded into the url, and set expires to "a couple if years", like its explained here:

http://www.die.net/musings/page_load_time/

cheers jasper

Todd Cullen said...

Good call with the expire header. That's definitely the way to handle it.

All the best with your Ajax work.

-Todd

Jason said...

I know this is old, but I just thought I'd let you know that it came in handy four years later. :)