In Rails 3.1 there's the option to enable HTTP streaming so that your page can be brought down in chunks. In the Railscast on this feature, Ryan recommended that it would be a good idea to enable this so that your CSS and JavaScript can be pulled down while the rest of your page is still being rendered.
I've always followed the guideline that scripts should be at the bottom of the page after all the page content is loaded so that it would reduce perceived loading time, but by doing this it won't be taking advantage of the HTTP streaming.
What do you think is the best practice now?
I think this is an excellent question; one for which I felt compelled to Google for an answer.
The argument for putting script assets at the bottom of the page was to prevent blocking a browser's renderer that could otherwise be painting pixels on the screen to keep the user busy while the rest of the page's functionality loaded. With HTTP streaming, we're talking about being able to do something about the server being the bottleneck. While we wait for all those expensive database queries and backend service calls to finish, we can load JS/CSS assets.
It seems to me that there's a tipping point around which you should <head> your assets or not <head> your assets. This is only a net performance gain if your JS/CSS assets can be downloaded before your server has the rest of the response ready.
Don't <head> a page's assets if:
Do <head> a page's assets if:
Does that sound about right?
For the general case, I think unfortunately they are still going to go to the bottom. Reason is Safari for Mac buffers 1024 bytes before it starts to issue requests for assets (and Safari for iPhone and iPad buffer 512 bytes).
Since the head of a document is typically smaller, Safari users would still get the ordinary bad experience.
Firefox, Opera, and IE8 do not buffer, and Chrome buffers 252 bytes, according to some test I've done together with Hongli Lai.
I am still doing research about this, but as of this writing this is my conclusion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With