如何将主分支中子目录中的React项目部署到GitHub Pages。

huangapple go评论67阅读模式
英文:

How to deploy a react project found in a subdirectory in the main branch to Github Pages

问题

上下文

我有一个名为my-projects的GitHub仓库,其中包含多个文件夹,每个文件夹代表一个独立的Web项目。此存储库的GitHub Pages站点当前是从main分支(而不是gh-pages分支)构建的,并且GitHub页面具有GitHub Jekyll主题。

存储库的简化文件结构如下:

my-projects/
├─ project1/
│  ├─ index.html
│  ├─ README.md
├─ project2/
│  ├─ index.html
│  ├─ README.md
├─ project3/
│  ├─ README.md
│  ├─ dist/
│  │  ├─ index.html
├─ project4/
│  ├─ README.md
│  ├─ node_modules/
│  ├─ public/
│  ├─ build/
│  ├─ package.json
│  ├─ package-lock.json
│  ├─ src/
├─ config.yml
├─ README.md

一些项目在dist文件夹中具有生产构建。以下链接都是由GitHub Pages部署的可用网站:

  • http://username.github.io/my-projects/
  • http://username.github.io/my-projects/project1
  • http://username.github.io/my-projects/project2
  • http://username.github.io/my-projects/project3/dist

部署React应用程序project4时的问题

project4文件夹是使用create-react-app生成的React应用程序。以下是package.json文件:

{
  "name": "my-projects",
  "version": "0.1.0",
  "private": true,
  "homepage": "http://username.github.io/my-projects/project4",
  "dependencies": {
    "@testing-library/jest-dom": "^5.16.5",
    "@testing-library/react": "^13.4.0",
    "@testing-library/user-event": "^13.5.0",
    "bootstrap": "^5.2.3",
    "date-fns": "^2.29.3",
    "react": "^18.2.0",
    "react-bootstrap": "^2.7.0",
    "react-dom": "^18.2.0",
    "react-icons": "^4.7.1",
    "react-scripts": "5.0.1",
    "react-to-print": "^2.14.12",
    "uniqid": "^5.4.0",
    "web-vitals": "^2.1.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts 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"
    ]
  }
}

我使用npm run build生成了生产构建,并将build文件夹推送到GitHub。然而,将新创建的构建文件夹推送到GitHub后,我无法查看网站。我尝试访问以下链接,但没有成功:

  • http://username.github.io/my-projects/project4:显示project4的README而不是实际项目

  • http://username.github.io/my-projects/project4/build:显示一个空白页面,并显示以下控制台错误:

main.80069716.js:1          Failed to load resource: the server responded with a status of 404 ()
main.0634878b.css:1          Failed to load resource: the server responded with a status of 404 ()
manifest.json:1          Failed to load resource: the server responded with a status of 404 ()
manifest.json:1 Manifest: Line: 1, column: 1, Syntax error.
main.0634878b.css:1          Failed to load resource: the server responded with a status of 404 ()
manifest.json:1          Failed to load resource: the server responded with a status of 404 ()
DevTools failed to load source map: Could not load content for chrome-extension://gighmmpiobklfepjocnamgkkbiglidom/browser-polyfill.js.map: System error: net::ERR_FILE_NOT_FOUND
DevTools failed to load source map: Could not load content for chrome-extension://clldacgmdnnanihiibdgemajcfkmfhia/contentScript.js.map: System error: net::ERR_BLOCKED_BY_CLIENT

我尝试过的解决方法

  • 我尝试按照此教程使用gh-pages分支,但不幸的是,它仍然无法按预期工作。如果我在存储库设置中将GitHub Pages分支从main更改为gh-pages,所有其他项目都无法正常工作。例如,访问http://username.github.io/my-projects/project2时会出现404错误。我必须将分支更改回main才能使其正常工作。
  • 我尝试将package.json中的homepage设置为,但获得了与之前相同的控制台错误的空白页面。
  • 我尝试将package.json中的homepage设置为http://username.github.io/my-projects/project4/build/,但获得了与之前相同的控制台错误的空白页面。

总结一下,这是我想要实现的:

  • http://username.github.io/my-projects/project4应显示project4的README。这已经可以工作。
  • http://username.github.io/my-projects/project4/build应显示project4的生产构建。这还没有成功。
  • 所有其他网站(例如http://username.github.io/my-projects/project2)仍然应该是可访问的。当前将分支设置为main时,这也是有效的。

参考文献

[https://stackoverflow.com/questions/68978742/react-app-shows-a-blank-page-after-deployment-to-github-pages](https://stackoverflow.com/questions/68978742/react-app-shows

英文:

Context

I have a github repository my-projects which contains multiple folders each representing a separate web project. The GitHub Pages site for this repository is currently being built from the main branch (not gh-pages branch) and the Github page has a Github Jekyll theme.

The simplified file structure of the repository is as follows:

my-projects/
├─ project1/
│  ├─ index.html
│  ├─ README.md
├─ project2/
│  ├─ index.html
│  ├─ README.md
├─ project3/
│  ├─ README.md
│  ├─ dist/
│  │  ├─ index.html
├─ project4/
│  ├─ README.md
│  ├─ node_modules/
│  ├─ public/
│  ├─ build/
│  ├─ package.json
│  ├─ package-lock.json
│  ├─ src/
├─ config.yml
├─ README.md


Some of the projects have the production build in the dist folder.
The following links are all working websites deployed by Github Pages:

  • http://username.github.io/my-projects/
  • http://username.github.io/my-projects/project1
  • http://username.github.io/my-projects/project2
  • http://username.github.io/my-projects/project3/dist

Issue with deploying react-app project4

project4 folder is a react app which was generated from create-react-app.
Here’s the package.json file:

{
  "name": "my-projects",
  "version": "0.1.0",
  "private": true,
  "homepage": "http://username.github.io/my-projects/project4",
  "dependencies": {
    "@testing-library/jest-dom": "^5.16.5",
    "@testing-library/react": "^13.4.0",
    "@testing-library/user-event": "^13.5.0",
    "bootstrap": "^5.2.3",
    "date-fns": "^2.29.3",
    "react": "^18.2.0",
    "react-bootstrap": "^2.7.0",
    "react-dom": "^18.2.0",
    "react-icons": "^4.7.1",
    "react-scripts": "5.0.1",
    "react-to-print": "^2.14.12",
    "uniqid": "^5.4.0",
    "web-vitals": "^2.1.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts 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"
    ]
  }
}

I generated the production build with npm run build and pushed the build folder to Github. However after pushing the newly created build folder to github, I am unable to view the website. I tried accessing the following links but had no success:

  • http://username.github.io/my-projects/project4 : Displays README of project4 instead of actual project

  • http://username.github.io/my-projects/project4/build : Displays a blank page with the following console errors:

main.80069716.js:1          Failed to load resource: the server responded with a status of 404 ()
main.0634878b.css:1          Failed to load resource: the server responded with a status of 404 ()
manifest.json:1          Failed to load resource: the server responded with a status of 404 ()
manifest.json:1 Manifest: Line: 1, column: 1, Syntax error.
main.0634878b.css:1          Failed to load resource: the server responded with a status of 404 ()
manifest.json:1          Failed to load resource: the server responded with a status of 404 ()
DevTools failed to load source map: Could not load content for chrome-extension://gighmmpiobklfepjocnamgkkbiglidom/browser-polyfill.js.map: System error: net::ERR_FILE_NOT_FOUND
DevTools failed to load source map: Could not load content for chrome-extension://clldacgmdnnanihiibdgemajcfkmfhia/contentScript.js.map: System error: net::ERR_BLOCKED_BY_CLIENT

What I tried

  • I tried to follow this tutorial which uses gh-pages branch but unfortunately it still does not work as desired. If I change the Github Pages branch from main to gh-pages in the repository settings, all my other projects stop working. For example, I error 404 when accessing http://username.github.io/my-projects/project2. I had to change the branch back to main to get it to work.
  • I tried setting homepage in package.json to . but got the a blank page with same console errors as before.
  • I tried setting homepage in package.json to http://username.github.io/my-projects/project4/build/ but got the a blank page with same console errors as before.

So to summarize, here's what I want to achieve:

  • http://username.github.io/my-projects/project4 should show the README of project4. This already works.
  • http://username.github.io/my-projects/project4/build should show the production build of project4. This does not work yet.
  • All other websites (for example http://username.github.io/my-projects/project2) should still be accessible. With the branch currently set to main, this also works.

References

https://stackoverflow.com/questions/68978742/react-app-shows-a-blank-page-after-deployment-to-github-pages

https://github.com/facebook/create-react-app/issues/478

https://stackoverflow.com/questions/58149928/deploying-react-app-to-github-pages-404-page-not-found

答案1

得分: 0

我成功修复了问题,以下是修复步骤:

  1. package.json 文件中设置 "homepage"http://username.github.io/my-projects/project4/build
  2. 运行 cd project4,然后使用 npm run build 更新生产构建。
  3. 运行 git push:将更新的生产构建和 package.json 文件推送到 Github 的 main 分支。

部署的网站可以在 http://username.github.io/my-projects/project4/build 找到,README 可以在 http://username.github.io/my-projects/project4 访问。所有其他项目仍然可以通过其各自的链接访问。

英文:

I managed to fix the issue with the following steps:

  1. Set "homepage" in package.json file to http://username.github.io/my-projects/project4/build.
  2. cd project4 and update production build with npm run build.
  3. git push : Push updated production build and package.json file to main branch on Github.

The deployed website will be found at http://username.github.io/my-projects/project4/build and the README will be accessible at http://username.github.io/my-projects/project4. All other projects are still accessible at their respective links.

huangapple
  • 本文由 发表于 2023年3月12日 16:15:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75711837.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定