I am currently creating a website and have been looking to start using renderToNodeStream
to increase the performance of the server side rendering instead of using renderToString
.
At the moment I am using renderToString
and then using Helmet.renderStatic
to get all the required meta data and title from each of the pages. When I switch to using renderToNodeStream
however, I will be writing to the head prior to rendering anything, and so can no longer use Helmet.renderStatic
anymore.
I was thinking I could do the below to solve this issue, but this involves first using renderToString
before then using renderToNodeStream
, and probably doesn't really provide much improvement...
app.use('*', (req, res) {
Loadable.preloadAll().then(() => {
const store = createStore(
reducers,
getDefaultStateFromProps(),
applyMiddleware(thunk)
);
const routeContext = {};
const router = (
<Provider store={store}>
<StaticRouter location={req.url} context={routeContext}>
<App/>
</StaticRouter>
</Provider>
);
res.setHeader('Content-Type', 'text/html');
renderToString(router);
const helmet = Helmet.renderStatic();
res.locals.title = helmet.title;
res.locals.meta = helmet.meta;
res.locals.link = helmet.link;
res.write(headTemplate(res.locals));
const stream = renderToNodeStream(router);
stream.pipe(res, { end: false });
stream.on('end', () => {
res.locals.context = JSON.stringify(store.getState());
res.end(bodyTemplate(res.locals));
});
});
}
Does anyone know how to get around this issue?