Skip to content

fix: use location.host for web browsers without breaking Tauri#5967

Closed
majiayu000 wants to merge 1 commit intoanomalyco:devfrom
majiayu000:fix/port-detection-web-only
Closed

fix: use location.host for web browsers without breaking Tauri#5967
majiayu000 wants to merge 1 commit intoanomalyco:devfrom
majiayu000:fix/port-detection-web-only

Conversation

@majiayu000
Copy link
Copy Markdown
Contributor

Summary

Changes

This implementation carefully preserves the execution order to avoid breaking Tauri:

  1. Preserved Tauri priority: window.__OPENCODE__ check remains in its original position
  2. Added web browser fallback: New localhost/127.0.0.1 check before the "/" fallback
  3. Dynamic port detection: Uses location.host which includes both hostname and port

Why This Works

Three Runtime Environments:

Environment window.__OPENCODE__ location.host Result
Tauri Desktop ✅ Exists tauri://localhost Uses http://127.0.0.1:${port} (line 37)
Web Browser (localhost) ❌ Not exists localhost:8080 Uses http://localhost:8080 (line 41-42)
Production (opencode.ai) ❌ Not exists opencode.ai Uses http://localhost:4096 (line 36)

Why Previous Fix Failed:

The previous PR #5949 changed the condition order, causing Tauri apps (which run on localhost) to use location.host instead of window.__OPENCODE__.port, breaking desktop functionality.

Why This Fix Works:

  • Tauri check happens before localhost check (line 37 vs line 41)
  • Web browsers don't have window.__OPENCODE__, so they skip to localhost check
  • Tauri apps have window.__OPENCODE__, so they never reach localhost check

Testing

  • ✅ Type checking passed
  • ✅ Build completed successfully
  • Ready for manual testing with opencode --port=8080

Fixes #5928

🤖 Generated with Claude Code

Co-Authored-By: Claude Sonnet 4.5 [email protected]

This fix addresses issue anomalyco#5928 where `opencode --port=8080` fails because
the port is hardcoded to 4096. The previous fix (anomalyco#5949) was reverted because
it broke the Tauri desktop application.

This implementation:
- Preserves the priority of `window.__OPENCODE__` check for Tauri apps
- Only adds localhost/127.0.0.1 detection as a fallback before "/"
- Uses `location.host` to dynamically get the correct port for web browsers
- Does not affect Tauri desktop app which has its own port management

Fixes anomalyco#5928

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
@rekram1-node
Copy link
Copy Markdown
Collaborator

5928 got fixed so I think we can close this?

@majiayu000 majiayu000 deleted the fix/port-detection-web-only branch December 23, 2025 01:19
@majiayu000
Copy link
Copy Markdown
Contributor Author

Thanks for the update.

@rmacklin
Copy link
Copy Markdown

@rekram1-node re:

5928 got fixed so I think we can close this?

#5928 was closed by #5949 which was then reverted in eebbd73 due to #5949 (comment) and this PR was the new attempt.

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.

opencode --port=8080 fails because port statically bound to 4096

3 participants