From 232ccd193e3d9575e412a903581ed8f9c5648d22 Mon Sep 17 00:00:00 2001 From: Gokulvarthan Ramesh Date: Sat, 29 Aug 2026 17:42:42 +0530 Subject: [PATCH] Fix API_BASE_URL fallback to point to local backend on localhost, resolving CORS error --- src/services/api.js | 10 +++++++++- src/services/api.ts | 7 ++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/services/api.js b/src/services/api.js index 14b586d..a960ecc 100644 --- a/src/services/api.js +++ b/src/services/api.js @@ -1,6 +1,14 @@ // Force relative paths on Netlify to route through Netlify proxy and eliminate CORS errors const isNetlify = typeof window !== 'undefined' && window.location.hostname.includes('netlify.app'); -const API_BASE_URL = isNetlify ? '' : (import.meta.env.VITE_API_BASE_URL || 'https://services-api.a2zhealth.in/onboarding').replace(/\/$/, ''); +const isLocalhost = typeof window !== 'undefined' && (window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1'); + +const DEFAULT_API_URL = isNetlify + ? '' + : isLocalhost + ? 'http://localhost:8000' + : 'https://services-api.a2zhealth.in/onboarding'; + +const API_BASE_URL = (import.meta.env.VITE_API_BASE_URL || DEFAULT_API_URL).replace(/\/$/, ''); /** * Health check endpoint ping diff --git a/src/services/api.ts b/src/services/api.ts index e5e9429..e9bfb38 100644 --- a/src/services/api.ts +++ b/src/services/api.ts @@ -2,7 +2,12 @@ import { CategoryInfo, PatientInfo, VoiceTurnResult } from "../types/voice"; import { DocumentExtractionResult, PatientOnboardingResult } from "../types/onboarding"; const envUrl = import.meta.env.VITE_API_BASE_URL; -const API_BASE_URL = (envUrl !== undefined && envUrl !== "" ? envUrl : "https://services-api.a2zhealth.in/onboarding").replace(/\/$/, ""); +const isNetlify = typeof window !== "undefined" && window.location.hostname.includes("netlify.app"); +const isLocalhost = typeof window !== "undefined" && (window.location.hostname === "localhost" || window.location.hostname === "127.0.0.1"); + +const DEFAULT_API_URL = isNetlify ? "" : isLocalhost ? "http://localhost:8000" : "https://services-api.a2zhealth.in/onboarding"; + +const API_BASE_URL = (envUrl !== undefined && envUrl !== "" ? envUrl : DEFAULT_API_URL).replace(/\/$/, ""); export async function startVoiceOnboarding( patient: PatientInfo,