Github 应该用作 javascript 库的 CDN 吗? [英] Should Github be used as a CDN for javascript libraries?

查看:18
本文介绍了Github 应该用作 javascript 库的 CDN 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从 CDN 而非您自己的服务器提供 javascript 库具有巨大的优势.您的服务器的工作较少,CDN 的副本可能比您的服务器更靠近用户,但最重要的是,您的用户浏览器已经从该 URL 缓存了它的好机会.最后一个意味着每个人的总工作量更少,所以这显然是一场胜利,而且我们(开发人员)越频繁地依赖 CDN 来为我们的 javascript 服务.

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.

但是流行的 javascript CDN(Google、Microsoft 或其他?)仅托管少量文件.对于其他人,我们可以选择自己托管它们,或者……使用源代码控制服务器作为一种 CDN.Github 或类似的东西不太可能有一个地理分布的文件缓存,为全球服务而优化.但是如果这是常见的做法,那么用户的浏览器很有可能会缓存它.将我们的服务器上的工作卸载到 github 的论点只有在 Github 自愿自愿这样做的情况下才有效.

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.

那么,这是常见的做法吗?我们应该互相鼓励这样做吗?Github 介意吗?他们有明确的官方政策吗?

So, is it common practice? Should we encourage each other to do this? Does Github mind? Do they have an official policy stated?

推荐答案

如果您关心性能或 IE9 兼容性,则不应对 JavaScript 文件这样做.

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

GitHub 不提供带有远期过期标头的原始"文件.如果没有跨站点缓存的可能性,您将失去使用公共 CDN 来托管您的 JavaScript 的最大好处.事实上,使用 GitHub 作为 CDN 会比在每个用户第一次请求文件后简单地将文件托管在您自己的服务器上要慢(假设您在服务器上正确配置了缓存).

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 不提供具有与文件的实际 MIME 类型匹配的内容类型标头的原始"文件.在 IE9(可能还有其他浏览器/代理/防火墙/等)中,默认情况下会阻止未提供正确内容类型的 JavaScript 文件.您可以在 BlockUI 演示页面上看到它的实际效果,例如:

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 应该用作 javascript 库的 CDN 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆