Skip to content

Commit b8eb6ac

Browse files
committed
perf(OpenNextResponse): do not store the chunks for streamed responses
1 parent c6e0005 commit b8eb6ac

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

.changeset/honest-lamps-smell.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"@opennextjs/aws": minor
3+
---
4+
5+
perf(OpenNextResponse): do not store the chunks for streamed responses
6+
7+
There is no need to store the chunks for streamed responses.
8+
Not storing the chunks allows saving memory.
9+
10+
Note that `OpenNextHandler` will now return an empty body for streamed responses.

packages/open-next/src/http/openNextResponse.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,10 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
291291
}
292292

293293
private _internalWrite(chunk: any, encoding: BufferEncoding) {
294-
this._chunks.push(Buffer.from(chunk, encoding));
294+
if (!this.streamCreator) {
295+
// Do not keep chunks around for streamed responses
296+
this._chunks.push(Buffer.from(chunk, encoding));
297+
}
295298
this.push(chunk, encoding);
296299
this.streamCreator?.onWrite?.();
297300
}
@@ -314,7 +317,7 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
314317
this.flushHeaders();
315318
}
316319
// In some cases we might not have a store i.e. for example in the image optimization function
317-
// We may want to reconsider this in the future, it might be intersting to have access to this store everywhere
320+
// We may want to reconsider this in the future, it might be interesting to have access to this store everywhere
318321
globalThis.__openNextAls
319322
?.getStore()
320323
?.pendingPromiseRunner.add(this.onEnd(this.headers));

0 commit comments

Comments
 (0)