Skip to content

Commit a60edf8

Browse files
authored
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.
1 parent c7fc025 commit a60edf8

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

packages/remix/src/utils/instrumentServer.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,19 @@ function isResponse(value: any): value is Response {
8484
);
8585
}
8686

87-
// Taken from Remix Implementation
87+
// Based on Remix Implementation
8888
// https://github.com/remix-run/remix/blob/7688da5c75190a2e29496c78721456d6e12e3abe/packages/remix-server-runtime/data.ts#L131-L145
8989
function extractData(response: Response): Promise<unknown> {
9090
const contentType = response.headers.get('Content-Type');
9191

92+
// Cloning the response to avoid consuming the original body stream
93+
const responseClone = response.clone();
94+
9295
if (contentType && /\bapplication\/json\b/.test(contentType)) {
93-
return response.json();
96+
return responseClone.json();
9497
}
9598

96-
return response.text();
99+
return responseClone.text();
97100
}
98101

99102
function captureRemixServerException(err: Error, name: string): void {

0 commit comments

Comments
 (0)