Tuesday, April 12, 2011

Experience with GZIP compression of GWT JavaScript files

As your GWT module code is growing, the compiled JavaScript file is growing also. One of the common practises is Code Splitting, which splits GWT compiled code into smaller javascript files. Another option is to compress JavaScript using gzip compression. This post deals with my experience about gzip compression using Apache Tomcat 6.
The size of my GWT modules grow up to few megabytes. Not a large size at all, but could be a damn problem with downloading on slower network links. So download time could take few minutes. That's no good.
One of the Tomcat feature is gzip response encoding. By the Tomcat 6 reference documentation this could be done by adding compression parameter into server.xml. Pay attention on note paragraph; failing which you could be surprised, why files above 49 kB will not be compressed. There is another important attribute, compressableMimeType,which I also used and add text/javascript MIME type. And that's enought about configuration. All you have to do is restart Tomcat and clear browser cache.
And what was the result? Gzip compression reduced JavaScript file size from 2,515 KB to 236 kB! Besides code splitting, gzip compression is another way to reduce size of GWT compiled files. Problem could be server CPU load due to on the fly compression of files during many user requests.