首页 > 解决方案 > Github 页面未显示网站内容和错误 404

问题描述

我在 github 页面上部署了我的 react 应用程序,我的网站使用多个 API 来显示内容。当我在 github 页面上部署我的 react 应用程序时,网站是空的。我遇到的另一个问题是在重定向到另一条路线时,例如:https://username.github.io/movies甚至https://username.github.io/website/movies,我收到此错误:

404
There isn't a GitHub Pages site here.

If you're trying to publish one, read the full documentation to learn how to set up GitHub Pages for your repository, organization, or user account.

我的 package.json:

"homepage": "https://username.github.io/website",
  "scripts": {
    "predeploy": "npm run build",
    "deploy": "gh-pages -d build",
}

标签: javascriptreactjsgithub-pages

解决方案


如果您确保对链接使用历史操作而不是真实链接,则可以在 Github 页面上运行您的 react 应用程序。您可以编写自己的 Link 组件来包装 a 标签并在单击时执行 history.push,而不是让浏览器处理链接,或者您可以使用 react-router 中的 HashRouter 之类的东西。

如果有人通过 /something 链接进入您的网站,github 将尝试从您的 repo 中加载相应的文件,但该文件不存在。

这也意味着任何指向您网站的直接/热链接仅适用于静态页面,不适用于从您的 React 应用程序动态生成的内容。


推荐阅读