Skip to content

Commit 28fe2b8

Browse files
make render function returns Readable stream instead of PassThrough
1 parent cba69ad commit 28fe2b8

File tree

3 files changed

+11
-10
lines changed

3 files changed

+11
-10
lines changed

node_package/src/ReactOnRails.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { ReactElement } from 'react';
2-
import type { PassThrough } from 'stream';
2+
import type { Readable } from 'stream';
33

44
import * as ClientStartup from './clientStartup';
55
import handleError from './handleError';
@@ -252,7 +252,7 @@ ctx.ReactOnRails = {
252252
* Used by server rendering by Rails
253253
* @param options
254254
*/
255-
streamServerRenderedReactComponent(options: RenderParams): PassThrough {
255+
streamServerRenderedReactComponent(options: RenderParams): Readable {
256256
return streamServerRenderedReactComponent(options);
257257
},
258258

node_package/src/serverRenderReactComponent.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import ReactDOMServer from 'react-dom/server';
2-
import { PassThrough } from 'stream';
2+
import { PassThrough, Readable } from 'stream';
33
import type { ReactElement } from 'react';
44

55
import 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

node_package/src/types/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { ReactElement, ReactNode, Component, ComponentType } from 'react';
2-
import type { PassThrough } from 'stream';
2+
import type { Readable } from 'stream';
33

44
// Don't import redux just for the type definitions
55
// See https://github.com/shakacode/react_on_rails/issues/1321
@@ -169,7 +169,7 @@ export interface ReactOnRails {
169169
): RenderReturnType;
170170
getComponent(name: string): RegisteredComponent;
171171
serverRenderReactComponent(options: RenderParams): null | string | Promise<RenderResult>;
172-
streamServerRenderedReactComponent(options: RenderParams): PassThrough;
172+
streamServerRenderedReactComponent(options: RenderParams): Readable;
173173
handleError(options: ErrorOptions): string | undefined;
174174
buildConsoleReplay(): string;
175175
registeredComponents(): Map<string, RegisteredComponent>;

0 commit comments

Comments
 (0)