fix: useSSE URL constructor crash with relative API_URL (#46)#49
Merged
Conversation
) 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) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
window.location.originas the base URL when constructing the SSE EventSource URL inuseSSE, so deployments that setNEXT_PUBLIC_API_URLto a relative path (e.g./apibehind nginx) no longer crash the dashboard.NEXT_PUBLIC_API_URL='/api'and asserts the hook can mount without throwing.Root cause
new URL("/api/events/abc")throwsTypeError: Failed to construct 'URL': Invalid URL— the constructor requires either an absolute URL or a base.UploadSSEBridgeopens its first SSE connection the moment any asset starts processing after an upload, which is exactly when the reporter saw the crash.The issue body attributes the crash to a null
thumbnail_urlin the layout chunk, but reproduction (re-running on a working dev build withNEXT_PUBLIC_API_URL='/api') confirmed the URL constructor is the actual culprit.useSSEgets bundled into the same layout chunk by Next.js, which is why the minified stack pointed there.Closes #46.
Test plan
npx vitest run hooks/__tests__/use-sse.test.ts— new test fails before fix, passes after (10 / 10 green)npx vitest run— full frontend suite (108 passing; 5 pre-existing failures inapi.test.tsandnotification-store.test.tsare unrelated to this change, present onmain)new URL('/api/events/x', window.location.origin)resolves tohttp://localhost:3000/api/events/xinstead of throwingNEXT_PUBLIC_API_URL='/api'🤖 Generated with Claude Code