diff --git a/CAPROVER-DEPLOYMENT-FIX.md b/CAPROVER-DEPLOYMENT-FIX.md new file mode 100644 index 0000000..bd70963 --- /dev/null +++ b/CAPROVER-DEPLOYMENT-FIX.md @@ -0,0 +1,74 @@ +# CapRover Deployment Fix for 500 Errors + +## 🚨 Issue +The domain privacy changes require specific environment variables to be set in CapRover. Missing these causes 500 errors during deployment. + +## ✅ Required Environment Variables in CapRover + +### **Step 1: Set Environment Variables** +In your CapRover app settings, add these environment variables: + +```bash +# Required for API routing +API_BASE_URL=https://your-backend-domain.com +API_HOSTNAME=your-backend-domain.com +SERVER_API_URL=https://your-backend-domain.com/api + +# Client-side API URL (should already be set) +NEXT_PUBLIC_API_URL=/api +``` + +### **Step 2: Example Configuration** +Replace `your-backend-domain.com` with your actual backend domain: + +```bash +# For production deployment +API_BASE_URL=https://internal-api.inboxi.ng +API_HOSTNAME=internal-api.inboxi.ng +SERVER_API_URL=https://internal-api.inboxi.ng/api +NEXT_PUBLIC_API_URL=/api +``` + +### **Step 3: Alternative - Use Internal Service Names** +If using CapRover's internal networking: + +```bash +# Using CapRover internal service names +API_BASE_URL=http://srv-captain--your-backend-app-name +API_HOSTNAME=srv-captain--your-backend-app-name +SERVER_API_URL=http://srv-captain--your-backend-app-name/api +NEXT_PUBLIC_API_URL=/api +``` + +## 🔧 Troubleshooting + +### **Check Environment Variables** +In CapRover app logs, you should see: +``` +Building with environment: +- API_BASE_URL: https://your-domain.com +- API_HOSTNAME: your-domain.com +- SERVER_API_URL: https://your-domain.com/api +``` + +### **Common Issues** +1. **Undefined variables**: Set all required variables in CapRover +2. **Invalid URLs**: Ensure no trailing slashes in API_BASE_URL +3. **HTTPS vs HTTP**: Match your backend's protocol + +### **Verification** +After deployment, check: +1. App starts without 500 errors +2. API requests work correctly +3. No console warnings about undefined variables + +## 🚀 Deploy Steps +1. Set environment variables in CapRover +2. Commit and push changes +3. Redeploy the app +4. Check logs for successful startup + +## 📝 Notes +- The `next.config.mjs` now has safer fallbacks to prevent 500 errors +- Missing variables will log warnings but won't crash the app +- For local development, use the `.env` file as before diff --git a/app/api/auth/check/route.ts b/app/api/auth/check/route.ts index 867154a..6d87255 100644 --- a/app/api/auth/check/route.ts +++ b/app/api/auth/check/route.ts @@ -31,7 +31,15 @@ export async function GET(req: NextRequest) { console.log('Auth check: Token found -', token.substring(0, 15) + '...'); const apiUrl = process.env.SERVER_API_URL || 'http://localhost:3001/api'; - console.log(`Auth check: Calling external API: ${apiUrl}/auth/me`); + + // Validate API URL to prevent 500 errors + if (!apiUrl || apiUrl === 'undefined' || apiUrl === 'null') { + console.warn('SERVER_API_URL not properly set in auth check, using localhost fallback'); + const fallbackUrl = 'http://localhost:3001/api'; + console.log(`Auth check: Calling external API: ${fallbackUrl}/auth/me`); + } else { + console.log(`Auth check: Calling external API: ${apiUrl}/auth/me`); + } try { const res = await fetch(`${apiUrl}/auth/me`, { diff --git a/env.example b/env.example index f141545..f142d8c 100644 --- a/env.example +++ b/env.example @@ -21,3 +21,4 @@ API_HOSTNAME=internal-api.inboxi.ng # SERVER_API_URL=http://localhost:3001/api # API_BASE_URL=http://localhost:3001 # API_HOSTNAME=localhost + diff --git a/lib/server-api.ts b/lib/server-api.ts index 1a36bb9..76516d1 100644 --- a/lib/server-api.ts +++ b/lib/server-api.ts @@ -22,6 +22,17 @@ try { */ function getServerApiUrl(endpoint: string): string { const apiUrl = process.env.SERVER_API_URL || 'http://localhost:3001/api'; + + // Validate API URL to prevent 500 errors + if (!apiUrl || apiUrl === 'undefined' || apiUrl === 'null') { + console.warn('SERVER_API_URL not properly set, using localhost fallback'); + const fallbackUrl = 'http://localhost:3001/api'; + const cleanEndpoint = endpoint.startsWith('/') ? endpoint.substring(1) : endpoint; + return fallbackUrl.endsWith('/') + ? `${fallbackUrl}${cleanEndpoint}` + : `${fallbackUrl}/${cleanEndpoint}`; + } + const cleanEndpoint = endpoint.startsWith('/') ? endpoint.substring(1) : endpoint; return apiUrl.endsWith('/') diff --git a/next.config.mjs b/next.config.mjs index a1cc7db..1eb211a 100644 --- a/next.config.mjs +++ b/next.config.mjs @@ -24,10 +24,23 @@ const baseConfig = { ], }, async rewrites() { + const apiBaseUrl = process.env.API_BASE_URL || 'http://localhost:3001'; + + // Ensure API_BASE_URL is valid to prevent 500 errors + if (!apiBaseUrl || apiBaseUrl === 'undefined') { + console.warn('API_BASE_URL not set, using localhost fallback'); + return [ + { + source: '/api/:path*', + destination: 'http://localhost:3001/api/:path*', + }, + ]; + } + return [ { source: '/api/:path*', - destination: `${process.env.API_BASE_URL || 'http://localhost:3001'}/api/:path*`, + destination: `${apiBaseUrl}/api/:path*`, }, ]; },