diff --git a/ui/package.json b/ui/package.json index 130f02be..1af93dc2 100644 --- a/ui/package.json +++ b/ui/package.json @@ -5,7 +5,6 @@ "scripts": { "dev": "vite dev --port 3000", "build": "vite build", - "vercel-build": "vite build && node scripts/build-vercel-output.mjs", "preview": "vite preview", "test": "vitest run", "lint": "eslint", diff --git a/ui/scripts/build-vercel-output.mjs b/ui/scripts/build-vercel-output.mjs deleted file mode 100644 index 0bb2c1fb..00000000 --- a/ui/scripts/build-vercel-output.mjs +++ /dev/null @@ -1,47 +0,0 @@ -// Repackage the static Vite/TanStack-Start build (.output/public) into the Vercel -// Build Output API layout (.vercel/output), generating the /dashboard/api/* proxy -// destination from a per-project env var. -// -// Why this exists: prod deploys via a fast-forward-only promotion, so the `dev` and -// `main` branches are byte-identical and a committed vercel.json CANNOT differ between -// them. The app is same-origin only (no backend CORS), so the per-environment backend -// must be the PROXY DESTINATION, supplied per Vercel project as LANGGRAPH_BACKEND_URL. -// Vercel reads vercel.json BEFORE the build and never interpolates env into it, so the -// routes are emitted here (read AFTER the build) where process.env is available. -import { cp, mkdir, writeFile, rm } from "node:fs/promises" - -const backend = process.env.LANGGRAPH_BACKEND_URL -if (!backend) { - throw new Error( - "LANGGRAPH_BACKEND_URL is required at build time (set it per Vercel project: " + - "dev project -> dev langgraph URL, prod project -> prod langgraph URL).", - ) -} -const base = backend.replace(/\/$/, "") - -await rm(".vercel/output", { recursive: true, force: true }) -await mkdir(".vercel/output/static", { recursive: true }) -await cp(".output/public", ".vercel/output/static", { recursive: true }) - -await writeFile( - ".vercel/output/config.json", - JSON.stringify( - { - version: 3, - routes: [ - // Same-origin proxy to this project's backend (proxy, not redirect, so the - // osw_session cookie stays first-party on the Vercel origin). - { src: "^/dashboard/api/(.*)$", dest: `${base}/dashboard/api/$1` }, - // Serve hashed assets, the PWA manifest/service worker, and _shell.html itself - // BEFORE the SPA catch-all, or static files get swallowed by the fallback. - { handle: "filesystem" }, - // SPA fallback (replaces the old vercel.json /(.*) -> /_shell.html rewrite). - { src: "^/.*$", dest: "/_shell.html" }, - ], - }, - null, - 2, - ) + "\n", -) - -console.log(`[build-vercel-output] wrote .vercel/output/config.json (backend: ${base})`) diff --git a/ui/vercel.json b/ui/vercel.json index 46980c33..7a3c56cc 100644 --- a/ui/vercel.json +++ b/ui/vercel.json @@ -1,4 +1,5 @@ { + "$schema": "https://openapi.vercel.sh/vercel.json", "framework": null, - "buildCommand": "bun run vercel-build" + "buildCommand": "bun run build" } diff --git a/ui/vite.config.ts b/ui/vite.config.ts index 3d30de8d..14c5d307 100644 --- a/ui/vite.config.ts +++ b/ui/vite.config.ts @@ -74,13 +74,44 @@ function mockHarnessProxy(): Plugin | null { } } +// Same-origin proxy for the dashboard API. Vercel reads vercel.json BEFORE the build +// and never interpolates env into it, and prod promotes by fast-forward (so `dev` and +// `main` ship a byte-identical vercel.json that cannot encode a per-project backend). +// So the per-project backend is driven through Nitro instead: this `routeRules` proxy +// is compiled by Nitro's Vercel preset into a CDN-level rewrite in +// `.vercel/output/config.json` at build time, reading LANGGRAPH_BACKEND_URL from the +// per-project Vercel env var. Proxy (not redirect) keeps the osw_session cookie +// first-party on the Vercel origin. The `**` wildcard maps 1:1 to the destination. +function backendProxyRouteRules(): + | Record + | undefined { + const backend = process.env.LANGGRAPH_BACKEND_URL?.replace(/\/$/, "") + // Nitro's Vercel preset auto-activates from the VERCEL env var; NITRO_PRESET=vercel + // forces it locally. On Vercel the backend is mandatory — fail loudly rather than + // ship a build whose API calls 404, which is exactly how the previous approach broke. + const onVercel = + !!process.env.VERCEL || process.env.NITRO_PRESET === "vercel" + if (!backend) { + if (onVercel) { + throw new Error( + "LANGGRAPH_BACKEND_URL is required for Vercel builds (set it per Vercel " + + "project: dev project -> dev langgraph URL, prod project -> prod langgraph URL).", + ) + } + // Off Vercel with no backend configured (plain local `vite build`, tests): omit the + // proxy. Local dev gets its API via the E2E_HARNESS mock proxy above instead. + return undefined + } + return { "/dashboard/api/**": { proxy: `${backend}/dashboard/api/**` } } +} + const config = defineConfig({ optimizeDeps: { include: ["workbox-window"] }, worker: { format: "es" }, plugins: [ mockHarnessProxy(), devtools(), - nitro(), + nitro({ routeRules: backendProxyRouteRules() }), viteTsConfigPaths({ projects: ["./tsconfig.json"], }),