Learn how to deploy your Next.js application to production using Vercel and configure your deployment environment.
Deploy your application to Vercel with zero configuration:
1# Install Vercel CLI2npm i -g vercel34# Login to your account5vercel login67# Deploy to production8vercel --prod
1# Add deployment script to package.json2{3 "scripts": {4 "dev": "next dev",5 "build": "next build",6 "start": "next start",7 "deploy": "vercel --prod"8 }9}
Configure environment variables for your production deployment:
1# Production environment variables2NEXT_PUBLIC_API_URL=https://api.your-app.com3NEXT_PUBLIC_SITE_URL=https://your-app.com4SUPABASE_URL=https://your-project.supabase.co5SUPABASE_ANON_KEY=your-anon-key6SUPABASE_SERVICE_ROLE_KEY=your-service-role-key78# Configure environment variables in vercel.json9{10 "env": {11 "NEXT_PUBLIC_API_URL": "@next_public_api_url",12 "NEXT_PUBLIC_SITE_URL": "@next_public_site_url",13 "SUPABASE_URL": "@supabase_url",14 "SUPABASE_ANON_KEY": "@supabase_anon_key",15 "SUPABASE_SERVICE_ROLE_KEY": "@supabase_service_role_key"16 }17}
Add environment variables using the Vercel CLI:
1# Add a single environment variable2vercel env add NEXT_PUBLIC_API_URL34# Add multiple environment variables from a file5vercel env pull .env.production
Monitor your application's performance and errors using Vercel Analytics and Error Tracking:
1import { Analytics } from '@vercel/analytics/react'2import { ErrorBoundary } from '@vercel/edge-runtime'34export default function RootLayout({5 children,6}: {7 children: React.ReactNode8}) {9 return (10 <html lang="en">11 <body>12 <ErrorBoundary>13 {children}14 <Analytics />15 </ErrorBoundary>16 </body>17 </html>18 )19}2021// Custom error reporting22import { captureError } from '@vercel/analytics'2324try {25 // Your code26} catch (error) {27 captureError(error, {28 tags: {29 route: '/api/data',30 method: 'GET',31 },32 })33}
Configure custom domains and SSL certificates for your deployment:
1{2 "name": "your-app",3 "version": "1.0.0",4 "scripts": {5 "dev": "next dev",6 "build": "next build",7 "start": "next start"8 },9 "vercel": {10 "domains": [11 "your-app.com",12 "www.your-app.com"13 ],14 "rewrites": [15 {16 "source": "/blog/:path*",17 "destination": "https://blog.your-app.com/:path*"18 }19 ],20 "redirects": [21 {22 "source": "/old-page",23 "destination": "/new-page",24 "permanent": true25 }26 ]27 }28}
Add a custom domain using the Vercel CLI:
1# Add a custom domain2vercel domains add your-app.com34# Add a subdomain5vercel domains add www.your-app.com67# List all domains8vercel domains ls910# Remove a domain11vercel domains rm your-app.com