Skip to content

Commit 624dce5

Browse files
committed
perf(OpenNextResponse): do not store the chunks for streamed responses
1 parent 75b9d18 commit 624dce5

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
@@ -283,7 +283,10 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
283283
}
284284

285285
private _internalWrite(chunk: any, encoding: BufferEncoding) {
286-
this._chunks.push(Buffer.from(chunk, encoding));
286+
if (!this.streamCreator) {
287+
// Do not keep chunks around for streamed responses
288+
this._chunks.push(Buffer.from(chunk, encoding));
289+
}
287290
this.push(chunk, encoding);
288291
this.streamCreator?.onWrite?.();
289292
}
@@ -306,7 +309,7 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
306309
this.flushHeaders();
307310
}
308311
// In some cases we might not have a store i.e. for example in the image optimization function
309-
// We may want to reconsider this in the future, it might be intersting to have access to this store everywhere
312+
// We may want to reconsider this in the future, it might be interesting to have access to this store everywhere
310313
globalThis.__openNextAls
311314
?.getStore()
312315
?.pendingPromiseRunner.add(this.onEnd(this.headers));

0 commit comments

Comments
 (0)