这可能是由于Github Pages在渲染React应用时会遇到路径问题,需要在应用的根目录添加一个名为homepage
的属性。可以在package.json
中添加以下代码:
"homepage": "https://[username].github.io/[reponame]"
将[username]
和[reponame]
替换成您的Github用户名和仓库名称。
如果还有问题,建议检查应用的构建过程是否正确,可以将应用的构建输出目录更改为docs
文件夹,这是Github Pages的默认要求,再将homepage
属性更改为:
"homepage": "https://[username].github.io/[reponame]/docs"
参考代码:
// package.json
{
"name": "my-app",
"version": "0.1.0",
"homepage": "https://[username].github.io/[reponame]",
"dependencies": {
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-scripts": "3.4.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"deploy": "npm run build && mv build docs && echo 'my-custom-domain.com' > docs/CNAME && git add docs && git commit -m 'Deploy to GH Pages' && git push",
"test": "react-scripts test",
"eject": "react-scripts eject"
}
}