Backend Environment Switching

This document explains how to switch between localhost and Vercel backends during development.

Overview

The frontend supports connecting to multiple backends:

  • Localhost: Backend running locally on http://localhost:8000 (for development)
  • Vercel: Production backend at https://plings-backend.vercel.app (for testing)
  • Custom IP: Backend running on any custom IP address (for remote development)

Environment Configuration

Environment Variables

Variable Description Example
VITE_GRAPHQL_ENDPOINT GraphQL endpoint URL http://localhost:8000/graphql/
VITE_BACKEND_URL Backend base URL http://localhost:8000
VITE_BACKEND_ENV Environment identifier localhost or vercel

Environment Files

  • .env.example - Template file with example configuration
  • .env.localhost - Localhost backend configuration
  • .env.vercel - Vercel backend configuration
  • .env.local - Active configuration (automatically managed)

Switching Methods

# Switch to localhost backend and start dev server
npm run dev:local

# Switch to Vercel backend and start dev server
npm run dev:vercel

# Switch backend without starting server
npm run backend:local
npm run backend:vercel

Method 2: Manual Environment File Copy

# Switch to localhost
cp .env.localhost .env.local

# Switch to Vercel
cp .env.vercel .env.local

Method 3: Development UI Switcher

In development mode, a “Backend” button appears:

  • Desktop: Bottom-right corner
  • Mobile: Bottom-center for better accessibility
  1. Click the “Backend” button to open the switcher panel
  2. View current backend configuration and connection status
  3. Switch between localhost/Vercel/Custom backends
  4. For custom backend:
    • Enter IP address with port (e.g., 192.168.1.100:8000)
    • Click “Apply” to save and switch
    • The IP is saved in localStorage for persistence
  5. Test connection to verify backend availability
  6. Follow CLI instructions to complete the switch

Backend Configuration Details

Localhost Backend

VITE_GRAPHQL_ENDPOINT=http://localhost:8000/graphql/
VITE_BACKEND_URL=http://localhost:8000
VITE_BACKEND_ENV=localhost

Vercel Backend

VITE_GRAPHQL_ENDPOINT=https://plings-backend.vercel.app/graphql/
VITE_BACKEND_URL=https://plings-backend.vercel.app
VITE_BACKEND_ENV=vercel

Custom IP Backend

# Example for a backend running on another machine in your network
VITE_GRAPHQL_ENDPOINT=http://192.168.1.100:8000/graphql/
VITE_BACKEND_URL=http://192.168.1.100:8000
VITE_BACKEND_ENV=custom

Development Workflow

Testing with Localhost Backend

  1. Ensure your local backend is running on port 8000
  2. Switch to localhost: npm run backend:local
  3. Start frontend: npm run dev
  4. Test API connectivity and image uploads

Testing with Vercel Backend

  1. Switch to Vercel: npm run backend:vercel
  2. Start frontend: npm run dev
  3. Test against production backend environment

Testing with Custom IP Backend

  1. Ensure the backend is running on the target machine
  2. Open the Backend switcher UI
  3. Enter the IP address and port (e.g., 192.168.1.100:8000)
  4. Click “Apply” to save and switch
  5. Update your .env.local file with the custom endpoint
  6. Restart the frontend dev server

Common Use Cases for Custom IP:

  • Testing from mobile devices on the same network
  • Developing on multiple machines
  • Remote development scenarios
  • Testing backend changes without deploying

Troubleshooting

Connection Issues

If you see connection errors:

  1. Check Backend Status: Use the “Test Connection” button in the dev UI
  2. Verify URLs: Ensure the backend URLs are correct for your environment
  3. CORS Issues: Localhost backend should allow http://localhost:8080 origin
  4. Firewall: Ensure no firewall is blocking the connection

Environment Not Switching

If the backend doesn’t switch:

  1. Restart Dev Server: Environment variables are loaded at startup
  2. Clear Browser Cache: Force reload with Ctrl+F5
  3. Check .env.local: Verify the file contains the correct configuration

Backend Switcher UI Not Visible

The switcher only appears in development mode. Ensure:

  1. You’re running npm run dev (not a production build)
  2. import.meta.env.DEV is true
  3. Check browser console for any JavaScript errors

Mobile Visibility Issues

The backend switcher is now mobile-responsive:

  • On mobile devices, the button appears at the bottom-center
  • On desktop, it remains in the bottom-right corner
  • The switcher panel is responsive and fits mobile screens

Console Logging

The application logs backend configuration on startup:

🔧 Backend Configuration: {
  endpoint: "http://localhost:8000/graphql/",
  environment: "localhost", 
  isLocalhost: true
}

Look for these logs to verify your backend configuration is correct.

Security Notes

  • Environment files containing localhost URLs should not be committed to production
  • The .env.local file is gitignored and auto-managed
  • The backend switcher UI only appears in development mode for security
  • Custom IP addresses are stored in localStorage and persist across sessions
  • Ensure your backend server has proper CORS configuration for custom IPs
  • Never expose sensitive backend endpoints to public networks