From ac59114208eae70dd53918b1f71d7921b5b201ae Mon Sep 17 00:00:00 2001 From: sauravniraula Date: Tue, 10 Mar 2026 19:28:05 +0545 Subject: [PATCH] feat: makes nextjs able to access backend in chromium --- electron/servers/nextjs/utils/api.ts | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/electron/servers/nextjs/utils/api.ts b/electron/servers/nextjs/utils/api.ts index 62488395..a0acb872 100644 --- a/electron/servers/nextjs/utils/api.ts +++ b/electron/servers/nextjs/utils/api.ts @@ -5,18 +5,22 @@ export function getFastAPIUrl(): string { return (window as any).env.NEXT_PUBLIC_FAST_API || ''; } - // Check if we're running in Electron vs Docker/web mode - if (typeof window !== 'undefined' && (window as any).electron) { - // Electron mode: direct access to FastAPI - return process.env.NEXT_PUBLIC_FAST_API || 'http://127.0.0.1:8000'; - } else { - // Docker/web mode: use current origin (goes through nginx) - if (typeof window !== 'undefined') { - return window.location.origin; - } - // Server-side fallback - return process.env.NEXT_PUBLIC_FAST_API || 'http://127.0.0.1:8000'; + // Prefer explicit env var when available in any mode + if (process.env.NEXT_PUBLIC_FAST_API) { + return process.env.NEXT_PUBLIC_FAST_API; } + + // Electron mode: direct access to FastAPI + if (typeof window !== 'undefined' && (window as any).electron) { + return 'http://127.0.0.1:8000'; + } + + // Docker/web mode: use current origin (goes through nginx) + if (typeof window !== 'undefined') { + return window.location.origin; + } + // Server-side fallback + return 'http://127.0.0.1:8000'; } // Utility to construct full API URL