Javascript 在本地托管 jQuery 的好处与陷阱
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3832446/
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
Benefits vs. Pitfalls of hosting jQuery locally
提问by orolo
We're currently pulling jQuery and jQueryUI (and jQueryUI CSS) libraries from the google CDN. I like this because I can call google.load("jquery", "1");
and the latest jQuery 1.x.x will be used.
我们目前正在从谷歌 CDN 中提取 jQuery 和 jQueryUI(和 jQueryUI CSS)库。我喜欢这个,因为我可以调用google.load("jquery", "1");
并且将使用最新的 jQuery 1.xx。
Now I am to pull the libraries locally because of security.
现在我为了安全起见要在本地拉库。
I'm happy to pull them locally but I'm wondering what are some of the other benefits and pitfalls to watch out for?
我很高兴将它们拉到本地,但我想知道还有哪些其他好处和需要注意的陷阱?
回答by Xaver
I always use the CDN (Content Delivery Network) from Google. But just in case it's offline:
我总是使用 Google 的 CDN(内容分发网络)。但以防万一它离线:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>!window.jQuery && document.write('<script src="jquery-1.4.2.min.js"><\/script>')</script>
Grab Google CDN's jQuery and fallback to local if necessary
如有必要,获取 Google CDN 的 jQuery 并回退到本地
Edit: If you don't need to support IE6 and your site has partial https usage you can remove the http as well:
编辑:如果您不需要支持 IE6 并且您的站点有部分 https 使用,您也可以删除 http:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
回答by Daniel Dyson
The main benefit of having them on a CDN is that the files can be downloaded in parallel to files downloaded from your own website. This reduces latency on every page. So, the flip side of this is a pitfall of hosting locally - increased latency. The main reason for that is that browsers are limited in the number of connections that they can make at the same time to the same web domain. In IE6 this was defaulted to 2 concurrent connections to the same domain - shared between all open windows of IE!!In IE8+ it improved, defaulting to 6, which is inline with FF/Chrome, but still, if you have a lot of images and you are not using sprites, you will experience heavy latency.
将它们放在 CDN 上的主要好处是,这些文件可以与从您自己的网站下载的文件并行下载。这减少了每个页面的延迟。因此,另一方面是托管本地增加延迟的陷阱。主要原因是浏览器可以同时连接到同一个网络域的连接数量受到限制。在 IE6 中,这被默认为 2 个到同一个域的并发连接 -在 IE 的所有打开的窗口之间共享!!在 IE8+ 中它有所改进,默认为 6,与 FF/Chrome 一致,但是,如果您有很多图像并且不使用精灵,您将遇到严重的延迟。
Using a CDN, I would always set the library version explicitlyrather than getting the latest one. This reduces the risk of new versions breaking your code. Not very likely with jQuery, but possible.
使用 CDN,我总是会明确设置库版本,而不是获取最新版本。这降低了新版本破坏代码的风险。jQuery 不太可能,但可能。
The other main benefit of using a CDN is reduced traffic on your site. If you pay per GB or you are on a virtual server with limited resources, you might find that overall site performance increases and hosting costs come down when you farm off some of your content to a public CDN.
使用 CDN 的另一个主要好处是减少您网站上的流量。如果您按 GB 付费,或者您使用的是资源有限的虚拟服务器,您可能会发现当您将某些内容转移到公共 CDN 时,整体站点性能会提高,托管成本会降低。
Make sure you also read the other answer to this question by @Xaver. This is a very good trick
确保您还阅读了@Xaver 对此问题的其他答案。这是一个很好的技巧
回答by bobince
Others have covered the benefits. Pitfalls:
其他人已经涵盖了这些好处。陷阱:
If you only include content from your own server, that's one server that needs to be running—and not blocked by firewalls etc—to make your site work. Pull script from a third party and now that's two servers that need to be running and unblocked to make your site work.
Any site you pull
<script>
from can completely control the user's experience on your site. If Google were feeling evil they could put something in their copy of jQuery to log your keypresses, steal personal information from the page you're on to tie into their web tracking database, make you post “I love Google!” comments to every form, and so on.
如果您只包含来自您自己的服务器的内容,则需要运行一台服务器——而不是被防火墙等阻止——以使您的站点正常工作。从第三方拉取脚本,现在需要运行和解锁两台服务器才能使您的站点正常工作。
您
<script>
从中提取的任何站点都可以完全控制用户在您站点上的体验。如果谷歌感到邪恶,他们可以在他们的 jQuery 副本中放一些东西来记录你的按键,从你所在的页面窃取个人信息以绑定到他们的网络跟踪数据库,让你发布“我爱谷歌!” 对每个表单的评论,等等。
Google probably aren't actually going to do that, but it's a factor that's out of your control, and certainly something to worry about with other script-hosting services. There have been incidents before where stats scripts have been compromised with malware loaders.
Google 可能实际上不会这样做,但这是一个不受您控制的因素,当然还有其他脚本托管服务需要担心的问题。之前曾发生过统计脚本被恶意软件加载程序破坏的事件。
Before including any script from a third party—even on one single page of your site—you must 100% trust them with all user-accessible functionality visible on that hostname (including web-facing admin functions).
在包含来自第三方的任何脚本之前(即使是在您网站的一个页面上),您必须 100% 信任它们,并使用该主机名上可见的所有用户可访问功能(包括面向 Web 的管理功能)。
回答by Moin Zaman
Google CDN:
谷歌 CDN:
- caching, good for performance, more users likely to have it already, and it downloads in parallel
- if ever, heaver forbid cdn goes down. you're screwed.
- if a new version breaks your existing plugins or site, you'll know about it possibly too late
- 缓存,有利于性能,更多用户可能已经拥有它,并且并行下载
- 如果有的话,heaver forbid CDN 会关闭。你完蛋了。
- 如果新版本破坏了您现有的插件或站点,您可能会为时已晚
Locally:
本地:
- development without being connected to the net is possible
- can still get some performance benefits by gzipping, in addition to minifying
- 无需联网即可开发
- 除了缩小之外,仍然可以通过 gzipping 获得一些性能优势
回答by Dr.Molle
I prefer to use my local version, because I don't have control about what they will provide. For example I don't want my users to get affected by google-analytics or anything similar, because this is a legal problem in my country.
我更喜欢使用我的本地版本,因为我无法控制他们将提供什么。例如,我不希望我的用户受到 google-analytics 或任何类似的影响,因为这在我的国家是一个法律问题。
回答by Praveen Prasad
Benefits: (Specifically for Google's CDN)
好处:(专门针对谷歌的CDN)
- Downloads in parallel with your files. Other answers address this further
- Google's Servers are likely to be able to physically deliver the content faster
- Common libraries and frameworks might already be on the user's machine, as the HTTP cache for a CDN is universalacross all sites
- Your bandwidth wouldn't have to go towards serving large library files
- 与您的文件并行下载。其他答案进一步解决了这个问题
- 谷歌的服务器可能能够更快地物理交付内容
- 通用库和框架可能已经在用户的机器上,因为 CDN 的 HTTP 缓存在所有站点上都是通用的
- 您的带宽不必用于提供大型库文件
回答by Spudley
Virtually every way you look at it, using Google's CDN is a good thing.
几乎从每个角度来看,使用 Google 的 CDN 都是一件好事。
Performance will be improved (albeit fairly marginally, unless your site is really busy), and the amount of data your servers have to transmit will go down (although jQuery isn't exactly a massive thing to download), etc.
性能将得到改善(尽管相当微不足道,除非您的站点真的很忙),并且您的服务器必须传输的数据量将会减少(尽管 jQuery 并不是一个需要下载的大量内容)等。
The only reason you wouldn't want to use it is if you don't trust Google. By using it, you are effectively giving Google an additional window of information into your site's traffic profile, including knowledge of URLs that you may otherwise not want to make public (eg secure areas of your site).
您不想使用它的唯一原因是您不信任 Google。通过使用它,您可以有效地为 Google 提供一个额外的信息窗口,了解您网站的流量概况,包括您可能不想公开的 URL(例如您网站的安全区域)。
If you are paranoid about security then this may be enough to persuade you not to use them (after all, hosting it yourself isn't exactly going to slow your site down to a crawl), but in general most people would take the pragmatic view that Google knows enough about their site already that adding this won't make much difference.
如果您对安全性有疑虑,那么这可能足以说服您不要使用它们(毕竟,自己托管并不会完全降低您的网站的速度),但通常大多数人会采取务实的观点谷歌已经足够了解他们的网站,添加这个不会有太大区别。
回答by The Godfather
Probably I'm in minority nowadays, but I'd say that you don't want to use CDN unless you really need to. Key factors to start using it are:
可能我现在是少数,但我会说除非你真的需要,否则你不想使用 CDN。开始使用它的关键因素是:
- Cross geo users. If you host your website in the US but have visible amount of European users - CDN will improve the loading time.
- Big amount of users and\or big content, so one main server is not enough any more. One can think of any porn-video website (or Netflix, if you want). Video stream is a heavy load, with CDN would be much much less load on the main server.
- 跨地域用户。如果您在美国托管您的网站,但有大量欧洲用户 - CDN 将缩短加载时间。
- 大量用户和/或大量内容,因此一台主服务器已不够用。人们可以想到任何视频网站(或 Netflix,如果您愿意)。视频流是一个沉重的负载,CDN 在主服务器上的负载要少得多。
But... the point is that these points are not really applicable to 90% of websites in the world. I bet you're not Facebook with millions of online users around the globe, you're not Pornhub with hundreds of GB transferred every second.
但是……关键是这些要点并不真正适用于世界上 90% 的网站。我敢打赌,您不是在全球拥有数百万在线用户的 Facebook,也不是每秒传输数百 GB 数据的 Pornhub。
If your website is targeted to users in your city/country and capacity of one server is enough for amount of users you have - why would you ever want a CDN? It's quicker for your users in your city and simpler for you to fetch everything from your main server locally.
如果您的网站面向您所在城市/国家/地区的用户,并且一台服务器的容量足以满足您拥有的用户数量 - 为什么要使用 CDN?您所在城市的用户可以更快地从本地主服务器获取所有内容。
It was more about CDNs in general, now let me be closer to the actual question about jQuery or any other library.
总的来说,更多的是关于 CDN,现在让我更接近关于 jQuery 或任何其他库的实际问题。
If you want your website to stay accessible and working without maintenance for more than a year, let's say - put it locally. Libraries nowadays are being updated in a crazy tempo which you probably don't want to follow. And old versions are being deleted eventually. Moreover, the whole library can die (probably not applicable to jQuery though).
如果您希望您的网站在一年多的时间里保持可访问性并且无需维护即可运行,比如说 - 将其放在本地。如今的图书馆正在以一种您可能不想跟随的疯狂节奏进行更新。旧版本最终会被删除。此外,整个库可能会消亡(尽管可能不适用于 jQuery)。
From my recent experience - I updated TinyMCE on the website I maintain from 3.x.x (dated 2012) to 5.x.x (dated Spring 2019). This website was working for 7(seven!) years without any maintenance in this part of the logic. There was no "minifying" concept back then and CDNs were not as common as now. But even if they would be common - you never know what will happen in 3-5-10 years from now. Usually you want your website to stay alive even without you maintaining it, don't you? However if you pull jQuery from CDN today, then this link may (and, probably, will) break in 5 years.
根据我最近的经验 - 我在我维护的网站上更新了 TinyMCE,从 3.xx(日期为 2012)到 5.xx(日期为 2019 年春季)。这个网站已经运行了 7(七!)年,这部分逻辑没有任何维护。当时还没有“缩小”的概念,CDN 也没有现在那么普遍。但即使它们很常见——你永远不知道从现在起 3-5-10 年后会发生什么。通常您希望您的网站即使没有维护也能保持活力,不是吗?但是,如果您今天从 CDN 中提取 jQuery,那么此链接可能(并且可能会)在 5 年内断开。
Solution with CDN AND fallback to local version as @Xaver suggested can be a good compromise. But... maybe just get rid of CDN link? ;)
正如@Xaver 建议的那样,使用 CDN 和回退到本地版本的解决方案可能是一个很好的折衷方案。但是......也许只是摆脱CDN链接?;)
回答by WebGoonie
To me it really depends on how much control you desire to have. If you are like me and need to develop on local host when working and traveling. Having the jquery files local is better than having it hosted on google or else where.
对我来说,这实际上取决于您希望拥有多少控制权。如果你和我一样,在工作和旅行时需要在本地主机上开发。将 jquery 文件放在本地比将其托管在 google 或其他地方要好。