GitHub 找不到 React 应用程序的页面页面

GitHub Pages Page Not Found for React App

我正在尝试将我的单页网站发布到 github 个页面,但我 运行 遇到了问题。当我转到我的网站的 link 时,它给我一个 404 错误消息:“找不到文件。在此地址配置的站点不包含请求的文件。如果这是您的站点,请确保文件名大小写与 URL 匹配。对于根 URLs(如 http://example.com/),您必须提供一个 index.html 文件。“

我查看了文档、观看了视频并查找了遇到此问题的其他人,但没有任何方法可以解决它。

我有一个主分支和一个 gh-pages 分支。现在我将其设置为从 gh-pages 分支构建。如果我将其更改为 main,我会遇到同样的问题。

这是我的 package.json 文件:

  "homepage": "http://bpyle02.github.io/portfolio",
  "name": "portfolio",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@craco/craco": "^6.4.2",
    "@testing-library/jest-dom": "^5.11.4",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "aos": "^3.0.0-beta.6",
    "autoprefixer": "9",
    "gh-pages": "^3.2.3",
    "postcss": "7",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-icon": "^1.0.0",
    "react-icons": "^4.3.1",
    "react-router": "^6.0.2",
    "react-router-dom": "^6.0.2",
    "react-scripts": "4.0.3",
    "react-scroll-into-view": "^1.10.1",
    "scroll-into-view": "^1.16.0",
    "tailwindcss": "npm:@tailwindcss/postcss7-compat",
    "web-vitals": "^1.0.1"
  },
  "scripts": {
    "predeploy": "npm run build",
    "deploy": "gh-pages -b main -d build",
    "start": "craco start",
    "build": "craco build",
    "test": "craco test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

这是回购的 link:https://github.com/bpyle02/portfolio

您只需将您的build 文件夹的内容 上传到gh-pages 分支。我认为阅读 how GitHub Pages works.

会对您有所帮助

create-react-app 文档解释了如何解决您的情况:

https://create-react-app.dev/docs/deployment/#github-pages

这是最重要的信息(如果在构建您的应用程序之前不进行设置,它将无法运行):

Open your package.json and add a homepage field for your project:

"homepage": "https://myusername.github.io/my-app",

Create React App uses the homepage field to determine the root URL in the built HTML file.

这可能也与您的应用相关:

https://create-react-app.dev/docs/deployment/#serving-the-same-build-from-different-paths