27

I have deployed react app on Amplify Console following their documentation. The site is deployed and running fine, I am able to navigate using links but when I try to land to any url directly I get redirected to my configured 404 page.

Below is the code I am using

ReactDOM.render(
  <Router>
    <Route path="/" component={App} />
  </Router>,
  document.getElementById("root"),
);

And here is how my route looks like -

<Switch>
    <Route
      exact
      path="/"
      render={(): JSX.Element => <Home auth={this.auth} />}
    />
    <Route path="/features" render={(): JSX.Element => <Features />} />
    <Route
      path="/pagenotfound"
      render={(): JSX.Element => <PageNotFound />}
    />
    <Redirect from="/**" to="/pagenotfound" />
</Switch>

Here is the link to the app - https://master.dkf0zemoh330o.amplifyapp.com/features

Yasser Shaikh
  • 46,934
  • 46
  • 204
  • 281

3 Answers3

70

This happens because navigating directly to a path or reloading a path requests that path on the server, but React Router can only control routes on the client.

I was able to work around that limitation with Amplify using redirect settings, as mentioned in these two GitHub issues:

From the Amplify console, you can access the Rewrites and redirects menu item in left sidebar. From there you can update the redirect rule to these settings:

Source address: </^[^.]+$|\.(?!(css|gif|ico|jpg|js|png|txt|svg|woff|ttf)$)([^.]+$)/>
Target address: /index.html
Type: 200

This uses their regex source feature that matches problematic routes and redirects them to index.html. You may need to replace this default route with / or something else depending on how your app is structured. Here's what it should look like:

enter image description here

Bjorn Hansen
  • 329
  • 5
  • 12
Yasser Shaikh
  • 46,934
  • 46
  • 204
  • 281
4

Thanks! I would add also |json| the string - it fixes "Manifest: Line: 1, column: 1, Syntax error on Chrome browser" problem:

Manifest: Line: 1, column: 1, Syntax error on Chrome browser

VladS
  • 251
  • 3
  • 8
  • Hi VladS. Where would you put the |json| in the string? – Chris Christensen Mar 05 '20 at 16:32
  • In AWS Amplify go to: All apps -> MySuperCoolApp -> App settings: Rewrites and redirects -> Edit. Then add rule: [ ^[^.]+$|\.(?!(css|gif|ico|json|jpg|js|png|txt|svg|woff|ttf)$)([^.]+$)/> ] [ /index.html ] [ 200(rewrite) ] (remove square brackets) – VladS Mar 05 '20 at 17:10
0

If the steps above don't fully work try adding <base href="/" /> to the <head/> section of your index.html as mentioned here, sadly this took a while to find.

For a complete answer this is my final "Rewrites and redirects" opened in the text editor:

[
    {
        "source": "https://example.com",
        "target": "https://www.example.com",
        "status": "302",
        "condition": null
    },
    {
        "source": "</^[^.]+$|\\.(?!(css|gif|ico|jpg|js|png|txt|svg|woff|woff2|ttf|map|json|webp|html|xml|webmanifest|mp4)$)([^.]+$)/>",
        "target": "/index.html",
        "status": "200",
        "condition": null
    },
    {
        "source": "/<*>",
        "target": "/index.html",
        "status": "404-200",
        "condition": null
    }
]
Daniel Olson
  • 73
  • 1
  • 3