diff --git a/examples/app-router/app/ssr/layout.tsx b/examples/app-router/app/ssr/layout.tsx index d51f11bc..5ab0b64a 100644 --- a/examples/app-router/app/ssr/layout.tsx +++ b/examples/app-router/app/ssr/layout.tsx @@ -1,13 +1,11 @@ import { PropsWithChildren } from "react"; -import Filler from "@example/shared/components/Filler"; - export default function Layout({ children }: PropsWithChildren) { return (

SSR

{/* 16 kb seems necessary here to prevent any buffering*/} - + {/* */} {children}
); diff --git a/packages/open-next/src/http/openNextResponse.ts b/packages/open-next/src/http/openNextResponse.ts index 503cd6c8..ba5648ef 100644 --- a/packages/open-next/src/http/openNextResponse.ts +++ b/packages/open-next/src/http/openNextResponse.ts @@ -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 @@ -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[] = []; @@ -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(); + } } } diff --git a/packages/open-next/src/wrappers/aws-lambda-streaming.ts b/packages/open-next/src/wrappers/aws-lambda-streaming.ts index 3f48a671..8e3a2fdd 100644 --- a/packages/open-next/src/wrappers/aws-lambda-streaming.ts +++ b/packages/open-next/src/wrappers/aws-lambda-streaming.ts @@ -119,6 +119,7 @@ const handler: WrapperHandler = async (handler, converter) => compressedStream?.end(new Uint8Array(8)); } }, + waitForFirstWrite: true, }; const response = await handler(internalEvent, streamCreator);