Skip to content

Commit

Permalink
make render function returns Readable stream instead of PassThrough
Browse files Browse the repository at this point in the history
  • Loading branch information
AbanoubGhadban committed Aug 24, 2024
1 parent 0d426c7 commit f38ed50
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
4 changes: 2 additions & 2 deletions node_package/src/ReactOnRails.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ReactElement } from 'react';
import type { PassThrough } from 'stream';
import type { Readable } from 'stream';

import * as ClientStartup from './clientStartup';
import handleError from './handleError';
Expand Down Expand Up @@ -246,7 +246,7 @@ ctx.ReactOnRails = {
* Used by server rendering by Rails
* @param options
*/
streamServerRenderedReactComponent(options: RenderParams): PassThrough {
streamServerRenderedReactComponent(options: RenderParams): Readable {
return streamServerRenderedReactComponent(options);
},

Expand Down
13 changes: 7 additions & 6 deletions node_package/src/serverRenderReactComponent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ReactDOMServer from 'react-dom/server';
import { PassThrough } from 'stream';
import { PassThrough, Readable } from 'stream';
import type { ReactElement } from 'react';

import ComponentRegistry from './ComponentRegistry';
Expand Down Expand Up @@ -167,17 +167,17 @@ const serverRenderReactComponent: typeof serverRenderReactComponentInternal = (o
}
};

const stringToStream = (str: string) => {
const stringToStream = (str: string): Readable => {
const stream = new PassThrough();
stream.push(str);
stream.push(null);
return stream;
};

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

let renderResult: null | PassThrough = null;
let renderResult: null | Readable = null;

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

renderResult = new PassThrough();
ReactDOMServer.renderToPipeableStream(reactRenderingResult).pipe(renderResult);
const renderStream = new PassThrough();
ReactDOMServer.renderToPipeableStream(reactRenderingResult).pipe(renderStream);
renderResult = renderStream;

// TODO: Add console replay script to the stream
// Ensure to avoid console messages leaking between different components rendering
Expand Down
4 changes: 2 additions & 2 deletions node_package/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ReactElement, ReactNode, Component, ComponentType } from 'react';
import type { PassThrough } from 'stream';
import type { Readable } from 'stream';

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

0 comments on commit f38ed50

Please sign in to comment.