Skip to content

fix(app): fall back to document.baseURI when no explicit base path is configured - #33253

Closed
awschmeder wants to merge 9 commits into
anomalyco:devfrom
awschmeder:fix/base-path-baseur-fallback
Closed

fix(app): fall back to document.baseURI when no explicit base path is configured#33253
awschmeder wants to merge 9 commits into
anomalyco:devfrom
awschmeder:fix/base-path-baseur-fallback

Conversation

@awschmeder

Copy link
Copy Markdown

Summary

This PR builds on top of #28326 (runtime base path support for reverse proxy deployments) by adding a zero-config fallback for environments where a reverse proxy injects a <base href> tag into the HTML but the operator cannot or does not pass --base-path to opencode web.

Problem

When opencode is served under a sub-path reverse proxy (e.g., JupyterHub via jupyter-server-proxy), the proxy injects <base href="/user/<u>/opencode/"> into the HTML response. Without --base-path, window.__OPENCODE_BASE_PATH__ is unset and getCurrentUrl() returns location.origin (the domain root). All API calls then go to the origin root instead of the proxy mount point, producing 404 errors.

Fix

In packages/app/src/entry.tsx, after resolving window.__OPENCODE_BASE_PATH__ and VITE_OPENCODE_SERVER_BASE_URL, if basePath is still empty, check document.baseURI. If its pathname diverges from the root, use it as the base path:

if (!basePath) {
  const baseUriPath = new URL(document.baseURI).pathname.replace(/\/+$/, "")
  if (baseUriPath && baseUriPath !== "/") basePath = baseUriPath
}

Priority order:

  1. window.__OPENCODE_BASE_PATH__ (set by --base-path, most explicit)
  2. VITE_OPENCODE_SERVER_BASE_URL (build-time env var)
  3. document.baseURI pathname (proxy-injected <base href>, zero-config fallback)
  4. Empty string (origin root, existing behavior)

Behavior

  • Direct access (no proxy): document.baseURI pathname is / -- fallback does nothing. No behavior change.
  • --base-path /opencode: window.__OPENCODE_BASE_PATH__ is set -- fallback never runs. No behavior change.
  • JupyterHub / jupyter-server-proxy: proxy injects <base href="/user/alice/opencode/"> -- fallback detects /user/alice/opencode and uses it as the API base. Works without --base-path.

Relation to #28326

This is an additive change on top of #28326. It does not conflict with the --base-path mechanism -- explicit configuration always wins. The fallback only activates when no base path is configured at all.

@github-actions github-actions Bot added needs:issue needs:compliance This means the issue will auto-close after 2 hours. labels Jun 21, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions

Copy link
Copy Markdown
Contributor

This PR doesn't fully meet our contributing guidelines and PR template.

What needs to be fixed:

  • PR description is missing required template sections. Please use the PR template.

Please edit this PR description to address the above within 2 hours, or it will be automatically closed.

If you believe this was flagged incorrectly, please let a maintainer know.

@github-actions

Copy link
Copy Markdown
Contributor

This pull request has been automatically closed because it was not updated to meet our contributing guidelines within the 2-hour window.

Feel free to open a new pull request that follows our guidelines.

@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Jun 21, 2026
@github-actions github-actions Bot closed this Jun 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants