我的HTML文件如何通过gh-pages访问我的Javascript文件?

时间:2018-08-28 19:11:51

标签: html git github github-pages

我目前在理解gh-pages如何与文件通信一起工作时遇到困难。

我目前在github上的gh-pages分支中有两个文件:bundle.jsindex.html。在本地运行此文件时,index.html可以通过在文件路径中移动来访问bundle.js。在线运行此程序时,我遇到问题,无法使index.html正确访问bundle.js

第一次尝试:我通过URL https://raw.githubusercontent.com/...找到了原始文本。不幸的是,这不起作用,因为存在MIME类型错误。

第二次尝试:我发现还有另一个原始文本来源,没有MIME类型错误,URL为:https://cdn.rawgit.com/...。不幸的是,这无法正常工作,因为cdn.rawgit.com不会自行更新。

第三次尝试:我发现我可以使用与第二次尝试相同的方法,但是我可以使用特定的提交来访问正确的cdn.rawgit.com/.../<commit path>。这似乎使事情变得更加困难,因为每次我要更新页面时都必须更新index.html。这似乎也是不可能的,因为在提交之前我怎么知道提交号?

相关代码是<script src = "https://cdn.rawgit.com/NumaK...">行。 我无法弄清楚我的gh-pages网站应该如何使用正确版本的bundle.js

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, 
    shrink-to-fit=no">
    <title>Numa Karolinski</title>
  </head>
  <body>
    <div id="root"></div>
    <noscript>
    Hello There. Code can be found in the "root" folder above ^^^ !
    </noscript>
    <script src="https://cdn.rawgit.com/NumaKarolinski/PersonalWebsite/ 
    websiteVersion1/dist/bundle.js"></script>
  </body>
</html>

1 个答案:

答案 0 :(得分:1)

在您的 index.html 中,替换:

<script src="https://cdn.rawgit.com/NumaKarolinski/PersonalWebsite/websiteVersion1/dist/bundle.js"></script>
<script type="text/javascript" src="C:\Users\numak\Desktop\MyWebsite\dist/bundle.js"></script></body>

作者

<script src="bundle.js"></script>