今天树莓派上的服务检测脚本忽然告警了,博客出现了大量资源 down 掉的情况,赶紧上线查看,页面都刷不出来,原来是 cdn.bootcss.com 这个 CDN down 掉了,我的 jquery,fontawesome 等 JS 库还有一些 CSS 库都引用这个 CDN 导致页面崩坏。坑爹呢 (; ̄Д ̄)

我心中顿时出现了一个解决方案,维护一个 CDN 列表,然后服务器上跑一个性能监测脚本,对这些 CDN 进行可达性和性能测试,然后在网站里去根据这个脚本动态修改依赖的 CDN。

等等好像有什么问题,服务器上进行性能监测好像没啥卵用啊,我们要保证客户端可达,这个好像是 CDN 要做的工作啊。

难道要在客户端去跑这个脚本? =͟͟͞͞(꒪⌓꒪*) 算了算了还是搜一个靠谱的 CDN 吧。

在 StackOverflow 搜索了一下发现有人推荐 jsDelivr 作为 CDN。

试验了一下,发现效果相当给力。推荐一下~

What is jsDelivr

jsDelivr - Open Source CDN free fast and reliable

它是一个高性能的 CDN,通过

How it work

官网上有一张原理的简要介绍

img

jsDelivr 通过多个 CDN 服务商对全球用户提供高性能的内容分发服务,其中对中国线路做了特别优化。整个大陆地区有超过 600 个边缘节点。默认使用 cedexis 提供的智能负载均衡服务来进行 CDN 选择,如果 cedexis 挂掉,则默认选择 StackPath CDN 或 CloudFlare CDN。如果请求内容未命中 jsDelivr 的缓存,则去上游的 Amazon S3 对象存储服务查找资源,Amazon S3 服务也会同步最终来自于 npm 和 Github 的源文件。如果 Amazon S3 服务 down 掉,jsDelivr 则把请求直接转发给存储源文件的 npm 和 github。

总之,jsDelivr 有众多的边缘服务节点,对中国大陆服务友好,并有宕机备份解决方案,保证了 CDN 的可达性和性能。

Performance test

我们利用 17ce 这个测速工具来测量 jsDelivr 的性能。

速度还是相当给力的。

How to use

jsDelivr 提供了三种 CDN 服务方式:npm,github 和 wordpress。我们着重讲讲 github 的方式。

github 方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//load any GitHub release or commit
//note: we recommend using npm for projects that support it
https://cdn.jsdelivr.net/gh/user/repo@version/file

//load jQuery v3.2.1
https://cdn.jsdelivr.net/gh/jquery/jquery@3.2.1/dist/jquery.min.js

//use a version range instead of a specific version
https://cdn.jsdelivr.net/gh/jquery/jquery@3.2/dist/jquery.min.js
https://cdn.jsdelivr.net/gh/jquery/jquery@3/dist/jquery.min.js

//omit the version completely to get the latest one
//you should NOT use this in production
https://cdn.jsdelivr.net/gh/jquery/jquery/dist/jquery.min.js

//add ".min" to any JS/CSS file to get a minified version
//if one doesn't exist, we'll generate it for you
https://cdn.jsdelivr.net/gh/jquery/jquery@3.2.1/src/core.min.js

//add/at the end to get a directory listing
https://cdn.jsdelivr.net/gh/jquery/jquery/

例如我们想要 pangu 的版本号为 3.3.0 的 pangu.min.js 插件的 CDN 地址,那么我们去 pangu 的 github 页面

从 commits 历史或 releases 中查找 3.3.0 版本,查看文件并搜索 pangu.min.js 的相对路径

其路径是 dist/browser/pangu.min.js,根据作者名称和 repo 名分别是 vinta/pangu.js。所以引用路径是

https://cdn.jsdelivr.net/gh/vinta/pangu.js@3.3.0/dist/browser/pangu.min.js,我们也可以对这个地址测速。

awesome!把它放在我们项目的配置文件中即可。