From 1f868f46ca433ac955aecb5066bc9fd6825d6b34 Mon Sep 17 00:00:00 2001 From: CS Date: Sun, 25 Jan 2026 20:42:25 -0500 Subject: [PATCH] Fix SSE duplicate Transfer-Encoding headers Replace Hono's streamSSE with manual stream implementation to avoid sending duplicate Transfer-Encoding: chunked headers which cause nginx/Cloudflare to reject responses with 502 errors. Manually formats SSE messages with proper event/data structure and sets Content-Type: text/event-stream headers. --- backend/src/routes/sse.ts | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/backend/src/routes/sse.ts b/backend/src/routes/sse.ts index 0af506a7..ea255c48 100644 --- a/backend/src/routes/sse.ts +++ b/backend/src/routes/sse.ts @@ -8,10 +8,6 @@ export function createSSERoutes() { const app = new Hono() app.get('/stream', async (c) => { - c.header('Cache-Control', 'no-cache, no-store, no-transform') - c.header('Connection', 'keep-alive') - c.header('X-Accel-Buffering', 'no') - const directoriesParam = c.req.query('directories') const directories = directoriesParam ? directoriesParam.split(',').filter(Boolean) : [] const clientId = `client_${Date.now()}_${Math.random().toString(36).slice(2)}` @@ -90,34 +86,28 @@ export function createSSERoutes() { }) app.get('/test-stream', async (c) => { - c.header('Content-Type', 'text/event-stream') - c.header('Cache-Control', 'no-cache, no-store, no-transform') - c.header('Connection', 'keep-alive') - c.header('X-Accel-Buffering', 'no') + return stream(c, async (writer) => { + const writeSSE = (event: string, data: string) => { + const lines = [] + if (event) lines.push(`event: ${event}`) + lines.push(`data: ${data}`) + lines.push('') + lines.push('') + writer.write(new TextEncoder().encode(lines.join('\n'))) + } - return streamSSE(c, async (stream) => { - await stream.writeSSE({ - event: 'test', - data: JSON.stringify({ message: 'SSE working', timestamp: Date.now() }) - }) + writeSSE('test', JSON.stringify({ message: 'SSE working', timestamp: Date.now() })) let count = 0 const interval = setInterval(async () => { count++ try { - await stream.writeSSE({ - event: 'ping', - data: JSON.stringify({ count, timestamp: Date.now() }) - }) + writeSSE('ping', JSON.stringify({ count, timestamp: Date.now() })) } catch { clearInterval(interval) } }, 1000) - stream.onAbort(() => { - clearInterval(interval) - }) - await new Promise(() => {}) }) })