koa2-streaming-react-render
v1.1.0
Published
Koa2 React streaming render helper
Downloads
4
Readme
Koa2 react 16+ streaming render helper
Very tiny, zero dependencies
Why ?
Because React server side streaming render can be tricky with koa2.
Install
npm i --save koa2-streaming-react-render
Use
const {renderToNodeStream} = require('react-dom/server');
const render = require('koa2-streaming-react-render');
koa.use(async (ctx) => {
const component = ...;
const stream = renderToNodeStream(yourComponent);
return render(
ctx,
'<html><head></head><body>', // head of document
stream, // streaming body of document
'</body></html>', // document tail
{ // options optional
onStats: (stats) => {
// here you can get responseSize, size and render time values
}
}
);
});
Examples
see under "examples" directory
Test
npm install
npm test
Faq
What will happen if an error occurs through react rendering process? How do I handle that?
Don't worry, you are covered. If an error occurs while render, it will follow standard koa2 error handling middleware