by on

by on

One of my favourite solutions to serve static files is on Heroku, with Node.js: lightweight, easy to setup, easy to configure and highly scalable – awesome features, these.

However, when you serve static files, the users’ internet provider sometimes changes those. An example is when the carrier compresses the javascript documents or resizes the images to provide faster downloads. This will result a mismatch in file hashes, which, with some javascript frameworks like the Sencha touch SDK, leads to fatal app breakdowns.

An easy method to avoid this is to gzip all data ahead. Fortunately, Node.js has a built-in zlib module after v0.6.13, so writing a static http server with gzip support is a breeze. You can get my code from github: it comes as a Heroku project, so if you don’t want to configure anything, just replace the files in the ./app folder and you are all set to go.

To check that your setup works, use the following commands:

$ curl http://nodejs-static-http-with-gzip.herokuapp.com/ --silent --write-out "%{size_download}\n" --output /dev/null

$ curl http://nodejs-static-http-with-gzip.herokuapp.com/ --silent -H "Accept-Encoding: gzip,deflate" --write-out "%{size_download}\n" --output /dev/null

…where the resulting number is the size of the downloaded file – if you get a smaller number running the second command, it means that the server is sending gzipped files for a browser that supports them.

(Remember that ngix, perhaps the most popular fronting server for Node.js already supports gzip – therefore you only need this trick on Heroku and similar environments.)

Did you enjoy this article? Share it with your followers!
Tagged with: ,
Posted in awesome resources
NEWS! LETTER!
Email Marketing Powered by MailChimp
Infrequent e-mails about code updates, new free resources and every few months, a recap of the latest blog posts.