22

I asked a question about including resources from GitHub and the answer was to use the raw link:

https://raw.github.com/username/repository/branch/file.js

I am trying to include a script using:

<script
   type="text/javascript"
   src="https://raw.github.com/username/repo/master/src/file.js"
></script>

but I get the following error:

Refused to execute script from 'https://raw.github.com/username/repo/master/src/file.js' because its MIME type ('text/plain') is not executable, and strict MIME type checking is enabled.

Are there any alternatives to fix this error?

Usage example

I don't use this in the production but for a demo page:

project-repository-from-github
  ├─ master
  │   ├─ src
  │   │   └─ my-jQuery-plugin.js
  │   └─ README.md
  └─ gh-pages
      ├─ css
      │   └─ style.css
      └─ index.html

In the index.html page I want to have the latest build of my-jQuery-plugin.js. So, I would include the raw URL to the script.

How do I fix the error?

Community
  • 1
  • 1
Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
  • 1
    Remember that little comment I wrote at the end of my answer about not using this in production? It's because these types of things. You won't be able to change the headers that Github sends with these "raw" files... – Lix Dec 01 '13 at 10:46
  • 1
    @Lix Yeah.. Your prediction was right.. unfortunately. :-) – Ionică Bizău Dec 01 '13 at 10:47

2 Answers2

42

Yes, Github changed this in April, 2013:

We added the X-Content-Type-Options: nosniff header to our raw URL responses way back in 2011 as a first step in combating hotlinking. This has the effect of forcing the browser to treat content in accordance with the Content-Type header. That means that when we set Content-Type: text/plain for raw views of files, the browser will refuse to treat that file as JavaScript or CSS.

But thanks to http://combinatronics.com/ we can include GH scripts. The only change is from raw.github.com that becomes combinatronics.com:

<script
   type="text/javascript"
   src="https://combinatronics.com/username/repo/master/src/file.js"
></script>

The project is hosted on Github being open-source.

And yes, @Lix is correct. The files are not being served from Github but from combinatronics.


Another workaround I found is that instead of:

<script
   type="text/javascript"
   src="https://combinatronics.com/username/repo/master/src/file.js"
></script>

you can use $.getScript jQuery function:

<script>
  $.getScript("https://combinatronics.com/username/repo/master/src/file.js", function () {
    /* do something when loaded */
  });
</script>
Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
  • 5
    Need to warn people that the content is now **not being served from GitHub**. Even though the owner states that they are not affiliated with GitHub, the domain name and similarity to the `raw` subdomain of the *actual* GitHub site is rather misleading and not immediately obvious. – Lix Dec 02 '13 at 18:55
  • 5
    I'm sure the owner is a nice dude, BUT maybe some day he forgets to renew his domain-name on time and some scammer interepts the name and BAM you now have a malware script on your site. BE WARNED. – Alex from Jitbit Dec 25 '14 at 23:10
  • Now they moved to https://rawgit.com for development and even have a CDN on https://cdn.rawgit.com. The ressource name is still the same "username/repo/master/src/..." – Felipe Jan 08 '15 at 16:04
  • @Felipe Thanks. It's nice they make redirects from the old domain. – Ionică Bizău Jan 08 '15 at 16:06
  • [Here](https://stackoverflow.com/a/18049842/5802289) another very similar alternative by using jsdelivr.net. – J0ANMM Jan 11 '21 at 21:43
7

The best solution for me that I found is to use GitHub pages: https://pages.github.com/

The restriction is that you can use only one branch from the repo for keeping your scripts (namely: gh-pages branch)

sample usage
push test.js script to gh-pages branch of the REPO.

the script is out there at yourusername.github.io/REPO/test.js (it may take about 10 minutes to update)

Other solutions
http://rawgithub.com good for development as well as for production (with CDN) if you don't change your scripts (the CDN caches a particular url forever).

Community
  • 1
  • 1
Menua
  • 318
  • 4
  • 6
  • 1
    RawGit is now in a sunset phase and will soon shut down. It's been a fun five years, but all things must end. GitHub repositories that served content through RawGit within the last month will continue to be served until at least October of 2019. URLs for other repositories are no longer being served. If you're currently using RawGit, please stop using it as soon as you can. from [rawgit website](https://rawgit.com/) – Jonatas Emidio Dec 28 '18 at 12:54