Github 应该被用作 javascript 库的 CDN 吗?

从 CDN 而不是您自己的服务器为 javascript 库提供服务具有巨大的优势。您的服务器工作量较少,CDN 的副本可能比您的服务器更接近用户,但最重要的是,您的用户的浏览器很有可能已经从该 URL 缓存了副本。最后一个意味着每个人的总工作量都减少了,所以这显然是一个全面的胜利,并且更有可能是我们(开发人员)更多地依赖 CDN 来服务我们的 javascript。

但是流行的 javascript CDN (谷歌,微软,其他?)只托管少量文件。对于其他人,我们可以选择自己托管它们,或者... 使用源代码管理服务器作为一种 CDN。Github 或类似网站不太可能拥有地理上分布的文件缓存,这些文件为全球服务而进行了优化。但是,如果这是常见的做法,那么用户的浏览器将有一个很好的机会来缓存它。将工作从我们的服务器卸载到 Github 的论点只有在 Github 自愿这样做的情况下才有效。

那么,这是常见的做法吗?我们应该鼓励彼此这样做吗?Github 介意吗?他们有正式的政策吗?

35522 次浏览

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.

This was recently asked in github's support forums, and the official answer was that it's ok.

Having said that, I agree with other answers: github was never really meant to be a CDN, while Google and Microsoft have specific infrastructure for that.

You should not do that for JavaScript files if you care about performance or IE9 compatibility.

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).

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:

enter image description here

Linking to GitHub "raw" files has some issues as outlined by Dave Ward's answer.

I suggest you look into GitHub Pages as an option.

Read this article:
GitHub as a CDN. Cache your Javascripts, Stylesheets and Web Assets with GitHub Pages.

It's fine for prototyping / personal stuff, but for production I would look at:

http://www.cdnjs.com/

http://cachedcommons.org/ -- no longer available