Use GitHub for hosting JS and CSS files

How to use your CSS and JS files direcly from GitHub

Recently I've started JQuery plugin project on GitHub and decided to provide examples via JSFiddler. I could easily take latest files and host it on the same host as this blog but that would make a headache for me as I would have to update files on my host as soon as I update file in GitHub.

So I googled a little bit and found a solution. People from GitHub were thinking about this so they provided a raw link for the files and it is pretty easy to use it. I'll show it on example for my JQuery plugin I'm working on currently.

First navigate to a file though GitHub website. For my project this will be as following:
 https://github.com/dejanstojanovic/JQuery-GoogleMaps/blob/master/src/jquery.googlemaps.js

If you reference this in your fiddle it won't work, because this url returns a page. What you have to do is to change URL a bit to get a raw file as a response.

  1. instead of github.com put rawgithub.com
  2. remove blob from the URL address

After these changes URL will be:
https://rawgit.com/dejanstojanovic/JQuery-GoogleMaps/master/src/jquery.googlemaps.js

This is fine to use it for you fiddles, but I do not recommend to use this as a CDN because simply GitHub is not a CDN. Time of response is not that short, so using it as a CDN is not a good idea, bot for using in fiddles makes it really useful.

Disclaimer

Purpose of the code contained in snippets or available for download in this article is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.


About the author

DEJAN STOJANOVIC

Dejan is a passionate Software Architect/Developer. He is highly experienced in .NET programming platform including ASP.NET MVC and WebApi. He likes working on new technologies and exciting challenging projects

CONNECT WITH DEJAN  Loginlinkedin Logintwitter Logingoogleplus Logingoogleplus

.NET

read more

SQL/T-SQL

read more

Umbraco CMS

read more

PowerShell

read more

Comments for this article