11import ReactDOMServer from 'react-dom/server' ;
2- import { PassThrough } from 'stream' ;
2+ import { PassThrough , Readable } from 'stream' ;
33import type { ReactElement } from 'react' ;
44
55import ComponentRegistry from './ComponentRegistry' ;
@@ -181,17 +181,17 @@ function serverRenderReactComponent(options: RenderParams): null | string | Prom
181181 return createFinalResult ( renderState , componentName , throwJsErrors ) ;
182182}
183183
184- const stringToStream = ( str : string ) => {
184+ const stringToStream = ( str : string ) : Readable => {
185185 const stream = new PassThrough ( ) ;
186186 stream . push ( str ) ;
187187 stream . push ( null ) ;
188188 return stream ;
189189} ;
190190
191- export const streamServerRenderedReactComponent = ( options : RenderParams ) => {
191+ export const streamServerRenderedReactComponent = ( options : RenderParams ) : Readable => {
192192 const { name, domNodeId, trace, props, railsContext, throwJsErrors } = options ;
193193
194- let renderResult : null | PassThrough = null ;
194+ let renderResult : null | Readable = null ;
195195
196196 try {
197197 const componentObj = ComponentRegistry . get ( name ) ;
@@ -213,8 +213,9 @@ See https://github.com/shakacode/react_on_rails#renderer-functions`);
213213 throw new Error ( 'Server rendering of streams is not supported for server render hashes or promises.' ) ;
214214 }
215215
216- renderResult = new PassThrough ( ) ;
217- ReactDOMServer . renderToPipeableStream ( reactRenderingResult ) . pipe ( renderResult ) ;
216+ const renderStream = new PassThrough ( ) ;
217+ ReactDOMServer . renderToPipeableStream ( reactRenderingResult ) . pipe ( renderStream ) ;
218+ renderResult = renderStream ;
218219
219220 // TODO: Add console replay script to the stream
220221 // Ensure to avoid console messages leaking between different components rendering
0 commit comments