Can I create routes with react-router for a github-pages site?

岁酱吖の 提交于 2019-11-28 01:39:05

I think you need to change your browserHistory to a hashHistory.. so you can use it with gh... it changes path from /home to #/home

If you are using create-react-app (I haven't tested this in any other environment) you can use browserRouter, you will need to pass a basename prop to the component with this env variable: process.env.PUBLIC_URL.

Your router should now look like this:

<BrowserRouter basename={process.env.PUBLIC_URL}>
  {/* routes */}
</BrowserRouter>

For more info you can checkout this Github thread

Use process.env.PUBLIC_URL in your route definitions so that they work both in development and after deployment. For example: . This will be empty in development and ... (inferred from homepage) in production.

Aside from using hashHistory as suggested in the accepted answer, there is another workaround. Look here.

Basically, you create a spoof 404.html file which has a script that converts the requested path into the query string & redirects the browser to the index page with the query string attached to the URL. After the index file is loaded, the original path is restored from the query string & ReactRouter picks up the changes.

A neat solution, but not production-ready, either.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!