opencode icon indicating copy to clipboard operation
opencode copied to clipboard

feat: base path support

Open hsteude opened this issue 3 days ago • 4 comments

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 :)

hsteude avatar Jan 10 '26 11:01 hsteude