Skip to content

feat: App - Support setting base URL during build - #18209

Open
Ark-kun wants to merge 3 commits into
anomalyco:devfrom
Ark-kun:feat--App---Support-setting-base-URL-to-during--build
Open

feat: App - Support setting base URL during build#18209
Ark-kun wants to merge 3 commits into
anomalyco:devfrom
Ark-kun:feat--App---Support-setting-base-URL-to-during--build

Conversation

@Ark-kun

@Ark-kun Ark-kun commented Mar 19, 2026

Copy link
Copy Markdown

This is needed to host OpenCode app under under some URL prefix. Usage: Set VITE_BASE_URL env var during build. This automatically sets the base in Vite config. It's also used in routers.
See https://vite.dev/config/shared-options#base

Issue for this PR

Issue: #7624

Type of change

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

What does this PR do?

This change is needed to host OpenCode app under under some URL prefix.
This tiny PR makes it possible to build OpenCode Web App with VITE_BASE_URL="/subdir/" and have it run under http://<host>:<port>/subdir/ instead of root (http://<host>:<port>/).

Set VITE_BASE_URL env var during build. This automatically sets the base in Vite config. Vite generates correct URLs in index.html.
import.meta.env.BASE_URL is also used in routers. See https://vite.dev/config/shared-options#base
You can also set VITE_OPENCODE_SERVER_BASE_URL to specify the base URL prefix for the server.

How did you verify your code works?

% VITE_BASE_URL="/XXX" VITE_OPENCODE_SERVER_BASE_URL="/" bun dev
$ vite
(!) "base" option should start with a slash.

  VITE v7.1.4  ready in 320 ms

  ➜  Local:   http://localhost:3000/XXX

Checklist

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

If you do not follow this template your PR will be automatically rejected.

@Ark-kun
Ark-kun requested a review from adamdotdevin as a code owner March 19, 2026 09:04
@github-actions

Copy link
Copy Markdown
Contributor

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

@Ark-kun
Ark-kun force-pushed the feat--App---Support-setting-base-URL-to-during--build branch from 4f6cd85 to 2a38d6f Compare March 20, 2026 05:51
@Ark-kun
Ark-kun force-pushed the feat--App---Support-setting-base-URL-to-during--build branch from 2a38d6f to 3d3f166 Compare April 15, 2026 23:25
@einarpersson

Copy link
Copy Markdown

If this works, I think this approach seems good as it has very few changes to the codebase and thus feels less brittle than patching everywhere.

I want to host opencode behind Caddy and tailscale (the latter not allowing for subdomains atm).

@solarkraft

Copy link
Copy Markdown

I have tested this and it works great! Same use case, comfortably statically hosting with Caddy now!

@Ark-kun
Ark-kun force-pushed the feat--App---Support-setting-base-URL-to-during--build branch from 3d3f166 to 0f54a7c Compare May 21, 2026 22:47
@thdxr
thdxr requested review from Brendonovich and Hona as code owners June 11, 2026 00:18
@Ark-kun
Ark-kun force-pushed the feat--App---Support-setting-base-URL-to-during--build branch from 0f54a7c to fbd733c Compare June 13, 2026 01:57
@Ark-kun

Ark-kun commented Jun 29, 2026

Copy link
Copy Markdown
Author

Hello, @Brendonovich. and @Hona . Would you please take a look at this PR? It's just couple simple lines of code, but if you have any concerns, our company can set up a video meeting so that we can explain each line.

@Ark-kun
Ark-kun force-pushed the feat--App---Support-setting-base-URL-to-during--build branch from fbd733c to fe53a70 Compare June 29, 2026 10:13
@Ark-kun

Ark-kun commented Aug 3, 2026

Copy link
Copy Markdown
Author

@thdxr @Brendonovich @Hona @rekram1-node @adamdotdevin Can you please take a look? It's 6 lines of code...

This is needed to host OpenCode app under under some URL prefix.
This minimal change makes it possible to build OpenCode Web App with VITE_BASE_URL="/subdir/" and have it run under `http://<host>:<port>/subdir/` instead of root (`http://<host>:<port>/`).

Set `VITE_BASE_URL` env var during build. This automatically sets the `base` in Vite config. Vite generates correct URLs in `index.html`.
`import.meta.env.BASE_URL` is also used in routers. See https://vite.dev/config/shared-options#base
You can also set `VITE_OPENCODE_SERVER_BASE_URL` to specify the base URL prefix for the server.

Usage:
```
% VITE_BASE_URL="/XXX" VITE_OPENCODE_SERVER_BASE_URL="/" bun dev
$ vite
(!) "base" option should start with a slash.

  VITE v7.1.4  ready in 320 ms

  ➜  Local:   http://localhost:3000/XXX
```
@Ark-kun
Ark-kun force-pushed the feat--App---Support-setting-base-URL-to-during--build branch from fe53a70 to a5a0a1a Compare August 8, 2026 03:09
@Brendonovich
Brendonovich enabled auto-merge (squash) August 8, 2026 03:35
…e consumer code is sensitive to it

For example, ~18 tests (e.g. e2e/regression/legacy-new-session.spec.ts) fail on "http://127.0.0.1:4096/", but succeed on "http://127.0.0.1:4096".
See https://github.com/anomalyco/opencode/actions/runs/31237532949/job/93052787926?pr=18209
auto-merge was automatically disabled August 8, 2026 08:47

Head branch was pushed to by a user without write access

@Ark-kun

Ark-kun commented Aug 8, 2026

Copy link
Copy Markdown
Author

@Brendonovich Thank you for approving my PR.

Thanks for improving the code. Your 1c15cef commit made the code more elegant. However, it changed the behavior slightly (URL.href adds trailing slash:new URL("http://localhost:4096").href == "http://localhost:4096/"). Such trailing slash triggers navigation bugs in consuming code. See the failing tests: https://github.com/anomalyco/opencode/actions/runs/31237532949/job/93052787926?pr=18209
I've fixed this new issue: 755fa7c

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.

4 participants