2011-08-24 75 views
53

我有一個github.com演示頁面,鏈接到https://raw.github.com/.../master/.../file.js這樣每次更改時,我都不需要始終將.js文件複製到gh-pages分支。這個工程除了IE每一個瀏覽器,抱怨:包括js from raw.github.com

SEC7112:從https://raw.github.com/cwolves/jQuery-iMask/master/dist/jquery-imask-min.js腳本被封鎖由於MIME類型不匹配

此投訴的事實,文件與轉移過來:

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

我不能改變。

任何人都有什麼想法如何做到這一點?不知怎的,允許我鏈接到master分支中的文件,而不必將其始終推送到gh-pages分支?

實際頁:http://cwolves.github.com/jQuery-iMask/

(小更新 - 我改變了這個確切實例的GH-頁,以包括.js文件,那麼IE將不再打破,但仍希望任何反饋:))

回答

14

我無法幫助你欺騙IE,我認爲從這個角度來看,你正在尋找的是不可能的(因爲這不是Github的原始URL的目的,所以不鼓勵)。

但是,您可以自動將更改提交到gh-pages並推動讓您的生活更輕鬆。您可以使用post-commit hook來自動更新gh-pages分支中的相關文件。我已經熟了這樣的post-commit腳本,手錶更改某些文件,並將它們提交到另一個分支:

#!/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 

注意,這是一個普遍的劇本,雖然我指定的配置瓦爾頂部爲您目的。 $WATCH_FILES可以設置爲由大括號|分隔的文件列表,如index.html|js/jquery.js。路徑必須相對於回購的根來指定。

讓我知道你是否有任何問題,如果腳本可以幫助你!

5

Github的原始URL並非設計成通用的Web主機。將這些東西推送到適當的主機上,比如說說pages.github.com。

+2

是的,這樣做的問題是,每次我提交一個提交給master切換到分支和複製文件的頁面時,它都需要額外的工作。我只是試圖避免這個,因爲它似乎沒有必要 –

0

我也試圖做到這一點。但是,我似乎無法從@shelhamer獲得解決方案以在我的代碼庫中工作。下面是得到它的工作更新的post-commit鉤子,我用的腳本:

#!/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 

我不得不更新使用grep來使正則表達式匹配成功(-P是不是對的grep執行的選項中包括在Windows的Git Bash shell中),添加一個git pull和一個git push origin $DEST_BRANCH。噢,我必須事先添加一個空目錄和文件的shell(也許只是目錄已經足夠了?)。

因爲這實際上是在推送,所以我認爲最好將這個腳本切換爲post-receive腳本而不是post-commit。否則,你可能會推動代碼到gh頁面,從來沒有成爲主人[如果你選擇不推動它]。

59

您可以嘗試使用https://rawgithub.com/服務。 只需將raw.github.com替換爲rawgithub.com即可。

UPDATE

Rawgithub現在Rawgit:https://rawgit.com/並增加了生產CDN爲好。

+1

因爲在那種情況下,你已經在使用一個網站沒有與github affillated,你不妨使用http://cdnjs.com/ –

+0

謝謝,rawgithub是完美的。 –

+2

另請參閱[raw.githack.com](http://raw.githack.com)。這個想法是一樣的,但它是一個普通的nginx解決方案,這意味着它應該工作得更快一點:) – neoascetic

6

看看raw.githack.com。這項服務的想法來自於rawgit.com。我只是意識到,使用整個框架(node.js + express.js)來完成簡單的事情,比如請求代理過度使用,並且只使用nginx製作相同的東西。

用「githack」替換github/gist URL中的「githubusercontent」域名塊,就完成了!

此外,它支持bitbucket.com - 簡單地用bb.githack.com替換整個bitbucket域。