Smile
Smile

Reputation: 2758

Is it better to reference jQuery file include through jQuery CDN?

Is it better to reference jQuery file inclusion through jQuery CDN for better performance?

Like

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" ></script>

Or

Is it better to reference stored jQuery file in our project?

Like

<script type="text/javascript" src="/js/jQuery.min.js"></script>

As jQuery CDN link handles caching, Does it improve performance more than jQuery file included from our project?

Upvotes: 3

Views: 3780

Answers (3)

l2aelba
l2aelba

Reputation: 22167

Do like this with a fallback

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

CDN first, if not (or blocked) ? so bring it from your server

Why should I use Google's CDN for jQuery? (For me, my point is about Cache Hit)

Upvotes: 7

PingOfDeath
PingOfDeath

Reputation: 137

First variant. Because google has best world-wide servers.

  • Decreased Latency
  • Increased parallelism
  • Better caching

The best way to initiate jQuery engine:

<script src="http://www.google.com/jsapi"></script>
<script>
  google.load("jquery", "1.10.2");

  google.setOnLoadCallback(function() {
  // Place init code here instead of $(document).ready()
 });
</script>

Here is a good article about this:

http://encosia.com/3-reasons-why-you-should-let-google-host-jquery-for-you/

Upvotes: 4

GG.
GG.

Reputation: 21844

With CDN :

  • If the user have already downloaded the file on an other website, the file is already in cache

  • If the user haven't the file, the file will always be located on the server nearest to your visitor

  • In addition, you will reduce the load on your server, which allows it to render the site faster

Upvotes: 1

Related Questions