Capítulo 71 de 456

Production

Core Idea

Checklist page: optimizations and patterns to apply during development and before shipping a Next.js app to production, plus the automatic optimizations that require no configuration.

Key Concepts

  • Automatic optimizations (no config needed): Server Components (zero client JS by default), code-splitting by route segment, viewport prefetching, prerendering with opt-in Dynamic Rendering, and caching of data requests/render results/static assets.
  • Request-time APIs: cookies() and the searchParams prop opt the entire route into Dynamic Rendering (or the whole app if used in the Root Layout) — wrap in <Suspense> where appropriate to scope the impact.
  • Data Access Layer: recommended place to move database access, kept server-only, so auth/authz checks aren't only at Proxy/layout/page level.
  • useReportWebVitals: hook to send Core Web Vitals data to analytics tools before going to production.

Reference Tables

CategoryKey recommendations
Routing/renderingLayouts for partial rendering; <Link> for prefetch; catch-all + not-found error handling; watch "use client" boundary placement
Data fetching/cachingServer Components for server-side fetch; Route Handlers only from Client Components (not from Server Components); Streaming/loading.js; Parallel Data Fetching; verify caching, use unstable_cache for non-fetch requests
UI/accessibilityServer Actions for forms; app/global-error.tsx; app/global-not-found.tsx; Font Module; <Image>; <Script>; eslint-plugin-jsx-a11y
SecurityTainting for sensitive data; verify auth/authz inside every Server Action (not just Proxy/layout checks); Data Access Layer + rate limiting; .env.* in .gitignore, only NEXT_PUBLIC_ vars exposed; Content Security Policy
Metadata/SEOMetadata API; OG images; sitemaps and robots files
Type safetyTypeScript + TS Plugin

Anti-patterns

  • Chamar Route Handler a partir de um Server Component: gera uma requisição de servidor extra desnecessária; busque os dados direto no Server Component.
  • Confiar só em checagem de auth no Proxy/layout/page: cada Server Action precisa verificar autenticação/autorização por conta própria.
  • Não colocar Request-time APIs (cookies(), searchParams) atrás de <Suspense>: opta a rota inteira (ou o app todo, se no Root Layout) em Dynamic Rendering sem intenção.

Key Takeaways

  1. Rode next build localmente para pegar erros de build, depois next start para medir performance num ambiente parecido com produção.
  2. Use Lighthouse em modo anônimo combinado com dados de campo reais (Core Web Vitals) antes de confiar só no teste simulado.
  3. NEXT_PUBLIC_-prefixed é a única forma seguro de expor env var ao cliente; todo .env.* precisa estar no .gitignore.
  4. Esta página é essencialmente um índice de checklist com links para páginas mais profundas — não define comportamento novo por si só.

Connects To

  • package-bundling: bundle analysis é citado aqui como parte de "Analyzing bundles" antes de produção.
  • content-security-policy: referenciado para hardening de segurança.
  • data-security: Data Access Layer detalhado ali.