Preparing for Production
Before deploying, run a production build locally to catch any issues:
npm run buildThis command does several things:
- Compiles your TypeScript and JSX
- Optimizes your JavaScript bundles with tree shaking and minification
- Pre-renders static pages at build time
- Reports page sizes and rendering strategies
You'll see output like this:
Route (app) Size First Load JS
┌ ○ / 5.2 kB 89 kB
├ ○ /about 1.8 kB 85 kB
├ ● /blog/[slug] 3.1 kB 87 kB
└ λ /api/contact 0 B 83 kB
○ Static — pre-rendered at build time
● SSG — pre-rendered with dynamic params
λ Dynamic — rendered on each requestReview these symbols to understand which pages are static and which are dynamic.
Testing the Production Build
After building, start the production server locally:
npm run startVisit http://localhost:3000 and test your app. Production mode behaves differently from dev:
- No hot reload
- Optimized bundles (smaller, faster)
- Cached data responses
- Real error pages instead of dev overlays
Always test the production build before deploying.
Environment Variables
Next.js supports .env files for environment variables:
# .env.local (never committed to git)
DATABASE_URL=postgresql://localhost:5432/mydb
API_SECRET=my-secret-key
# Public variables (available in the browser)
NEXT_PUBLIC_API_URL=https://api.example.com
Key rules:
NEXT_PUBLIC_prefix makes a variable available in Client Components- Variables without the prefix are server-only — never exposed to the browser
.env.localis for local development and should be in.gitignore- Set production variables in your hosting platform's dashboard
Metadata and SEO
Before deploying, add metadata to help search engines index your pages:
// app/layout.tsx
import type { Metadata } from 'next'
export const metadata: Metadata = {
title: {
default: 'My App',
template: '%s | My App'
},
description: 'A Next.js application',
openGraph: {
title: 'My App',
description: 'A Next.js application',
url: 'https://myapp.com',
siteName: 'My App',
type: 'website'
}
}Each page can export its own metadata to override the defaults:
// app/about/page.tsx
export const metadata = {
title: 'About',
description: 'Learn more about us'
}With the template '%s | My App', the about page title becomes "About | My App".
Deploying to Vercel
Vercel is the company behind Next.js and offers the smoothest deployment experience.
Step 1: Push to GitHub
Make sure your code is in a GitHub repository:
git init
git add .
git commit -m "Initial commit"
git remote add origin https://github.com/you/my-app.git
git push -u origin mainStep 2: Connect to Vercel
- Go to vercel.com and sign in with GitHub
- Click "New Project"
- Select your repository
- Vercel auto-detects Next.js and configures the build settings
- Add your environment variables in the project settings
- Click "Deploy"
That's it. Vercel will:
- Install dependencies
- Run
npm run build - Deploy to a global CDN
- Give you a URL like
my-app.vercel.app
Step 3: Custom Domain
In your Vercel project settings, go to Domains and add your custom domain. Vercel handles SSL certificates automatically.
Other Hosting Options
While Vercel is the easiest option for Next.js, you can deploy anywhere:
Self-hosted (Node.js)
Run npm run build then npm run start on any server with Node.js — a VPS, Docker container, or cloud VM.
Static Export
If your app is fully static (no server-side features), you can export it as plain HTML:
// next.config.js
const nextConfig = {
output: 'export'
}
module.exports = nextConfigThen npm run build generates a static out/ folder you can deploy to any static host.
Production Checklist
Before going live, make sure you've covered these essentials:
- Production build runs without errors (
npm run build) - Environment variables are set in your hosting platform
- Metadata and Open Graph tags are configured
- Images use
next/imagefor automatic optimization - Error pages exist (
not-found.tsx,error.tsx) - Performance is acceptable (check with Lighthouse)
What's Next
Congratulations — you've completed Next.js Fundamentals! You now know how to:
- Set up a Next.js project
- Create routes with the App Router
- Fetch data with Server Components
- Build and deploy to production
From here, explore the Fullstack Next.js course to learn about API routes, database integration, authentication, and more advanced patterns.