为什么我应该为 jQuery 使用 Google 的 CDN?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2180391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why should I use Google's CDN for jQuery?
提问by Yvette
This may be obvious to some, but I've been wondering: why should I depend on Google's serverto host jQuery for my site?
这对某些人来说可能很明显,但我一直在想:为什么我要依赖Google 的服务器来为我的网站托管 jQuery?
Is it only because it loads faster this way?
仅仅是因为这样加载速度更快吗?
回答by John Gietzen
This is because:
这是因为:
- It increases the parallelism available.
(Most browsers will only download 3 or 4 files at a time from any given site.) - It increases the chance that there will be a cache-hit.
(As more sites follow this practice, more users already have the file ready.) - It ensures that the payload will be as small as possible.
(Google can pre-compress the file in a wide array of formats (like GZIP or DEFLATE). This makes the time-to-download very small, because it is super compressed and it isn't compressed on the fly.) - It reduces the amount of bandwidth used by your server.
(Google is basically offering free bandwidth.) - It ensures that the user will get a geographically close response.
(Google has servers all over the world, further decreasing the latency.) - (Optional) They will automatically keep your scripts up to date.
(If you like to "fly by the seat of your pants," you can always use the latestversion of any script that they offer. These could fix security holes, but generally just break your stuff.)
- 它增加了可用的并行度。
(大多数浏览器一次只能从任何给定站点下载 3 或 4 个文件。) - 它增加了缓存命中的机会。
(随着越来越多的网站遵循这种做法,越来越多的用户已经准备好文件。) - 它确保有效载荷尽可能小。
(Google 可以以多种格式(如 GZIP 或 DEFLATE)预压缩文件。这使得下载时间非常短,因为它是超级压缩的,并且不会即时压缩。) - 它减少了服务器使用的带宽量。
(谷歌基本上提供免费带宽。) - 它确保用户将获得地理位置接近的响应。
(谷歌在世界各地都有服务器,进一步降低了延迟。) - (可选)他们将自动使您的脚本保持最新。
(如果您喜欢“坐享其成”,您可以随时使用他们提供的任何脚本的最新版本。这些可以修复安全漏洞,但通常只会破坏您的东西。)
回答by Lance Fisher
There are several scenarios when you might notwant to use jQuery from Google's CDN:
有几种方案时,你可能不希望使用jQuery来自谷歌的CDN:
When you are building an intranet application where the web server is hosted on the same network as the clients.If you use Google's CDN jQuery, you will be making a call to the internet rather than a webserver on the local network. This increases bandwidth for your organization, and is slower.
When you want to run your application offline. (Quite linked to the first issue) If you need to work on a development environment (managed for example with Bower), you might need to be able to make your application work without any internet connection (ie: in a train :)
When you need to customize it. For example if you use Gruntto build the library in order to use only certain modules or setting the AMD name
When you are serving pages over SSL that require jQuery.You should serve the JavaScript over SSL as well as your page to avoid security problems and warnings.
当您构建一个 Intranet 应用程序时,其中 Web 服务器与客户端托管在同一网络上。如果您使用 Google 的 CDN jQuery,您将调用 Internet 而不是本地网络上的网络服务器。这会增加组织的带宽,但速度较慢。
当您想要离线运行您的应用程序时。(与第一个问题密切相关)如果您需要在开发环境中工作(例如使用Bower管理),您可能需要能够使您的应用程序在没有任何互联网连接的情况下工作(即:在火车上:)
当您通过 SSL 提供需要 jQuery 的页面时。您应该通过 SSL 以及您的页面提供 JavaScript,以避免出现安全问题和警告。
Also, Microsoft hosts jQueryon their CDN. That is another choice comparable to using Google's hosted jQuery.
此外,Microsoft在其 CDN 上托管 jQuery。这是与使用 Google 托管的 jQuery 相当的另一种选择。
回答by cantera
This studyby TJ VanTollconvinced me that it's better to concatenate jQuery with other scripts rather than load it from a CDN.
该研究由TJ VanToll使我确信,这是更好地串连的jQuery与其他脚本,而不是它的负载从CDN。
The reason is the latency involved in fetching jQuery on mobile devices:
原因是在移动设备上获取 jQuery 所涉及的延迟:
"In 2012 the average RTT time on a mobile network in the United States was 344ms. And that 344ms applies to not only every HTTP request – which the average web page now makes 93 of – but also every DNS lookup and TCP connection... While average RTTs are improving, there are only small additional gains to be had, as current networks are within a small factor of the theoretical limit dictated by physics."
“2012 年,美国移动网络上的平均 RTT 时间为 344 毫秒。这 344 毫秒不仅适用于每个 HTTP 请求(现在平均网页占 93 个),还适用于每个 DNS 查找和 TCP 连接......虽然平均 RTT 正在提高,但只有很小的额外收益,因为当前的网络在物理学规定的理论限制的一小部分范围内。”
He also cites this postfrom Steve Souders that shows why you're actually unlikely to get the caching benefitfrom using a CDN:
他还引用了Steve Souders 的这篇文章,其中说明了为什么您实际上不太可能从使用 CDN 中获得缓存优势:
"Because of the fragmentation in CDN providers, jQuery versions, and protocol usage (http vs. https), the chances of getting a CDN cache hit are shockingly low – and downloading from an external domain has the potential to perform not one, but three round trips (a DNS lookup, a TCP connection, and an HTTP GET)."
“由于 CDN 提供商、jQuery 版本和协议使用(http 与 https)的碎片化,获得 CDN 缓存命中的机会低得惊人——而且从外部域下载有可能执行的不是一个,而是三个往返(DNS 查找、TCP 连接和 HTTP GET)。”
回答by Matt Moor
The greatest benefit is from caching. The theory is that if a visitor visited a site that was loading their JavaScript libraries, say jQuery for example from the Google CDN, then when they visit your website, the library is already in that user's browser cache and will not have to be downloaded again. This sounds great in theory.
最大的好处是缓存。理论是,如果访问者访问了加载其 JavaScript 库的站点,例如来自 Google CDN 的 jQuery,那么当他们访问您的网站时,该库已在该用户的浏览器缓存中,无需再次下载. 这在理论上听起来很棒。
The benefits being shared here and elsewhere are all theoretical. Just came across an in-depth analysis of using a CDN and if it provides the expected performance benefits. http://www.root777.com/appdev/does-using-google-libraries-api-cdn-give-you-performance-benefits
在这里和其他地方分享的好处都是理论上的。刚刚对使用 CDN 以及它是否提供预期的性能优势进行了深入分析。 http://www.root777.com/appdev/does-using-google-libraries-api-cdn-give-you-performance-benefits
回答by Joshua Maddox
A major reason NOT to let Google host your jQuery, one that many people don't think about, is that it won't download if you are in China. It's blocked along with many other scripts, fonts etc... hosted by Google CDN's. If you need to reach a Chinese audience you're best to always use a fall back hosted on your own server. Google APIS Blocked in China
不让 Google 托管您的 jQuery(很多人没有想到)的一个主要原因是,如果您在china,它就不会下载。它与许多其他脚本、字体等一起被阻止......由 Google CDN 托管。如果您需要接触china观众,最好始终使用托管在您自己的服务器上的备用服务器。 Google APIS 在china被封锁
回答by EdwardM
Some good answers here to "Why you should..." and "Why you should not..."
这里有一些关于“为什么你应该......”和“为什么你不应该......”的好答案
I simply want to add a list of alternatives to Google if you did want to load jQuery from a CDN.
如果您确实想从 CDN 加载 jQuery,我只是想添加一个替代 Google 的列表。
- CDN JS - https://cdnjs.com/libraries/jquery
- Microsoft - https://docs.microsoft.com/en-us/aspnet/ajax/cdn/overview#jQuery_Releases_on_the_CDN_0]2
- jsDelivr - https://www.jsdelivr.com/package/npm/jquery
- code.jquery.com (StackPath) - https://code.jquery.com/
- Google - https://developers.google.com/speed/libraries/#jquery
- CDN JS - https://cdnjs.com/libraries/jquery
- 微软 - https://docs.microsoft.com/en-us/aspnet/ajax/cdn/overview#jQuery_Releases_on_the_CDN_0] 2
- jsDelivr - https://www.jsdelivr.com/package/npm/jquery
- code.jquery.com (StackPath) - https://code.jquery.com/
- 谷歌 - https://developers.google.com/speed/libraries/#jquery
But to sum it up, you are basically improving your overall website/application performance.
但总而言之,您基本上是在提高整体网站/应用程序性能。
回答by Oscar Gardiazabal
Usign CDN with a Service Worker, you can download the CDN once in the client lifetime, and not every time you update your code.
使用 Service Worker 签名 CDN,您可以在客户端生命周期内下载一次 CDN,而不是每次更新代码时。