Skip to content

Commit 1abd014

Browse files
fix(next): make SSE route stream on GET with query and POST body; factor helper
1 parent e65026e commit 1abd014

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

next-app/app/api/chat/stream/route.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ function* fakeStream(text: string) {
88
}
99
}
1010

11-
export async function POST(req: NextRequest) {
12-
const { message } = await req.json();
11+
function streamForMessage(message: string) {
1312
const ctrl = new AbortController();
14-
const stream = new ReadableStream({
13+
const stream = new ReadableStream<Uint8Array>({
1514
async start(controller) {
1615
try {
1716
const reply = `Echo: ${message}`;
@@ -33,10 +32,15 @@ export async function POST(req: NextRequest) {
3332
return new Response(stream, { headers: { 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache', Connection: 'keep-alive' } });
3433
}
3534

36-
export async function GET() {
37-
// Keep the SSE connection open; body is ignored, we stream via POST response
38-
const stream = new ReadableStream({ start(){} });
39-
return new Response(stream, { headers: { 'Content-Type': 'text/event-stream' } });
35+
export async function POST(req: NextRequest) {
36+
const { message } = await req.json();
37+
return streamForMessage(message);
38+
}
39+
40+
export async function GET(req: NextRequest) {
41+
const { searchParams } = new URL(req.url);
42+
const message = searchParams.get('q') ?? '';
43+
return streamForMessage(message);
4044
}
4145

4246
function encode(s: string) { return new TextEncoder().encode(s); }

0 commit comments

Comments
 (0)