The first time I shipped a Next.js App Router app to production, it was 1.2MB of JavaScript for a landing page. The second time, I leaked a database connection string into the client bundle. The third time, my static pages started returning 500s because a build-time fetch was hitting a rate-limited API. Next.js is a fantastic framework, but its defaults will quietly ruin you if you don't understand the model. This article is what I wish I'd known on day one.
We're on Next.js 15 with the App Router. The Pages Router still exists and still works, but for new projects, App Router is the path forward. I'll cover the model, the gotchas, and the production setup.
$32
$33
$34
$35
$36
$37
$38
$39
$3a
$3b
Testing
For unit tests of pure logic, Vitest is fine. For component tests, React Testing Library. For end-to-end, Playwright.
// tests/dashboard.spec.ts
import { test, expect } from "@playwright/test"
test("dashboard shows user stats", async ({ page }) => {
await page.goto("/login")
await page.fill("[name=email]", "test@example.com")
await page.fill("[name=password]", "password")
await page.click("button[type=submit]")
await page.waitForURL("/dashboard")
await expect(page.locator("h1")).toHaveText("Dashboard")
await expect(page.locator("[data-testid=stats]")).toBeVisible()
})
Playwright works against the dev server or a production preview. Test against the production build for accurate results: next build && next start then run Playwright against :3000.
CI/CD
GitHub Actions example:
# .github/workflows/ci.yml
name: CI
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
- run: npm ci
- run: npm run lint
- run: npm run typecheck
- run: npm run test
- run: npm run build
- name: Upload build
uses: actions/upload-artifact@v4
with:
name: build
path: .next
Add npm run typecheck as tsc --noEmit in package.json. Lint catches maybe 20% of issues; types catch another 40%.
Cost considerations
- Vercel Pro: $20/user/month. Bandwidth overage at 1TB. Function executions metered. For a small team with moderate traffic, ~$80-200/month all-in.
- Self-hosted on Fly.io / Railway: $20-100/month for a small app. You operate it.
- Self-hosted on AWS ECS / GKE: ~$100-300/month compute + your time. Best at scale.
-
Cloudflare Pages: free for most apps, supports Next.js via
@cloudflare/next-on-pages. Edge runtime only-some Node features unavailable.
Vercel wins until your bill crosses ~$500/month. Past that, the math favors self-hosting if you have any platform engineering capacity.
Real-world use cases
- Marketing site: App Router, ISR (revalidate hourly), Vercel deploy. $20/month.
- SaaS dashboard: App Router with auth, server actions for mutations, Postgres on RDS, self-hosted on Fly. ~$80/month.
- E-commerce: ISR for product pages, dynamic for cart/checkout, Stripe webhooks via Route Handlers, Cloudflare CDN in front. ~$200-500/month.
- Internal admin tool: All server components, no client JS beyond a date picker, Okta SSO via Auth.js. Self-hosted, ~$50/month.
Conclusion
Next.js App Router is a real paradigm shift, not a refactor. The defaults are correct but unfamiliar: server components everywhere, opt-in client components, caching at build time, server actions replacing API routes. Learn the model, respect the boundaries ("use client" to leaves, server-only for secrets, revalidatePath after mutations), and instrument your bundle. Get those right and you'll ship fast, secure, small apps. Skip them and you'll be on a debugging call at midnight wondering why your dashboard is showing another user's data.
Originally published on Mango Developer.
Top comments (0)