Skip to content

Commit 2932436

Browse files
committed
1 parent 356a573 commit 2932436

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

examples/app-router/app/ssr/layout.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import { PropsWithChildren } from "react";
22

3-
import Filler from "@example/shared/components/Filler";
4-
53
export default function Layout({ children }: PropsWithChildren) {
64
return (
75
<div>
86
<h1>SSR</h1>
97
{/* 16 kb seems necessary here to prevent any buffering*/}
10-
<Filler size={16} />
8+
{/* <Filler size={16} /> */}
119
{children}
1210
</div>
1311
);

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export interface StreamCreator {
1414
// Just to fix an issue with aws lambda streaming with empty body
1515
onWrite?: () => void;
1616
onFinish: () => void;
17+
waitForFirstWrite?: boolean;
1718
}
1819

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

@@ -144,7 +146,18 @@ export class OpenNextNodeResponse extends Transform {
144146
if (!this.headersSent) {
145147
this.flushHeaders();
146148
}
147-
this._internalWrite(chunk, encoding);
148-
callback();
149+
if (this.streamCreator?.waitForFirstWrite && !this.hasDoneFirstWrite) {
150+
const waitTime = parseInt(
151+
process.env.STREAMING_INITIAL_WRITE_WAIT_TIME ?? "25",
152+
);
153+
new Promise((resolve) => setTimeout(resolve, waitTime)).then(() => {
154+
this._internalWrite(chunk, encoding);
155+
this.hasDoneFirstWrite = true;
156+
callback();
157+
});
158+
} else {
159+
this._internalWrite(chunk, encoding);
160+
callback();
161+
}
149162
}
150163
}

packages/open-next/src/wrappers/aws-lambda-streaming.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ const handler: WrapperHandler = async (handler, converter) =>
119119
compressedStream?.end(new Uint8Array(8));
120120
}
121121
},
122+
waitForFirstWrite: true,
122123
};
123124

124125
const response = await handler(internalEvent, streamCreator);

0 commit comments

Comments
 (0)