Spaces:
Running
Running
Ashraf Al-Kassem Claude Sonnet 4.6 commited on
Commit ·
3b7374c
1
Parent(s): 86e8e8d
fix: production API base URL fallback — use relative paths on non-localhost browser
Browse filesNEXT_PUBLIC_API_BASE_URL is not baked into the Stage 1 build (set in
Stage 2 after the frontend is already built), so process.env.NEXT_PUBLIC_API_BASE_URL
is undefined in the client bundle on HuggingFace. The old fallback returned
'http://127.0.0.1:8000' for any non-localhost browser, which is the
client's own loopback — inaccessible from HF — blocking all client-side
API calls including Google OAuth.
Fix: when running in the browser on a non-localhost host, return '' so
fetch uses relative paths (/api/v1/...). nginx then proxies /api/* to
the FastAPI backend. SSR (window === undefined) continues to use the
direct Docker-internal address http://127.0.0.1:8000.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- frontend/src/lib/api.ts +4 -2
frontend/src/lib/api.ts
CHANGED
|
@@ -13,12 +13,14 @@ const getBaseUrl = () => {
|
|
| 13 |
if (process.env.NEXT_PUBLIC_API_URL) return process.env.NEXT_PUBLIC_API_URL;
|
| 14 |
|
| 15 |
if (typeof window !== "undefined") {
|
|
|
|
|
|
|
| 16 |
if (window.location.hostname === "localhost") {
|
| 17 |
return "http://localhost:8000";
|
| 18 |
}
|
|
|
|
| 19 |
}
|
| 20 |
-
//
|
| 21 |
-
// where window is undefined and env vars might be missing.
|
| 22 |
return "http://127.0.0.1:8000";
|
| 23 |
};
|
| 24 |
|
|
|
|
| 13 |
if (process.env.NEXT_PUBLIC_API_URL) return process.env.NEXT_PUBLIC_API_URL;
|
| 14 |
|
| 15 |
if (typeof window !== "undefined") {
|
| 16 |
+
// Browser context: localhost dev hits backend directly; production uses
|
| 17 |
+
// relative paths so nginx can proxy /api/* to the backend.
|
| 18 |
if (window.location.hostname === "localhost") {
|
| 19 |
return "http://localhost:8000";
|
| 20 |
}
|
| 21 |
+
return ""; // relative URLs — works in any production deployment (HF, Docker, etc.)
|
| 22 |
}
|
| 23 |
+
// Server-side (SSR inside Docker): backend is accessible at 127.0.0.1:8000 internally.
|
|
|
|
| 24 |
return "http://127.0.0.1:8000";
|
| 25 |
};
|
| 26 |
|