reactjs - React app on gh pages showing 404s for static files
问题描述
I know there have been a lot of questions for this topic but I've tried everything and still no luck.
I have this in my index.js
file
const routing = (
<HashRouter basename={process.env.PUBLIC_URL}>
<div>
<Route exact path="/" component={Home} />
<Route path="/year_2019" render={(props) => <Year {...props} year={`2019`} />} />
<Route path="/year_2018" render={(props) => <Year {...props} year={`2018`} />} />
<Route path="/year_2017" render={(props) => <Year {...props} year={`2017`} />} />
<Route path="/year_2016" render={(props) => <Year {...props} year={`2016`} />} />
<Route path="/decade" render={(props) => <Year {...props} year={`Decade`} />} />
<Route path="/ShowAlbums" component={ShowAlbums} />
<Route path="/ShowSongs" component={ShowSongs} />
<Route path="/collage" component={Collage} />
</div>
</HashRouter>
)
ReactDOM.render(routing, document.getElementById('root'));
I have this in my package.json
{
"name": "albums-site",
"version": "0.1.0",
"private": true,
"homepage": "https://mrkerr.github.io/react-music",
"dependencies": {
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.3",
"@testing-library/user-event": "^7.1.2",
"gh-pages": "^2.1.1",
"react": "^16.12.0",
"react-burger-menu": "^2.6.13",
"react-dom": "^16.12.0",
"react-router": "^5.1.2",
"react-router-dom": "^5.1.2",
"react-scripts": "3.3.0",
"react-twitter-embed": "^3.0.3",
"react-typed": "^1.2.0"
},
"scripts": {
"start": "react-scripts start",
"predeploy": "npm run build",
"deploy": "gh-pages -d build",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
and I have this in my index.html
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
I run npm run deploy
and go to https://mrkerr.github.io/react-music/ and it loads the index.html
but gives 404s for the manifest.json
and says net::ERR_ABORTED 404
for the main.chunk.js
. I'm not sure what else to try, any help is greatly appreciated!
解决方案
All of your asset links are resolving to an incorrect URL. Specifically there is an extra mrkerr
in the path to the assets which is not exactly where they are located.
For the manifest.json specifically, the browser is trying to download
https://mrkerr.github.io/mrkerr/react-music/manifest.json
instead of
https://mrkerr.github.io/react-music/manifest.json
I think the solution is to change your "homepage" in the package.json to "homepage": "https://mrkerr.github.io/react-music"
.
There might also be another configuration option that is setting the PUBLIC_URL
variable incorrectly.
推荐阅读
- chilkat - IPVanish - 等待连接完成超时
- java - Stax XMLStreamReader 检查下一个事件而不继续前进
- wordpress - WordPress youtube iframe 在帖子中更改内容输出功能后无法正常工作
- javascript - ReactJs:从父 URL 获取数据并将其传递给子组件
- javascript - 使用时区和星期几名称字符串格式化完整日期
- mysql - 使用 adonisJs 和 ReactJs 的通知,例如 facebook
- html - Go 中的 HTTPTest - 没有返回 Echo 的正文
- reactjs - Reactjs 发送和接收连续数据
- vb.net - WinForm 捕捉到自定义监视器设置
- ethereum - 为什么构造函数在被引用时不被识别?