# Next.js Deployment on Tencent Cloud

Deploying Next.js apps on Chinese cloud servers with GFW restrictions.

## Prerequisites

- Node.js ≥ 18 (check: `node -v`)
- npm configured with domestic mirror: `npm config set registry https://registry.npmmirror.com`

## Build and Runtime

```bash
cd /path/to/nextjs-project
npm install                    # Works via npmmirror.com
npm run build                  # Compiles TypeScript, generates .next/
npm run start -- -p 3000       # Production server on port 3000
```

**Key difference from Python apps:** Next.js build may succeed even with missing env vars (Clerk, Supabase), but **runtime will fail** if the app wraps everything in `<ClerkProvider>`. The error won't show in background process stdout — redirect to a file:

```bash
node node_modules/.bin/next start -p 3000 > /tmp/nextjs.log 2>&1 &
sleep 6 && cat /tmp/nextjs.log | tail -20
```

## Required Environment Variables

Next.js projects typically need `.env.local`:

| Variable | Purpose | Prefix |
|----------|---------|--------|
| `NEXT_PUBLIC_*` | Client-side, embedded in JS bundle | Must be `NEXT_PUBLIC_` |
| `*` (no prefix) | Server-side only | No prefix |

Example:
```env
DEEPSEEK_API_KEY=sk-xxx
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=pk_xxx
CLERK_SECRET_KEY=sk_xxx
NEXT_PUBLIC_SUPABASE_URL=https://xxx.supabase.co
NEXT_PUBLIC_SUPABASE_ANON_KEY=eyJxxx
```

## Nginx Reverse Proxy

Next.js runs on port 3000. Point Nginx at it (same pattern as Flask):

```nginx
location / {
    proxy_pass http://127.0.0.1:3000;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
}
```

## Diagnosing 500 Errors

1. Check the log file: `cat /tmp/nextjs.log`
2. Common causes:
   - `Missing publishableKey` → Clerk env vars not set
   - `Missing supabase URL` → Supabase env vars not set
   - `Error fetching` → API key invalid or network blocked
3. Next.js `next build` ≠ runtime success — it only compiles TypeScript
