当我试图将本地JavaScript文件的链接引用更改为GitHub原始版本时,我的测试文件停止工作。错误是:

拒绝执行脚本…因为它的MIME类型(文本/纯)是不可执行的,并且启用了严格的MIME类型检查。

有没有办法禁用这种行为,或者有没有服务允许链接到GitHub原始文件?

工作代码:

<script src="bootstrap-wysiwyg.js"></script>

故障代码:

<script src="https://raw.github.com/mindmup/bootstrap-wysiwyg/master/bootstrap-wysiwyg.js"></script>

当前回答

GitHub Pages是GitHub针对这个问题的官方解决方案。

生的。githubusercontent使所有文件都使用文本/纯MIME类型,即使文件是CSS或JavaScript文件。因此,访问https://raw.githubusercontent.com/ user / repo / branch / filepath将不是正确的MIME类型,而是一个明文文件,并通过<link href="…/>或<script src="…"></script>将不工作- CSS将不应用/ JS将不运行。

GitHub Pages在一个特殊的URL上托管你的回购,所以你所要做的就是签入你的文件并推送。请注意,在大多数情况下,GitHub Pages要求你提交到一个特殊的分支,gh-pages。

在您的新站点上,通常是https:// user›.github.io/ repo›,提交到g -pages分支的每个文件(最近提交的)都出现在这个url中。所以你可以通过<script src="https:// user›.github.io/ repo›/file.js"></script>链接到你的js文件,这将是正确的MIME类型。

你有构建文件吗?

Personally, my recommendation is to run this branch parallel to master. On the gh-pages branch, you can edit your .gitignore file to check in all the dist/build files you need for your site (e.g. if you have any minified/compiled files), while keeping them ignored on your master branch. This is useful because you typically don’t want to track changes in build files in your regular repo. Every time you want to update your hosted files, simply merge master into gh-pages, rebuild, commit, and then push.

(提示:你可以通过以下步骤在同一个提交中合并和重建:)

$ git checkout gh-pages
$ git merge --no-ff --no-commit master  # prepare the merge but don’t commit it (as if there were a merge conflict)
$ npm run build                         # (or whatever your build process is)
$ git add .                             # stage the newly built files
$ git merge --continue                  # commit the merge
$ git push origin gh-pages

其他回答

Raw.github.com并不是真正的原始访问文件资产, 而是Rails渲染的视图。 所以访问raw.github.com比需要的要重得多。 我不知道为什么raw.github.com被实现为Rails视图。 GitHub没有修复这个路由问题,而是添加了一个X-Content-Type-Options: nosniff报头。

处理:

将脚本放到user.github.io/repo 使用第三方CDN,如rawgit.com。

最简单的方法: <script type="text/plain" src="http://raw.githubusercontent.com/user/repo/branch/file.js"></script> . 由GitHub提供服务,非常可靠。 与文本/平原 没有文本/平原

https://raw.githack.com/

发现这个网站提供了一个CDN

删除nosniff HTTP报头 修复mime类型的ext名称

这个网站:

https://rawgit.com/

注意:RawGit已经达到了它的使用寿命

rawgithub。com重定向到rawgithub。com上面的例子是

http://rawgit.com/user/package/master/link.min.js

我发现错误是由于文件开头的注释导致的,你可以解决这个问题,只需创建自己的文件,不加注释,并推送到git,它就不会显示错误

为了证明你可以尝试这两个文件的相同代码的容易分页:

没有评论

与评论