加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

twitter-bootstrap – 我应该使用从CDN的Bootstrap还是在我的服

发布时间:2020-12-17 23:58:55 所属栏目:安全 来源:网络整理
导读:使用Twitter Bootstrap的最佳实践是什么,从CDN引用它或在我的服务器上创建本地副本? 由于Bootstrap不断进化,恐怕如果我参考CDN,用户会看到不同的网页随着时间的推移,一些标签甚至可能会损坏。最多人的选择是什么? 解决方法 Why Not Both ˉ_(ツ)_/ˉ
使用Twitter Bootstrap的最佳实践是什么,从CDN引用它或在我的服务器上创建本地副本?

由于Bootstrap不断进化,恐怕如果我参考CDN,用户会看到不同的网页随着时间的推移,一些标签甚至可能会损坏。最多人的选择是什么?

解决方法

Why Not Both ˉ_(ツ)_/ˉ ? Scott Hanselman有一篇关于使用CDN获得性能提升的优秀文章,但优雅 falling back to a local copy in case the CDN is down。

具体到bootstrap,你可以做到以下到load from a CDN with a local fallback:

<head>
  <!-- Bootstrap CSS CDN -->
  <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
</head>
<body>
  <!-- APP CONTENT -->

  <!-- jQuery CDN -->
  <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
  <!-- jQuery local fallback -->
  <script>window.jQuery || document.write('<script src="/local/jquery.min.js"></script>')</script>
  <!-- Bootstrap JS CDN -->
  <script src="//netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
  <!-- Bootstrap JS local fallback -->
  <script>if(typeof($.fn.modal) === 'undefined') {document.write('<script src="/local/bootstrap.min.js"></script>')}</script>
  <!-- Bootstrap CSS local fallback -->
  <script>
    $(document).ready(function() {
    var bodyColor = $('body').css('color');
    if(bodyColor != 'rgb(51,51,51)') {
    $("head").prepend('<link rel="stylesheet" href="/local/bootstrap.min.css">');}});
  </script>
</body>

对你的最佳实践问题,有很多very good reasons to use a CDN in a production environment:

  1. It increases the parallelism available.
  2. It increases the chance that there will be a cache-hit.
  3. It ensures that the payload will be as small as possible.
  4. It reduces the amount of bandwidth used by your server.
  5. It ensures that the user will get a geographically close response.

对于你的版本控制关注,任何值得它的CDN让你定位一个特定版本的库,所以你不会不小心引入破坏性的更改每个版本。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读