I have a React app with SSR that is running in Kubernetes. After a few days without restarting Pods I get this error: https://reactjs.org/docs/error-decoder.html/?invariant=304.
I use ReactDOMServer.renderToNodeStream and pipe() and according to the error:
This can happen if you are not properly destroying the Readable provided by React. Ensure that you call .destroy() on it if you no longer want to read from it, and did not read to the end. If you use .pipe() this should be automatic.
Here is my server renderer:
export function renderOnServer(res, controller) {
return new Promise((resolve, reject) => {
try {
const index = controller.getIndexHTML();
const view = controller.getView();
const indexHTML = `<!DOCTYPE html>${ReactDOMServer.renderToStaticMarkup(index)}`;
const chunks = indexHTML.split("{{STREAMED_CONTENT}}");
const firstChunk = chunks.shift();
const lastChunk = chunks.shift();
res.write(firstChunk);
const stream = ReactDOMServer.renderToNodeStream(view);
stream.pipe(res, { end : false });
stream.on("end", () => {
res.write(lastChunk);
res.end();
resolve();
});
} catch(err) {
reject(err);
}
});
}
I use pipe() but I also handle end event as seen in the code. Could it be the cause of the problem? Or should I focus on another part of the code?
Any help is appreciated. Thanks.