在没有CORS问题的情况下获取GitHub源代码tarball



是否可以在浏览器中直接从公共GitHub repo下载tarball而无需CORS代理?

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.*/

在前端(即浏览器(不可能做到这一点,因为提供这些归档的服务不支持CORS。无论如何,你真的不想一次又一次地为每个用户下载相同的存档,因为(a(这很慢,用户体验很差,(b(GitHub可能会将其视为DDoS并挂起你的存储库。让每个用户都通过API请求每个文件也是同样的问题,甚至更慢。

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

只需在url前加上https://cors-anywhere.herokuapp.com/

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

最新更新