# Production Setup - Code Persistence Fix

## Problem Diagnosed
The frontend was running in **development mode** (`next dev`), which has Hot Module Replacement (HMR) and Fast Refresh that can revert file changes automatically. This caused code changes to disappear after saving.

## Solution Applied

### 1. Production Build
- Frontend is now built for production: `npm run build`
- Production build creates static files in `.next/` directory
- These built files are the **single source of truth** for runtime

### 2. PM2 Configuration (`ecosystem.config.js`)
- **Frontend**: Runs `npm start` (production mode) instead of `npm run dev`
- **Backend**: Already using release binary (production)
- **Watch disabled**: `watch: false` prevents file watching from reverting changes
- **Auto-restart**: Only on crashes, not on file changes

### 3. Security Verification
- ✅ Login credentials are sent via POST request body (secure)
- ✅ No credentials in URL query parameters
- ✅ Authentication uses secure session storage

## How It Works Now

### Development Workflow
1. **Edit source files** in `frontend/src/`
2. **Build for production**: `cd frontend && npm run build`
3. **Restart PM2**: `pm2 restart erp-frontend`
4. **Changes persist** - production server serves built files

### Production Runtime
- Frontend serves pre-built static files from `.next/`
- No hot reloading or file watching
- Changes only take effect after rebuild + restart
- This ensures **code persistence** - changes don't revert

## PM2 Commands

```bash
# View status
pm2 list

# View logs
pm2 logs erp-frontend
pm2 logs erp-backend

# Restart after code changes
cd frontend && npm run build
pm2 restart erp-frontend

# Stop/Start
pm2 stop all
pm2 start ecosystem.config.js

# Save PM2 configuration
pm2 save
pm2 startup
```

## Important Notes

⚠️ **DO NOT** run `npm run dev` in production - it enables hot reload that reverts changes

✅ **DO** use `npm run build` + `pm2 restart` to apply changes

✅ **DO** edit source files - they are the source of truth for builds

✅ **DO** rebuild after making changes to see them in production

## File Structure
```
/var/www/html/erp_codearya/
├── ecosystem.config.js    # PM2 production configuration
├── frontend/
│   ├── src/              # Source files (edit these)
│   ├── .next/            # Built production files (auto-generated)
│   └── package.json      # Scripts: build, start, dev
└── backend/
    └── target/release/    # Production binary
```

## Why This Fixes the Issue

1. **No Hot Reload**: Production mode doesn't watch files or reload modules
2. **Static Build**: Changes are compiled into static files that persist
3. **No File Watching**: PM2 watch is disabled, preventing automatic reverts
4. **Single Source of Truth**: Built files in `.next/` are what's served
5. **Explicit Updates**: Changes require explicit rebuild + restart

This ensures code changes **never revert automatically** - they persist until you explicitly rebuild.

