为什么建议从 CDN 加载 jQuery 或任何其他库?

发布于 2024-09-30 03:21:43 字数 45 浏览 6 评论 0原文

一种不言自明的问题:为什么建议从 CDN 加载 jQuery 或任何其他库?

Kind of self-explanatory question: Why is it recommended loading jQuery or any other library from a CDN?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

噩梦成真你也成魔 2024-10-07 03:21:43
  • CDN 会比您的服务器更快。
  • 服务器有最大连接数。浏览器限制对单个域的同时请求的数量。将库加载到 CDN 会阻止这两者。
  • 如果每个人都从 Google 的 CDN 加载他们的 jQuery 库,资源将在网站之间共享,并且相同版本的 jQuery 不需要多次加载(从不同的服务器)。
  • 这对您来说更便宜;) - 节省带宽!
  • A CDN will be quicker than your server.
  • Servers have maximum number of connections. Browsers restrict the number of simultaneous requests to a single domain. Off loading your libraries to CDN stops both of these.
  • If everyone loads their jQuery library from Google's CDN, the resource will be shared between websites, and the same version of jQuery doesn't need to be loaded multiple times (from different servers).
  • It's cheaper for you ;) - Save bandwidth!
风渺 2024-10-07 03:21:43

据我所知,有两个主要原因:

1 - 它可以通过提供更接近客户端的路径来加快托管文件的交付速度。

2 - 它可以为常用文件提供缓存优势,因为用户可能已经从同一个 CDN 下载了该文件

Two main reasons as far as i know:

1 - It can speed delivery of the hosted files by providing a more proximate route to the client.

2 - It can provide caching benefits for commonly used files since the user may have already downloaded the file from the same cdn

泪眸﹌ 2024-10-07 03:21:43

@Matt 击中要害。除了 CDN 之外,您还可以回退到本地版本的 jQuery,以防 CDN 出现故障。下面的例子:

<!-- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> -->
<script>!window.jQuery && document.write('<script src="js/jquery-1.4.2.min.js"><\/script>')</script>

@Matt hit the nail on the head. In addition to a CDN, you can do a fallback to your local version of jQuery in case the CDN goes down. Example below:

<!-- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> -->
<script>!window.jQuery && document.write('<script src="js/jquery-1.4.2.min.js"><\/script>')</script>
ぇ气 2024-10-07 03:21:43

原因有很多,但以下是一些突出的原因:

  1. 节省带宽。对于非自定义的内容,当您可以依靠 google、MS 或 yahoo 来托管网站时,为什么还要浪费网站的带宽呢?
  2. 如果许多站点使用托管版本,那么用户的浏览器缓存该版本的可能性就会增加。这意味着访问您的网站时可以减少一次下载。
  3. CDN 可能有一个在地理位置上更适合您的用户的位置,并且能够更快地提供服务。
  4. 由于托管版本将位于不同的域中,因此将避免同时下载的 Bowser 限制。

很多人说依赖CDN有风险,如果CDN宕机了怎么办?这是事实,但很可能您的网站比大公司更有可能失败。话虽如此,如果 CDN 确实出现故障,可以使用一些方法回退到您网站上托管的版本。

There are many reasons, but here are a few that stand out:

  1. Saves you bandwidth. For something that is not custom, why waste your site's bandwidth when you can rely on google, MS or yahoo to host it.
  2. If a lot of sites use a hosted version, then the probability that the user's browser will have it cached already increases. This means one less download to visit your site.
  3. The CDN may have a location more geographically appropraite for you users and be able to serve it up much faster.
  4. Since the hosted version will be in a different domain, the bowser limitation of simultaneous downloads will be avoided.

Many people say that it is risky to rely on the CDN, as what happens if the CDN goes down. This is true, but most likely your site will be more likely to go down that the big guys. That being said, there are approaches that can be used to have a fallback to a version hosted on your site if the CDN does go down.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文