使用 jsDelivr 加速 Github 资源,加快博客打开速度


GitHub CDN 使用说明

在 GitHub 创建公开项目即可使用 jsDelivr 访问:

We recommend using npm for projects that support it for better UX - npm packages are searchable on our website, and package pages show additional useful information, such as description and link to homepage.

We use a permanent S3 storage to ensure all files remain available even if GitHub goes down, or a repository or a release is deleted by its author. Files are fetched directly from GitHub only the first time, or when S3 goes down.

Load any GitHub release, commit, or branch:

基本格式:

https://cdn.jsdelivr.net/gh/user/repo@version/file
https://cdn.jsdelivr.net/gh/用户名/仓库名@版本号/文件路径

例如:
https://cdn.jsdelivr.net/gh/eished/jkforum_helper@0.5.8/video/light.mp4

https://cdn.jsdelivr.net/gh/eished/JavaScript_notes/JavaScript_code/lib/move.min.js

Load an exact version of a file:

精确版本号:

https://cdn.jsdelivr.net/gh/jquery/jquery@3.2.1/dist/jquery.min.js
https://cdn.jsdelivr.net/gh/jquery/jquery@32b00373b3f42e5cdcb709df53f3b08b7184a944/dist/jquery.min.js

Use a version range instead of an exact 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

If you use this feature and a file you requested is not available in the newest release, the link will keep working thanks to our version-fallback feature. We’ll continue to serve the file from older release instead of failing with a 404 error.

Omit the version completely or use “latest” to load the latest one (not recommended for production usage):

忽略版本号,使用最新版(不推荐生产环境使用):

https://cdn.jsdelivr.net/gh/jquery/jquery@latest/dist/jquery.min.js
https://cdn.jsdelivr.net/gh/jquery/jquery/dist/jquery.min.js

Requesting the latest version (as opposed to “latest major” or “latest minor”) is dangerous because major versions usually come with breaking changes. Only do this if you really know what you are doing.

Add “.min” to any JS/CSS file to get a minified version - if one doesn’t exist, we’ll generate it for you. All generated files come with source maps and can be easily used during development:

自动生成 JS 压缩文件:文件名.min.js

https://cdn.jsdelivr.net/gh/jquery/jquery@3.2.1/src/core.min.js

例如:
https://cdn.jsdelivr.net/gh/eished/jkforum_helper@0.5.8/main.min.js

Minifying a large file can take several seconds. However, we store all generated files in our permanent storage, so this delay only applies to the first few requests.

Get a directory listing:

获取文件目录:

https://cdn.jsdelivr.net/gh/jquery/jquery@3.2.1/
https://cdn.jsdelivr.net/gh/jquery/jquery@3.2.1/dist/

例如:
https://cdn.jsdelivr.net/gh/eished/jkforum_helper/

hexo 主题 Matery 加速

分析加载过慢的资源:

  1. GitHub 加载过慢的主要是图片和 js 文件。
  2. 主题 _config.yml 中开启 jsDelivr 加速:
# CDN访问加速 
# 第一次使用本功能,一定要先配置url,再`hexo cl && hexo g && hexo d`部署到GitHub的仓库,注意!必须是GitHub的仓库!
# 如果必须要使用国内的coding或者gitee,可以采用双部署,同时将网站部署到两个仓库(其中一个必须是GitHub的仓库)
# URL配置规则(例子如下): https://cdn.jsdelivr.net/gh/你的GitHub用户名/你的仓库名
# 如果想关闭此功能,将 url地址 注释或删除即可!
#
# 注:配置了此项,就代表着本地调试的时候,网站依然会去GitHub请求资源(原来的资源),本地调试的时候记得将 此项配置 注释或者删除掉
# 更多关于访问速度优化,请查看:https://blog.sky03.cn/posts/42790.html
jsDelivr:
  url: https://cdn.jsdelivr.net/gh/eished/eished.github.io #https://github.com/eished/eished.github.io # https://cdn.jsdelivr.net/gh/skyls03/skyls03.github.io

比较速度:

  • 之前测试没禁用缓存,实际加载速度明显提升。


文章作者: iKnow
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 iKnow !
  目录