Skip to content

Commit

Permalink
tiny changes
Browse files Browse the repository at this point in the history
  • Loading branch information
AbanoubGhadban committed Oct 31, 2024
1 parent 3b7728a commit 276ac40
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions node_package/src/serverRenderReactComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ type RenderOptions = {
renderingReturnsPromises: boolean;
};

function ensureError(e: unknown): Error {
return e instanceof Error ? e : new Error(String(e));
}

function validateComponent(componentObj: RegisteredComponent, componentName: string) {
if (componentObj.isRenderer) {
throw new Error(`Detected a renderer while server rendering component '${componentName}'. See https://github.com/shakacode/react_on_rails#renderer-functions`);
Expand Down Expand Up @@ -92,7 +96,7 @@ function handleRenderingError(e: unknown, options: { componentName: string, thro
if (options.throwJsErrors) {
throw e;
}
const error = e instanceof Error ? e : new Error(String(e));
const error = ensureError(e);
return {
hasErrors: true,
result: handleError({ e: error, name: options.componentName, serverSide: true }),
Expand Down Expand Up @@ -241,7 +245,7 @@ const transformRenderStreamChunksToResultObject = (renderState: StreamRenderStat
transformStream.end();
pipedStream?.abort();
}
return { readableStream: readableStream as Readable, pipeToTransform, writeChunk, emitError, endStream };
return { readableStream, pipeToTransform, writeChunk, emitError, endStream };
}

const streamRenderReactComponent = (reactRenderingResult: ReactElement, options: RenderParams) => {
Expand All @@ -262,7 +266,7 @@ const streamRenderReactComponent = (reactRenderingResult: ReactElement, options:

const renderingStream = ReactDOMServer.renderToPipeableStream(reactRenderingResult, {
onShellError(e) {
const error = e instanceof Error ? e : new Error(String(e));
const error = ensureError(e);
renderState.hasErrors = true;
renderState.error = error;

Expand All @@ -282,7 +286,7 @@ const streamRenderReactComponent = (reactRenderingResult: ReactElement, options:
if (!renderState.isShellReady) {
return;
}
const error = e instanceof Error ? e : new Error(String(e));
const error = ensureError(e);
if (throwJsErrors) {
emitError(error);
}
Expand Down Expand Up @@ -313,13 +317,13 @@ export const streamServerRenderedReactComponent = (options: RenderParams): Reada
throw new Error('Server rendering of streams is not supported for server render hashes or promises.');
}

return streamRenderReactComponen(reactRenderingResult, options);
return streamRenderReactComponent(reactRenderingResult, options);
} catch (e) {
if (throwJsErrors) {
throw e;
}

const error = e instanceof Error ? e : new Error(String(e));
const error = ensureError(e);
const htmlResult = handleError({ e: error, name: componentName, serverSide: true });
const jsonResult = JSON.stringify(createResultObject(htmlResult, buildConsoleReplay(), { hasErrors: true, error, result: null }));
return stringToStream(jsonResult);
Expand Down

0 comments on commit 276ac40

Please sign in to comment.