Skip to content

Commit ec0d6a2

Browse files
on component streaming: report error at onError callback and return fallback html at onShellError callback
1 parent e83e1dc commit ec0d6a2

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

node_package/src/streamServerRenderedReactComponent.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,16 @@ const streamRenderReactComponent = (
142142
const { readableStream, pipeToTransform, writeChunk, emitError, endStream } =
143143
transformRenderStreamChunksToResultObject(renderState);
144144

145-
const onShellError = (e: unknown) => {
146-
const error = convertToError(e);
145+
const reportError = (error: Error) => {
147146
renderState.hasErrors = true;
148147
renderState.error = error;
149148

150149
if (throwJsErrors) {
151150
emitError(error);
152151
}
152+
};
153153

154+
const sendErrorHtml = (error: Error) => {
154155
const errorHtml = handleError({ e: error, name: componentName, serverSide: true });
155156
writeChunk(errorHtml);
156157
endStream();
@@ -171,26 +172,24 @@ const streamRenderReactComponent = (
171172
}
172173

173174
const renderingStream = ReactDOMServer.renderToPipeableStream(reactRenderedElement, {
174-
onShellError,
175+
onShellError(e) {
176+
sendErrorHtml(convertToError(e));
177+
},
175178
onShellReady() {
176179
renderState.isShellReady = true;
177180
pipeToTransform(renderingStream);
178181
},
179182
onError(e) {
180-
if (!renderState.isShellReady) {
181-
return;
182-
}
183-
const error = convertToError(e);
184-
if (throwJsErrors) {
185-
emitError(error);
186-
}
187-
renderState.hasErrors = true;
188-
renderState.error = error;
183+
reportError(convertToError(e));
189184
},
190185
identifierPrefix: domNodeId,
191186
});
192187
})
193-
.catch(onShellError);
188+
.catch((e: unknown) => {
189+
const error = convertToError(e);
190+
reportError(error);
191+
sendErrorHtml(error);
192+
});
194193

195194
return readableStream;
196195
};

0 commit comments

Comments
 (0)