DEV Community

loading...
Cover image for Page Not Found Error on Netlify Reactjs React Router solved

Page Not Found Error on Netlify Reactjs React Router solved

rajeshroyal profile image Rajesh Royal Originally published at rajeshroyal.com on ・3 min read

So you made a Single Page Application and It’s working great on localhost npm or yarn server. And now you made a build of this site and deploy it to the Netlify.

nooo… I got an error.

Page Not Found

Looks like you’ve followed a broken link or entered a URL that doesn’t exist on this site.

Many might run into the following error after deploying a single page React site using React Router to Netlify.

netlify page not found error react router after deploy
Netlify page not found error react-router after deploy

How did the error occur?

React Router handles routing on the client-side (browser) so when you visit the non-root page (e.g. https://yoursite.netlify.com/login), Netlify (server-side) does not know how to handle the route.

(As your routes are set up in the root level).

And the error occurring on Netlify when you go to https://<netlify domain>/route directly.

netlify site not found error react router
Page Not Found Demo Netlify

How can we fix it?

Netlify offers a special file, _redirects, which you can add to your codebase and let Netlify handle how to handle the URL that is not handled on the client-side.

Netlify documentation has a section, History Pushstate and Single Page Apps, which shows you how to redirect to the root of your SPA URL (but doesn’t mention React Router, as it applies to other client-side frameworks/libraries).

So to fix the issue, we need to create a file named _redirects to the root of our site [public folder of App] with the following content.

/* /index.html 200

View file on github

redirects file content netlify page not found spa error solved
redirects file content

Here is the working example site: React Todo App with firebase Authentication.

solved netlify error video
Netlify successfully redirected to the SPA routes

For more ways & restrictions for redirect, check out the official Netlify documentation.

Resources

The post Page Not Found Error on Netlify Reactjs React Router solved appeared first on Rajesh Royal.

Discussion (24)

pic
Editor guide
Collapse
felipekafuri profile image
Felipe Ramos Kafuri

Thanks thats saved me haha

Collapse
rajeshroyal profile image
Rajesh Royal Author

you're welcome 😀

Collapse
mrisrael profile image
MrIsrael

Thanks a lot, man!

Collapse
jarodsim profile image
Collapse
kmanadkat profile image
Krupesh Anadkat

Thanks man :)

Collapse
rajeshroyal profile image
Rajesh Royal Author

you're welcome 😀

Collapse
nstsfd profile image
Can

Thank you! That was what I needed.

Collapse
sahla profile image
Sahl Khalifa

Thank you. Simple & effective

Collapse
devcoder profile image
devcoder

life saver!, thanks for this, i spent days researching this issue and was about to give up

Collapse
zaqar profile image
Za-Qar

This is very helpful, thank you!

Collapse
dhruvindsddev profile image
Collapse
zubisofts profile image
zubisoft

Wow! Thanks a lot man!

Collapse
kizito007 profile image
Kizito Nwaka

Thanks 🙏

Collapse
hamzaaouni profile image
Hamza-Aouni

Arigato !!

Collapse
matcs06 profile image
Mateus Costa Silva

You saved me!! Thank you so much!!!

Collapse
codeclassifiers profile image
codeclassifiers

Thank you so much for this fix. This is a really useful tip.

Collapse
nutlope profile image
Hassan El Mghari

You are a live saver my man

Collapse
guimaraesmiguel profile image
Miguel Guimaraes

I had the same issue and it worked for me. Thank you man!

Collapse
mohamed_atta98 profile image
mohamed atta

Thanks man 🙏🙏🙏

Collapse
largenty profile image
Largenty

You are the best!

Collapse
osamakhalil98 profile image
osama

Thanks man <3

Collapse
spiropoulos94 profile image
NikosSp

You are awesome, thanks!!!
I have on question though, will I have the same problem if I deploy my app to another hosting provider?
Or is it a strictly netlify issue??

Collapse
rajeshroyal profile image
Rajesh Royal Author

depends on the configuration of that host, please read their documentation related to routing.

Collapse
jonatasoc profile image
Jonatas de Oliveira Coêlho

Hey, Rajesh! You helped me a lot! Thank you very much!