英文:
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 ofproject4
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 frommain
togh-pages
in the repository settings, all my other projects stop working. For example, I error 404 when accessinghttp://username.github.io/my-projects/project2
. I had to change the branch back tomain
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 ofproject4
. This already works.http://username.github.io/my-projects/project4/build
should show the production build ofproject4
. 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 tomain
, this also works.
References
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
我成功修复了问题,以下是修复步骤:
- 在
package.json
文件中设置"homepage"
为http://username.github.io/my-projects/project4/build
。 - 运行
cd project4
,然后使用npm run build
更新生产构建。 - 运行
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:
- Set
"homepage"
inpackage.json
file tohttp://username.github.io/my-projects/project4/build
. cd project4
and update production build withnpm run build
.git push
: Push updated production build andpackage.json
file tomain
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论