Github免费CDN:jsDelivr

date
Oct 3, 2021
slug
kidaehjt
status
Published
tags
Github
summary
type
Post
 
CDN 的全称是 Content Delivery Network,即内容分发网络。CDN 是构建在网络之上的内容分发网络,依靠部署在各地的边缘服务器,通过中心平台的负载均衡、内容分发、调度等功能模块,使用户就近获取所需内容,降低网络拥塞,提高用户访问响应速度和命中率。CDN 的关键技术主要有内容存储和分发技术。——百度百科
使用 Github 仓库来做图床,但在国内访问 Github 的资源速度较慢,所以一般会通过挂梯子来访问。但还有另外一种方法,就是通过 Github 的免费 CDN——jsDelivr,实现高速访问。

jsDelivr

官网介绍(
jsDelivr is the only public CDN with a valid ICP license issued by the Chinese government, and hundreds of locations directly in Mainland China. jsDelivr 是在中国大陆唯一有 license 的公有 CDN,并且有数以千计的直连地区。
notion image

引用方式

公开资源

jsDelivr 提供 Npm、Github、Wordpress 的所有资源,所以对于前端开发常用的 Npm 公开包的引用可以直接从 jsDelivr,直接在官网搜索即可。
jsDelivr 提供的公开 Npm 包引用规则:
// load any project hosted on npm
https://cdn.jsdelivr.net/npm/package@version/file

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

// use a version range instead of a specific version
https://cdn.jsdelivr.net/npm/jquery@3.2/dist/jquery.min.js
https://cdn.jsdelivr.net/npm/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/npm/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/npm/jquery@3.2.1/src/core.min.js

// omit the file path to get the default file
https://cdn.jsdelivr.net/npm/jquery@3.2

// add / at the end to get a directory listing
https://cdn.jsdelivr.net/npm/jquery/
jQuery 为例,贴上使用教程
notion image

个人资源

jsDelivr 提供的公开 Github 仓库文件引用规则:
// 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/
以我的Github 图床中为例
仓库名称填写oss,选择Public类型,不能是私有的。通过本地 Git 仓库、第三方图床(推荐uPic)工具上传图片到 Github 上,贴出两种 URL 和实际效果:
  • 原始 URL(Github 版本):https://github.com/stelalae/oss/blob/master/files/2021/10/03/OVRzwP.png
  • 新 URL(CDN 版本):https://cdn.jsdelivr.net/gh/stelalae/oss@master/files/2021/10/03/OVRzwP.png
notion image
原始 URL(Github 版本),图片不会显示
notion image
新 URL(CDN 版本),图片正常显示
简单来说,CDN 版本的规则是:
https://cdn.jsdelivr.net/gh/<用户名>/<仓库名>/<文件及路径>
另外也支持分支及版本(Tag)号的引用,规则是:
https://cdn.jsdelivr.net/gh/<用户名>/<仓库名>@[分支/版本号]/<文件及路径>

缓存问题

CDN 缓存是个谜,包括我在国内使用收费 CDN,所以我的通用做法是带上 hash 值,反正存储费用也是超级低(Github 是免费的)。大家可以看看这两篇关于 jsdelivr 的缓存:
总结规避缓存的方式:
  • 图床(或不需要修改文件,或有 hash 值的文件),可以直接使用latest 分支或者 master 分支即可
  • 静态文件仓库,或经常需要改动的文件,建议使用版本号方式
  • 使用 purge 进行刷新(有第三方工具)
  • 切换其他收费云

文件&仓库的限制问题

Packages larger than 50 MB or single files larger than 20 MB (in case of GitHub) are not supported by default. We recommend removing files that are not needed from your package when possible. If you need to set a higher limit for your package, open an issue in this repo. 默认情况下,不支持大于 50MB 的包或大于 20MB 的单个文件(对于 GitHub)。我们建议尽可能从包中删除不需要的文件。
所以最佳实践是:
  • Github 单文件上传限制为 100M,但 jsdelivr 加速的单文件大小为 20M,jsdelivr 加速的 relase 包大小限制为 50M。
  • GitHub 上单仓库不能超过 1G,一个账号下所有仓库仓库大小为 100G,提前规划和创建多个仓库。超过限制会有人工审核,删号警告!
  • 文件类型方面,基本的图片、视频、静态文件等都可以。
如果 release 包大小超过 50M,就无法通过 jsdelivr 查看了,只能在 GitHub 仓库查看。jsdelivr 查看仓库文件有两种方式:
  • 查看仓库版本号,格式:https://www.jsdelivr.com/package/gh/<用户名>/<仓库名>
  • 查看仓库文件,格式:https://cdn.jsdelivr.net/gh/<用户名>/<仓库名[@分支]>

jsDelivr API

API 的Github 地址,支持以下内容:
  • list package versions
  • list package files
  • resolve a version range/tag
  • get package entry points
  • get package usage stats
  • get package version usage stats
  • get the most popular packages
  • get a hits badge for your project
  • get a rank badge for your project
  • get a CDN link/metadata from file hash
  • search npm packages

参考资料:

© 刘德华 2020 - 2023