简体   繁体   English

使用异步数据提取的服务器端呈现

[英]Server side rendering with async data fetch

We are building our website with react/react-router/redux 我们正在使用react / react-router / redux构建我们的网站

We want to server side render our pages that should be filled by the data from our data sources. 我们希望服务器端渲染我们的页面,这些页面应该由我们的数据源中的数据填充。 This transaction has to be asynchronous and unfortunately since we want to server side render, we can not use "componentDidMount" function. 这个事务必须是异步的,不幸的是因为我们想要服务器端渲染,我们不能使用“componentDidMount”函数。

In the redux tutorial page at server side rendering section here , it has been advised to : 这里的服务器端渲染部分的redux教程页面中,建议:

If you use something like React Router, you might also want to express your data fetching dependencies as static fetchData() methods on your route handler components. 如果使用React Router之类的东西,您可能还希望将数据提取依赖关系表示为路由处理程序组件上的静态fetchData()方法。 They may return async actions, so that your handleRender function can match the route to the route handler component classes, dispatch fetchData() result for each of them, and render only after the Promises have resolved. 它们可能会返回异步操作,因此handleRender函数可以匹配到路由处理程序组件类的路由,为每个类调度fetchData()结果,并且只在Promises解析后才进行渲染。 This way the specific API calls required for different routes are colocated with the route handler component definitions. 这样,不同路由所需的特定API调用与路由处理程序组件定义共同定位。 You can also use the same technique on the client side to prevent the router from switching the page until its data has been loaded. 您还可以在客户端使用相同的技术,以防止路由器切换页面,直到其数据已加载。

This is currently how we handle our data fetch. 这是我们目前处理数据获取的方式。 I personally did not like this approach it looks quite clumsy and it is too coupled to the routing library. 我个人不喜欢这种方法,它看起来很笨拙,而且它太过耦合到路由库。 Are there any better ways to do it - hopefully with standard react/router/redux components ? 有没有更好的方法 - 希望使用标准的react / router / redux组件?

Something like a static fetchData() method is the correct way to handle data fetching with React Router in the general case, though it can reach down into child components as needed (which is eg how Relay works). 类似于静态fetchData()方法的东西是在一般情况下使用React Router处理数据获取的正确方法,尽管它可以根据需要到达子组件(例如Relay如何工作)。

The reason you want to do it this way is that React Router resolves all the matched routes all at once. 您希望这样做的原因是React Router一次性解析所有匹配的路由。 Given that, you can then run data fetching for all of your route handlers simultaneously. 鉴于此,您可以同时为所有路由处理程序运行数据获取。

If instead you tied data fetching to instance-level handlers on components, you'd always end up with fetch waterfalls, where a component could not fetch its required data until all of its parents receive their required data, and so forth. 相反,如果您将数据提取绑定到组件上的实例级处理程序,那么您最终会得到提取瀑布,其中组件无法获取其所需数据,直到其所有父级都接收到所需数据,依此类推。 While that may not be a big problem on the server, it's hugely suboptimal on the client. 虽然这可能不是服务器上的大问题,但它在客户端上非常不理想。

If you really want to colocate data dependencies to components, you can consider using something like React Resolver , but this can easily lead to a suboptimal experience for your users. 如果您确实希望将数据依赖项与组件共存,则可以考虑使用React Resolver之类的东西,但这很容易导致用户的次优体验。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM