aicode8
Next.jsUpdated: 2026-09-10

Next.js best practices: engineering conventions for production

Field-tested Next.js practices from production, avoiding the common anti-patterns.

On this page

Quick answer

Which config enables Next.js static export?

Use output:export. Export behavior changes across Next majors — check the export limitations list before upgrading.

Machine-readable page data: /data/nextjs-best-practices.json

In-depth analysis

Next.js is the dominant React meta-framework. output:export produces pure static output, but the core engine here is still Astro — Next pages exist here only for comparison and traffic capture. Production engineering conventions revolve around "single source of truth + explicit boundaries", steering clear of the common anti-patterns.

Next.js · Config example
// next.config.mjs — pure static export (no server functions)
const nextConfig = { output: 'export', images: { unoptimized: true } };
export default nextConfig;
// Build: next build && next export
Best practices:One package manager (pnpm), lockfile in version control. Secrets stay as placeholders — docs and code stay cleanly separated.
Pitfalls:output:export forbids dynamic-route server data fetching and middleware. Forgetting images.unoptimized breaks the export. Lock the package manager to one choice (pnpm), commit the lockfile, and never mix npm/yarn.

Next.js Best Practices · Key takeaways

  • Config centralized in a single source
  • CI uses --frozen-lockfile
  • Secrets are placeholders, never real values in code
  • Docs and code live in separate places

FAQ & pitfalls

How do I invalidate cache for Next static export?

The export is pure static files; atomically replace and use long cache for hash-named assets. HTML must-revalidate ensures the origin refreshes.

Share & save

My shelf0
← Back to homeTopic: Nextjs Best Practices

What readers are asking

Questions that keep coming back in reader feedback, already answered.

  1. 01
    How do I fix a failing Next.js output: export build?

    Server-side data fetching in dynamic routes, and middleware, both break the export; next/image also needs unoptimized or the export aborts.

    See the full guide →
  2. 02
    Why is my Astro component's interactivity not working?

    Components ship as zero-JS HTML by default, so an interactive one needs a client:* directive (client:load, client:visible, …). Without it the script is never bundled into the page.

    See the full guide →
  3. 03
    Why is my custom Tailwind v4 brand colour not applied?

    Colours must be declared in @theme (for example --color-brand). A missing @tailwindcss/vite plugin, or a leftover v3 config, makes bg-brand silently render nothing.

    See the full guide →
All questions →

Follow along as I build with AI

Get the honest, real-time record: what I build, what breaks, and what I learn — from a non-technical founder. No jargon, no fluff.

We respect your inbox. One click to unsubscribe, anytime.