@@ -19,6 +19,7 @@ import {
1919
2020import { logError } from '../../errors' ;
2121import { decodeBody } from '../../services/ui-worker-api' ;
22+ import { decodingRequired } from '../events/bodies' ;
2223
2324export class HttpBody implements MessageBody {
2425
@@ -116,13 +117,17 @@ export class HttpBody implements MessageBody {
116117 // One is always set - so if _decoded is not set, _encoded must be.
117118 const encodedBuffer = this . _encoded as Buffer ;
118119
119- // Temporarily change to a fake buffer, while the web worker takes the data to decode
120+ // Change to a fake buffer, while the web worker takes the data to decode. If we
121+ // decoded successfully, we never put this back (to avoid duplicting the data).
120122 const encodedLength = encodedBuffer . byteLength ;
121123 this . _encoded = fakeBuffer ( encodedLength ) ;
122124
123125 try {
124- const { decoded, encoded } = await decodeBody ( encodedBuffer , this . _contentEncoding ) ;
125- this . _encoded = encoded ;
126+ // We short-circuit (to avoid the render+async+re-render) if we know that
127+ // decoding is not actually required here.
128+ const { decoded } = decodingRequired ( encodedBuffer , this . _contentEncoding )
129+ ? await decodeBody ( encodedBuffer , this . _contentEncoding )
130+ : { decoded : encodedBuffer } ;
126131
127132 runInAction ( ( ) => {
128133 this . _decoded = decoded ;
0 commit comments