Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8b51108

Browse files
conico974vicb
andauthored
fix 304 incorrectly set as 200 (#632)
* fix 304 incorrectly set as 200 * Update packages/open-next/src/http/openNextResponse.ts Co-authored-by: Victor Berchet <[email protected]> * Create slow-icons-grin.md --------- Co-authored-by: Victor Berchet <[email protected]>
1 parent fe600ac commit 8b51108

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

.changeset/slow-icons-grin.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/aws": patch
3+
---
4+
5+
fix 304 incorrectly set as 200

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

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -78,23 +78,11 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
7878

7979
constructor(
8080
private fixHeaders: (headers: OutgoingHttpHeaders) => void,
81-
onEnd: (headers: OutgoingHttpHeaders) => Promise<void>,
81+
private onEnd: (headers: OutgoingHttpHeaders) => Promise<void>,
8282
private streamCreator?: StreamCreator,
8383
private initialHeaders?: OutgoingHttpHeaders,
8484
) {
8585
super();
86-
this.once("finish", () => {
87-
if (!this.headersSent) {
88-
this.flushHeaders();
89-
}
90-
// In some cases we might not have a store i.e. for example in the image optimization function
91-
// We may want to reconsider this in the future, it might be intersting to have access to this store everywhere
92-
globalThis.__openNextAls
93-
?.getStore()
94-
?.pendingPromiseRunner.add(onEnd(this.headers));
95-
const bodyLength = this.getBody().length;
96-
this.streamCreator?.onFinish(bodyLength);
97-
});
9886
}
9987

10088
// Necessary for next 12
@@ -305,16 +293,26 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
305293
callback();
306294
}
307295

308-
//This is only here because of aws broken streaming implementation.
309-
//Hopefully one day they will be able to give us a working streaming implementation in lambda for everyone
310-
//If you're lucky you have a working streaming implementation in your aws account and don't need this
311-
//If not you can set the OPEN_NEXT_FORCE_NON_EMPTY_RESPONSE env variable to true
312-
//BE CAREFUL: Aws keeps rolling out broken streaming implementations even on accounts that had working ones before
313-
//This is not dependent on the node runtime used
314-
//There is another known issue with aws lambda streaming where the request reach the lambda only way after the request has been sent by the client. For this there is absolutely nothing we can do, contact aws support if that's your case
315296
_flush(callback: TransformCallback): void {
297+
if (!this.headersSent) {
298+
this.flushHeaders();
299+
}
300+
// In some cases we might not have a store i.e. for example in the image optimization function
301+
// We may want to reconsider this in the future, it might be intersting to have access to this store everywhere
302+
globalThis.__openNextAls
303+
?.getStore()
304+
?.pendingPromiseRunner.add(this.onEnd(this.headers));
305+
const bodyLength = this.getBody().length;
306+
this.streamCreator?.onFinish(bodyLength);
307+
308+
//This is only here because of aws broken streaming implementation.
309+
//Hopefully one day they will be able to give us a working streaming implementation in lambda for everyone
310+
//If you're lucky you have a working streaming implementation in your aws account and don't need this
311+
//If not you can set the OPEN_NEXT_FORCE_NON_EMPTY_RESPONSE env variable to true
312+
//BE CAREFUL: Aws keeps rolling out broken streaming implementations even on accounts that had working ones before
313+
//This is not dependent on the node runtime used
316314
if (
317-
this.getBody().length < 1 &&
315+
bodyLength === 0 &&
318316
// We use an env variable here because not all aws account have the same behavior
319317
// On some aws accounts the response will hang if the body is empty
320318
// We are modifying the response body here, this is not a good practice

0 commit comments

Comments
 (0)