|
| 1 | +# Dev Environment LAN Access Issue |
| 2 | + |
| 3 | +**Date:** 2025-12-30 |
| 4 | +**Status:** Unresolved |
| 5 | +**Affects:** Development environment only (not production) |
| 6 | + |
| 7 | +## Problem Summary |
| 8 | + |
| 9 | +When accessing the Vite dev server from a LAN IP address (e.g., `http://10.0.2.100:3000/`), the web app fails to connect to the backend opencode server, even though both servers are bound to `0.0.0.0`. |
| 10 | + |
| 11 | +## Environment |
| 12 | + |
| 13 | +- **Vite dev server:** `bun run dev` → listening on `0.0.0.0:3000` |
| 14 | +- **OpenCode server:** `bun run dev serve --port 4096 --hostname 0.0.0.0 --print-logs` |
| 15 | +- **Access method:** Browser on same machine or LAN device via IP address |
| 16 | + |
| 17 | +## Error Messages |
| 18 | + |
| 19 | +### Original (before attempted fix): |
| 20 | +``` |
| 21 | +Error: Could not connect to server. Is there a server running at `http://localhost:4096`? |
| 22 | + at bootstrap (http://10.0.2.100:3000/src/context/global-sync.tsx:317:31) |
| 23 | +``` |
| 24 | + |
| 25 | +### After attempted fix (using `location.hostname`): |
| 26 | +``` |
| 27 | +Error: Could not connect to server. Is there a server running at `http://10.0.2.100:4096`? |
| 28 | + at bootstrap (http://10.0.2.100:3000/src/context/global-sync.tsx:317:31) |
| 29 | +``` |
| 30 | + |
| 31 | +## Analysis |
| 32 | + |
| 33 | +### What's happening: |
| 34 | + |
| 35 | +1. When accessing via `http://10.0.2.100:3000/`, the browser correctly loads the Vite dev server |
| 36 | +2. The app tries to connect to the OpenCode API server |
| 37 | +3. With original code: tries `http://localhost:4096` (wrong host from browser's perspective on LAN) |
| 38 | +4. With fix attempt: tries `http://10.0.2.100:4096` (correct host, but still fails) |
| 39 | + |
| 40 | +### Why the fix didn't work: |
| 41 | + |
| 42 | +The issue is **NOT** the URL resolution logic. The URL `http://10.0.2.100:4096` is correct. The problem is one of: |
| 43 | + |
| 44 | +1. **CORS (Cross-Origin Resource Sharing)** |
| 45 | + - Browser origin: `http://10.0.2.100:3000` |
| 46 | + - API request to: `http://10.0.2.100:4096` |
| 47 | + - These are different origins (different ports) |
| 48 | + - The OpenCode server may not be sending proper CORS headers for this origin |
| 49 | + |
| 50 | +2. **Vite Proxy Not Being Used** |
| 51 | + - In dev mode, Vite is configured with a proxy to forward `/api/*` requests to the backend |
| 52 | + - But if the app is constructing absolute URLs like `http://10.0.2.100:4096`, it bypasses the Vite proxy entirely |
| 53 | + - The proxy only works for relative URLs or same-origin requests |
| 54 | + |
| 55 | +3. **Network/Firewall** |
| 56 | + - Less likely since both servers are on same machine, but port 4096 could be blocked for non-localhost |
| 57 | + |
| 58 | +## Current Server URL Resolution Logic |
| 59 | + |
| 60 | +```typescript |
| 61 | +const defaultServerUrl = iife(() => { |
| 62 | + // 1. Query parameter (highest priority) |
| 63 | + const param = new URLSearchParams(document.location.search).get("url") |
| 64 | + if (param) return param |
| 65 | + |
| 66 | + // 2. Known production hosts -> localhost |
| 67 | + if (location.hostname.includes("opencode.ai") || location.hostname.includes("shuv.ai")) |
| 68 | + return "http://localhost:4096" |
| 69 | + |
| 70 | + // 3. Desktop app (Tauri) with injected port |
| 71 | + if (window.__SHUVCODE__?.port) return `http://127.0.0.1:${window.__SHUVCODE__.port}` |
| 72 | + if (window.__OPENCODE__?.port) return `http://127.0.0.1:${window.__OPENCODE__.port}` |
| 73 | + |
| 74 | + // 4. Dev mode -> explicit host:port from env |
| 75 | + if (import.meta.env.DEV) |
| 76 | + return `http://${import.meta.env.VITE_OPENCODE_SERVER_HOST ?? "localhost"}:${import.meta.env.VITE_OPENCODE_SERVER_PORT ?? "4096"}` |
| 77 | + |
| 78 | + // 5. Default -> same origin (production web command) |
| 79 | + return window.location.origin |
| 80 | +}) |
| 81 | +``` |
| 82 | + |
| 83 | +## Potential Solutions |
| 84 | + |
| 85 | +### Option 1: Use Vite Proxy in Dev Mode (Recommended) |
| 86 | + |
| 87 | +Instead of returning an absolute URL in dev mode, return a relative URL so requests go through Vite's proxy: |
| 88 | + |
| 89 | +```typescript |
| 90 | +// 4. Dev mode -> use relative URL to go through Vite proxy |
| 91 | +if (import.meta.env.DEV) return "/" |
| 92 | +``` |
| 93 | + |
| 94 | +This requires the Vite proxy to be properly configured in `vite.config.ts` to forward API requests to `localhost:4096`. |
| 95 | + |
| 96 | +**Pros:** |
| 97 | +- Works regardless of how you access the dev server (localhost, IP, hostname) |
| 98 | +- No CORS issues since requests are same-origin |
| 99 | +- Already partially configured in vite.config.ts |
| 100 | + |
| 101 | +**Cons:** |
| 102 | +- Need to ensure all API routes are proxied |
| 103 | +- Slightly different behavior than production |
| 104 | + |
| 105 | +### Option 2: Configure CORS on OpenCode Server |
| 106 | + |
| 107 | +Add CORS headers to the OpenCode server to allow requests from any origin in dev mode: |
| 108 | + |
| 109 | +```typescript |
| 110 | +// In packages/opencode/src/server/server.ts |
| 111 | +if (isDev) { |
| 112 | + app.use('*', (c, next) => { |
| 113 | + c.header('Access-Control-Allow-Origin', '*') |
| 114 | + c.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS') |
| 115 | + c.header('Access-Control-Allow-Headers', '*') |
| 116 | + return next() |
| 117 | + }) |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +**Pros:** |
| 122 | +- Allows direct access to API from any origin |
| 123 | +- Useful for debugging API directly |
| 124 | + |
| 125 | +**Cons:** |
| 126 | +- Security consideration (dev only) |
| 127 | +- Need to modify server code |
| 128 | + |
| 129 | +### Option 3: Environment Variable Override |
| 130 | + |
| 131 | +Set `VITE_OPENCODE_SERVER_HOST=0.0.0.0` or the specific IP when starting dev server. |
| 132 | + |
| 133 | +**Pros:** |
| 134 | +- Simple, no code changes |
| 135 | +- Explicit control |
| 136 | + |
| 137 | +**Cons:** |
| 138 | +- Manual configuration required |
| 139 | +- Still has CORS issues |
| 140 | + |
| 141 | +### Option 4: Use Same-Origin Detection (Our Previous Implementation) |
| 142 | + |
| 143 | +Our previous (more complex) implementation had logic to detect when to use same-origin requests: |
| 144 | + |
| 145 | +```typescript |
| 146 | +const isLoopback = ["localhost", "127.0.0.1", "0.0.0.0"].includes(location.hostname) |
| 147 | +const isWebCommand = !import.meta.env.DEV |
| 148 | +const useSameOrigin = isSecure || isKnownHost || (isLoopback && !import.meta.env.DEV) || isWebCommand |
| 149 | + |
| 150 | +if (useSameOrigin) return "/" |
| 151 | +``` |
| 152 | + |
| 153 | +This was more complex but handled the case of non-loopback access in dev mode. |
| 154 | + |
| 155 | +## Recommended Next Steps |
| 156 | + |
| 157 | +1. **Verify the Vite proxy configuration** in `packages/app/vite.config.ts` |
| 158 | +2. **Test Option 1** - Return `/` in dev mode and ensure Vite proxy forwards correctly |
| 159 | +3. **If proxy approach doesn't work**, investigate CORS headers on the OpenCode server |
| 160 | + |
| 161 | +## Files Involved |
| 162 | + |
| 163 | +- `packages/app/src/app.tsx` - Server URL resolution |
| 164 | +- `packages/app/vite.config.ts` - Vite proxy configuration |
| 165 | +- `packages/app/src/context/global-sync.tsx` - Where the connection error originates |
| 166 | +- `packages/opencode/src/server/server.ts` - Backend server (if CORS fix needed) |
| 167 | + |
| 168 | +## Workaround |
| 169 | + |
| 170 | +For now, access the dev server via `http://localhost:3000/` instead of IP address. |
0 commit comments