From a60edf84053af65a9e54ead9793aab696bacb836 Mon Sep 17 00:00:00 2001 From: Onur Temizkan Date: Thu, 21 Jul 2022 19:52:07 +0100 Subject: [PATCH] fix(remix): Clone erroneous responses not to consume their body streams. (#5429) Fixes: #5423 Ref: #5405 We were extracting the bodies of 4xx/5xx responses to capture, but Remix also uses them, we should not consume their `body` streams, as they are only available once in response lifespans. This PR updates `extractData` to work on a clone response. --- packages/remix/src/utils/instrumentServer.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/remix/src/utils/instrumentServer.ts b/packages/remix/src/utils/instrumentServer.ts index 8f2179633e24..4f1195aa5796 100644 --- a/packages/remix/src/utils/instrumentServer.ts +++ b/packages/remix/src/utils/instrumentServer.ts @@ -84,16 +84,19 @@ function isResponse(value: any): value is Response { ); } -// Taken from Remix Implementation +// Based on Remix Implementation // https://github.com/remix-run/remix/blob/7688da5c75190a2e29496c78721456d6e12e3abe/packages/remix-server-runtime/data.ts#L131-L145 function extractData(response: Response): Promise { const contentType = response.headers.get('Content-Type'); + // Cloning the response to avoid consuming the original body stream + const responseClone = response.clone(); + if (contentType && /\bapplication\/json\b/.test(contentType)) { - return response.json(); + return responseClone.json(); } - return response.text(); + return responseClone.text(); } function captureRemixServerException(err: Error, name: string): void {