I know this sounds like a silly question, but hear me out. These days, there are a lot of 'media aggregator' tools that will take your globs of javascript (laden with coffee and underscores and backbones and who knows what else) and string them together and rewrite them and munge them and uglify and minify them them and tape them together so that clients only have to make one request to get all the javascript or style sheets for a site.

This is pretty cool. I like keeping my java(coffee)script files small and separate, and I like my clients making only one request.

But if we go through all that trouble to make our javascript really small and stuff, why not just send it WITH the first request? Why not just have tools that do the minification and aggregation and then write it directly to the html?

Common sense tells me that this would be bad, but I'm not sure why. Can anyone explain?


Because it's in the browser's cache when the next page on that domain is accessed and does not need to be re-loaded from the web server.

It saves bytes when you access more than one page on the server that share the same javascript and css files.

  • Oh of course. I feel silly now. Although I guess that that's also a reason not to completely combine all your JS / CSS into one request per page if there is a lot of shared data between pages, so as to use the cache as much as possible.
    – So8res
    Aug 25 '11 at 15:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy