feat: add --base-path support for reverse proxy sub-path deployment - #34822
feat: add --base-path support for reverse proxy sub-path deployment#34822superafun wants to merge 1 commit into
Conversation
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.
|
This PR doesn't fully meet our contributing guidelines and PR template. What needs to be fixed:
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. |
|
The following comment was made by an LLM, it may be inaccurate: I found potential duplicate or related PRs:
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. |
|
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. |
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
opencode web --base-path /opencode/sets the base pathindex.html, the server injects the base path into a<script id="oc-base-path" type="application/json">taggetCurrentUrl()inentry.tsx)@solidjs/routerbaseprop (so routes match correctly under the sub-path)terminalWebSocketURLwhich builds on the server URL)index.htmlare changed to relative paths so the browser resolves them against the document URLnginx config (strips prefix before forwarding)
Usage
The base path defaults to
/so existing deployments are unaffected.Files changed
packages/app/index.htmloc-base-pathscript tagpackages/app/src/entry.tsxgetBasePath()reads injected value;getCurrentUrl()uses itpackages/app/src/app.tsxbasePathto@solidjs/routerbaseproppackages/opencode/src/cli/network.ts--base-pathCLI optionpackages/opencode/src/cli/cmd/web.tspackages/opencode/src/server/server.tsbasePaththroughListenOptionspackages/opencode/src/server/routes/instance/httpapi/server.tsbasePathtoserveUIEffectpackages/opencode/src/server/shared/ui.tsinjectBasePath()rewrites HTML at runtimeNotes
og:image/twitter:imagekeep their absolute/paths since social crawlers require absolute URLs and social preview images are not critical for sub-path deployments.oc-base-pathscript usestype="application/json"so it is not executed by the browser and does not require a CSP hash.