小编Geo*_*nov的帖子

服务器端渲染的React应用程序因加载而崩溃

我正在使用react-boilerplate(使用react-router,sagas,express.js)作为我的React应用程序,并且在它之上我添加了SSR逻辑,这样一旦它收到HTTP请求,它就会根据URL呈现反应组件到字符串并发送HTML字符串返回客户端.

虽然反应呈现在服务器端发生,但它也fetch通过传真向一些API(基于URL的最多5个端点)发出请求,以便在组件实际呈现组件之前获取组件的数据.

如果我同时向Node服务器发出几个请求,那么一切都很好,但是一旦我模拟了100多个并发请求的加载并且它开始处理它,那么在某些时候它会崩溃而没有任何异常的迹象.

我在尝试调试应用程序时注意到的是,一旦节点服务器开始处理100多个传入请求,它就会同时向API发送请求,但在停止堆叠这些请求之前不会收到任何实际响应.

用于在服务器端呈现的代码:

async function renderHtmlDocument({ store, renderProps, sagasDone, assets, webpackDllNames }) {
  // 1st render phase - triggers the sagas
  renderAppToString(store, renderProps);

  // send signal to sagas that we're done
  store.dispatch(END);

  // wait for all tasks to finish
  await sagasDone();

  // capture the state after the first render
  const state = store.getState().toJS();

  // prepare style sheet to collect generated css
  const styleSheet = new ServerStyleSheet();

  // 2nd render phase - the sagas triggered …
Run Code Online (Sandbox Code Playgroud)

node.js express reactjs server-side-rendering ssr

9
推荐指数
1
解决办法
1146
查看次数

如何在Yii2 Asset Bundle中使用JSX文件

我有Yii2项目设置,我决定使用Facebook的JavaScript框架React.js,它提供了一种方便的方法来在JavaScript代码中声明HTML模板,称为JSX.

我的JavaScript看起来如下:

(function () {
    'use strict';

    MyBlock = React.createClass({
        getInitialState: function () {
            return {data: []};
        },
        componentDidMount: function () {
            $.ajax({
                url: '/api/',
                dataType: 'json',

                success: function (data) {
                    this.setState({ data: data });
                }.bind(this)
            });
        },

        render: function () {
            <div class="block">
                {this.props.variable}
            </div>
        }
    });

    React.render(
        <ProfileQuestion />,
        document.getElementById('profile_question_wrapper')
    );
}());
Run Code Online (Sandbox Code Playgroud)

AssetBundle帮助我在我的视图中包含所需的库,所以我将来自CDN的React.JS和JSX文件添加到我的AssetBundle中:

class MyAsset extends AssetBundle
{
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $css = [
    ];
    public $js = …
Run Code Online (Sandbox Code Playgroud)

javascript yii2 reactjs assetbundle react-jsx

5
推荐指数
1
解决办法
2262
查看次数