By pcx99
via blogs.msdn.com
Published: May 02 2007 / 02:22
In this post, we'll look at a technique to combine CSS with JS and get the resource download count to one! I discovered this technique while desperately trying to improve the page load time for the pages in Microsoft Office Live.
Tweet
SaveShareSend
Tags: css-html, javascript, web design
Comments
bloid replied ago:
This sends a cold shiver down my spine...
em116845 replied ago:
Such a ugly hack to save 100 bytes. Why am I not surprised it's a .Net programmer?
pcx99 replied ago:
It's not a hack to save 100 bytes, it's a hack to save 1 network connection durring page load (and the retransmitting of all browser cookies for that domain and the renegotiation of headers between browser and server).
It's unfortunate this isn't going to frontpage, it's one of the most innovative techniques I've yet seen to help streamline page-loading.
bloid replied ago:
But why not just stick everything in <style> and <script> tags then?
And save yourself another network connection?
rasman replied ago:
Good point, but I assume that these cssjs beasts could be used on multiple pages, and thus only downloaded once by the browser.
I'm with you on the "that's disgusting!" side of the line, though.
pcx99 replied ago:
Because you can't cache that for future page loads? :)
Cache is still the most efficient tool in page optimizations. I think that's why I found this article so fascinating because it ties directly into that. It not only saves that 1 network connection during page load, it does it for every page that reuses that css and jscript.
bloid replied ago:
But surely once the CSS and JS is loaded (in seperate files) they will be cached?
I would have thought that intelligent caching, and correctly set headers would be a better solution than crushing code and layout into one less maintainable file
pcx99 replied ago:
There's still a negotiation between client and server to verify freshness (assuming the most common cache settings, for firefox, IE is more aggressive much to ajax programmer's consternation sometimes).
But this is all part of min/maxing the page. It's why some people pass through js through jsmin (see making page less maintainable) in addition to gzipping (also more work to maintain if you go static) and why there's been an interest in server-side concatenation of jscript files http://ajaxian.com/archives/jscsscomp-javascript-and-css-files-compressor
By itself the technique on this page isn't useful but when aggressively combined with other connection saving methods and page optimizations it CAN make a difference!
bloid replied ago:
But passing it through JSMin and GZip still leaves you with the original script for development...
Hehe...whatever... I'll never do this, but I'm sure others will find great joy in doing so ;-)
hates.myopenid.com replied ago:
I could understand the usefulness if we were still all surfing the net on dialup, but with broadband so widely prevalent it just seems like overkill to me. Not to mention messing up the cleanliness point of separate CSS and Javascript files.
pcx99 replied ago:
It's actually a bit more than that. For a high-traffic site this is great because those additional network connections do add up. But it's more than that -- with EVERY single http request there's a negotiation that takes place between client and server and yes on broadband that is quick but all those connections add up which is why so many people look to reduce the number of network connections.
Finally, if a site is storing a large amount of data in cookies then EVERY SINGLE NETWORK REQUEST will retransmit those cookies and broadband is usually asynchronous ;)
itchyrich replied ago:
The problem is that it's a hack. It works by accident not by design. Your pages may all have to be re-coded when Firefox 3 or IE 8 comes out.... (although to be fair IE 8 will probably just be IE 7 with an extra toolbar, so you're fine, hack away)
pcx99 replied ago:
Actually the comment behavior is described by the official specifications and so they won't be changing. Granted it's a hack based on fortuitous happenstance but the conditions which allow the hack to exist will not be changing.
daniel replied ago:
Voted up because there's some good conversation going.
Voters For This Link (10)
Voters Against This Link (8)