在客户端反应路由器应用程序中使用 HistoryLocation

Using HistoryLocation in a client side react router app

我正在使用 React 路由器设置一个纯客户端 React 应用程序。该应用程序需要是静态的,而不是 运行 在服务器上,因为我必须在 S3 中托管它。

在我的 Router.run 中,我尝试使用 HistoryLocation,但它似乎不起作用。仅客户端路由是不可能的,所以我需要使用 HashLocation 吗?

Router.run(routes, Router.HistoryLocation, function (Handler) {  
  React.render(<Handler/>, document.getElementById('app'));
});

正在查看 HistoryLocation 的 React 路由器文档:

使用 HTML5 历史记录 API 创建真实的 URL。我们建议您使用此位置。它具有更好看的 URL,并且是在浏览器中获取服务器呈现的应用程序所必需的。

您必须将服务器配置为使用 HistoryLocation

要支持 HistoryLocation,您将无法将请求直接指向您的 S3 html 资源。您需要将资源放在应用服务器(node.js、rails)或网络服务器后面,例如nginx 或 apache,设置为 return 您希望在客户端支持的任何路由上的静态页面。

您参考的文档提供了 express 示例:http://rackt.github.io/react-router/#configuring-your-server

这是一篇提供更多信息的文章:http://www.staticapps.org/articles/routing-urls-in-static-apps