Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gh-pages deployment problems with react-router #1765

Closed
rockchalkwushock opened this issue Mar 8, 2017 · 10 comments
Closed

Gh-pages deployment problems with react-router #1765

rockchalkwushock opened this issue Mar 8, 2017 · 10 comments

Comments

@rockchalkwushock
Copy link

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

@gaearon
Copy link
Contributor

gaearon commented 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 (#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!

@gaearon gaearon closed this as completed Mar 8, 2017
@rockchalkwushock
Copy link
Author

@gaearon

Worked perfectly using process.env.PUBLIC_URL + '/'. Noticed my Page404 will show up on a wrong route but the redirect button doesn't work. I think I will read up on how to make custom 404's through gh-pages and figure out how to integrate that.

Thanks for the help!

@gaearon
Copy link
Contributor

gaearon commented Mar 8, 2017

@rockchalkwushock After you figure out, would you mind sending a PR to User Guide to make this experience better for the next person?

@rockchalkwushock
Copy link
Author

@gaearon sorry for the late response I never got a notification on this. Sure thing I will try and do so this evening.

@Kritika2808
Copy link

Thank you so much for the solution @gaearon

@willdurand
Copy link

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>

@sam-at-work
Copy link

@willdurand your solution is (for react-router users) is the best one. You should make a PR to User Guide for it

bobheadxi added a commit to nwplus/nwhacks2019 that referenced this issue Aug 2, 2018
gh-pages is served on .../nwhacks2019, which will mess with react-router: facebook/create-react-app#1765
@newswim
Copy link

newswim commented Oct 29, 2018

If anyone got here via searching for reach-router, follow Dan's earlier post (#1765 (comment)).

For any Link element, prefix the route:

<Link to={process.env.PUBLIC_URL + '/'}>

Likewise for Route's:

<Route path={process.env.PUBLIC_URL + '/'}>

@nathanchapman
Copy link

nathanchapman commented Dec 3, 2018

None of the suggestions here worked for me (using a custom domain). But I finally found this, which worked for both Netlify and gh-pages:
https://hugogiraudel.com/2017/05/13/using-create-react-app-on-netlify/

I just needed to change the build script to copy the index into the 404 page that gets served:

"build": "react-scripts build && cp build/index.html build/404.html",

Hope this helps someone else out there.

@KrisBierma
Copy link

@nathanchapman Yay!!! I've been searching for hours and this is the only thing that's worked! Thanks

@lock lock bot locked and limited conversation to collaborators Jan 8, 2019
hgarciasoftware referenced this issue in hgarciasoftware/pokemon-basics Feb 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants