Spaces:
Sleeping
Sleeping
Hetansh Waghela Amp commited on
Commit ·
9065a81
1
Parent(s): 72cfcbf
fix: frontend API_BASE_URL respects empty VITE_API_URL for same-origin deploy
Browse filesAmp-Thread-ID: https://ampcode.com/threads/T-019c22db-46b4-7732-8f60-50293667624e
Co-authored-by: Amp <amp@ampcode.com>
- frontend/src/config/api.ts +12 -3
frontend/src/config/api.ts
CHANGED
|
@@ -1,11 +1,20 @@
|
|
| 1 |
/**
|
| 2 |
* Single source for API and WebSocket base URLs.
|
| 3 |
* Uses VITE_API_URL so one env var works for both REST and WebSocket.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
*/
|
| 5 |
-
const
|
| 6 |
-
const API_BASE_URL =
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
/** WebSocket base URL derived from API URL (http -> ws, same host/port) */
|
| 9 |
-
const WS_BASE_URL = API_BASE_URL
|
|
|
|
|
|
|
| 10 |
|
| 11 |
export { API_BASE_URL, WS_BASE_URL };
|
|
|
|
| 1 |
/**
|
| 2 |
* Single source for API and WebSocket base URLs.
|
| 3 |
* Uses VITE_API_URL so one env var works for both REST and WebSocket.
|
| 4 |
+
*
|
| 5 |
+
* When VITE_API_URL is defined (even as empty string), use it as-is.
|
| 6 |
+
* Empty string = same origin (for HF Spaces / single-container deploy).
|
| 7 |
+
* Only fall back to localhost:8000 when the var is completely undefined.
|
| 8 |
*/
|
| 9 |
+
const envVal = import.meta.env.VITE_API_URL;
|
| 10 |
+
const API_BASE_URL =
|
| 11 |
+
typeof envVal === 'string'
|
| 12 |
+
? envVal.trim().replace(/\/+$/, '')
|
| 13 |
+
: 'http://localhost:8000';
|
| 14 |
|
| 15 |
/** WebSocket base URL derived from API URL (http -> ws, same host/port) */
|
| 16 |
+
const WS_BASE_URL = API_BASE_URL
|
| 17 |
+
? API_BASE_URL.replace(/^http/, 'ws')
|
| 18 |
+
: `${window.location.protocol === 'https:' ? 'wss:' : 'ws:'}//${window.location.host}`;
|
| 19 |
|
| 20 |
export { API_BASE_URL, WS_BASE_URL };
|