From 944c788f18c359ef14ac3ad2dd8b5da13a4ba910 Mon Sep 17 00:00:00 2001 From: Ricky Notaro Date: Tue, 24 Mar 2026 22:39:23 -0400 Subject: [PATCH] ix: Fix rece condition when detecting the browser extension. --- src/http-common.ts | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/src/http-common.ts b/src/http-common.ts index 22fad5b..6d70f37 100644 --- a/src/http-common.ts +++ b/src/http-common.ts @@ -6,18 +6,22 @@ const TARGET_BASE = 'https://www.reservauto.net'; // The extension injects CORS headers + cookies via declarativeNetRequest, // so we can call the API directly without a proxy. let extensionDetected = false; +let detectionDone = false; -async function detectExtension(): Promise { - try { - const resp = await fetch( - `${TARGET_BASE}/WCF/LSI/LSIBookingServiceV3.svc/GetAvailableVehicles?BranchID=1&LanguageID=2`, - { method: 'HEAD', mode: 'cors' }, - ); - return resp.ok; - } catch { - return false; - } -} +const detectionPromise = !import.meta.env.DEV + ? (async () => { + try { + const resp = await fetch( + `${TARGET_BASE}/WCF/LSI/LSIBookingServiceV3.svc/GetAvailableVehicles?BranchID=1&LanguageID=2`, + { method: 'HEAD', mode: 'cors' }, + ); + extensionDetected = resp.ok; + } catch { + extensionDetected = false; + } + detectionDone = true; + })() + : Promise.resolve(); // Public API client — proxied through Vite in dev, direct with extension, corsproxy.io as fallback. const apiClient = axios.create({ @@ -29,8 +33,10 @@ const apiClient = axios.create({ }); if (!import.meta.env.DEV) { - // In production, use corsproxy.io as default, but skip if extension is detected - apiClient.interceptors.request.use((config) => { + // In production, wait for extension detection then decide proxy vs direct + apiClient.interceptors.request.use(async (config) => { + if (!detectionDone) await detectionPromise; + if (extensionDetected) { // Extension handles CORS + cookies — call API directly return config; @@ -47,11 +53,6 @@ if (!import.meta.env.DEV) { config.url = `https://corsproxy.io/?url=${encodeURIComponent(targetUrl.toString())}`; return config; }); - - // Detect extension on startup - detectExtension().then((detected) => { - extensionDetected = detected; - }); } // Authenticated REST API client — direct to restapifrontoffice