| 1 | /** |
| 2 | * Streaming SSE proxy for Canopy events. |
| 3 | * |
| 4 | * Next.js rewrites buffer responses, which breaks Server-Sent Events. |
| 5 | * This route handler streams the SSE response directly from grove-api |
| 6 | * to the browser without buffering. |
| 7 | */ |
| 8 | |
| 9 | const GROVE_API_URL = process.env.GROVE_API_URL ?? "http://localhost:4000"; |
| 10 | |
| 11 | export const dynamic = "force-dynamic"; |
| 12 | |
| 13 | export async function GET(request: Request) { |
| 14 | const { searchParams } = new URL(request.url); |
| 15 | const upstream = `${GROVE_API_URL}/api/canopy/events?${searchParams}`; |
| 16 | |
| 17 | const res = await fetch(upstream, { |
| 18 | headers: { |
| 19 | Accept: "text/event-stream", |
| 20 | "Cache-Control": "no-cache", |
| 21 | }, |
| 22 | signal: request.signal, |
| 23 | }); |
| 24 | |
| 25 | if (!res.ok || !res.body) { |
| 26 | return new Response("upstream error", { status: 502 }); |
| 27 | } |
| 28 | |
| 29 | return new Response(res.body, { |
| 30 | headers: { |
| 31 | "Content-Type": "text/event-stream", |
| 32 | "Cache-Control": "no-cache", |
| 33 | Connection: "keep-alive", |
| 34 | "X-Accel-Buffering": "no", |
| 35 | }, |
| 36 | }); |
| 37 | } |
| 38 | |