feat: base path support
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-pathCLI option,OPENCODE_BASE_PATHenv var, andserver.basePathconfig - Rewrites HTML/JS/CSS responses at runtime to include the base path
- Wraps
history.pushState/replaceStateto 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.