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