1. Building for Production
When you run npm run dev, Next.js compiles your app on-demand for fast iteration. Production is different โ Next.js pre-renders everything it can, tree-shakes unused code, minifies JavaScript and CSS, and generates an optimised build artefact ready to serve millions of requests.
The build command is npm run build. Run it before every deployment.
After the build completes, Next.js prints a route table showing exactly what it produced for each page:
Static (โ)
Pre-rendered at build time to HTML. Served instantly from a CDN. Zero server cost per request. Best for pages that don't change per-user: Home, Products listing, Checkout.
Dynamic (โ)
Rendered on the server at request time. Used when the page depends on runtime data โ e.g. /product/[id] needs to fetch the right product based on the URL parameter.
The UrbanCart homepage (/) and products listing (/products) are marked โ โ they're statically generated at build time. The product detail page (/product/[id]) is โ โ it's dynamic because the product data depends on which id is in the URL. The Checkout page is static because its layout doesn't change per-user.
2. Environment Variables
Production apps need secrets โ API keys, database URLs, payment gateway credentials. You should never hard-code these values in your source code. Next.js has a structured system for managing them across different environments.
The .env files
The NEXT_PUBLIC_ prefix
By default, environment variables are only available on the server. If you want a variable accessible in browser (client) code โ for example, a publishable API key that goes into a JavaScript SDK โ prefix it with NEXT_PUBLIC_.
Server-only (no prefix)
Available in Server Components, API Routes, getServerSideProps. Never sent to the browser. Use this for secrets: DATABASE_URL, STRIPE_SECRET_KEY.
Client-exposed (NEXT_PUBLIC_)
Baked into the browser bundle at build time. Use for non-secret public values: NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY, NEXT_PUBLIC_API_URL.
.env.local to your .gitignore. Never commit real API keys to git โ even a private repo. On Vercel, set production secrets in the dashboard under Project Settings โ Environment Variables.
3. Vercel Deployment
Vercel is the company that created Next.js, and their hosting platform is designed around it. Deploying is as close to zero-configuration as anything in web development gets.
Push to GitHub/GitLab
Auto-triggered build
Runs in Vercel CI
Preview or Production URL
Step-by-step first deploy
- Push your Next.js project to a GitHub repository.
- Go to
vercel.comand sign in with GitHub. - Click "Add New Project" and import your repository.
- Vercel auto-detects Next.js โ no build command configuration needed.
- Add your environment variables in the project settings.
- Click "Deploy". Your app is live in ~60 seconds.
Preview URLs
Every pull request and every branch push gets its own preview URL like urbancart-git-feature-abc.vercel.app. Share with your team before merging.
Production URL
Merging to main deploys to your production domain โ either the auto-generated urbancart.vercel.app or your custom domain like urbancart.com.
4. Other Hosting Options
Vercel is the easiest choice, but it's not the only one. Depending on your project's needs โ a Postgres database, a longer-running Node process, cost constraints โ other platforms may suit you better.
| Platform | Best for | Free tier | Notes |
|---|---|---|---|
| Vercel | Next.js, static sites, Edge Functions | Yes | Zero-config, made by Next.js team |
| Railway | Node.js + Postgres, full-stack apps | $5 credit | Best when you need a managed database alongside your app |
| Render | Node.js services, Postgres, cron jobs | Yes (spins down) | Free tier spins down after 15 min inactivity โ slow cold start |
| Self-hosted | Full control, VPS, Docker | Your server | Run npm run build && npm start on any Node.js server |
5. Performance Optimisation
Next.js ships built-in components that handle the most common performance mistakes automatically. Two you'll use in every project: next/image and next/font.
next/image โ Automatic image optimisation
A plain <img> tag serves whatever file you uploaded โ full-resolution JPEG, even to a user on mobile with a small screen. next/image fixes this automatically.
Plain img tag (slow)
Serves full-size image to all devices. No lazy loading. Browser does extra work to resize. No WebP conversion.
next/image (fast)
Resizes to viewport, converts to WebP, lazy-loads off-screen images, prevents layout shift with reserved space.
next/font โ Zero layout shift fonts
Loading Google Fonts the traditional way (<link> in the head) causes a flash of unstyled text โ your fallback font renders, then swaps to the real font mid-page. next/font downloads the font at build time and inlines a CSS variable, eliminating the shift entirely.
Bundle analysis
If your First Load JS is unexpectedly large, use the bundle analyser to see what's taking up space:
6. SEO with the Metadata API
Next.js 13+ introduces the Metadata API โ export a metadata object or a generateMetadata function from any page.tsx file and Next.js automatically injects the right <meta> tags into the <head>. No manual head management needed.
7. next.config.js
The next.config.js file in your project root controls Next.js behaviour: which external image domains are allowed, URL redirects, headers, and more.
remotePatterns. If you use next/image with a URL from Cloudinary or your CDN and the domain isn't listed, Next.js will throw an error and the image won't load.
8. Checking Lighthouse Scores
Lighthouse is Google's built-in tool for auditing web app quality. It scores your app across four dimensions and flags specific issues you can fix. Run it in Chrome DevTools โ Lighthouse tab, or via the command line.
A well-optimised Next.js app with next/image, next/font, and proper metadata can hit near-perfect scores. The most common culprits that drag scores down:
Unoptimised images
Using plain <img> instead of next/image. Fix by migrating all product images to the Image component.
Large JS bundles
Importing heavy libraries (Lodash, Moment.js) that ship too much code. Use lighter alternatives or dynamic imports.
Missing alt text
Images without alt attributes fail accessibility. Always provide descriptive alt text for product images.
npm run dev are artificially low because development builds include extra debugging code. Always run Lighthouse against your deployed production URL or a npm run build && npm start local server.
Quiz
NEXT_PUBLIC_ prefix do for environment variables?next/image over a regular img tag?npm run build do in a Next.js project?You completed the Deployment & Production quiz.
You just shipped UrbanCart to production.
Now launch a career that's just as live.
Join the Best AI Programs โ three 5-month courses, fully online, with placement assistance. Week 1 is free.
Explore our AI-era courses โ