fix(tinyoffice): connect SSE directly to API, bypass Next.js rewrite#232
fix(tinyoffice): connect SSE directly to API, bypass Next.js rewrite#232crisdias wants to merge 1 commit intoTinyAGI:mainfrom
Conversation
Next.js rewrites buffer SSE responses, causing the TinyOffice office view to always show "Disconnected". This makes the EventSource connect directly to the API server on port 3777 instead of going through the Next.js rewrite proxy. Fixes TinyAGI#231 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Greptile SummaryThis PR fixes a real-time SSE connectivity issue where the Next.js rewrite proxy was buffering Key changes:
Issue found:
Confidence Score: 3/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant Browser
participant NextJS as Next.js (port 3000)
participant API as API Server (port 3777)
Note over Browser,API: Before fix — SSE via Next.js rewrite (buffered)
Browser->>NextJS: EventSource /api/events/stream
NextJS-->>API: proxied request (buffers SSE)
API-->>NextJS: SSE events (held in buffer)
NextJS--xBrowser: events never flushed → "Disconnected"
Note over Browser,API: After fix — SSE direct to API
Browser->>API: EventSource http://hostname:3777/api/events/stream
API-->>Browser: SSE events streamed in real-time → "Live"
Last reviewed commit: d9d018e |
| const sseBase = typeof window !== "undefined" | ||
| ? (process.env.NEXT_PUBLIC_SSE_URL ?? `${window.location.protocol}//${window.location.hostname}:3777`) | ||
| : ""; |
There was a problem hiding this comment.
Fallback URL ignores NEXT_PUBLIC_API_URL
The fallback constructs the SSE URL from window.location.hostname hardcoded to port 3777. However, API_BASE at the top of the file already respects NEXT_PUBLIC_API_URL, which users may have configured to point to an API server on a different hostname (e.g., http://my-api-server:3777). In that case, the new fallback will silently connect SSE to the wrong host (${window.location.hostname}:3777), breaking SSE for any deployment where the API is not co-located with the frontend server — even though all other API calls route correctly via API_BASE.
Using NEXT_PUBLIC_API_URL in the fallback chain (before the window.location guess) would preserve consistency and avoid surprises:
| const sseBase = typeof window !== "undefined" | |
| ? (process.env.NEXT_PUBLIC_SSE_URL ?? `${window.location.protocol}//${window.location.hostname}:3777`) | |
| : ""; | |
| const sseBase = typeof window !== "undefined" | |
| ? (process.env.NEXT_PUBLIC_SSE_URL ?? process.env.NEXT_PUBLIC_API_URL ?? `${window.location.protocol}//${window.location.hostname}:3777`) | |
| : ""; |
This way:
NEXT_PUBLIC_SSE_URL– explicit SSE override (current behaviour)NEXT_PUBLIC_API_URL– reuse the already-configured API host (fixes the regression)window.location.hostname:3777– last-resort guess for plain local-dev setups
|
Note: this can also be solved by setting |
Summary
EventSourceconnect directly to the API server (port 3777) instead of going through the Next.js rewrite proxyNEXT_PUBLIC_SSE_URLenv var for custom deploymentsFixes #231
Test plan
🤖 Generated with Claude Code