General
Deployment

Netlify

Learn how to deploy your application to Netlify.

Netlify is a popular platform for deploying web applications. While it's optimized for static sites, you can deploy Next.js applications using Netlify's Next.js runtime.

Why Netlify?

  • Easy deployment - Connect GitHub and deploy automatically
  • Free tier available - Great for getting started
  • Automatic HTTPS - SSL certificates included
  • Edge functions - Run serverless functions at the edge
  • Preview deployments - Automatic previews for PRs

Deploying to Netlify

1. Create Netlify Account

Sign up for a free account at netlify.com.

2. Create New Site

  1. Click "Add new site""Import an existing project"
  2. Connect your Git provider (GitHub, GitLab, or Bitbucket)
  3. Select your repository

3. Configure Build Settings

Netlify will auto-detect Next.js, but verify these settings:

  • Build command: npm run build
  • Publish directory: .next
  • Framework preset: Next.js

For Next.js standalone output, you may need to adjust:

  • Build command: npm run build && npm run export (if using static export)
  • Or use Netlify's Next.js runtime (recommended)

4. Add Environment Variables

Add all required environment variables:

  1. Go to Site settingsEnvironment variables
  2. Add variables from your .env:
Netlify Environment Variables
DATABASE_URL=postgresql://...
BETTER_AUTH_SECRET=...
NEXT_PUBLIC_SITE_URL=https://your-app.netlify.app
STRIPE_SECRET_KEY=...
STRIPE_PUBLISHABLE_KEY=...
RESEND_API_KEY=...
EMAIL_FROM=...

5. Deploy

Click "Deploy site" and Netlify will:

  • Install dependencies
  • Build your application
  • Deploy to their CDN

Your app will be available at https://your-app.netlify.app.

Netlify Configuration

Create a netlify.toml in your project root:

netlify.toml
[build]
  command = "npm run build"
  publish = ".next"

[build.environment]
  NODE_VERSION = "20"

[[plugins]]
  package = "@netlify/plugin-nextjs"

Database Migrations

Netlify doesn't support running migrations during build. You have a few options:

  1. Run migrations manually before deploying
  2. Use a build plugin to run migrations
  3. Run migrations via API route (not recommended for production)

Custom Domain

To use a custom domain:

  1. Go to Domain settingsAdd custom domain
  2. Follow DNS configuration instructions
  3. Update NEXT_PUBLIC_SITE_URL environment variable

Functions Region

For better performance, select the region closest to your database:

  1. Go to Site configurationBuild & deployFunctions
  2. Select the Functions region closest to your database
  3. Redeploy your site

Preview Deployments

Netlify automatically creates preview deployments for:

  • Pull requests
  • Branch pushes
  • Merge commits

Each preview gets its own URL for testing.

Environment Variables by Context

Netlify supports different environment variables for:

  • Production - Production deployments
  • Deploy previews - Preview deployments
  • Branch deploys - Branch-specific deployments

Troubleshooting

Build Failures

  • Check build logs in the Netlify dashboard
  • Verify Node.js version (set in netlify.toml)
  • Ensure all dependencies are in package.json

Function Timeouts

  • Netlify Functions have a 10-second timeout on free tier
  • Upgrade to Pro for longer timeouts
  • Optimize your API routes

Database Connection Issues

  • Verify DATABASE_URL is set correctly
  • Check if database requires SSL (add ?sslmode=require)
  • Ensure database allows connections from Netlify's IPs

Environment Variables

  • Verify variables are set in the correct context
  • Redeploy after adding new variables
  • Check for typos in variable names