CDN vs. join JS / CSS

7

Among the performance recommendations of a web system are:

  • Use CDN (in the case of jQuery, Bootstrap, etc.)
  • Add JS and CSS to decrease the number of requests

It turns out that these two rules go against each other. If I use jQuery and Bootstrap CDN, for example, I'll be increasing the number of requests. On the other hand, if I join jQuery and Bootstrap in a single JS and CSS, I will be decreasing the number of requests, but I will not be using CDN.

In this way I would like to know in which situations each of the two alternatives gives me the best performance.

    
asked by anonymous 12.06.2014 / 18:24

2 answers

6
  

If I use jQuery and Bootstrap CDN, for example, I'll be increasing the number of requests

Not really.

First, the request will only be made once and then the browser will cache the script.

Second, if the user has already accessed another site that uses the same CDN and jQuery version, there will be no requests.

In general, it's worth adding just the dozens of jQuery plugins you use, plus the application's own scripts or website.

But consider that there is no definitive answer that will be best for all cases. It would be more a matter of probability.

    
12.06.2014 / 18:34
7

The solution depends on your scenario.

Be guided by the amount of files your application uses. If your pages use many files (ie many requests are made), it tends to be more advantageous to keep bundling (join the files). Otherwise it will be more advantageous to use only one CDN.

But nothing prevents you from combining the solutions. It is very common to use CDN for external libraries (like jQuery and Bootstrap) by "unpacking" your server and taking advantage of browser caching and bundling for your own internal files so to speak).

    
12.06.2014 / 18:37