Skip to content

Commit

Permalink
improved streaming
Browse files Browse the repository at this point in the history
  • Loading branch information
conico974 committed Mar 12, 2024
1 parent 356a573 commit 2932436
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
4 changes: 1 addition & 3 deletions examples/app-router/app/ssr/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { PropsWithChildren } from "react";

import Filler from "@example/shared/components/Filler";

export default function Layout({ children }: PropsWithChildren) {
return (
<div>
<h1>SSR</h1>
{/* 16 kb seems necessary here to prevent any buffering*/}
<Filler size={16} />
{/* <Filler size={16} /> */}
{children}
</div>
);
Expand Down
17 changes: 15 additions & 2 deletions packages/open-next/src/http/openNextResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface StreamCreator {
// Just to fix an issue with aws lambda streaming with empty body
onWrite?: () => void;
onFinish: () => void;
waitForFirstWrite?: boolean;
}

// We only need to implement the methods that are used by next.js
Expand All @@ -23,6 +24,7 @@ export class OpenNextNodeResponse extends Transform {
headers: OutgoingHttpHeaders = {};
private _cookies: string[] = [];
private responseStream?: Writable;
private hasDoneFirstWrite: boolean = false;
headersSent: boolean = false;
_chunks: Buffer[] = [];

Expand Down Expand Up @@ -144,7 +146,18 @@ export class OpenNextNodeResponse extends Transform {
if (!this.headersSent) {
this.flushHeaders();
}
this._internalWrite(chunk, encoding);
callback();
if (this.streamCreator?.waitForFirstWrite && !this.hasDoneFirstWrite) {
const waitTime = parseInt(
process.env.STREAMING_INITIAL_WRITE_WAIT_TIME ?? "25",
);
new Promise((resolve) => setTimeout(resolve, waitTime)).then(() => {
this._internalWrite(chunk, encoding);
this.hasDoneFirstWrite = true;
callback();
});
} else {
this._internalWrite(chunk, encoding);
callback();
}
}
}
1 change: 1 addition & 0 deletions packages/open-next/src/wrappers/aws-lambda-streaming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ const handler: WrapperHandler = async (handler, converter) =>
compressedStream?.end(new Uint8Array(8));
}
},
waitForFirstWrite: true,
};

const response = await handler(internalEvent, streamCreator);
Expand Down

0 comments on commit 2932436

Please sign in to comment.