GitHub Raw 文件链接获取及 jsDelivr CDN 使用指南

 

在 GitHub 上查看文件的 Raw(原始)视图,可以直接看到文件的纯文本内容,通常用于复制文件内容或用于其他程序访问。

如何在 GitHub 上打开 Raw 视图:

打开你想查看的文件页面(例如一个 .html.js.md 文件)。

在文件内容展示区的右上角,点击 “Raw” 按钮。这个按钮通常位于 “Blame”、“History”等按钮旁边。

点击后,GitHub 会跳转到该文件的原始文本地址,URL 类似于:

https://raw.githubusercontent.com/用户名/仓库名/分支名/文件路径

示例:

如果你在浏览这个文件:

https://github.com/octocat/Hello-World/blob/main/README.md

点击 Raw 后会跳转到:

https://raw.githubusercontent.com/octocat/Hello-World/main/README.md

你可以将这个 Raw 链接复制到其他地方,如 curlwget 下载,或在网页 <script src="..."> 中引用 JavaScript 文件。某些脚本工具、CDN 服务(如 jsDelivr)可以将 GitHub 仓库文件转换为 CDN 链接,避免被 GitHub 限速。注意 有的时候地址会变为内部稳定的某些地址:ref/head/

raw.githubusercontent.com/xykt/NetQuality/refs/heads/main/ref/speedtest_cn.json

实际的地址:raw.githubusercontent.com/xykt/NetQuality/main/ref/speedtest_cn.json删掉后,就可以找到源地址。

jsDelivr

官网: https://www.jsdelivr.com/

用途: 将 GitHub、npm、WordPress 插件等文件通过 CDN 加速。

用法(GitHub)

https://cdn.jsdelivr.net/gh/:user/:repo@version/:path

示例

https://github.com/username/repo/blob/main/script.js
jsDelivr CDN:
https://cdn.jsdelivr.net/gh/username/repo@main/script.js

说明

支持自动缓存、全球加速。@main 表示分支,也可指定 tag 或 commit hash。

举个例子:

NetQuality/ref/speedtest_cn.json at main · xykt/NetQuality

点击文件Raw 就可以跳转到下面

raw.githubusercontent.com/xykt/NetQuality/refs/heads/main/ref/speedtest_cn.json

实际地址,手动修改为:

raw.githubusercontent.com/xykt/NetQuality/main/ref/speedtest_cn.json

CDN 极速的地址

https://cdn.jsdelivr.net/gh/xykt/NetQuality@main/ref/speedtest_cn.json

补充:

github.com/user/repo/raw/branch/file(GitHub 网页跳转)这是 GitHub 的 网页跳转 URL,并不是直接的原始内容链接。手动在浏览器中打开查看文件内容。它会跳转(302)到 raw.githubusercontent.com实际下载。

发表评论

后一页 前一页