首页 > 解决方案 > 获取没有 CORS 问题的 GitHub 源 tarball

问题描述

是否可以在没有 CORS 代理的情况下直接在浏览器中从公共 GitHub 存储库下载压缩包?

fetch('https://api.github.com/repos/vadimkantorov/torchwav/tarball/master')

/*Refused to connect to 'https://api.github.com/repos/vadimkantorov/torchwav/tarball/master' because it violates the following Content Security Policy directive: "connect-src mc.admetrica.ru wss://webasr.voicetech.yandex.net mc.yandex.ru yandex.ru".

(anonymous) @ VM24:1
VM24:1 Refused to connect to 'https://api.github.com/repos/vadimkantorov/torchwav/tarball/master' because it violates the document's Content Security Policy.
(anonymous) @ VM24:1
Promise {<rejected>: TypeError: Failed to fetch
    at <anonymous>:1:1}
VM24:1 Uncaught (in promise) TypeError: Failed to fetch
    at <anonymous>:1:1
*/

fetch('https://github.com/vadimkantorov/torchwav/archive/master.tar.gz')
/*VM52:1 Refused to connect to 'https://github.com/vadimkantorov/torchwav/archive/master.tar.gz' because it violates the following Content Security Policy directive: "connect-src mc.admetrica.ru wss://webasr.voicetech.yandex.net mc.yandex.ru yandex.ru".
(anonymous) @ VM52:1
VM52:1 Refused to connect to 'https://github.com/vadimkantorov/torchwav/archive/master.tar.gz' because it violates the document's Content Security Policy.*/

标签: javascriptgithubcors

解决方案


由于提供这些存档的服务不支持 CORS,因此无法在前端(即浏览器)中执行此操作。无论如何,您真的不想一次又一次地为每个用户下载相同的存档,因为 (a) 这很慢并且用户体验不好,并且 (b) GitHub 可能会将其视为 DDoS 并暂停您的存储库。让每个用户都通过 API 请求每个文件同样有问题,甚至更慢。

您需要通过下载此存档一次并通过您的应用程序提供对所需文件的访问来在后端提供某种缓存。这将更快,并导致使用更少的带宽。这也意味着如果用户重新加载页面,他们将受益于您的应用程序为下载的文件提供的缓存,而不必再次重新加载相同的资产。


推荐阅读