Skip to content

fix 304 incorrectly set as 200 #632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/slow-icons-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/aws": patch
---

fix 304 incorrectly set as 200
40 changes: 19 additions & 21 deletions packages/open-next/src/http/openNextResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,11 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {

constructor(
private fixHeaders: (headers: OutgoingHttpHeaders) => void,
onEnd: (headers: OutgoingHttpHeaders) => Promise<void>,
private onEnd: (headers: OutgoingHttpHeaders) => Promise<void>,
private streamCreator?: StreamCreator,
private initialHeaders?: OutgoingHttpHeaders,
) {
super();
this.once("finish", () => {
if (!this.headersSent) {
this.flushHeaders();
}
// In some cases we might not have a store i.e. for example in the image optimization function
// We may want to reconsider this in the future, it might be intersting to have access to this store everywhere
globalThis.__openNextAls
?.getStore()
?.pendingPromiseRunner.add(onEnd(this.headers));
const bodyLength = this.getBody().length;
this.streamCreator?.onFinish(bodyLength);
});
}

// Necessary for next 12
Expand Down Expand Up @@ -305,16 +293,26 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
callback();
}

//This is only here because of aws broken streaming implementation.
//Hopefully one day they will be able to give us a working streaming implementation in lambda for everyone
//If you're lucky you have a working streaming implementation in your aws account and don't need this
//If not you can set the OPEN_NEXT_FORCE_NON_EMPTY_RESPONSE env variable to true
//BE CAREFUL: Aws keeps rolling out broken streaming implementations even on accounts that had working ones before
//This is not dependent on the node runtime used
//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
_flush(callback: TransformCallback): void {
if (!this.headersSent) {
this.flushHeaders();
}
// In some cases we might not have a store i.e. for example in the image optimization function
// We may want to reconsider this in the future, it might be intersting to have access to this store everywhere
globalThis.__openNextAls
?.getStore()
?.pendingPromiseRunner.add(this.onEnd(this.headers));
const bodyLength = this.getBody().length;
this.streamCreator?.onFinish(bodyLength);

//This is only here because of aws broken streaming implementation.
//Hopefully one day they will be able to give us a working streaming implementation in lambda for everyone
//If you're lucky you have a working streaming implementation in your aws account and don't need this
//If not you can set the OPEN_NEXT_FORCE_NON_EMPTY_RESPONSE env variable to true
//BE CAREFUL: Aws keeps rolling out broken streaming implementations even on accounts that had working ones before
//This is not dependent on the node runtime used
if (
this.getBody().length < 1 &&
bodyLength === 0 &&
// We use an env variable here because not all aws account have the same behavior
// On some aws accounts the response will hang if the body is empty
// We are modifying the response body here, this is not a good practice
Expand Down
Loading