0

First of all, my app works well on localhost. Every component and URL work on the server except for NotFound (404) page.

<Switch>

        <Route
          exact
          path={'/'}
          render={() => (
          {/*something here*/}
       )} />

       <Route
        exact
        path={'/secondPage'}
        render={() => (
        {/*something here*/}
      )} />

        <Route 
         path={'*'} 
         render={() => (
         <NotFound />
      )} />

 </Switch>

Also, I tried just <Route render={() => ( <NotFound /> )} />

Index.js:

ReactDOM.render(
    <BrowserRouter basename={process.env.PUBLIC_URL}><App /></BrowserRouter>, 
    document.getElementById('root')
)

I tried to deploy the react app like deploying to GitHub pages. Here is my package.json

{
  "homepage": "https://mywebsite.com/react-app/",

  "dependencies": {
    "escape-string-regexp": "^1.0.5",
    "lodash": "^4.17.10",
    "prop-types": "^15.6.1",
    "react": "^16.3.2",
    "react-dom": "^16.3.2",
    "react-router-dom": "^4.3.1",
    "sort-by": "^1.2.0"
  },
  "devDependencies": {
    "gh-pages": "^1.2.0",
    "react-scripts": "1.1.4"
  },
  "scripts": {
    "predeploy": "npm run build",
    "deploy": "gh-pages -d build",
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  }
}

What am I missing?

blocker
  • 95
  • 1
  • 10
  • Maybe this answer will be helpfull: [React-Router: No Not Found Route?](https://stackoverflow.com/a/37491381/5275890) – Denis Bubnov Jul 27 '18 at 20:41
  • Nope it doesn't work at all. – blocker Jul 27 '18 at 21:10
  • If you want to catch all the routes, you don't need to specify a path. Can you try ``? Also, there is no need to use render if you are not providing any props to a component. In those cases use `component={}` instead. – Roy Scheffers Jul 28 '18 at 16:02
  • @RoyScheffers what do you mean? I wan to catch just routes that are not in my app. Also I triead ``. Nothing changed. – blocker Jul 28 '18 at 19:09
  • Sorry for being unclear. I was referring to any other routes that are not specified. If you don't specify `path` then that route is always rendered if the above paths are not matched. Can you also share package.json and webpack.config.js perhaps there's something in there to give us a clue as to why the NotFound route is not working on the server? What type of server are you using? – Roy Scheffers Jul 28 '18 at 19:26
  • I am using my personal hosting, I don't know there are some restrictions for react app. – blocker Jul 28 '18 at 19:35
  • I've edited my question with package.json, however, I have no idea webpack.config.js. @RoyScheffers – blocker Jul 28 '18 at 20:09
  • I assume you followed the instructions for deployment in the [docs](https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/template/README.md#deployment). Which [solution](https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/template/README.md#notes-on-client-side-routing) did you use to enable client-side rendering on GitHub? If you haven't you should either use hashRouter or option for [GitHub Pages to handle 404 by redirecting to your index.html page with a special redirect parameter](https://github.com/rafrex/spa-github-pages). – Roy Scheffers Jul 28 '18 at 20:50

0 Answers0