使用React SSR renderToNodeStream

时间:2019-01-08 04:20:16

标签: error-handling serverside-rendering node-streams react-16 react-ssr

我试图弄清楚如何正确地处理React流。假设<App />内部存在错误。捕获错误的最佳方法是什么?当我在<App />组件内抛出错误时,catch块似乎并没有捕获错误,因为它没有console.log任何内容。我尝试添加stream.on('error', (err) => {console.log(err)});,但也没有捕获到该错误。我应该如何在服务器端的App中捕获错误?

try {
  const header = <div>Hello</div>
  const footer = <div>Bye</div>
  const sheet = new ServerStyleSheet();
  const jsx = sheet.collectStyles(<App />);
  const stream = sheet.interleaveWithNodeStream(renderToNodeStream(jsx));
  ctx.body = multi_stream([
    string_stream(header),
    stream,
    string_stream(footer)
  ]);
} catch (err) {
  console.log('There is an error', err);
  ctx.throw(err.status, 'Failed to SSR');
}

0 个答案:

没有答案