question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Gh-pages deployment problems with react-router

See original GitHub issue

Added react-router and just a Home page and Page404 to the router. When I navigate to the declared homepage: https://rockchalkwushock.github.io/rcws-development/ I am first being directed to my Page404.

I thought that it was possibly because in the router I have 2 paths / & *:

export default () => (
  <Router history={browserHistory} onUpdate={logPageView}>
    <Route path='/' component={App}>
      <IndexRoute component={Home} />
    </Route>
    <Route path='*' component={Page404} />
  </Router>
);

Seeing that my homepage is pointing to /rcws-development/ I thought perhaps this was the issue; because when I use the redirect button on the Page404 I’m sent to https://rockchalkwushock.github.io/. So I changed my homepage: https://rockchalkwushock.github.io/ but no such luck. I then get Github’s 404.

I followed the instructions from the README and went to the following repository after reading that gh-pages does not natively support front-end routing. However I still am first being directed to my Page404 upon visiting the website.

Any ideas why this is? Link to repo

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:11
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

134reactions
gaearoncommented, Mar 8, 2017

The homepage setting only affects paths to JS and CSS in the produced HTML. It can’t affect your routing configuration.

path='/' in your router configuration means you’re literally matching /. Only https://rockchalkwushock.github.io/ would match /.

But your project is on /rcws-development/. However if you change the routing configuration to say /rcws-development/ then this won’t work in development on npm start because the development server serves from /.

Two solutions:

  1. Don’t use HTML5 history on GitHub pages. Use hashHistory instead. Your URLs will look like https://rockchalkwushock.github.io/rcws-development/#path/inside/the/app.

  2. Use process.env.PUBLIC_URL in your route definitions so that they work both in development and after deployment. For example: <Route path={process.env.PUBLIC_URL + '/'}>. This will be empty in development and rcws-development (inferred from homepage) in production.

In the future, we might flip the development server to also take homepage into account (https://github.com/facebookincubator/create-react-app/issues/1582). If this happens, your project would get served at localhost:3000/rcws-development even locally so you’d bump into this issue earlier (and would have to use process.env.PUBLIC_URL in route definitions anyway). But we have not really decided on this yet.

Hope this helps!

99reactions
willdurandcommented, Sep 6, 2017

I know I am a bit late here but the BrowserRouter has a basename prop that can be used for that: https://reacttraining.com/react-router/web/api/BrowserRouter/basename-string.

<BrowserRouter basename={process.env.PUBLIC_URL}>
  {/* routes */}
</BrowserRouter>
Read more comments on GitHub >

github_iconTop Results From Across the Web

React Router not working with Github Pages - Stack Overflow
My Problem: Routes are working fine at my local system but when I deployed my web app to gh-pages I wasn't able to...
Read more >
Problem with react-router app and Github Pages (Solved !)
However, you will notice some problems when deploying the react-router app to the GitHub pages. The solution is to change our path of...
Read more >
How to Deploy a Routed React App to ... - freeCodeCamp
In this article, I'll show you how to create a simple React application that uses routing and then we'll learn how to upload...
Read more >
React-router problem with gh-pages | by Ngoc Vuong - Medium
Recently, I has been working on my personal project created with React, React-router and Redux. Everything worked pretty neat on my local.
Read more >
How to deploy a React app on GitHub pages - Educative.io
Step 1: Create a Github repository · Step 2: Add a homepage key · Step 3: Wrap your routes inside <HashRouter basename="/"> ·...
Read more >

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found