How To Fix MIME Type Error When Linking To Github Resource

By: Yusuf Odukoya /
 September 15, 2021
  / < 1 minute
Categories: Coding

The MIME type error can happen when you attempt to hotlink a JavaScript file hosted on GitHub in your <script src=""> tag. GitHub has explicitly disabled JavaScript hotlinking, and newer versions of browsers respect that setting.

In the year 2013, GitHub started using X-Content-Type-Options: nosniff, which instructs modern browsers to enforce strict MIME type checking. For browsers that honour this setting, they would be unable to parse or process the file as intended, instead, they return the raw files as is.

In other words, you’ll find an error in your console, similar to what is shown below;

Refused to execute https://raw.githubusercontent.com/username/repo/branch/path/file.js as script because "X-Content-Type-Options: nosniff" was given and its Content-Type is not a script MIME type.

To permanently solve this problem, follow the jsdelivr CDN workaround below;

Steps:

  1. Find the file you are linking to on GitHub, and click to the “Raw” version.
  2. Copy the URL.
  3. Replace raw.githubusercontent.com with cdn.jsdelivr.net
  4. Add /gh/ just before your username.
  5. Remove the branch name.

Here is an illustration;

From

<script src="http://raw.githubusercontent.com/<username>/<repo>/<branch>/path/to/file.js">

to 👇🏽

<script src="http://cdn.jsdelivr.net/gh/<username>/<repo>/path/to/file.js">

I hope that helps.

You May Also Like: Free Static Website Hosting With Wordpress, Namecheap, & Github Pages

Share:

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on whatsapp
WhatsApp
close
Share via
Copy link
Powered by Social Snap