Skip to content

fix: useSSE URL constructor crash with relative API_URL (#46)#49

Merged
ravirajsinh45 merged 1 commit into
mainfrom
fix/sse-url-relative-api-url
Apr 13, 2026
Merged

fix: useSSE URL constructor crash with relative API_URL (#46)#49
ravirajsinh45 merged 1 commit into
mainfrom
fix/sse-url-relative-api-url

Conversation

@ravirajsinh45

@ravirajsinh45 ravirajsinh45 commented Apr 13, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Pass window.location.origin as the base URL when constructing the SSE EventSource URL in useSSE, so deployments that set NEXT_PUBLIC_API_URL to a relative path (e.g. /api behind nginx) no longer crash the dashboard.
  • Adds a regression test that stubs NEXT_PUBLIC_API_URL='/api' and asserts the hook can mount without throwing.

Root cause

new URL("/api/events/abc") throws TypeError: Failed to construct 'URL': Invalid URL — the constructor requires either an absolute URL or a base. UploadSSEBridge opens 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_url in the layout chunk, but reproduction (re-running on a working dev build with NEXT_PUBLIC_API_URL='/api') confirmed the URL constructor is the actual culprit. useSSE gets 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 in api.test.ts and notification-store.test.ts are unrelated to this change, present on main)
  • Verified in browser: new URL('/api/events/x', window.location.origin) resolves to http://localhost:3000/api/events/x instead of throwing
  • Manually retest in a deployment with NEXT_PUBLIC_API_URL='/api'

🤖 Generated with Claude Code

)

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>
@ravirajsinh45 ravirajsinh45 merged commit fdfbf5d into main Apr 13, 2026
4 checks passed
@ravirajsinh45 ravirajsinh45 deleted the fix/sse-url-relative-api-url branch April 13, 2026 06:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Frontend crashes with "TypeError: Failed to construct 'URL'" when asset thumbnail_url is null during processing

1 participant