Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

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?

share|improve this question
add comment

migrated from webapps.stackexchange.com Aug 25 '11 at 19:12

This question came from our site for power users of web applications.

1 Answer

up vote 8 down vote accepted

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.

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.