Skip to content

Commit 8d6e2a7

Browse files
make render function returns Readable stream instead of PassThrough
1 parent a41b988 commit 8d6e2a7

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';
@@ -193,17 +193,17 @@ const serverRenderReactComponent: typeof serverRenderReactComponentInternal = (o
193193
}
194194
};
195195

196-
const stringToStream = (str: string) => {
196+
const stringToStream = (str: string): Readable => {
197197
const stream = new PassThrough();
198198
stream.push(str);
199199
stream.push(null);
200200
return stream;
201201
};
202202

203-
export const streamServerRenderedReactComponent = (options: RenderParams) => {
203+
export const streamServerRenderedReactComponent = (options: RenderParams): Readable => {
204204
const { name, domNodeId, trace, props, railsContext, throwJsErrors } = options;
205205

206-
let renderResult: null | PassThrough = null;
206+
let renderResult: null | Readable = null;
207207

208208
try {
209209
const componentObj = ComponentRegistry.get(name);
@@ -225,8 +225,9 @@ See https://github.com/shakacode/react_on_rails#renderer-functions`);
225225
throw new Error('Server rendering of streams is not supported for server render hashes or promises.');
226226
}
227227

228-
renderResult = new PassThrough();
229-
ReactDOMServer.renderToPipeableStream(reactRenderingResult).pipe(renderResult);
228+
const renderStream = new PassThrough();
229+
ReactDOMServer.renderToPipeableStream(reactRenderingResult).pipe(renderStream);
230+
renderResult = renderStream;
230231

231232
// TODO: Add console replay script to the stream
232233
// 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)