0

I have several scripts on my website. Would it make sense to group them all into one single file? Or is it preferable to keep them separate?

Greg
  • 3,025
  • 13
  • 58
  • 106

2 Answers2

5

For production I would always group them into one file and compress and/or minify them. Browser can only (used to?) make 2 requests at the same time to the same domain.

If it is a onetime action you can use this website to minify the files. Other wise you would have to look into automating the process.

But please also keep your original uncompressed files for if you want to make changes at some point.

Mr. Alien
  • 153,751
  • 34
  • 298
  • 278
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
  • Depends on what type of javascript is being included. CDN resources should be separate to take advantage of browser caching. – zzzzBov Jul 28 '12 at 14:28
  • @zzzzBov I figured OP was talking about local files, but yeah. CDN files ofc shouldn't be added to the file otherwise it wouldn't be a CDN anymore. – PeeHaa Jul 28 '12 at 14:30
  • 1
    2 [connections per domain](http://stackoverflow.com/q/985431/1048572) seems a bit too low. Current browsers can handle more. – Bergi Jul 28 '12 at 14:36
  • The actual number of simultaneous requests is typically actually 4. I'm not sure about webkit browsers, but in Firefox, this value is configurable in `about:config` under `network.http.pipelining.maxrequests` (and in IE it was configurable through a registry edit). This value hasn't changed since I first learned about it more than 10 years ago. Note that this is not the same as `persistent-connections`, which has more to do I think with `keepalive` than anything else. – Andrew Jul 28 '12 at 14:54
  • Nobody (almost) changes the config of their browser. And the spec also states 2 connections. And if you learned about it 10 years ago it still was 2 (IE7 was still something to keep in mind and that was max 2 by default AFAIK). Thanks for further info though. – PeeHaa Jul 28 '12 at 14:59
2

For production best:

  • load all frameworks minified from google CDN
  • collect all script for site in one file minify/compress it.
  • if minified file larger 256kB and some scripts use not on all pages strip until it will be 2 or 3
  • if script files more then 3 it is bad (most browsers load it step by step and block page for user)
  • post all scripts at bottom of html code

For fallback frameworks load (google can be down too)

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">window.jQuery || document.write('<script type="text/javascript" src="js/jquery.min.js"><\/script>')</script>
b1_
  • 2,069
  • 1
  • 27
  • 39