服务器端渲染 - renderToString返回空的html

时间:2017-07-20 14:32:59

标签: javascript reactjs redux react-router serverside-rendering

我正在尝试在服务器上呈现react应用程序,但似乎renderToString函数没有正确呈现标记,当我执行console.log(标记)时,我得到的内容如下:

<div data-reactroot="" data-reactid="1" data-react-checksum="-1424217736">
<!-- react-empty: 2 --><!-- react-empty: 3 --><!-- react-empty: 4 --><!-- react-empty: 5 --></div>

我的代码如下所示:

app.use('*', (req, res) => {

  const context = {}
  const store = configureStore()


  const markup = renderToString(
    <Provider store={store}>
      <StaticRouter
        location={req.url}
        context={context}
      >
        <Routes/>
      </StaticRouter>
    </Provider>
  )

  console.log(markup)

  if (context.url) {
    // Somewhere a `<Redirect>` was rendered
    redirect(301, context.url)
  } else {
    // we're good, send the response
    res.send(`
      <!doctype html>
      <head>
      <meta name="google-site-verification" content="P5pcBDBR0SrpH4kFMOp5Ush5JxDdG-3oPhjKO8Zf5WE" />
      </head>
      <body>
      <div id="app">${markup}</div>
      </body>
    `)


}
})

我正在使用React-Router v4

0 个答案:

没有答案