Skip to content

feat: Add basePath support on web app - Closes #7624#21243

Closed
thebreiflabb wants to merge 1 commit intoanomalyco:devfrom
thebreiflabb:feat/add-dynamic-basepath-for-web
Closed

feat: Add basePath support on web app - Closes #7624#21243
thebreiflabb wants to merge 1 commit intoanomalyco:devfrom
thebreiflabb:feat/add-dynamic-basepath-for-web

Conversation

@thebreiflabb
Copy link
Copy Markdown

Allow serving web app within a subPath of a host

  • Make app build with relative basepath
  • Initialize app router with basepath if defined from server
  • Add --base-path CLI param and server.basePath option
  • Remove basePath in Server before processing routing
  • Add basePath in requestContext for easy access in handlers
  • Normalize CSP on nonce for easily handling inline scripts
  • Use built-in Bun HTMLRewriter for manipulating HTML basepath before serving to client

Issue for this PR

Closes #7624

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Add support for serving the web app under a basePath. This is useful when serving OpenCode on web through an ingress / proxy setup, where you might have https://www.mydomain.com/apps/opencode/

How to test:

(dev still works as before, but the new basePath functionality only works in built binary until the new app is deployed to https://app.opencode.ai)

bun packages/opencode/script/build.ts --single

./packages/opencode/dist/opencode-linux-x64/bin/opencode web --base-path /my-base/sub-path/

Will open on http://127.0.0.1:4096/my-base/sub-path/

Alternatively, configure opencode.json with:

{
  "server": {
    "basePath": "/my-base/sub-path/"
  }
}

This is solved with combining changes in:

Web App

  1. Configure Vite to build with relative basePath, where assets will load relatively from the current path
  2. Load __OPENCODE__.basePath if defined from the server, use to initialize Solid Router and as base for API calls to server

Opencode Server app

  1. Add server.basePath config option and --base-path CLI argument
  2. Configure Server listener to remove basePath before Hono app route handling, making it transparent /my-base/path/global/event => /global/event
  3. To avoid complicating the Hono setup, passing around Hono<Env> types, a simple AsyncLocalStorage - requestContext holds the basePath context to use for nested handlers
  4. When serving the HTML, Bun's built-in HTMLRewriter handles:
    • Rewriting of the basePaths on links in the HTML
    • Adding script nonce
    • Add basePath to/initialize window.__OPENCODE__
  5. Switch to using a nonce for CSP, to simplify multiple inline scripts being allowed in the CSP

How did you verify your code works?

I have tested built binary:

./script/build.ts --single

With no basePath, --base-path /various/levels/ as well as through config server.basePath option. This looks to work very well


When running bun dev, this works quite well, and does not break, however since this proxies to https://app.opencode.ai - the referenced JS assets are not updated with the new basePath handling, and the basePath is ignored until this app is deployed with these changes (works as if there is no basePath defined)

Screenshots / recordings

If this is a UI change, please include a screenshot or recording.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 6, 2026

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

Potential duplicate PRs found:

  1. PR feat: base path support #7625 - "feat: base path support"

    • feat: base path support #7625
    • This appears to be directly related to the basePath feature and may be a prior or related attempt at implementing the same functionality.
  2. PR feat: App - Support setting base URL during build #18209 - "feat: App - Support setting base URL during build"

Why they might be related:

I recommend checking the status and content of these PRs to see if they've been superseded, are still open, or contain overlapping work.

@thebreiflabb thebreiflabb force-pushed the feat/add-dynamic-basepath-for-web branch from 87b9d5d to b01ed1d Compare April 6, 2026 23:13
@thebreiflabb
Copy link
Copy Markdown
Author

Not sure why the test failed after rebasing, I don't believe the code has a touchpoint on the failing test, and tests are passing locally. Could it be a flaky test?

Allow serving web app within a subpath of a host

- Make app build with relative basepath
- Initialize app router with basepath if defined from server
- Add `--base-path` CLI param and `server.basePath` option
- Remove basePath in Server before processing routing
- Add basePath  in requestContext for easy access in handlers
- Normalize CSP on `nonce` for easily handling inline scripts
- Use built-in Bun `HTMLRewriter` for manipulating HTML basepath before serving to client
@thebreiflabb thebreiflabb force-pushed the feat/add-dynamic-basepath-for-web branch from b01ed1d to 932f1fb Compare April 7, 2026 06:26
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

1 participant