Link Details

Link 20712 thumbnail
User 209881 avatar

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.
  • 10
  • 8
  • 4508
  • 1691

Comments

Add your comment
User 111696 avatar

bloid replied ago:

1 votes Vote down Vote up Reply

This sends a cold shiver down my spine...

User 81385 avatar

em116845 replied ago:

1 votes Vote down Vote up Reply

Such a ugly hack to save 100 bytes. Why am I not surprised it's a .Net programmer?

User 209881 avatar

pcx99 replied ago:

0 votes Vote down Vote up Reply

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.

User 111696 avatar

bloid replied ago:

2 votes Vote down Vote up Reply

But why not just stick everything in <style> and <script> tags then?

And save yourself another network connection?

User 211189 avatar

rasman replied ago:

1 votes Vote down Vote up Reply

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.

User 209881 avatar

pcx99 replied ago:

0 votes Vote down Vote up Reply

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.

User 111696 avatar

bloid replied ago:

0 votes Vote down Vote up Reply

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

User 209881 avatar

pcx99 replied ago:

0 votes Vote down Vote up Reply

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!

User 111696 avatar

bloid replied ago:

0 votes Vote down Vote up Reply

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 ;-)

User 226984 avatar

hates.myopenid.com replied ago:

2 votes Vote down Vote up Reply

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.

User 209881 avatar

pcx99 replied ago:

0 votes Vote down Vote up Reply

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 ;)

User 208620 avatar

itchyrich replied ago:

0 votes Vote down Vote up Reply

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)

User 209881 avatar

pcx99 replied ago:

0 votes Vote down Vote up Reply

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.

User 107114 avatar

daniel replied ago:

0 votes Vote down Vote up Reply

Voted up because there's some good conversation going.

Add your comment


Html tags not supported. Reply is editable for 5 minutes. Use [code lang="java|ruby|sql|css|xml"][/code] to post code snippets.