views:

92

answers:

5

Hi everybody,

I have had some thoughts recently on how to handle shared javascript and css files across a web application.

In a current web application that I am working on, I got quite a large number of different javascripts and css files that are placed in an folder on the server. Some of the files are reused, while others are not.

In a production site, it's quite stupid to have a high number of HTTP requests and many kilobytes of unnecessary javascript and redundant css being loaded. The solution to that is of course to create one big bundled file per page that only contains the necessary information, which then is minimized and sent compressed (GZIP) to the client.

There's no worries to create a bundle of javascript files and minimize them manually if you were going to do it once, but since the app is continuously maintained and things do change and develop, it quite soon becomes a headache to do this manually while pushing out new updates that features changes to javascripts and/or css files to production.

What's a good approach to handle this? How do you handle this in your application?

A: 

What you are talking about is called minification.

There are many libraries and helpers for different platforms and languages to help with this. As you did not post what you are using, I can't really point you towards something more relevant to yourself.

Here is one project on google code - minify.

Here is an example of a .NET Http handler that does all of this on the fly.

Oded
Well, I am aware of minification and doing it manually with tools, but what I am looking for is an approach to make it more automatic to ease pushing out updates to production
Industrial
@Industrial - that's also what I am talking about.
Oded
Why the downvote?
Oded
Hi Oded, did visit your link. Great resource. Thanks a lot! I have actually made an upvote to your post :)
Industrial
Tried it out today. Can't believe I haven't found minify earlier! Unbelievable handy feature. Thanks a lot Oded :)
Industrial
+1  A: 

CSS files can be categorized and partitioned to logical parts (like common, print, vs.) and then you can use CSS's import feature to successfully load the CSS files. Reusing of these small files also makes it possible to use client side caching. When it comes to Javascript, i think you can solve this problem at server side, multiple script files added to the page, you can also dynamically generate the script file server side but for client side caching to work, these parts should have different and static addresses.

Deniz Acay
+1  A: 

I am dealing with the exact same issue on a site I am launching.

I recently found out about a project named SquishIt (see on GitHub). It is built for the Asp.net framework. If you aren't using asp.net, you can still learn about the principles behind what he's doing here.

SquishIt allows you to create named "bundles" of files and then to render those combined and minified file bundles throughout the site.

jessegavin
+1  A: 

I wrote an ASP.NET handler some time ago that combines, compresses/minifies, gzips, and caches the raw CSS and Javascript source code files on demand. To bring in three CSS files, for example, it would look like this in the markup...

<link rel="stylesheet" type="text/css"
      href="/getcss.axd?files=main;theme2;contact" />

The getcss.axd handler reads in the query string and determines which files it needs to read in and minify (in this case, it would look for files called main.css, theme2.css, and contact.css). When it's done reading in the file and compressing it, it stores the big minified string in server-side cache (RAM) for a few hours. It always looks in cache first so that on subsequent requests it does not have to re-compress.

I love this solution because...

  • It reduces the number of requests as much as possible
  • No additional steps are required for deployment
  • It is very easy to maintain

Only down-side is that all the style/script code will eventually be stored within server memory. But RAM is so cheap nowadays that it is not as big of a deal as it used to be.

Also, one thing worth mentioning, make sure that the query string is not succeptible to any harmful path manipulation (only allow A-Z and 0-9).

Josh Stodola
+2  A: 

I built a library, Combres, that does exactly that, i.e. minify, combine etc. It also automatically detects changes to both local and remote JS/CSS files and push the latest to the browser. It's free & open-source. Check this article out for an introduction to Combres.

Buu Nguyen
Hi Buu, thanks a lot for your answer. I will check out your link to see if it fits my needs!
Industrial
I've been looking around and so far Combres has the best configuration and setup that I've seen. I have yet to use it in a production project but it seems great.
Mr Rogers
Thanks, Rogers. I'm glad you like it.
Buu Nguyen