CDN的全稱是Content Delivery Network,即內容分發網絡。CDN是構建在網絡之上的內容分發網絡,依靠部署在各地的邊緣服務器,通過中心平台的負載均衡、內容分發、調度等功能模塊,使用戶就近獲取所需內容,降低網絡擁塞,提高用戶訪問響應速度和命中率。CDN的關鍵技術主要有內容存儲和分發技術。——百度百科
由於某些原因,很多公用免費的 CDN 資源在中國大陸並不很好用,就算是付費的,也有一定的限制,例如每天的刷新次數有限之類的。那有沒有一款造福人類的,或者造福中國大陸的公用 CDN 呢?
jsDelivr
先看看官方的介紹:
這是在中國大陸唯一有 license 的公有 CDN,而且實際使用中的訪問速度也是極快的(雖然官網打開速度慢
與 Github、NPM、WorldPress 整合
官網地址:https://www.jsdelivr.com 沒梯子訪問可能有點慢,不過CDN的節點是很快的 可以引用的資源包括NPM、github、wordpress的所有資源,github可以是任意體積小於50M的倉庫。
以github為例,只需要通過符合 JSDelivr 規則的 URL 引用,即可直接使用 Github 中的資源。
// 用戶名/倉庫名@版本號/文件名 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 // 使用一個范圍內的版本 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 // 忽略版本號則默認使用最新版 // you should NOT use this in production https://cdn.jsdelivr.net/gh/jquery/jquery/dist/jquery.min.js // 在任意JS/CSS文件后添加 .min 能得到一個縮小版 // 如果它本身不存在,我們將會為你生成 https://cdn.jsdelivr.net/gh/jquery/jquery@3.2.1/src/core.min.js // 在末尾加 / 則得到目錄列表 https://cdn.jsdelivr.net/gh/jquery/jquery/
詳細規則參考官網:https://www.jsdelivr.com/
以我一張圖片為例:
原github鏈接:https://raw.githubusercontent.com/growvv/img/master/images/20200118222911.jpeg
CDN后的鏈接:https://cdn.jsdelivr.net/gh/growvv/img@master/images/20200118222911.jpeg
其他
除了jsdelivr
, 推薦幾個免費的且不限流量的CDN,還有 staticaly
githack
都是全球通用的,
staticaly
官網地址:https://www.staticaly.com
輕松地從GitHub / GitLab / Bitbucket等加載您的項目 沒有流量限制或限制。
文件通過超快速全球CDN提供。 在URL(不是分支)中使用特定標記或提交哈希。
根據URL永久緩存文件。 除master分支外,文件在瀏覽器中緩存1年。 具體用法:
# GitHub CDN https://cdn.staticaly.com/gh/:user/:repo/:tag/:file https://cdn.staticaly.com/gh/growvv/growvv.github.io/master/README.md # GitLab CDN https://cdn.staticaly.com/gl/:user/:repo/:tag/:file # Bitbucket CDN https://cdn.staticaly.com/bb/:user/:repo/:tag/:file # WordPress CDN https://cdn.staticaly.com/wp/c/:version/wp-includes/:file https://cdn.staticaly.com/wp/p/:plugin_name/:version/:file https://cdn.staticaly.com/wp/t/:theme_name/:version/:file # Imgpx CDN https://cdn.staticaly.com/img/:image_url # Favicons CDN https://cdn.staticaly.com/favicons/:favicon_url
githack
直接從GitHub,Bitbucket或GitLab提供原始文件
官網地址:http://raw.githack.com/ 具體用法和上面的staticaly
很類似
# Github CDN //主分支 https://rawcdn.githack.com/liub1934/LB-Blog/master/wp-content/themes/Memory/emoji/xiaodianshi/baiyan.png //版本分支 https://rawcdn.githack.com/liub1934/LB-Blog/8806f440d3f9a7cc3e6125d7d75564e40262c6a8/wp-content/themes/Memory/emoji/xiaodianshi/baiyan.png
參考鏈接:
1. https://segmentfault.com/a/1190000020239193
2. https://blog.csdn.net/qq_36759224/article/details/86936453
3. https://liubing.me/recommend-several-free-unlimited-cdns.html
4. https://fileem.com/jsdelivr-free-public-cdn-acceleration-service