Smarter Javascript for Java Web Applications With JAWR
One thing that has always annoyed me a little about javascript is the burden induced by modularity. Sure, you can keep your javascript in many small, testable files… but good grief it’s annoying when you have to put 10 or 15 script tags in the head of your document and thereby inducing much overhead on the client side. I mean, first you have a good chunk of markup taking up bandwidth and then you have the overhead of each http request for each javascript file. Once you take into consideration that some browsers only allow two http connections at a time things start to slip. Jamming everything in one file is annoying too, and always feels sloppy.
Recently I’ve discovered jawr, and it’s alieviated all of my javascript modularization problems. Jawr is cool in that it allows me to define “modules” that consist of many javascript files… I can either list the files I need, in the order they should be included, point to a directory to include all scripts in it, or even use some pattern matching. The simply define a mapping for the module to serve up the javascript (i.e. /js/all-the-scripts-i-need.js) and jawr goes to work, stitching the javascript together and serving it up… minimized and gzipped. I’ve only began delving into it, but so far it’s been great. On a single page on our site we had about 60KB of js, 30KB of it jQuery. After defining a module, the resulting file served to the client side was a mere 22Kb. Similarly it also can do the same for CSS, which slimmed our 8 css files from a total of 28KB to a single 5Kb file.
The idea is pretty sweet… keep a collection of javascript objects/functions with appropriate namespaces, fit together into modules for each place they are needed. Inside of each of those modules though will probably be our usual “binder” functions… you know, the usual “hunt down and find the DOM elements I need to bind events to” type behavior.
My only annoyance so far? Sometimes the minification process exposes javascript that, shall we say, does not minify well thanks to it’s layout. In my case, there was a javascript file with a multi-line string used for templating… my deployment just outright failed because of a “unterminated string literal” and took a bit of mucking around to find exactly what file it was that it hated. Other than that, it’s well worth the look.
If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!








March 4th, 2008 at 12:20 pm
Hi James,
I’m glad you found Jawr useful. Thanks a lot for posting about it.
I’m working on the next version and I’ll try to make it easier to spot these nasty ‘unterminated string literal’ exceptions. These are actually thrown by JSMin. If you have the problem again, a good way to spot the failure is to use JSlint on your scripts. But as I said, I’m about to (try to) make it friendlier for this case.
April 5th, 2008 at 4:38 pm
Hi James,
A new version (2.0) of Jawr is available with some great new features. JSmin errors are now much easier to track and solve. Cheers!
April 5th, 2008 at 5:08 pm
Cool! I’ll write a review once I get a chance to check it out.