Serving gzip compressed Scriptaculous and Prototype with ColdFusion
I love Scriptaculous, and mixing it with ColdFusion to create some very nice Rich Internet Applications. But at nearly 300KB, loading Prototype+Scriptaculous is kinda heavy. The workaround for that is shrinking the javascript libraries.
There are a few utilities around the net that will 'compact' or 'shrink' javascript. Variables are renamed, and all white space is removed - including comments and credits. Not too cool.
But here I'll show you how to take it a step further and serve javascript compressed with coldFusion. I am able to get Prototype v1601 and Scriptaculous v181 download size down from 288KB to 42KB - with credits intact. ColdFusion 8 is not required - I believe CF5 or better will do (maybe even earlier!). This is a great way to serve gzip compressed content when not being able to control the web server and you don't have CF8.
Here is how to do it with ColdFusion. For this demonstration, the files are named:
<scriptIt's important that your charset is ISO-8859-1.
src="/includes/js/scriptaculous/prototype1601scriptaculous181.cfm"
type="text/javascript"
charset="ISO-8859-1"/>
This is the contents of prototype1601scriptaculous181.cfm:
<cfif cgi.HTTP_ACCEPT_ENCODING contains "gzip">
<cfheader name="Content-Encoding" value="gzip">
<cfcontent
type="application/x-javascript"
deletefile="no"
file="#expandpath('./prototype1601scriptaculous181.gz')#">
<cfelse>
<cfinclude template="prototype1601scriptaculous181.js">
</cfif>
This beauty first checks to make sure the browser can handle gzip.If not, it sends the uncompressed js data.
Since you read through all of this you deserve a reward:
prototype1601scriptaculous181.zip
This zip has all three files, ready to go.
In my free time (HA) I'll create and post PHP, ASP, Ruby, etc version as well. Or if someone else would like to do it I'd gladly post them here.
Other Resources:
http://compressorrater.thruhere.net/
http://shrinksafe.dojotoolkit.org/
http://www.bananascript.com/?home
http://blog.mohanjith.net/2008/03/recipe-for-compressed-scriptaculous.html
http://cfspaghetti.blogspot.com/2007/07/how-to-gzip-compress-coldfusion-8.html
![Comments [image]](http://mowser.com/img?url=http%3A%2F%2Fwww.webveteran.com%2Fblog%2Fimages%2Fcomment.gif)
![Print [image]](http://mowser.com/img?url=http%3A%2F%2Fwww.webveteran.com%2Fblog%2Fimages%2Fprinter.gif)
![Send [image]](http://mowser.com/img?url=http%3A%2F%2Fwww.webveteran.com%2Fblog%2Fimages%2Femail.gif)
![Download [image]](http://mowser.com/img?url=http%3A%2F%2Fwww.webveteran.com%2Fblog%2Fimages%2Fdisk.png)
![Digg It! [image]](http://mowser.com/img?url=http%3A%2F%2Fwww.webveteran.com%2Fblog%2Fimages%2Fdigg.gif)
![Linking Blogs [image]](http://mowser.com/img?url=http%3A%2F%2Fwww.webveteran.com%2Fblog%2Fimages%2Ftechnorati.gif)
![[image]](http://mowser.com/img?url=http%3A%2F%2Fwww.webveteran.com%2Fblog%2Fimages%2Frssbutton.gif)
There are no comments for this entry.
[Add Comment]