简体   繁体   English

包括本地和远程 javascript 库

[英]Including local vs. remote javascript libraries

I'm using jsSHA 1.3.1 which I downloaded here and used in learning project on my localhost.我正在使用 jsSHA 1.3.1,我在此处下载并在我的本地主机上用于学习项目。 It gives a slightly different result than the copy I got by referring to the remote as follows:它给出的结果与我通过参考遥控器获得的副本略有不同,如下所示:

<script src="https://raw.github.com/Caligatio/jsSHA/master/src/sha1.js"></script>

The remote copy works well for me, now, thanks to this excellent answer by @Andreas here .现在,远程副本对我来说效果很好,这要感谢@Andreas here的出色回答。

But it leaves me with a new question: what's the rationale for including a copy vs. referring to a remote js library?但这给我留下了一个新问题:包含副本与引用远程 js 库的理由是什么? Is it like 'vendoring' the library, insulating my app from subsequent changes in the code?它是否像“供应”库,将我的应用程序与代码的后续更改隔离开来?

If your application is available on the WWW, you should consider using a well-known external URL.如果您的应用程序在 WWW 上可用,您应该考虑使用众所周知的外部 URL。


<script type="text/javascript"
   src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js">
</script> 

This example below gets the minified version of jquery 1.8.0 from google's servers.下面的示例从谷歌的服务器获取 jquery 1.8.0 的缩小版本。


  • The benefit obtained by this method comes from caching:这种方法获得的好处来自于缓存:

  • You do not want the first visit a potential user makes to your website to be slow and disappointing.您不希望潜在用户对您网站的第一次访问缓慢而令人失望。 If your first-time visitor has visited my site which uses this URL for jQuery, her browser will probably have cached it so it will not need to load it.如果您的第一次访问者访问了我的网站,该网站使用此 URL 进行 jQuery,她的浏览器可能已经缓存了它,因此不需要加载它。

  • Using immutable versioned resources (jquery/1.8.0 instead of something like jquery/current) both helps developers not have to track down breaking changes in their production code and ensures that these resources can be cached.使用不可变的版本化资源(jquery/1.8.0 而不是 jquery/current 之类的东西)既可以帮助开发人员不必跟踪其生产代码中的重大更改,又可以确保可以缓存这些资源。

  • If the resource has to be downloaded and the URL is hosted on a CDN you are likely to get lower latency as the resource will probably be loaded from a server closer to the user's network.如果必须下载资源并且 URL 托管在 CDN 上,您可能会获得较低的延迟,因为资源可能会从更靠近用户网络的服务器加载。 The URL in the example is hosted on Google Hosted Libraries which is a CDN.示例中的 URL 托管在作为 CDN 的 Google Hosted Libraries 上。 See https://developers.google.com/speed/libraries/devguide for more information.有关详细信息,请参阅https://developers.google.com/speed/libraries/devguide

  • Another argument often seen in such discussions is that when the resource has to be downloaded, you will be able to get better client-side resource loading parallelism if the resource is not on your own servers together with 10 more resources your page includes because browsers limit themselves to loading up to a small number (6 or so in modern browsers) of resources form the same server.在此类讨论中经常看到的另一个论点是,当必须下载资源时,如果资源不在您自己的服务器上以及您的页面包含的 10 个以上资源,您将能够获得更好的客户端资源加载并行性,因为浏览器限制他们自己加载来自同一服务器的少量资源(在现代浏览器中大约为 6 个)。

  • If your internet-wide web application is security-critical, you must keep control of as much of it as you can securely manage (and static immutable or nearly immutable resources are relatively easy to manage securely).如果您的 Internet 范围的 Web 应用程序是安全关键的,您必须尽可能多地控制它,以便安全地管理(静态不可变或几乎不可变的资源相对容易安全地管理)。

  • If my bank's e-banking application which runs over HTTPS were to rely on google's HTTP servers for serving, it would both be granting Google authority over the client-side part of its e-banking application and eliminating practically all benefits of the HTTPS connection to its servers.如果我的银行通过 HTTPS 运行的电子银行应用程序依赖于谷歌的 HTTP 服务器来提供服务,它既会授予谷歌对其电子银行应用程序客户端部分的权限,也会消除 HTTPS 连接的几乎所有好处它的服务器。 There are very few things that a rogue client script cannot do...流氓客户端脚本几乎不能做的事情......

  • If your application is accessed locally, you should probably include it in your application for both performance (access to your servers should be faster than accessing some remote server both in terms of latency and in terms of bandwidth) and reliability reasons (you are not relying on the external internet connection and on the remote servers being up and running).如果您的应用程序是在本地访问的,您可能应该将它包括在您的应用程序中,以提高性能(在延迟和带宽方面访问您的服务器应该比访问某些远程服务器更快)和可靠性原因(您不依赖在外部互联网连接和正在启动和运行的远程服务器上)。

It comes down to whether or not the developer is happy with you 'hotlinking' to the JS library.这取决于开发人员是否对您“热链接”到 JS 库感到满意。

Regarding the specific URL that you have posted,关于您发布的具体网址,

https://raw.github.com/Caligatio/jsSHA/master/src/sha1.js

I would be wary of referencing that.我会谨慎引用它。 It is the master branch of their source code, they may include breaking changes at any time.它是他们源代码的主分支,他们可能随时包含中断更改。 They may even decide to move and restructure their codebase, in which case your application will break as the URL above will lead to a 404.他们甚至可能决定移动和重组他们的代码库,在这种情况下,您的应用程序将崩溃,因为上面的 URL 将导致 404。

The better solution is indeed referencing a specific version of the library.更好的解决方案确实是引用库的特定版本。 This ensures that your application behaves, and will continue to behave, in an expected and known manner as far as the jsSHA library is concerned.就 jsSHA 库而言,这可确保您的应用程序以预期和已知的方式运行并将继续运行。

There are a few ways to do this.有几种方法可以做到这一点。 You can reference a specific, fixed URL that explicitly specifies a version您可以引用明确指定版本的特定固定 URL

http://crypto-js.googlecode.com/svn/tags/3.0.2/build/rollups/hmac-sha1.js

Because its tagged (3.0.2), you are a bit safer, but you are still referencing source control.因为它被标记为 (3.0.2),所以您会更安全一些,但您仍然在引用源代码管理。 This should really be done via a CDN if one exists.如果存在的话,这真的应该通过 CDN 来完成。 I do not believe that the googlecode.com URL is a CDN URL (I could be wrong).我不相信 googlecode.com URL 是 CDN URL(我可能是错的)。 It is of course a common practice to use a proper CDN or external link to reference a JS library and you can see an example of this on the JQuery page , but this is often done as part of page performance enhancements.使用适当的 CDN 或外部链接来引用 JS 库当然是一种常见的做法,您可以在 JQuery 页面上看到这样的示例,但这通常是作为页面性能增强的一部分完成的。 A CDN is optimized for a user's geographic location so a reference to any content on a CDN (as opposed to the main server) will be served faster. CDN 针对用户的地理位置进行了优化,因此可以更快地提供对 CDN(相对于主服务器)上任何内容的引用。 It has user experience benefits.它具有用户体验优势。 It's useful if page load times are important for your userbase.如果页面加载时间对您的用户群很重要,这将很有用。

The alternative, and the safest way, is to keep a copy of the JS library in your own source control and reference it using your own infrastructure.另一种也是最安全的方法是在您自己的源代码管理中保留一份 JS 库的副本,并使用您自己的基础设施引用它。 It's with you, it is a fixed version and there are no surprises if something changes, such as restructuring, sites going down, and so on.它与您同在,它是一个固定版本,如果发生某些变化(例如重组、站点关闭等),您也不会感到意外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM