Beginner12 min1 prerequisite

Deploy your Next.js application to Vercel with automatic builds, preview URLs, and production deployments.

Deploying to Vercel

Deploy your Next.js application to production with automatic builds on every push.

Deployment Methods

1. Import from Git (Recommended)

Connect your GitHub/GitLab/Bitbucket repository:

Terminal
Dashboard  Add New  Project  Import Git Repository

Automatic deployments:

  • Push to main → Production deployment
  • Push to branch → Preview deployment
  • Pull request → Preview URL in PR

2. Vercel CLI

Deploy from command line:

Terminal
# Install CLI
npm install -g vercel

# Login
vercel login

# Deploy (development)
vercel

# Deploy to production
vercel --prod

3. AI Tool Export

From AI builders:

Lovable:

Terminal
Export  Deploy to Vercel  Follow prompts

Bolt.new:

Terminal
Deploy  Vercel  Authorize

Git-Based Deployment

Initial Setup

  1. Create repository (if not exists):
Terminal
git init
git add .
git commit -m "Initial commit"
git remote add origin https://github.com/user/repo.git
git push -u origin main
  1. Import to Vercel:
  • Go to vercel.com/new
  • Select repository
  • Configure project settings
  • Click Deploy

Project Settings

Vercel auto-detects Next.js, but you can customize:

Terminal
Framework Preset: Next.js
Build Command: npm run build
Output Directory: (auto-detected)
Install Command: npm install
Node.js Version: 20.x

Build Settings

For custom build requirements:

Terminal
// package.json
{
  "scripts": {
    "build": "next build",
    "vercel-build": "prisma generate && next build"
  }
}

Production Deployments

Trigger Production Deploy

Production deploys when you push to the production branch:

Terminal
# Default production branch: main
git checkout main
git merge feature-branch
git push origin main
#  Production deployment started

Manual Production Deploy

From dashboard:

Terminal
Project  Deployments  Three dots  Promote to Production

Or via CLI:

Terminal
vercel --prod

Instant Rollback

Rollback to previous deployment:

Terminal
Deployments  Previous deployment  Three dots  Instant Rollback

Rollback is instant—no rebuild required.

Preview Deployments

Automatic Previews

Every non-production branch gets a preview URL:

Terminal
Branch: feature/new-ui
URL: my-app-git-feature-new-ui-username.vercel.app

PR Comments

Vercel adds deployment status to pull requests:

Terminal
 Preview deployment ready
   Visit: https://my-app-abc123.vercel.app

Preview Environment

Previews can use different environment variables:

Terminal
Production: NEXT_PUBLIC_API_URL=https://api.example.com
Preview:    NEXT_PUBLIC_API_URL=https://staging-api.example.com

Deployment Configuration

vercel.json

Create vercel.json in project root:

Terminal
{
  "buildCommand": "npm run build",
  "outputDirectory": ".next",
  "installCommand": "npm install",
  "framework": "nextjs",
  "regions": ["iad1"],
  "headers": [
    {
      "source": "/api/(.*)",
      "headers": [
        { "key": "Cache-Control", "value": "no-store" }
      ]
    }
  ],
  "redirects": [
    {
      "source": "/old-page",
      "destination": "/new-page",
      "permanent": true
    }
  ]
}

Build Environment Variables

Set during build only:

Terminal
{
  "build": {
    "env": {
      "DATABASE_URL": "@database-url"
    }
  }
}

Regions

Deploy to specific regions:

Terminal
{
  "regions": ["iad1", "sfo1", "cdg1"]
}

Available regions:

  • iad1 - Washington D.C.
  • sfo1 - San Francisco
  • cdg1 - Paris
  • hnd1 - Tokyo
  • syd1 - Sydney

Monorepo Deployments

Multiple Projects

For monorepos with multiple apps:

Terminal
my-monorepo/
├── apps/
   ├── web/         Project: my-app-web
   └── docs/        Project: my-app-docs
├── packages/
└── package.json

Root Directory Setting

Configure in project settings:

Terminal
Root Directory: apps/web

Include/Exclude Files

Terminal
// vercel.json in apps/web/
{
  "ignoreCommand": "git diff HEAD^ HEAD --quiet ."
}

Serverless Functions

API Routes

API routes become serverless functions automatically:

Terminal
// app/api/users/route.ts
export async function GET() {
  return Response.json({ users: [] })
}

// Deployed to: /api/users
// Type: Serverless Function

Function Config

Configure function settings:

Terminal
// app/api/heavy/route.ts
export const maxDuration = 60  // 60 seconds max
export const dynamic = 'force-dynamic'

export async function POST(request: Request) {
  // Long-running operation
}

Memory and Timeout

Terminal
// vercel.json
{
  "functions": {
    "app/api/heavy/route.ts": {
      "memory": 1024,
      "maxDuration": 60
    }
  }
}

Build Optimization

Caching

Vercel caches:

  • node_modules (based on lockfile)
  • .next/cache (build cache)
  • Custom cache directories

Incremental Builds

Next.js ISR works automatically:

Terminal
// Revalidate every hour
export const revalidate = 3600

export default async function Page() {
  const data = await fetch('...')
  return <div>{data}</div>
}

Build Output

Check build output in dashboard:

Terminal
Build Logs
├── Installing dependencies
├── Building (3 pages, 5 API routes)
├── Generating static pages
└── Deployment complete (45s)

Monitoring Deployments

Deployment Status

Terminal
Building  Ready / Error

Building: Compiling your application
Ready: Deployment successful
Error: Build or deployment failed

Build Logs

View real-time build logs:

Terminal
Dashboard  Deployments  Click deployment  Build Logs

Function Logs

Monitor serverless function executions:

Terminal
Dashboard  Logs  Select function  View invocations

Troubleshooting

Build Failures

Common issues:

Missing dependencies:

Terminal
# Ensure dependencies are in package.json
npm install missing-package --save

TypeScript errors:

Terminal
# Fix locally first
npm run build

Environment variables:

Terminal
# Add missing vars in dashboard
Settings  Environment Variables

504 Gateway Timeout

Function timeout (default 10s on Hobby):

Terminal
// Increase timeout (Pro plan)
export const maxDuration = 60

// Or optimize code
export async function GET() {
  // Break into smaller operations
}

Module Not Found

Terminal
// vercel.json - include extra files
{
  "includeFiles": ["data/**"]
}

Summary

  • Git deployment: Auto-deploys on push to main
  • Preview URLs: Every branch gets a unique URL
  • Instant rollback: Revert without rebuilding
  • Serverless: API routes become functions
  • vercel.json: Configure builds, headers, redirects

Next Steps

Learn to manage environment variables securely in Vercel.

Mark this lesson as complete to track your progress