Fix API connectivity in Docker Space
Browse filesRoot cause: NEXT_PUBLIC_API_URL was defaulting to 'http://localhost:8000' which made
the browser try to reach localhost instead of using Next.js proxy.
Fixes:
- Remove default value for NEXT_PUBLIC_API_URL in next.config.js
- Only set NEXT_PUBLIC_API_URL if explicitly provided
- In Docker Space, frontend uses relative URLs (empty string)
- Next.js server proxies /api/* to localhost:8000 backend
- Add console.log debugging to trace API URL resolution
- Add comments explaining Docker Space proxy architecture
This ensures:
✅ Production: Browser -> /api/models -> Next.js proxy -> localhost:8000/api/models
✅ Dev: Browser -> http://localhost:8000/api/models (direct)
Files changed (3)
63 unmodified lines646566676869707163 unmodified linesCOPY --chown=user:user --from=frontend-builder /build/node_modules ./frontend/node_modules
# Set environment variables for the application# BACKEND_HOST is used by Next.js server for proxying# Do NOT set NEXT_PUBLIC_API_URL - let frontend use relative URLsENV BACKEND_HOST=http://localhost:8000 \ PORT=7860
1 unmodified line23456756789101112131415161718191 unmodified lineconst nextConfig = { output: 'standalone', reactStrictMode: true, // Only set NEXT_PUBLIC_API_URL if explicitly provided // In Docker Space, we want it undefined so frontend uses relative URLs (proxied by Next.js) ...(process.env.NEXT_PUBLIC_API_URL && { env: { NEXT_PUBLIC_API_URL: process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000', }, env: { NEXT_PUBLIC_API_URL: process.env.NEXT_PUBLIC_API_URL, }, }), async rewrites() { // In Docker Space, proxy /api/* requests to backend on port 8000 // This allows frontend (7860) to communicate with backend (8000) internally const API_HOST = process.env.BACKEND_HOST || 'http://localhost:8000'; console.log(`[Next.js] Setting up API proxy: /api/* -> ${API_HOST}/api/*`); return [ { source: '/api/:path*',12 unmodified lines131415161617181920212223242526272628293031323334353637383912 unmodified lines// Use relative URLs in production (Next.js rewrites will proxy to backend)// In local dev, use localhost:8000 for direct backend accessconst getApiUrl = () => { // If explicitly set via env var, use it // If explicitly set via env var, use it (for development) if (process.env.NEXT_PUBLIC_API_URL) { console.log('[API Client] Using explicit API URL:', process.env.NEXT_PUBLIC_API_URL); return process.env.NEXT_PUBLIC_API_URL; } // For server-side rendering, always use relative URLs if (typeof window === 'undefined') { console.log('[API Client] SSR mode: using relative URLs'); return ''; } // On localhost (dev mode), use direct backend URL // On localhost (dev mode), use direct backend URL const hostname = window.location.hostname; if (hostname === 'localhost' || hostname === '127.0.0.1') { console.log('[API Client] Localhost dev mode: using http://localhost:8000'); return 'http://localhost:8000'; } // In production (HF Space), use relative URLs (Next.js proxies to backend) console.log('[API Client] Production mode: using relative URLs (proxied by Next.js)'); return '';};
Dockerfile+2
frontend/next.config.js+8-3
frontend/src/lib/api.ts+6-2