From 032b94902d2aa7dc38a4bf08d06d795f81f40185 Mon Sep 17 00:00:00 2001 From: ravirajsinh45 Date: Mon, 13 Apr 2026 11:35:02 +0530 Subject: [PATCH] fix: pass base URL to new URL() in useSSE so relative API_URL works (#46) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When NEXT_PUBLIC_API_URL is set to a relative path (e.g. "/api" for deployments behind a reverse proxy), `new URL("/api/events/abc")` throws "Failed to construct 'URL': Invalid URL" without a base. UploadSSEBridge opens the first SSE connection right after an upload starts, so the dashboard crashed the moment any asset was uploaded. Pass window.location.origin as the base so relative paths resolve. The reporter attributed the crash to a null thumbnail_url in the layout chunk, but reproduction in dev showed the real culprit is the URL constructor in useSSE — the layout chunk minifies useSSE into the same bundle, which is why the stack pointed there. Co-Authored-By: Claude Opus 4.6 (1M context) --- apps/web/hooks/__tests__/use-sse.test.ts | 27 ++++++++++++++++++++++++ apps/web/hooks/use-sse.ts | 6 +++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/apps/web/hooks/__tests__/use-sse.test.ts b/apps/web/hooks/__tests__/use-sse.test.ts index d373b08..582bbb8 100644 --- a/apps/web/hooks/__tests__/use-sse.test.ts +++ b/apps/web/hooks/__tests__/use-sse.test.ts @@ -156,3 +156,30 @@ describe('useSSE hook', () => { vi.useRealTimers() }) }) + +describe('useSSE with relative NEXT_PUBLIC_API_URL', () => { + beforeEach(() => { + MockEventSource.reset() + vi.stubGlobal('EventSource', MockEventSource) + }) + + afterEach(() => { + vi.unstubAllGlobals() + vi.unstubAllEnvs() + vi.resetModules() + }) + + // Regression for issue #46: deployments behind nginx set NEXT_PUBLIC_API_URL + // to a relative path like "/api". `new URL("/api/events/abc")` throws + // "Failed to construct 'URL': Invalid URL" without a base, crashing the + // dashboard the moment UploadSSEBridge first opens an SSE connection. + it('builds a valid URL when NEXT_PUBLIC_API_URL is a relative path', async () => { + vi.stubEnv('NEXT_PUBLIC_API_URL', '/api') + vi.resetModules() + const { useSSE: useSSEFresh } = await import('../use-sse') + + expect(() => renderHook(() => useSSEFresh('project-123'))).not.toThrow() + expect(MockEventSource.instances).toHaveLength(1) + expect(MockEventSource.instances[0].url).toContain('/api/events/project-123') + }) +}) diff --git a/apps/web/hooks/use-sse.ts b/apps/web/hooks/use-sse.ts index c52c288..affb0db 100644 --- a/apps/web/hooks/use-sse.ts +++ b/apps/web/hooks/use-sse.ts @@ -126,7 +126,11 @@ export function useSSE(projectId: string | null | undefined, options: UseSSEOpti if (destroyed) return const token = getAccessToken() - const url = new URL(`${API_URL}/events/${projectId}`) + // Use window.location.origin as a base so deployments behind a reverse + // proxy can set NEXT_PUBLIC_API_URL to a relative path like "/api" + // without crashing the URL constructor. + const base = typeof window !== 'undefined' ? window.location.origin : 'http://localhost' + const url = new URL(`${API_URL}/events/${projectId}`, base) if (token) { url.searchParams.set('token', token) }