Skip to content

feat: add --base-path support for reverse proxy sub-path deployment - #34822

Closed
superafun wants to merge 1 commit into
anomalyco:devfrom
superafun:feat/base-path-support
Closed

feat: add --base-path support for reverse proxy sub-path deployment#34822
superafun wants to merge 1 commit into
anomalyco:devfrom
superafun:feat/base-path-support

Conversation

@superafun

Copy link
Copy Markdown

Problem

Users want to deploy the OpenCode Web UI behind a reverse proxy under a sub-path (e.g. https://example.com/opencode/), but all resource paths, API calls, WebSocket connections, and frontend routes are hardcoded to root /.

Solution

Allow the web UI to be served under a configurable base path at runtime — no rebuild required. This works for the embedded UI in pre-built binaries (the common deployment).

How it works

  1. CLI flag: opencode web --base-path /opencode/ sets the base path
  2. Server-side HTML injection: When returning index.html, the server injects the base path into a <script id="oc-base-path" type="application/json"> tag
  3. Frontend reads it: Before the JS bundle executes, the frontend reads the injected value and uses it for:
    • API base URL (getCurrentUrl() in entry.tsx)
    • @solidjs/router base prop (so routes match correctly under the sub-path)
    • WebSocket URLs (via the existing terminalWebSocketURL which builds on the server URL)
  4. Relative asset paths: Static asset references in index.html are changed to relative paths so the browser resolves them against the document URL

nginx config (strips prefix before forwarding)

location /opencode/ {
    proxy_pass http://localhost:4096/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_read_timeout 86400;
}

Usage

opencode web --base-path /opencode/ --hostname 0.0.0.0

The base path defaults to / so existing deployments are unaffected.

Files changed

File Change
packages/app/index.html Relative asset paths + oc-base-path script tag
packages/app/src/entry.tsx getBasePath() reads injected value; getCurrentUrl() uses it
packages/app/src/app.tsx Pass basePath to @solidjs/router base prop
packages/opencode/src/cli/network.ts Add --base-path CLI option
packages/opencode/src/cli/cmd/web.ts Display URLs with base path
packages/opencode/src/server/server.ts Pass basePath through ListenOptions
packages/opencode/src/server/routes/instance/httpapi/server.ts Pass basePath to serveUIEffect
packages/opencode/src/server/shared/ui.ts injectBasePath() rewrites HTML at runtime

Notes

  • og:image / twitter:image keep their absolute / paths since social crawlers require absolute URLs and social preview images are not critical for sub-path deployments.
  • The oc-base-path script uses type="application/json" so it is not executed by the browser and does not require a CSP hash.

Allow the web UI to be served behind a reverse proxy under a sub-path
(e.g. https://example.com/opencode/) without rebuilding the binary.

How it works:
- The server injects the configured base path into index.html at runtime
  via a <script id=oc-base-path type=application/json> tag
- The frontend reads this value before the JS bundle executes and uses it
  for API baseUrl, WebSocket URLs, and @solidjs/router's  prop
- Static asset paths in index.html are changed to relative paths so the
  browser resolves them against the document URL

CLI usage:
  opencode web --base-path /opencode/ --hostname 0.0.0.0

nginx config (strips the prefix before forwarding):
  location /opencode/ {
      proxy_pass http://localhost:4096/;
      ...
  }

The base path defaults to "/" so existing deployments are unaffected.
@github-actions github-actions Bot added the needs:compliance This means the issue will auto-close after 2 hours. label Jul 2, 2026
@github-actions

github-actions Bot commented Jul 2, 2026

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

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

I found potential duplicate or related PRs:

  1. PR feat(server): runtime base path support for reverse proxy deployments #28326 - feat(server): runtime base path support for reverse proxy deployments

  2. PR feat: base path support #7625 - feat: base path support

Both PRs appear to be addressing the same core issue of adding base path support for deploying the UI behind a reverse proxy. PR #28326 in particular seems to be tackling the exact same solution (runtime base path support). You should review these PRs to understand any prior work and avoid duplicating effort.

@superafun

Copy link
Copy Markdown
Author

Closing as duplicate of #28326, which already implements runtime base path support with config file integration, server-side prefix stripping, redirects, 404 handling, and 18 unit tests. My PR covers the same core idea (server-side HTML injection + frontend reads it for Router base + API baseUrl) but #28326 is more complete.

@superafun superafun closed this Jul 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs:compliance This means the issue will auto-close after 2 hours.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant