如何直接从Github链接文件(raw.github.com)

问题描述 投票:48回答:9

我们是否允许直接从Github链接文件?

<link rel="stylesheet" href="https://raw.github.com/username/project/master/style.css"/>
<script src="https://raw.github.com/username/project/master/script.js"></script>

我知道Google Code上允许这样做。这样我就不用担心更新本地文件了。

github github-pages cdn
9个回答
0
投票

当然,为什么不?它甚至更新保存,除非有人在git中更改历史记录并强制推送。


45
投票

已经提到了优质的服务RawGit,但我会把另一个扔进戒指:GitCDN.link

优点:

  • 允许您链接到特定提交,以及自动获取最新(也称为主)
  • 高流量不会造成损坏; RawGit要求它的dev.rawgit.com链接仅在开发期间使用,而GitCDN可以让您访问最新版本,而不会有服务器爆炸的危险
  • 为您提供自动缩小HTML,CSS和JavaScript或以书面形式提供服务的选项(https://min.gitcdn.link)。
  • 添加压缩(GZip)
  • 添加所有正确的标题(内容类型,缓存控制,电子标签等)

完全披露,我是GitCDN.link的项目维护者


41
投票

您可以使用外部服务器rawgithub.com。只需删除单词'raw'和'github'https://raw.github.com/ .. => https://rawgithub.com/之间的点,然后使用它。 More info you find in this question.

然而,根据rawgithub网站,它将在2019年10月底关闭。


13
投票

您需要执行以下步骤

  1. 从github获取文件的原始URL。这就像https://raw.githubusercontent.com/username/folder/example.css
  2. 访问http://rawgit.com/。将git url粘贴到输入框中。它将生成两个URL,一个用于开发,另一个用于生产目的。
  3. 复制其中任何一个,你就完成了。

该文件将充当CDN。您还可以使用gist网址。


12
投票

您可以直接链接到原始文件,但最好不要这样做,因为原始文件始终使用普通/文本标头发送,并且可能导致加载问题。


7
投票

使用名称“gh-pages”为您的项目添加分支,然后您(在分支后不久)能够使用https://username.github.io/project/master/style.css等直接URL(使用您的URL,并假设“style.css”是一个文件, “master”文件夹位于“项目”存储库的根目录中......并且您的Github帐户是“用户名”)。


1
投票

GitHub页面:https://yourusername.github.io/script.js GitHub repo原始文件:https://github.com/yourusername/yourusername.github.io/blob/master/script.js

使用GitHub页面,不要使用原始文件。

原因:GitHub页面基于CDN,原始文件不是。访问原始文件将直接命中GitHub服务器并增加服务器负载。


0
投票

在搜索相同的功能后,我最终编写了自己的PHP脚本作为代理。我遇到的麻烦就是当你从Github获得RAW版本/链接并在你自己的页面中链接到它时,发送的标题是'text / plain'而Chrome没有从JavaScript执行我的Github文件。我也不喜欢使用第三方服务发布的其他链接,因为可能存在明显的安全/篡改问​​题。

因此,使用此脚本,我可以从Github传递RAW链接,让脚本设置正确的标头,然后输出文件,就像它来自我自己的服务器一样。此脚本还可以与安全应用程序一起使用,以引入非安全脚本,而不会抛出“使用非安全链接”的SSL错误警告。

链接:

<script src =“proxy.php?link = https://raw.githubusercontent.com/UserName/repo/master/my_script.js”>

proxy.php

<?php
###################################################################################################################
# 
# This script can take two URL variables
# 
# "type"
#   OPTIONAL
#   STRING
#   Sets the type of file that is output
# 
# "link"
#   REQUIRED
#   STRING
#   The link to grab and output through this proxy script
# 
###################################################################################################################



# First we need to set the headers for the output file
# So check to see if the type is specified first and if so, then set according to what is being requested
if(isset($_GET['type']) && $_GET['type'] != ''){
    switch($_GET['type']){
        case 'css':
            header('Content-Type: text/css');
            break;

        case 'js':
            header('Content-Type: text/javascript');
            break;

        case 'json':
            header('Content-Type: application/json');
            break;

        case 'rss':
            header('Content-Type: application/rss+xml; charset=ISO-8859-1');
            break;

        case 'xml':
            header('Content-Type: text/xml');
            break;

        default:
            header('Content-Type: text/plain');
            break;
    }

# Otherwise, try and determine what file type should be output by the file extension from the link
}else{
    # See if we can find a file type in the link specified and set the headers accordingly

    # If css file extension is found, then set the headers to css format
    if(strstr($_GET['link'], '.css') != FALSE){
        header('Content-Type: text/css');

    # If javascript file extension is found, then set the headers to javascript format
    }elseif(strstr($_GET['link'], '.js') != FALSE){
        header('Content-Type: text/javascript');

    # If json file extension is found, then set the headers to json format
    }elseif(strstr($_GET['link'], '.json') != FALSE){
        header('Content-Type: application/json');

    # If rss file extension is found, then set the headers to rss format
    }elseif(strstr($_GET['link'], '.rss') != FALSE){
        header('Content-Type: application/rss+xml; charset=ISO-8859-1');

    # If css xml extension is found, then set the headers to xml format
    }elseif(strstr($_GET['link'], '.xml') != FALSE){
        header('Content-Type: text/xml');

    # If we still haven't found a suitable file extension, then just set the headers to plain text format
    }else{
        header('Content-Type: text/plain');
    }
}

# Now get the contents of our page we're wanting
$contents = file_get_contents($_GET['link']);

# And finally, spit everything out
echo $contents;
?>

0
投票

如果您的网络服务器有活动的allow_url_include,那么将文件作为原始普通/文本提供服务的GitHub不是问题,因为您可以先将文件包含在PHP脚本中并将其标题修改为正确的MIME类型。

© www.soinside.com 2019 - 2024. All rights reserved.