Seeing empty page after deployed to S3 with React Route

后端 未结 1 1347
遇见更好的自我
遇见更好的自我 2021-01-13 22:26

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 u

相关标签:
1条回答
  • 2021-01-13 23:03

    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.

    0 讨论(0)
提交回复
热议问题