Deploying And Upgrading Next Apps
Ship safely to production and upgrade Next.js with confidence using disciplined release practices.
Production Build and Deploy
`next build` produces optimized production output. Verify build succeeds locally before pushing. Environment variables required at build time (NEXT_PUBLIC_*) must be set in CI/CD.
Vercel integrates natively with Next.js — preview deployments per pull request. Self-hosting requires Node.js server (`next start`) or standalone output for Docker containers.
- next build before every deploy
- Env vars in CI for build-time values
- Standalone output for Docker
// next.config.js
module.exports = { output: 'standalone' };Environment Management
Separate development, staging, and production environments. Never use production database from preview deployments. NEXT_PUBLIC_ variables expose to browser — never put secrets there.
Validate required env vars at startup with Zod schema — fail fast on missing DATABASE_URL rather than cryptic runtime errors mid-request.
- Isolated env per deployment stage
- Secrets server-side only
- Startup validation for required vars
Upgrade Discipline
Read Next.js release notes and codemods for each major upgrade. Upgrade incrementally — major versions one at a time. Run full test suite and manual smoke tests on critical flows after upgrade.
Pin next version in package.json exactly or use dependabot with CI gates. Canary releases expose new features early but carry instability risk — use in non-production experiments only.
- Read release notes and codemods
- Incremental major version upgrades
- CI gates on dependency updates
Monitoring and Rollback
Monitor error rates, latency P95, and Core Web Vitals after deploy. Vercel instant rollback to previous deployment; self-hosted needs container image tags or blue-green deployment strategy.
Feature flags decouple deploy from release — ship code dark, enable gradually. Kill switch flags for risky features without redeployment.
- Monitor vitals post-deploy
- Instant rollback on Vercel
- Feature flags for gradual release
Operational Checklist
Pre-release: green CI, E2E pass, database migrations applied, env vars updated, CDN cache purge plan. Post-release: verify health endpoints, smoke test auth and payment flows, watch error dashboards for 30 minutes.
Document runbooks for common incidents — database connection exhaustion, memory leaks, third-party API outages. On-call engineers should reach for runbooks before debugging from scratch.
- Pre-release CI and migration checklist
- Post-release smoke and monitoring
- Runbooks for common incidents