Github 应该用作 javascript 库的 CDN 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5502540/
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
Should Github be used as a CDN for javascript libraries?
提问by Leopd
Serving javascript libraries from a CDN instead of your own server comes with tremendous advantages. Less work for your server, possibility for the CDN to have a copy closer to the user than your server, but most importantly a good chance that your user's browser already has it cached from that URL. The last one means less total work for everybody, so it's clearly a win all around, and is more likely the more often we (developers) rely on the CDNs to serve our javascript.
从 CDN 而非您自己的服务器提供 javascript 库具有巨大的优势。您的服务器的工作较少,CDN 的副本可能比您的服务器更靠近用户,但最重要的是,您的用户浏览器已经从该 URL 缓存了它的好机会。最后一个意味着每个人的总工作量更少,所以这显然是一场胜利,而且我们(开发人员)越频繁地依赖 CDN 来为我们的 javascript 提供服务,就越有可能。
But the popular javascript CDNs (Google, Microsoft, others?) only host a small number of files. For others we have the choice of hosting them ourselves, or ... using the source control server as a kind of CDN. It's unlikely Github or similar has a geographically distributed cache of files optimized for serving globally. But if it's common practice, then there is a decent chance that the user's browser will have it cached. The argument of off-loading work from our servers to github is only valid if Github has willingly volunteered to do this.
但流行的 javascript CDN(谷歌、微软、其他?)只托管少量文件。对于其他人,我们可以选择自己托管它们,或者……使用源代码控制服务器作为一种 CDN。不太可能 Github 或类似的东西有一个地理分布的文件缓存,为全球服务而优化。但如果这是常见的做法,那么用户的浏览器很有可能会缓存它。将工作从我们的服务器卸载到 github 的论点只有在 Github 自愿自愿这样做时才有效。
So, is it common practice? Should we encourage each other to do this? Does Github mind? Do they have an official policy stated?
那么,这是普遍做法吗?我们应该互相鼓励这样做吗?Github 介意吗?他们有明确的官方政策吗?
采纳答案by Dave Ward
You should not do that for JavaScript files if you care about performance or IE9 compatibility.
如果您关心性能或 IE9 兼容性,则不应对 JavaScript 文件执行此操作。
GitHub doesn't serve its "raw" files with a far-future expires header. Without the possibility of cross-site caching, you lose the biggest benefit of using a public CDN to host your JavaScript. In fact, using GitHub as a CDN will be slower than simply hosting the files on your own server after each user's first request for the file (assuming you configure caching correctly on your server).
GitHub 不提供带有远期到期标题的“原始”文件。如果没有跨站点缓存的可能性,您将失去使用公共 CDN 来托管您的 JavaScript 的最大好处。事实上,使用 GitHub 作为 CDN 会比在每个用户第一次请求文件后将文件托管在您自己的服务器上要慢(假设您在服务器上正确配置了缓存)。
Another problem is that GitHub doesn't serve "raw" files with a content-type header that matches the file's actual MIME type. In IE9 (and perhaps other browsers/proxies/firewalls/etc), JavaScript files that aren't served with the correct content-type are blocked by default. You can see that in action on the BlockUI demo page, for example:
另一个问题是 GitHub 不提供具有与文件的实际 MIME 类型匹配的内容类型标头的“原始”文件。在 IE9(可能还有其他浏览器/代理/防火墙/等)中,默认情况下会阻止未提供正确内容类型的 JavaScript 文件。您可以在 BlockUI 演示页面上看到它的实际效果,例如:
回答by Chris Jacob
Linking to GitHub "raw" files has some issues as outlined by Dave Ward's answer.
链接到 GitHub“原始”文件存在一些问题,如Dave Ward 的回答所述。
I suggest you look into GitHub Pages as an option.
我建议您将 GitHub Pages 作为一种选择。
Read this article:
GitHub as a CDN. Cache your Javascripts, Stylesheets and Web Assets with GitHub Pages.
阅读这篇文章:
GitHub 作为 CDN。使用 GitHub Pages 缓存您的 Javascript、样式表和 Web 资产。
回答by Mauricio Scheffer
This was recently asked in github's support forums, and the official answer was that it's ok.
最近在github的支持论坛上问到了这个问题,官方的回答是可以。
Having said that, I agree with other answers: github was never really meantto be a CDN, while Google and Microsoft have specific infrastructure for that.
话虽如此,我同意其他答案:github 从来都不是真正意义上的 CDN,而谷歌和微软为此拥有特定的基础设施。
回答by meleyal
It's fine for prototyping / personal stuff, but for production I would look at:
对于原型制作/个人物品很好,但对于生产,我会看:
http://cachedcommons.org/-- no longer available
http://cachedcommons.org/-- 不再可用
回答by ezmilhouse
I'm doing it for months now, had some concerns first but it's totally cool if you have no problems with your files being publicly available, use minified versions if you care.
我已经做了几个月了,首先有一些担忧,但是如果您的文件公开可用没有问题,那就太酷了,如果您关心,请使用缩小版本。
But still - Google & MS rule the space for jQuery & jQuery Templates - so I use them for that.
但仍然 - Google 和 MS 统治着 jQuery 和 jQuery 模板的空间 - 所以我使用它们。