Topic 39 of 39
Production Deployment
Overview
Deploying a Next.js app requires running a build process (next build) to generate static HTML, CSS, and optimize code. Vercel is the creator of Next.js and provides zero-config deployments, but it can also be self-hosted via Docker or Node servers.
Syntax
bash
# Standard build and start process for self-hosting
npm run build # Compiles application, generates static files
npm run start # Starts production Node.js serverCommon Pitfalls
- Running
npm run devin production. It is highly unoptimized, includes hot-reloading code, and handles requests slowly. - Forgetting to configure caching correctly (e.g., Redis) when self-hosting across multiple Docker containers.
Interview Questions
Q:
What happens when you run
next build?A:
Next.js compiles TypeScript, optimizes images/fonts, statically generates all eligible routes (HTML/JSON), and creates optimized production bundles for the server and client.
Real-World Example
A standard Dockerfile for self-hosting Next.js using standalone mode.
example
bash
// next.config.js
module.exports = {
output: 'standalone', // Significantly reduces Docker image size
};Check Your Knowledge
Test your understanding of Production Deployment with these quick questions.