From 770ccb494066fd8f1a485c76fbbf5cdd5d0cf79c Mon Sep 17 00:00:00 2001 From: Brendan Date: Wed, 18 Nov 2026 06:35:15 +0000 Subject: [PATCH] refactor(app): take the base path from Vite's base config Drop VITE_BASE_URL. vite build --base=/opencode/ (or base in the config) now drives everything: the icons plugin and service worker read the resolved base, and a base without its trailing slash gets one. --- packages/app/src/env.d.ts | 2 +- packages/app/vite.config.ts | 18 ++++++++++----- packages/app/vite.icons.test.ts | 2 +- packages/app/vite.icons.ts | 39 ++++++++++++++++++++------------- packages/app/vite.pwa.ts | 26 ++++++++++++++-------- 5 files changed, 55 insertions(+), 32 deletions(-) diff --git a/packages/app/src/env.d.ts b/packages/app/src/env.d.ts index eb53462e72c..64057f079c3 100644 --- a/packages/app/src/env.d.ts +++ b/packages/app/src/env.d.ts @@ -1,5 +1,5 @@ interface ImportMetaEnv { - /** The path the build is served under, from `VITE_BASE_URL`; `/` at the root. */ + /** Vite's `base`, the path the build is served under, such as `/opencode/`; `/` at the root. */ readonly BASE_URL: string readonly VITE_OPENCODE_SERVER_MODE?: "none" | "origin" readonly VITE_OPENCODE_SERVER_HOST: string diff --git a/packages/app/vite.config.ts b/packages/app/vite.config.ts index b1f599cae71..9472376184d 100644 --- a/packages/app/vite.config.ts +++ b/packages/app/vite.config.ts @@ -1,6 +1,6 @@ import { sentryVitePlugin } from "@sentry/vite-plugin" import { fileURLToPath } from "node:url" -import { defineConfig } from "vite" +import { defineConfig, type Plugin } from "vite" import desktopPlugin, { channel } from "./vite.js" import { icons } from "./vite.icons" import { serviceWorker } from "./vite.pwa" @@ -22,15 +22,21 @@ const sentry = }) : false -// `VITE_BASE_URL=/opencode` builds the app to be served under that path; it is the root by default. -const base = `/${(process.env.VITE_BASE_URL ?? "").replace(/^\/+|\/+$/g, "")}/`.replace(/^\/\/$/, "/") +// The app is built for the root by default; `vite build --base=/opencode/` serves it under that path. A base without +// its trailing slash would reach `import.meta.env.BASE_URL` and `%BASE_URL%` as is and glue paths onto it. +const basePath: Plugin = { + name: "opencode-app:base-path", + config(config) { + if (config.base?.startsWith("/") && !config.base.endsWith("/")) return { base: `${config.base}/` } + }, +} export default defineConfig({ - base, plugins: [ + basePath, desktopPlugin, - icons(channel, base), - serviceWorker(fileURLToPath(new URL("./dist", import.meta.url)), base), + icons(channel), + serviceWorker(fileURLToPath(new URL("./dist", import.meta.url))), sentry, ] as any, server: { diff --git a/packages/app/vite.icons.test.ts b/packages/app/vite.icons.test.ts index b01910db320..7bdbc4b404f 100644 --- a/packages/app/vite.icons.test.ts +++ b/packages/app/vite.icons.test.ts @@ -40,7 +40,7 @@ test("bundles app icons under a base path", async () => { logLevel: "silent", base: "/opencode/", plugins: [ - icons("prod", "/opencode/"), + icons("prod"), { name: "icons-only-fixture", transformIndexHtml: { diff --git a/packages/app/vite.icons.ts b/packages/app/vite.icons.ts index 9bf4302f3cf..5d589a3eec3 100644 --- a/packages/app/vite.icons.ts +++ b/packages/app/vite.icons.ts @@ -2,11 +2,11 @@ import { readFileSync } from "node:fs" import type { Plugin } from "vite" import manifest from "./manifest.json" with { type: "json" } -export function icons(channel: string, base = "/"): Plugin { +export function icons(channel: string): Plugin { const selected = channel === "beta" || channel === "prod" ? channel : "dev" const prefix = `icons/${selected}` - const files = [ + const iconFiles = [ ...Object.entries({ "favicon.ico": "icon.ico", "apple-touch-icon.png": "ios/AppIcon-60x60@3x.png", @@ -17,27 +17,36 @@ export function icons(channel: string, base = "/"): Plugin { source: readFileSync(new URL(`../desktop/icons/${selected}/${source}`, import.meta.url)), type: name.endsWith(".ico") ? "image/x-icon" : "image/png", })), - { - fileName: "site.webmanifest", - source: JSON.stringify({ - ...manifest, - id: base, - start_url: base, - scope: base, - icons: manifest.icons.map((icon) => ({ ...icon, src: `${base}${prefix}${icon.src}` })), - }), - type: "application/manifest+json", - }, ] + // Vite's resolved `base`, such as `/opencode/` for `vite build --base=/opencode/`. + let base = "/" + + const webManifest = () => ({ + fileName: "site.webmanifest", + source: JSON.stringify({ + ...manifest, + id: base, + start_url: base, + scope: base, + icons: manifest.icons.map((icon) => ({ ...icon, src: `${base}${prefix}${icon.src}` })), + }), + type: "application/manifest+json", + }) + + const files = () => [...iconFiles, webManifest()] + return { name: "opencode-app:icons", + configResolved(config) { + base = config.base + }, generateBundle() { - files.forEach((file) => this.emitFile({ type: "asset", fileName: file.fileName, source: file.source })) + files().forEach((file) => this.emitFile({ type: "asset", fileName: file.fileName, source: file.source })) }, configureServer(server) { server.middlewares.use((request, response, next) => { - const file = files.find((file) => `${base}${file.fileName}` === request.url?.split("?")[0]) + const file = files().find((file) => `${base}${file.fileName}` === request.url?.split("?")[0]) if (!file) return next() response.setHeader("Content-Type", file.type) diff --git a/packages/app/vite.pwa.ts b/packages/app/vite.pwa.ts index 9c6f669795f..92f45b1f07e 100644 --- a/packages/app/vite.pwa.ts +++ b/packages/app/vite.pwa.ts @@ -13,14 +13,28 @@ export function onDemandAsset(path: string) { return officeAsset.test(path) } -export function serviceWorker(directory: string, base = "/") { - const prefix = base.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") - +export function serviceWorker(directory: string) { return VitePWA({ strategies: "generateSW", registerType: "prompt", injectRegister: false, manifest: false, + integration: { + // The fallback and denylist follow Vite's resolved `base`, such as `/opencode/` for `vite build --base=/opencode/`. + configureOptions(config, options) { + const prefix = config.base.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") + + options.workbox = { + ...options.workbox, + navigateFallback: `${config.base}index.html`, + // Pairing links must reach the server so it can set the session cookie. + navigateFallbackDenylist: [ + new RegExp(`^${prefix}(?:api|auth)(?:/|$)`), + new RegExp(`^${prefix}(?:_assets|assets)(?:/|$)`), + ], + } + }, + }, workbox: { // Workbox runs after Sentry's upload and cleanup, so do not publish an unuploaded map. sourcemap: false, @@ -29,12 +43,6 @@ export function serviceWorker(directory: string, base = "/") { // Keep each open tab on its complete build until all old clients close. skipWaiting: false, inlineWorkboxRuntime: true, - navigateFallback: `${base}index.html`, - // Pairing links must reach the server so it can set the session cookie. - navigateFallbackDenylist: [ - new RegExp(`^${prefix}(?:api|auth)(?:/|$)`), - new RegExp(`^${prefix}(?:_assets|assets)(?:/|$)`), - ], // Include lazy chunks and non-JS dependencies, not just the startup bundle. globPatterns: ["**/*"], globIgnores: ["**/*.map", "_headers", "_redirects"],