Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seeing empty page after deployed to S3 with React Route

I build a SPA with React and React Router. I'm also using https://github.com/facebookincubator/create-react-app as it's a really simple application. When I'm developing using webpack I can see the page fine. However, after I build for production using npm run build from create-react-app I get HTML file and css and js normally. I uploaded everything to S3 but when I go to the page I only get blank page

And this is what I'm seeing

<!-- react-empty: 1 -->

I'm guessing it's like this because S3 is default to index.html and I cannot change that. And React Router doesn't know how to deal with index.html but I also have / root as the default but I'm still seeing a blank page. Not sure how to fix this issue?

Here's my Router

ReactDOM.render(
  <Router history={browserHistory}>
    <Route path="/" component={App}>
      <IndexRoute component={Home} />
      <Route path="question2" component={Question2} />
      <Route path="question3" component={Question3} />
      <Route path="thankyou" component={Thankyou} />
    </Route>
  </Router>,
  document.getElementById('root')
);

And this is the template creawte-react-app use and it's working fine on development.

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
    <!--
      Notice the use of %PUBLIC_URL% in the tag above.
      It will be replaced with the URL of the `public` folder during the build.
      Only files inside the `public` folder can be referenced from the HTML.

      Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
      work correctly both with client-side routing and a non-root public URL.
      Learn how to configure a non-root public URL by running `npm run build`.
    -->
    <title>React App</title>
  </head>
  <body>
    <div id="root"></div>
    <!--
      This HTML file is a template.
      If you open it directly in the browser, you will see an empty page.

      You can add webfonts, meta tags, or analytics to this file.
      The build step will place the bundled scripts into the <body> tag.

      To begin the development, run `npm start`.
      To create a production bundle, use `npm run build`.
    -->
  </body>
</html>
like image 328
toy Avatar asked Oct 17 '22 22:10

toy


1 Answers

You are attempting to use a browserHistory on a static web page. You should use a hashHistory for static pages.

Why happens when you use browser history for static pages?

When React Router is first mounting, it (actually the history module that it uses) examines the current URL to determine the initial location. With a browserHistory, this is anything after the domain, so example.com/index.html's initial location would be /index.html.

If you have a route for index.html, that will be matched when the page loads and things might appear to work. If your app has a <Link> to an /other route, you could even click on it and the URL would be changed to example.com/other.

However, because you are using a static web page, you could not link to example.com/other. If someone were to try to load that page, they would receive a 404 error because the server does not have an /other page to serve.

Enter hashHistory

When you use hashHistory, the only part of the URL that is considered when determining the location is what comes after the hash.

If you navigate to example.com/index.html while using a hashHistory, you will notice that the URL is changed to example/com/index.html#/. The hash is inserted for you, and set as the root (absolute path of /), if the URL doesn't include one.

Going back to the previous example where a <Link> links to /other, when that link is clicked the URL will change to example.com/index.html#/other. Now, if you navigate directly to that URL, the server will load example.com/index.html, React Router will examine the hash, see that it is #/other and set the initial location as the /other route.

like image 182
Paul S Avatar answered Oct 27 '22 11:10

Paul S