[PR #7625] feat: base path support #12458

Open
opened 2026-02-16 18:17:21 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/7625

State: open
Merged: No


Add support for running OpenCode behind a reverse proxy with a configurable base path prefix (e.g., /myapp/)

Fixes this issue.

Why

When deploying OpenCode with path-based routing, the application needs to serve assets and handle routing under a URL prefix. Without this, OpenCode can only run at the root path.

What it does

  • Adds --base-path CLI option, OPENCODE_BASE_PATH env var, and server.basePath config
  • Rewrites HTML/JS/CSS responses at runtime to include the base path
  • Wraps history.pushState/replaceState to prepend base path to URLs

Design decisions

Runtime rewriting: We rewrite content at runtime because the frontend is proxied from app.opencode.ai and we don't control the build.

Regex-based Vite patching: Vite bakes the base path into a function at build time (function(t){return"/"+t}). Since we don't know the base path until runtime, we patch this via regex. It's not great, but the only alternative would require rebuilding the frontend for each deployment.

Double mounting: The app is mounted at both basePath AND / because some reverse proxies strip the path before forwarding. Without this, requests would 404.

No CSP with basePath: We inject inline scripts for the history wrapper, which is incompatible with strict CSP.

Feedback and Ideas are welcome :)

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/7625 **State:** open **Merged:** No --- # Add support for running OpenCode behind a reverse proxy with a configurable base path prefix (e.g., `/myapp/`) Fixes [this issue](https://github.com/anomalyco/opencode/issues/7624). ## Why When deploying OpenCode with path-based routing, the application needs to serve assets and handle routing under a URL prefix. Without this, OpenCode can only run at the root path. ## What it does - Adds `--base-path` CLI option, `OPENCODE_BASE_PATH` env var, and `server.basePath` config - Rewrites HTML/JS/CSS responses at runtime to include the base path - Wraps `history.pushState/replaceState` to prepend base path to URLs ## Design decisions **Runtime rewriting**: We rewrite content at runtime because the frontend is proxied from `app.opencode.ai` and we don't control the build. **Regex-based Vite patching**: Vite bakes the base path into a function at build time (`function(t){return"/"+t}`). Since we don't know the base path until runtime, we patch this via regex. It's not great, but the only alternative would require rebuilding the frontend for each deployment. **Double mounting**: The app is mounted at both `basePath` AND `/` because some reverse proxies strip the path before forwarding. Without this, requests would 404. **No CSP with basePath**: We inject inline scripts for the history wrapper, which is incompatible with strict CSP. ## Feedback and Ideas are welcome :)
yindo added the pull-request label 2026-02-16 18:17:21 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#12458