Thursday, October 21, 2010

Concatenating, Minifying and Optimizing JavaScript

Doing this is as important as making sure your code compiles :). I've been reading several articles on how to do this, and they all present different approaches, this is what worked for me:

1) Concatenate the .js files: I use the copy command; however, it adds some extra characters that later need to be removed for YUI Compressor to work. It does the trick for now though.

2) Compress the files: YUI Compressor is the best choice. For it to work correctly I had to use the
--charset flag.

3) Set the content expiration date (using IIS). In my case I set it to expire after 20 days.

4) Finally, enable gzip compression. At first I thought I would need to gzip the files, but it turns out that II5 does this for you. Setting it up is not really straight-forward, check out the section Scenario 1 of this article for IIS 5.0 instructions.

No comments: