开发者

Why is it important to load a JS framework from Google's AJAX Libraries API?

I remember reading somewhere that it is a lot better to load a JS framework from Google's AJAX Libraries API, rather than using a locally hosted one.

This means that instead of :

<script src="jquery.js"></script>

You load the framework from Google:

<script src="http://ajax.g开发者_开发百科oogleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

I think that the advantage was mostly about caching but I'm not sure about it.

Can someone explain to me the reason exactly on why it is better to load frameworks from Google instead of hosting them locally ?


There is a good chance another site will also be using the same js files from Google's servers, so the file will already be cached by your browser and it won't need to download it again for your site.

Check out this article for some of the other benefits as well.


There are pros and cons to having Google host your javascript libs.

PROS:

  • Google's CDN is way better than yours
  • loads of data gathered by Google about your site and usage
  • the user may have the libs cached from another website

CONS:

  • 1 extra DNS look-up
  • loads of data gathered by Google about your site and usage
  • Google may be down or blocked (uniquely but can happen, i.e. some crazy corporate policy)
  • 1 new connection the user browser has to make
  • in Safari the jquery file is not served gzipped

In my experience, I got excellent results in hosting locally because I unified Google's JQuery lib with my other javascript code, gzipped it and got great compression rates in the unified javascript file. This way, the browser uses an already opened connection to download a "tiny" file with everything on it.


Firstly, it means the load is on Google's servers, rather than your own, which will save you both server processing time and bandwidth. Secondly, it's quite likely that Google's servers are faster than your own for the vast majority of internet users.

Plus, from Google's point of view, it lets them get more data about how people use the internet and their APIs.

ETA: Also, if Google update their APIs, it means you'll always be using the latest version. This may or may not be a good thing (bug fixes vs. back-compatibility).


It comes down to footprint: Google has a distributed set of network locations thereby ensuring a low latency delivery to almost everywhere.

So, if you get to serve as much as possible from Google, your customers QoE (Quality of Experience) will improve.

  1. Customers care about this because the faster they get their web page, the more than can get done.

  2. Google cares about this because the faster customers get their web page, the more they can serve per day and thus the more money$ they make (from advertising of course).


Why only Google? Microsoft has launched a CDN too and these days I am linking to the MS CDN instead of Google to fetch Jquery.

If I rephrased your question to read 'What is the advantage of linking from a CDN?', I would answer it thus.

  1. The CDN will have a number of servers and there might be one closely geo-located and better serviced than yours (faster), though it won't matter cause typically JQuery will be a small fragment of your data.

That's it. I can't think of any other advantage. None. Nada. Nil.

In fact I can think of a disadvantage that you are going to have slightly less control over your content.

Don't think that the big corps are being generous with their money giving you free bandwidth. Being in control of your website is their business.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜