# Deployment Guide

This guide covers deploying your VIN decoder application to various platforms.

## Vercel (Recommended)

Vercel is the easiest way to deploy Next.js applications.

### One-Click Deploy

[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https://github.com/cardog-ai/nextjs-corgi-starter&env=NEXT_PUBLIC_CARDOG_API_URL,NEXT_PUBLIC_CARDOG_API_KEY&envDescription=Optional%20API%20credentials%20for%20server-side%20VIN%20decoding&envLink=https://cardog.ai)

### Manual Deploy

```bash
# Install Vercel CLI
npm i -g vercel

# Deploy
vercel
```

### Environment Variables

If using API mode, add these in your Vercel dashboard:
- `NEXT_PUBLIC_CARDOG_API_URL`
- `NEXT_PUBLIC_CARDOG_API_KEY`

### Database Hosting for Browser Mode

For browser-based decoding in production:

1. **Upload Database to Vercel Blob Storage**:

```bash
npm i -g vercel
vercel blob put public/vpic.lite.db.gz --name vpic.lite.db.gz
```

2. **Update fetch URL** in `lib/vin-decoder-browser.ts`:

```typescript
const response = await fetch("https://your-blob-url.vercel-storage.com/vpic.lite.db.gz");
```

## Netlify

```bash
# Install Netlify CLI
npm i -g netlify-cli

# Build
npm run build

# Deploy
netlify deploy --prod --dir=.next
```

Add environment variables in Netlify dashboard under Site settings → Environment variables.

## AWS (S3 + CloudFront)

### 1. Build the Application

```bash
npm run build
npm run export  # If using static export
```

### 2. Upload to S3

```bash
aws s3 sync out/ s3://your-bucket-name
```

### 3. Set up CloudFront

Create a CloudFront distribution pointing to your S3 bucket.

### 4. Host Database

Upload `vpic.lite.db.gz` to S3 or CloudFront and update the fetch URL.

## Docker

### Build Image

```bash
# Create Dockerfile
cat > Dockerfile << EOF
FROM node:18-alpine AS base

# Install dependencies only when needed
FROM base AS deps
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci

# Rebuild the source code only when needed
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN npm run build

# Production image
FROM base AS runner
WORKDIR /app
ENV NODE_ENV production
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
COPY --from=builder /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
USER nextjs
EXPOSE 3000
ENV PORT 3000
CMD ["node", "server.js"]
EOF

# Build
docker build -t vin-decoder .

# Run
docker run -p 3000:3000 vin-decoder
```

## Self-Hosted

### Using PM2

```bash
# Install PM2
npm i -g pm2

# Build
npm run build

# Start with PM2
pm2 start npm --name "vin-decoder" -- start

# Save PM2 configuration
pm2 save
pm2 startup
```

### Using Nginx as Reverse Proxy

```nginx
server {
    listen 80;
    server_name yourdomain.com;

    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}
```

## Database Hosting Options

### Cloudflare R2 (Recommended)

```bash
# Install Wrangler
npm i -g wrangler

# Create bucket
wrangler r2 bucket create vin-decoder-db

# Upload database
wrangler r2 object put vin-decoder-db/vpic.lite.db.gz --file=public/vpic.lite.db.gz

# Make public
wrangler r2 bucket public-access enable vin-decoder-db
```

Access URL: `https://pub-xxx.r2.dev/vpic.lite.db.gz`

### AWS S3

```bash
# Upload
aws s3 cp public/vpic.lite.db.gz s3://your-bucket/vpic.lite.db.gz

# Make public
aws s3api put-object-acl --bucket your-bucket --key vpic.lite.db.gz --acl public-read
```

Access URL: `https://your-bucket.s3.amazonaws.com/vpic.lite.db.gz`

### Vercel Blob

```bash
vercel blob put public/vpic.lite.db.gz --name vpic.lite.db.gz --token YOUR_TOKEN
```

## Performance Optimization

### Enable Compression

Ensure your hosting platform serves the database with gzip compression:

```nginx
# Nginx
gzip on;
gzip_types application/octet-stream;
```

### Cache Headers

Set proper cache headers for the database:

```
Cache-Control: public, max-age=86400
```

### CDN

Use a CDN like Cloudflare to cache and distribute the database globally.

## Monitoring

### Vercel Analytics

Enable analytics in your Vercel dashboard for free.

### Custom Analytics

Add your preferred analytics:

```typescript
// app/layout.tsx
import { Analytics } from '@vercel/analytics/react';

export default function RootLayout({ children }) {
  return (
    <html>
      <body>
        {children}
        <Analytics />
      </body>
    </html>
  );
}
```

## Troubleshooting

### Database fails to load

- Check CORS headers on your database host
- Verify the database URL is accessible
- Check browser console for errors

### API mode not working

- Verify environment variables are set
- Check API key permissions
- Review API endpoint URL

### Build errors

```bash
# Clear cache and rebuild
rm -rf .next
npm run build
```

Need help? Open an issue or visit [cardog.ai](https://cardog.ai)
