Skip to content

Drop in a screenshot and convert it to clean code (HTML/Tailwind/React/Vue)

Python

Support same-origin backend URLs + dev-server proxy for tunneled previews

Lilo48b54a687d30
diff --git a/frontend/src/config.ts b/frontend/src/config.ts
index 4f0de2fe..14b94129 100644
--- a/frontend/src/config.ts
+++ b/frontend/src/config.ts
@@ -2,11 +2,21 @@
export const IS_RUNNING_ON_CLOUD =
import.meta.env.VITE_IS_DEPLOYED === "true" || false;
+// When no explicit backend URLs are provided, default to the same origin the
+// app is served from. Combined with the Vite dev-server proxy, this makes the
+// app work behind tunnels/preview URLs where "localhost" would point at the
+// viewer's machine instead of the sandbox.
+const SAME_ORIGIN_HTTP =
+ typeof window !== "undefined"
+ ? window.location.origin
+ : "http://127.0.0.1:5173";
+const SAME_ORIGIN_WS = SAME_ORIGIN_HTTP.replace(/^http/, "ws");
+
export const WS_BACKEND_URL =
- import.meta.env.VITE_WS_BACKEND_URL || "ws://127.0.0.1:7001";
+ import.meta.env.VITE_WS_BACKEND_URL || SAME_ORIGIN_WS;
export const HTTP_BACKEND_URL =
- import.meta.env.VITE_HTTP_BACKEND_URL || "http://127.0.0.1:7001";
+ import.meta.env.VITE_HTTP_BACKEND_URL || SAME_ORIGIN_HTTP;
export const PICO_BACKEND_FORM_SECRET =
import.meta.env.VITE_PICO_BACKEND_FORM_SECRET || null;
diff --git a/frontend/vite.config.ts b/frontend/vite.config.ts
index c3ad856c..14272480 100644
--- a/frontend/vite.config.ts
+++ b/frontend/vite.config.ts
@@ -7,8 +7,23 @@ import { createHtmlPlugin } from "vite-plugin-html";
// https://vitejs.dev/config/
export default ({ mode }) => {
process.env = { ...process.env, ...loadEnv(mode, process.cwd()) };
+ const CODEGEN_BACKEND =
+ process.env.PROXY_CODEGEN_BACKEND || "http://127.0.0.1:7001";
+
return defineConfig({
base: "",
+ server: {
+ // Listen on all interfaces so sandbox preview tunnels can reach the
+ // dev server (default binding is loopback-only).
+ host: true,
+ // Route backend traffic through the frontend origin so the app works
+ // via tunnels/preview URLs (no hardcoded localhost from the browser).
+ proxy: {
+ "/generate-code": { target: CODEGEN_BACKEND, ws: true },
+ "/api": { target: CODEGEN_BACKEND },
+ "/local-assets": { target: CODEGEN_BACKEND },
+ },
+ },
plugins: [
react(),
checker({