Question

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?

Était-ce utile?

La solution 2

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/

Autres conseils

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)

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top