小编use*_*322的帖子

是否可以使用新的 renderToNodeStream?

我在一个项目中使用服务器端渲染,我刚刚将我的堆栈更新到 React 16。

我当前的堆栈是:Node 8.4 Express React 16 Redux Webpack Pug 用于静态内容(如页眉和页脚)

我想知道是否可以使用我的堆栈在我的应用程序中使用新的 renderToNodeStream。

要打印路线,我正在使用:

const matchedRoute = matchRoutes(routes, req.url);
let context = {};

if (matchedRoute) {
    const initialState = JSON.stringify(store.getState()).replace(/</g, '\\u003c');
    const criticalCSSPath = manifest['criticalCss.css'].replace('http://s.domain.com.br/', '/');
    const criticalCss = fs.readFileSync(`./public${criticalCSSPath}`, 'utf-8');

    if (context.url) {
        return res.redirect(302, context.url);
    }else {
        const content = renderToString(
            <Provider store={store}>
                <StaticRouter location={req.url} context={context}>
                    {renderRoutes(routes)}
                </StaticRouter>
            </Provider>
        );

        return res.render(view, {initialState: initialState, content, view, criticalCss});
    }
}
Run Code Online (Sandbox Code Playgroud)

但是如果我想渲染到节点流,我需要做这样的事情,根据文档:

app.get("/", (req, res) => {
   res.write("<!DOCTYPE html><html><head><title>My …
Run Code Online (Sandbox Code Playgroud)

node.js express reactjs redux

7
推荐指数
2
解决办法
3377
查看次数

标签 统计

express ×1

node.js ×1

reactjs ×1

redux ×1