从 google 的 ajax api 库中包含 Jquery 库有什么优点?
我见过许多 Web 程序员都使用来自 google 的 ajax api 库的 Jquery 库。除了节省“一些”空间之外,不在我们的服务器中托管这些库是否有任何真正的优势?这样做是一个好习惯吗?
Possible Duplicate:
Why should I use Google's CDN for jQuery?
I have seen many of the web programmers including Jquery library from google's ajax api library. Is there any real advantage in not hosting those libraries in our server other than saving 'some' space?. Is it a good habit doing so?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
企业应使用 CDN 的 5 个理由
使用内容分发网络的 7 个理由
CDN 是内容分发网络(托管库)
尽管您应该始终拥有本地缓存,以防远程存储失败。 (HTML5 Boilerplate 很好地演示了这一点):
5 Reasons Why Businesses Should Use CDN
7 Reasons to use a Content Delivery Network
CDN being Content Delivery Network (hosted libraries)
Though you should always have a local cache in case a remote store fails. (The HTML5 Boilerplate demonstrates this very well):
让 Google 为您托管 API 有一些优势。
降低延迟
CDN(内容交付网络的缩写)可将您的静态内容分发到位于不同物理位置的服务器上。当用户的浏览器解析这些文件的 URL 时,它们的下载将自动定位到网络中最近的可用服务器。
就 Google 的 AJAX Libraries CDN 而言,这意味着任何不在您的服务器附近的用户都能够比您强制他们从任意位置的服务器下载 jQuery 更快地下载 jQuery。
有一些 CDN 服务可以与 Google 的服务相媲美,但其免费的价格却无可比拟!仅这个好处就可以决定问题,但还有更多。
增加并行性
为了避免服务器不必要的超载,浏览器限制了可以同时建立的连接数量。根据浏览器的不同,此限制可能低至每个主机名两个连接。
使用 Google AJAX Libraries CDN 消除了对您站点的一次请求,从而允许并行下载更多本地内容。对于拥有六个并发连接浏览器的用户来说,这不会产生巨大的差异,但对于那些仍然运行仅允许两个并发连接的浏览器的用户来说,差异是显而易见的。
更好的缓存
使用 Google AJAX Libraries CDN 的最大好处可能是您的用户可能根本不需要下载 jQuery。
无论您的网站优化得有多好,如果您在本地托管 jQuery,那么您的用户必须至少下载一次。您的每个用户的浏览器缓存中可能已经有数十个相同的 jQuery 副本,但当他们访问您的站点时,这些 jQuery 副本将被忽略。
然而,当浏览器看到对 CDN 托管的 jQuery 副本的引用时,它会明白所有这些引用确实引用了完全相同的文件。由于所有这些 CDN 引用都指向完全相同的 URL,浏览器可以相信这些文件确实是相同的,并且如果文件已缓存,则不会浪费时间重新请求该文件。因此,浏览器能够使用缓存在磁盘上的单个副本,无论 CDN 引用出现在哪个站点上。
这会产生强大的“跨站点缓存”效果,所有使用 CDN 的站点都会受益。由于 Google 的 CDN 提供的文件标头试图将文件缓存长达一年,因此这种效果确实具有惊人的潜力。由于 Internet 上数千个流量最大的网站已经使用 Google CDN 来提供 jQuery,因此您的许多用户在访问使用 CDN 的网站时很可能永远不会对 jQuery 发出单个 HTTP 请求。
即使有人使用相同的 Google 托管版本的 jQuery 访问数百个网站,他们也只需要下载一次!
There are a few advantages to letting Google host the API for you.
Decreased Latency
A CDN — short for Content Delivery Network — distributes your static content across servers in various, diverse physical locations. When a user’s browser resolves the URL for these files, their download will automatically target the closest available server in the network.
In the case of Google’s AJAX Libraries CDN, what this means is that any users not physically near your server will be able to download jQuery faster than if you force them to download it from your arbitrarily located server.
There are a handful of CDN services comparable to Google’s, but it’s hard to beat the price of free! This benefit alone could decide the issue, but there’s even more.
Increased Parallelism
To avoid needlessly overloading servers, browsers limit the number of connections that can be made simultaneously. Depending on which browser, this limit may be as low as two connections per hostname.
Using the Google AJAX Libraries CDN eliminates one request to your site, allowing more of your local content to downloaded in parallel. It doesn’t make a gigantic difference for users with a six concurrent connection browser, but for those still running a browser that only allows two, the difference is noticeable.
Better Caching
Potentially the greatest benefit of using the Google AJAX Libraries CDN is that your users may not need to download jQuery at all.
No matter how well optimized your site is, if you’re hosting jQuery locally then your users must download it at least once. Each of your users probably already has dozens of identical copies of jQuery in their browser’s cache, but those copies of jQuery are ignored when they visit your site.
However, when a browser sees references to CDN-hosted copies of jQuery, it understands that all of those references do refer to the exact same file. With all of these CDN references point to exactly the same URLs, the browser can trust that those files truly are identical and won't waste time re-requesting the file if it's already cached. Thus, the browser is able to use a single copy that's cached on-disk, regardless of which site the CDN references appear on.
This creates a potent "cross-site caching" effect which all sites using the CDN benefit from. Since Google's CDN serves the file with headers that attempt to cache the file for up to one year, this effect truly has amazing potential. With many thousands of the most trafficked sites on the Internet already using the Google CDN to serve jQuery, it's quite possible that many of your users will never make a single HTTP request for jQuery when they visit sites using the CDN.
Even if someone visits hundreds of sites using the same Google hosted version of jQuery, they will only need download it once!
优点:
您可以节省流量。 jQuery 等能够占用大量流量。
谷歌及其公司CDN 就是为了完成这项工作而创建的。如果有人能够比这样的网络更快地提供此类内容。
长期以来,浏览器无法从一个域加载两个以上的 Javascript 文件。如果您使用 google.com 的 jQuery,您的浏览器将能够同时加载其他应用程序特定的文件。如果这在今天很重要,那么问题就是
许多人使用 Google 或 Microsoft 服务或访问通过其 jQuery 版本链接的网站。这意味着,您的浏览器很有可能已经在某处缓存了 jQuery - 加载时的等待时间更短!
反对:
Pro:
You save the traffic. jQuery and co. are able to take a lot of space on the traffic.
Google and co. CDNs are created to do this job. If someone is able to offer such content very fast than networks like this.
For a long time browsers are not able to load more than two Javascript files from one domain. If you use jQuery from google.com your Browser will be able to load other application specific files at the same time. If this is important today is the question
Many people use Google or Microsoft services or visit websites which are linked by their jQuery Version. That means, you have the big chance, that your browser already cached jQuery somewhere – shorter waiting times while loading!
Contra:
访问您网站的用户更有可能缓存 Google 的版本,而不是您自己托管的版本。 (可能会更快)
您不必托管它
您不需要处理缓存 >(好吧,除非您使用后备方法并且 Google 的版本已关闭)
如果用户是在欧洲,而您的服务器在法戈,他们很可能会从更近的服务器上下载 Google 的版本(可能更快)
节省您的带宽(在您付费时,每一比特都很重要)
我建议您查看 Html5Boilerplate,他们有当您无法访问 Google 的 CDN 时实施的一种很好的后备方法。
There would be a much better chance that a user visiting your site would already have Google's version cached, instead of your own hosted version. (faster, potentially)
You don't have to host it
You don't need to deal with caching it (well unless your using a fallback approach and Google's version is down)
If a user is in Europe and your server is in Fargo they would most likely pull Google's version down from a much closer server (faster, potentially)
Saves your bandwidth (every bit counts when you're paying)
I'd recommend taking a look at the Html5Boilerplate, they've got a good fallback approach implemented for when you can't reach Google's CDN.