'Including js from raw.github.com

I have a github.com demo page that is linking to https://raw.github.com/.../master/.../file.js so that I don't need to always copy the .js file over to the gh-pages branch every time it's changed. This works in every browser except IE which complains:

SEC7112: Script from https://raw.github.com/cwolves/jQuery-iMask/master/dist/jquery-imask-min.js was blocked due to mime type mismatch

This complaint is coming from the fact that the file is transferred with:

X-Content-Type-Options: nosniff
Content-Type: text/plain

which I can't change.

Anyone have any ideas how to accomplish this same thing? Somehow allowing me to link to the file in the master branch without having to always push it to the gh-pages branch?

Actual page: http://cwolves.github.com/jQuery-iMask/

(Minor update -- I changed the gh-pages in this exact instance to include the .js file, so IE is no longer broken, but would still like any feedback :))



Solution 1:[1]

I can't help you with tricking IE, and I think from that angle what you are looking for is impossible (and discouraged, since that is not the purpose of Github's raw URLs).

However, you can automate committing the changes to gh-pages and pushing to make your life easier. You can do it with a post-commit hook to update the relevant files in the gh-pages branch automatically. I've cooked up such a post-commit script that watches for changes to certain files and commits them to another branch:

#!/bin/sh

WATCH_BRANCH="master"
WATCH_FILES="jquery-imask-min.js"
DEST_BRANCH="gh-pages"

# bail out if this commit wasn't made in the watched branch
THIS_BRANCH=$(git branch --no-color | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/');
if [ "$THIS_BRANCH" != "$WATCH_BRANCH" ]; then
  exit 0
fi

# only update if watched files have changed in the latest commit
CHANGED_FILES=$(git show --pretty="format:" --name-only $WATCH_BRANCH)
if $(echo "$CHANGED_FILES" | grep "^$WATCH_FILES$" -q); then
  # checkout destination branch, then
  # checkout latest version of each watched file and add to index
  git checkout -q $DEST_BRANCH
  git pull -q
  SAVEIFS=$IFS
  IFS=$(echo -n "|")
  for file in $WATCH_FILES; do
    git checkout $WATCH_BRANCH -- $file
    git add $file > /dev/null
  done
  IFS=$SAVEIFS
  # commit with a chance to edit the message, then go back to watched branch
  LATEST_COMMIT=$(git rev-parse $WATCH_BRANCH)
  git commit -m "Also including changes from $WATCH_BRANCH's $LATEST_COMMIT"
  git push origin $DEST_BRANCH
  git checkout -q $WATCH_BRANCH
fi

Note that this is a general script, though I have specified the config vars at the top for your purposes. $WATCH_FILES can be set to a list of files delimited by braces | such as index.html|js/jquery.js. Paths must be specified relative to the root of the repo.

Let me know if you have any questions, and if the script helps you!

Solution 2:[2]

You can try using https://rawgit.com/ service. Just replace raw.github.com with rawgit.com

UPDATE

The Rawgit service (former Rawgithub) has been shutdown.

RawGit has reached the end of its useful life October 8, 2018

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.

Solution 3:[3]

Take a look at raw.githack.com. The idea of this service is inspired from rawgit.com. I just realized that using a whole framework (node.js + express.js) for such simple thing as requests proxying is overkilling, and made same stuff using nginx only.

Replace "githubusercontent" domain name chunk in your github/gist URL with "githack" and you're done!

Furthermore, it supports bitbucket.com - simply replace whole bitbucket domain with bb.githack.com.

Solution 4:[4]

Github's raw URLs aren't designed to be a generic web host. Push that stuff off to proper host, like say pages.github.com.

Solution 5:[5]

Nowadays theres jsDelivr, its open source, free and fast. And supports GitHub! https://www.jsdelivr.com/

Furthermore its from trusted people/company.

I say this because I'm not sure we can trust https://raw.githack.com/

Solution 6:[6]

I am also trying to achieve this. However, I cannot seem to get the solution from @shelhamer to work in my codebase. Below is the updated post-commit hook script that I used to get it working:

#!/bin/sh

WATCH_BRANCH="master"
WATCH_FILES="jquery-imask-min.js"
DEST_BRANCH="gh-pages"

# bail out if this commit wasn't made in the watched branch
THIS_BRANCH=$(git branch --no-color | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/');
if [ "$THIS_BRANCH" != "$WATCH_BRANCH" ]; then
  exit 0
fi

# only update if watched files have changed in the latest commit
CHANGED_FILES=$(git show --pretty="format:" --name-only $WATCH_BRANCH)
if $(echo "$CHANGED_FILES" | grep "^$WATCH_FILES$" -q); then
  # checkout destination branch, then
  # checkout latest version of each watched file and add to index
  git checkout -q $DEST_BRANCH
  git pull -q
  SAVEIFS=$IFS
  IFS=$(echo -n "|")
  for file in $WATCH_FILES; do
    git checkout $WATCH_BRANCH -- $file
    git add $file > /dev/null
  done
  IFS=$SAVEIFS
  # commit with a chance to edit the message, then go back to watched branch
  LATEST_COMMIT=$(git rev-parse $WATCH_BRANCH)
  git commit -m "Also including changes from $WATCH_BRANCH's $LATEST_COMMIT"
  git push origin $DEST_BRANCH
  git checkout -q $WATCH_BRANCH
fi

I had to update the use of grep to make the regex successfully match (-P was not an option on the grep implementation included in Git Bash shell for Windows), add a git pull and a git push origin $DEST_BRANCH. Oh, and I had to add an empty shell of the directories and files in advance (perhaps just the directories would have sufficed?).

Since this is actually doing a push, I think it may be better to switch this script to being a post-receive script instead of post-commit. Otherwise, you could be pushing code to gh-pages that never made it into master [if you choose not to push it].

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 James M. Greene
Solution 2 Michael Kargl
Solution 3
Solution 4 Tekkub
Solution 5 Rodrigo Graça
Solution 6