17

I'm hosting some JSON files on Github pages, but I am unable to use $.getJSON to retrieve them unless they come from the exact same domain.

Is there a way to enable CORS for Github pages?

Daniel X Moore
  • 14,637
  • 17
  • 80
  • 92

4 Answers4

15

Github Pages now has CORS enabled.

The CORS header:

Access-Control-Allow-Origin: *

Is added by default on all responses from Github pages!

Daniel X Moore
  • 14,637
  • 17
  • 80
  • 92
13

As a workaround one can use JSONP, but it's still slightly a pain because the files are static, and each one will need a unique callback method.

Another alternative would be to store the files in Gists and use the Github API which works with CORS.

A third possibility is to not store the files on Github pages and instead host them on an S3/Cloudfront distribution with CORS enabled.

Daniel X Moore
  • 14,637
  • 17
  • 80
  • 92
2

Something to note (may be obvious to others, but tripped me up):

  • URLs like https://github.com/josephrocca/anime-gan-v2-web/blob/main/anime-gan-v2.onnx?raw=true do not have Access-Control-Allow-Origin: * (i.e. can't access from another site). This is the URL that you'll get if you right click on "view raw" and copy the URL.
  • But that URL actually redirects to https://raw.githubusercontent.com/josephrocca/anime-gan-v2-web/main/anime-gan-v2.onnx, which does have the Access-Control-Allow-Origin: * header (as of 2022, at least).

So you can make requests to files on Github from client-side code on other domains if put the URL in the raw.githubusercontent.com format.

This doesn't directly answer the question, but may be useful for people who are just trying to serve content from their Github repo to users who are on a different domain.

joe
  • 3,752
  • 1
  • 32
  • 41
  • 1
    As of January of 2023 I'm unable to pull raw.githubusercontent.com files into kepler.gl as input. I get a CORS error message. – dolphus333 Jan 12 '23 at 21:47
  • @dolphus333 I just opened https://example.com in Chrome, and opened the browser console and ran `await fetch("https://raw.githubusercontent.com/josephrocca/anime-gan-v2-web/main/anime-gan-v2.onnx").then(r => r.arrayBuffer())` and it seems to work fine. Can you give an example URL? – joe Jan 14 '23 at 10:11
  • I might misunderstand the context, but when I use https://raw.githubusercontent.com/hcarter333/rm-rbn-history/main/rm_rnb_history.csv to add data in https://kepler.gl/ I get back a 'Incorrect URL' message. If I add the same file via upload, it works. – dolphus333 Jan 15 '23 at 13:57
0

It's possible with the API, but it has a limit...

"For requests using Basic Authentication or OAuth, you can make up to 5,000 requests per hour. For unauthenticated requests, the rate limit allows you to make up to 60 requests per hour. Unauthenticated requests are associated with your IP address, and not the user making requests."

var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://api.github.com/repos/jimmywarting/diezyweb/contents/index.html?ref=gh-pages');
xhr.setRequestHeader("Accept", "application/vnd.github.3.raw");
xhr.send();

xhr.onload = function(e){
   alert(xhr.response)
}
Endless
  • 34,080
  • 13
  • 108
  • 131
  • 3
    In the question I was specifically asking about Github Pages and not the general Github API, but thanks! – Daniel X Moore Sep 21 '14 at 18:53
  • Would also want to know that... but here is a tip: you can change branch master/gh-pages/etc with ref parameter – Endless Sep 21 '14 at 19:46
  • Yes, but you still cannot, as far as I know, enable CORS for the content that is hosted on Github Pages. So for example any thing at somerepo.github.io/data.json will not be accessible. Using the Github API "works" but it's just another workaround and not a solution. – Daniel X Moore Sep 22 '14 at 15:03