Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is better: public javascript CDN or self-hosted combined javascript? [closed]

My web page uses several javascript libraries, like jquery, jquery-ui, underscore, backbone, and a few other well-known jquery plugins.

Now I face a dilemma, I could:

  1. use a public CDN that hosts all of these libraries. And if the user has visited another site using the same CDN, the scripts may have been cached. But since each library is separate, the page need to include many script tags, hence many http requests.

  2. Combine all the required scripts into one (including my own script files) and host it on my own server. Only one request for everything, but I couldn't serve the script faster than CDN, and I cannot take advantage of the cache.

From your own experience, which is better?

And I want to estimate whether enough websites use the mentioned CDN (if many well-known sites use them, then the probability of scripts being cached is high), how can I investigate? I tried googling the script src attribute, but it seems google doesn't index that.

like image 520
NeoWang Avatar asked Apr 07 '14 11:04

NeoWang


People also ask

Is it better to download or use CDN?

Because of its very core and design, CDNs can deliver content much speedier to any user and on any device. Whether a tablet or a laptop, your web content gets to each of your users much quickly. This can mean more money for your business! This is perhaps one of the best advantages a website owner gets from CDNs.

Is it good practice to use CDN?

It is still beneficial to use a CDN, and it is still for your users' benefit, but the benefit is in reduced latency with geo-located servers, not in general performance due to a hot cache hit.

Is it better to use CDN jQuery?

If you know what you want to use, use a local copy of jQuery, so you are not dependent on a third party. If you try out things with several libraries, use the CDN, because it's quicker and once you're done, use local copies for the reason mention before.

What is a CDN Javascript?

A content delivery network (CDN) refers to a geographically distributed group of servers which work together to provide fast delivery of Internet content. A CDN allows for the quick transfer of assets needed for loading Internet content including HTML pages, javascript files, stylesheets, images, and videos.


1 Answers

If using only 1-2 libraries, a good practice is too include the CDN version, with a fallback to local version. This is the practice that most HTML boilerplates/frameworks use .e.g .

http://html5boilerplate.com/

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="//myserver/js/libs/jquery-1.8.3.min.js"><\/script>')</script>

However,you are using a lot of libraries, so there might be dependencies, and specific ordering of them on the page, and some of them might not be present on standard CDNs,so you will have to anyway include some of your javascripts from your server. Hence for your case, a better bet would be to use good combining/versioning of all your javascript code into 1-2 files .e.g one for libraries(usually not touched), and one for your own custom code(cache busted when you make changes to it).

like image 146
DhruvPathak Avatar answered Sep 28 '22 06:09

DhruvPathak