利用Jsdelivr加速引用Github资源

2021-11-30


不知道从什么时候开始Github就时好时坏,下载东西也时快时慢,再加上raw.githubusercontent.com被墙了,这导致我们引用Github的资源非常麻烦,这时我们可以利用jsdelivr很好的解决这个问题。

jsdelivr是一个免费,快速,可靠的开源CDN

CDN的全称是Content Delivery Network,即内容分发网络。CDN是构建在现有网络基础之上的智能虚拟网络,依靠部署在各地的边缘服务器,通过中心平台的负载均衡、内容分发、调度等功能模块,使用户就近获取所需内容,降低网络拥塞,提高用户访问响应速度和命中率。由于CDN是为加快网络访问速度而被优化的网络覆盖层,因此被形象地称为“网络加速器”。

jsdelivr使用方法如下

https://cdn.jsdelivr.net/gh/你的用户名/你的仓库名@分支/文件路径
https://cdn.jsdelivr.net/gh/你的用户名/你的仓库名@发布的版本号/文件路径
举例:
https://cdn.jsdelivr.net/gh/user/[email protected]/file
https://cdn.jsdelivr.net/gh/user/repo@branch/file

官方示例如下:

// load any GitHub release, commit, or branch
// 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/