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

前端 未结 5 845
灰色年华
灰色年华 2020-12-03 20:01

Ok, so I\'ve made a SPA using React and React-Router and have pushed it to github pages, but none of the routes I have written work when I refresh or click back in my browse

5条回答
  •  情深已故
    2020-12-03 21:02

    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:

    
      {/* routes */}
    
    

    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.

提交回复
热议问题