Add CapRover deployment fix and improve env var handling
Added CAPROVER-DEPLOYMENT-FIX.md with instructions for required environment variables to prevent 500 errors. Improved validation and fallback logic for SERVER_API_URL and API_BASE_URL in server-api.ts, route.ts, and next.config.mjs to handle missing or invalid values gracefully and log warnings instead of crashing.
This commit is contained in:
74
CAPROVER-DEPLOYMENT-FIX.md
Normal file
74
CAPROVER-DEPLOYMENT-FIX.md
Normal file
@@ -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
|
||||
@@ -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`, {
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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('/')
|
||||
|
||||
@@ -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*`,
|
||||
},
|
||||
];
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user