skip to Main Content

I am having the problem discussed in the React Create App docs about how routers that use the HTML5 pushState history API will fail on static file servers without configuring it to serve index.html everytime. How can I fix this problem while using github pages? Also I’m not using the Create React App or react-scripts

I have tried adding a basename to the BrowserRouter component

<BrowserRouter basename={process.env.PUBLIC_URL}>

Much better explanation of the problem here:

https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/template/README.md#serving-apps-with-client-side-routing

2

Answers


  1. When using react-router-dom with GitHub pages, you’ll need to use HashRouter instead of BrowserRouter. It uses the # fragment of the URL to keep the route, circumventing the lack of support for pushState on GH pages.

    (There are some awkward ways to make pushState for GitHub pages, but I don’t personally recommend them. Here’s a guide for that)

    Login or Signup to reply.
  2. import { HashRouter, BrowserRouter, Route, Switch } from 'react-router-dom';
    ReactDOM.render(
    
          <HashRouter>
            <App />
          </HashRouter>,
      document.getElementById('root')
    );
    

    I resolve this problem use HashRouter.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search