Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting 404 for links with create-react-app deployed to github pages

I'm trying to deploy a create-react-app to a relative path on GitHub pages with a custom domain. E.g. www.example.com/myproject

I'm using react-router-dom, react-router-redux and react-router-bootstrap

I've set homepage to http://www.example.com/myproject in packages.json (tried homepage = "." too) and also configured basename for my history:

...
export const history = createHistory({ basename: '/myproject' });
const middleware = [thunk, routerMiddleware(history)];
...
const composedEnhancers = compose(applyMiddleware(...middleware), ...enhancers);
const store = createStore(rootReducer, initialState, composedEnhancers);

The deployed app works on www.mydomain.com/myproject and I can navigate via the app links.

But I got 404 when I try to enter a path directly (eg. www.example.com/myproject/account) or if I do browser refresh on a subpage.

Long term goal is to configure different relative paths for dev and prod environments as described in this answer but first I just need to make it work in deployment.

like image 598
szerte Avatar asked Sep 05 '17 13:09

szerte


People also ask

How do I fix HTTP 404 on GitHub?

If you saw 404 even everything looks right, try switching https/http. The original question has the url wrong, usually you can check repo settings and found the correct url for generated site.

Can I deploy react app to GitHub Pages?

Follow the command below and generate a production build of your app, to deploy your code on GitHub pages. That's it. Your React application is published on GitHub pages and if you want to verify it just go to the settings tab of your application in your Github repository and scroll down.


Video Answer


3 Answers

Problem: URL gets evaluated on server side

When you enter a new URL into address bar of the browser or refreshes the page, browser requests server (in this case GitHub pages server) for that URL. At this point, client side router (react-router) can't take action as it is not yet loaded for that page. Now server looks for a route that matches /accounts won't find it (because routing is done on client side) and returns 404.

Solution

If you had control over the server, you can serve index.html for all routes. This is explained in create react app documentation serving apps with client side routing. As we don't have that control in case of GitHub pages, We can try these.

Easy Solution

Switch from browserHistory to hashHistory With this change, your URLs will go from looking like

www.example.com/myproject/account

to

www.example.com/myproject/#/account

So it's a bit messy.

Harder solution

Get GitHub pages to redirect to index.html on all requests. Basically you have to add a 404.html in your build directory with code to redirect to index.html. More on how to do that.

Create React App has documentation around client-side routing in GitHub pages too

like image 70
sudo bangbang Avatar answered Sep 30 '22 11:09

sudo bangbang


The best way to solve the issue is create a copy of index.html and call it 404.html for production build. To make it add this to package.json scripts:

"build": "react-scripts build && cp build/index.html build/404.html"
like image 37
chrtx Avatar answered Sep 30 '22 11:09

chrtx


The reason is because the browser may not have cached the routing code yet so it causes a 404 error if you go to a route other than the index.

Side note if anyone is using ZEIT Now to deploy (although gh-pages would be similar); I managed (after a deal of time) to work out how to fix it. It was a bit of an effort so I decided to make an article on it for anyone else who gets stuck.

https://itnext.io/fix-404-error-on-single-page-app-with-zeit-now-b35b8c9eb8fb

like image 29
Jack Avatar answered Sep 30 '22 11:09

Jack