107

Serving javascript libraries from a CDN instead of your own server comes with tremendous advantages. Less work for your server, possibility for the CDN to have a copy closer to the user than your server, but most importantly a good chance that your user's browser already has it cached from that URL. The last one means less total work for everybody, so it's clearly a win all around, and is more likely the more often we (developers) rely on the CDNs to serve our javascript.

But the popular javascript CDNs (Google, Microsoft, others?) only host a small number of files. For others we have the choice of hosting them ourselves, or ... using the source control server as a kind of CDN. It's unlikely Github or similar has a geographically distributed cache of files optimized for serving globally. But if it's common practice, then there is a decent chance that the user's browser will have it cached. The argument of off-loading work from our servers to github is only valid if Github has willingly volunteered to do this.

So, is it common practice? Should we encourage each other to do this? Does Github mind? Do they have an official policy stated?

Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
Leopd
  • 41,333
  • 31
  • 129
  • 167
  • 6
    What happens if the author re-arranges his file structure? It's not his problem code on hundreds of websites breaks. – Raynos Mar 31 '11 at 15:55
  • 2
    @Raynos If you are the "author" of the GitHub repo then you control the changes. – Chris Jacob May 17 '11 at 14:46
  • 2
    @ChrisJacob that's the point. If I change my own file structure it's not my problem your pointing at a piece of code that no longer exists. – Raynos May 17 '11 at 15:09
  • 5
    You can use https://rawgithub.com/ to share work-in-progress HTML, JavaScript, or CSS with someone for a quick demo, or maybe to use it in a jsPerf test. – Giovanni Cappellotto Aug 10 '13 at 13:05
  • The question should be reopen as there is now a right answer in the above comment from @GiovanniCappellotto. – Supersharp Jan 10 '17 at 13:28

5 Answers5

103

You should not do that for JavaScript files if you care about performance or IE9 compatibility.

GitHub doesn't serve its "raw" files with a far-future expires header. Without the possibility of cross-site caching, you lose the biggest benefit of using a public CDN to host your JavaScript. In fact, using GitHub as a CDN will be slower than simply hosting the files on your own server after each user's first request for the file (assuming you configure caching correctly on your server).

Another problem is that GitHub doesn't serve "raw" files with a content-type header that matches the file's actual MIME type. In IE9 (and perhaps other browsers/proxies/firewalls/etc), JavaScript files that aren't served with the correct content-type are blocked by default. You can see that in action on the BlockUI demo page, for example:

enter image description here

Dave Ward
  • 59,815
  • 13
  • 117
  • 134
  • RawGit serves raw files directly from GitHub with proper Content-Type headers. Use a specific tag or commit hash in the URL (not a branch). Files are cached permanently based on the URL. https://rawgit.com/ – Kerem Baydoğan May 02 '16 at 12:10
19

Linking to GitHub "raw" files has some issues as outlined by Dave Ward's answer.

I suggest you look into GitHub Pages as an option.

Read this article:
GitHub as a CDN. Cache your Javascripts, Stylesheets and Web Assets with GitHub Pages.

anderium
  • 194
  • 5
  • 11
Chris Jacob
  • 11,878
  • 7
  • 47
  • 42
12

This was recently asked in github's support forums, and the official answer was that it's ok.

Having said that, I agree with other answers: github was never really meant to be a CDN, while Google and Microsoft have specific infrastructure for that.

Mauricio Scheffer
  • 98,863
  • 23
  • 192
  • 275
  • 7
    To clarify. That support forum response is in relation to the article I've linked to in my answer (GitHub Pages as CDN - not GitHub "raw" files): http://stackoverflow.com/questions/5502540/should-github-be-used-as-a-cdn-for-javascript-libraries/6032500#6032500). – Chris Jacob May 17 '11 at 14:40
10

It's fine for prototyping / personal stuff, but for production I would look at:

http://www.cdnjs.com/

http://cachedcommons.org/ -- no longer available

meleyal
  • 32,252
  • 24
  • 73
  • 79
  • I hope you know CachedCommons.com is just serving from github.com – ocodo Dec 27 '12 at 06:00
  • But the urls seem to point at CachedCommons http://cachedcommons.org/cache/mootools/1.2.4/javascripts/mootools.js, could be a proxy I guess. – meleyal Jan 08 '13 at 09:08
  • Sorry, what I mean is the old info about using Github as a CDN no longer applies, apparently, and it's all good. Apart from possible availability issues in the future, it's worth forking a library to avoid this. – ocodo Jan 09 '13 at 06:23
  • 2
    Looks like linking to Github will soon not be an option: https://github.com/blog/1482-heads-up-nosniff-header-support-coming-to-chrome-and-firefox – meleyal May 02 '13 at 10:27
  • 1
    Actually that's not an issue, if you are going to use a GitHub based resource as a cdn, just make sure it's hosted as part of a static pages site (now GitHub.io) - That's how you should've been doing it in the first place by the way. – ocodo May 03 '13 at 00:19
  • @meleyal cachedcommons.org is down, domain is on sale !! – Akash Kava May 10 '16 at 07:42
-2

I'm doing it for months now, had some concerns first but it's totally cool if you have no problems with your files being publicly available, use minified versions if you care.

But still - Google & MS rule the space for jQuery & jQuery Templates - so I use them for that.

ezmilhouse
  • 8,933
  • 7
  • 29
  • 38