2

I added a No Match 404 error page to my website, and it works fine when I go to the wrong path on localhost, but after I deploy and go to an invalid path like .../invalidpath I get the default netlify page not found error. Below is my App.js component:

import React from 'react';
import { Switch, Route } from 'react-router-dom';
import Navigation from './components/navigation/Navigation';
import Home from './pages/home/Home';
import Projects from './pages/projects/Projects';
import Contact from './pages/contact/Contact';
import NoMatch from './pages/404page/404Page';

import './App.scss';

function App() {
  return (
    <div className='app'>
      <Navigation />
      <Switch>
        <Route exact path='/' component={Home} />
        <Route path='/projects' component={Projects} />
        <Route path='/contact' component={Contact} />
        <Route path='*' component={NoMatch} />
      </Switch>
    </div>
  );
}

export default App;
Drew Reese
  • 165,259
  • 14
  • 153
  • 181
  • Have you tried *not* specifying a `path` for your 404 route? This is the more typical way of defining a 404 route. I don't know if it'll effect netlify however. – Drew Reese Mar 14 '20 at 23:49
  • Yeah I tried it without the path and still wouldn’t work. –  Mar 14 '20 at 23:54

1 Answers1

4

If you've built the app with create-react-app and you're deploying to Netlify, you need to add a _redirects file inside the public directory of your project in order to support client side routing (React Router). It should have this inside:

/*  /index.html  200

Details here. Also, check the official docs from Netlify regarding for Redirects and rewrites

Juan Marco
  • 3,081
  • 2
  • 28
  • 32