I don't think GitHub allows to use its raw hosted files elsewhere and hence Github has blocked requests from Cross Domains.
It's a server-side configuration and you cannot enable CORS on Github servers.
An alternative way would be to host the file in some CDN or download the file and keep it locally.
Another alternative I found was to download the contents of the file via JS and then append it as style element.
Here is the updated CodeSandbox which works!
I never worked in React but I am sure you know better what would be the best place to keep the function that downloads the file.
function loadCssDynamically(fileUrl){
// read text from URL location
var request = new XMLHttpRequest();
request.open('GET', fileUrl, true);
request.send(null);
request.onreadystatechange = function () {
if (request.readyState === 4 && request.status === 200) {
var type = request.getResponseHeader('Content-Type');
if (type.indexOf("text") !== 1) {
var style = document.createElement('style');
style.innerHTML = request.responseText;
document.head.appendChild(style);
// console.log(request.responseText);
// return request.responseText;
}
}
}
}
var file = "https://gist.githubusercontent.com/eirikbakke/1059266/raw/d81dba46c76169c2b253de0baed790677883c221/gistfile1.css";
loadCssDynamically(file);
className