0

I have a website that runs Express on the backend, where I define an api and serve my build directory. My app looks like this:

function App() {
    return (
        <BrowserRouter>
                <Route path="/" component={Home} exact />
                <Route path="/explore" component={Explore} exact />
        </BrowserRouter>
    );
}

I would like to be able to drop the build directory into an S3 bucket then have my API requests to a hardcoded location.

The problem is, when I open up index.html, my and components don't render. What are some possible solutions?

Bryn
  • 49
  • 6

3 Answers3

1

This is a common pitfall of client-side routing, the navigation will work when started from the index, but fail when the users refresh or start their session from another page (for example by clicking on a link).

This is because there won't be any /explore page, since the whole app is bundled in one index.html file.

To make it work you have to configure S3 to serve index.html when it does not find the requested page, by setting index.html as the Error document as explained in this answer.

Anthony Garcia-Labiad
  • 3,531
  • 1
  • 26
  • 30
0

Try to use a HashRouter instead, like this:

import { HashRouter } from 'react-router-dom';

function App() {
    return (
        <HashRouter>
            //your Routes here
        </HashRouter>
    );
}
k-wasilewski
  • 3,943
  • 4
  • 12
  • 29
0

In the S3 Management Console, under Static website hosting, set the error page to be the index.html. S3 Console Image

Bryn
  • 49
  • 6