Skip to content

Conversation

@hsteude
Copy link

@hsteude hsteude commented Jan 10, 2026

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

@github-actions
Copy link
Contributor

Hey! Your PR title Feature/base path support doesn't follow conventional commit format.

Please update it to start with one of:

  • feat: or feat(scope): new feature
  • fix: or fix(scope): bug fix
  • docs: or docs(scope): documentation changes
  • chore: or chore(scope): maintenance tasks
  • refactor: or refactor(scope): code refactoring
  • test: or test(scope): adding or updating tests

Where scope is the package name (e.g., app, desktop, opencode).

See CONTRIBUTING.md for details.

@github-actions
Copy link
Contributor

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

No duplicate PRs found

@hsteude hsteude changed the title Feature/base path support feat: base path support Jan 10, 2026
@github-actions
Copy link
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@PhantomYdn
Copy link

Really looking for this PR to be merged! Thank you, @hsteude. Dear maintainers - please check/merge.

@PhantomYdn
Copy link

@hsteude I was playing around with your PR. Seems that additional fix is needed. When I run from a custom base path I see the following error:

Error: Could not connect to server. Is there a server running at `http://localhost:8080`?
    at l (http://localhost:8080/my/custom/base/assets/index-DT_hEdoD.js:1728:18349)

@hsteude hsteude force-pushed the feature/base-path-support branch from 77978fe to e393b1f Compare January 14, 2026 09:38
@hsteude
Copy link
Author

hsteude commented Jan 14, 2026

@hsteude I was playing around with your PR. Seems that additional fix is needed. When I run from a custom base path I see the following error:

Error: Could not connect to server. Is there a server running at `http://localhost:8080`?
    at l (http://localhost:8080/my/custom/base/assets/index-DT_hEdoD.js:1728:18349)

@PhantomYdn : Thanks for taking a look into this, good catch! I didn't realize because we tested with our own internal build where the basePath fix is baked in at build time. Added a commit that fixes the regex pattern. Also rebased on dev. Would you mind checking again if it works for you now?

@PhantomYdn
Copy link

@hsteude It partially helped, but not yet fully solve the issue. The fonts (may be other resources as well) are loaded through JavaScript and still assume hardcoded paths like /assets/. If it will help - I have patched version of base-path.ts with which everything works for me. But(!) it's solving the problem in a little bit ugly way: patching JS on the fly.
base-path.ts

PhantomYdn added a commit to SkyNetCMS/SkyNetCMS that referenced this pull request Jan 16, 2026
- Install Bun for building OpenCode from source
- Clone OpenCode from prokube/opencode fork with base-path PR (#7625)
- Build OpenCode with 'bun install' and 'bun run build'
- Add --base-path /sn_admin/oc flag to opencode web command
- Update health check URL to use base path
- Remove nginx rewrite since OpenCode handles base path internally

This fixes OpenCode assets (JS/CSS) returning 404 when running behind
nginx reverse proxy at /sn_admin/oc/

PR: anomalyco/opencode#7625
TODO: Switch back to npm install when PR is merged to mainline
PhantomYdn added a commit to SkyNetCMS/opencode that referenced this pull request Jan 16, 2026
Add support for running OpenCode behind a reverse proxy with a configurable base path prefix (e.g., /myapp/).

- 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
@PhantomYdn
Copy link

@hsteude I have solved this problem here: https://github.com/SkyNetCMS/opencode. There are few complexities, like availablility of frontend beyond reverse proxy with fixated opencode version (apparantly "opencode web" proxy all requests to app.opencode.ai and it's posslb to have case when opencode is older and not compatible with frontend)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE]: Base path / prefix routing support

2 participants