Next.js

React interfaces with server rendering, routes and application logic.

React · Next.js · PostgreSQL · Drizzle

These recipes and dependency combinations need checking in your actual project before you rely on them.

Give this guide to your agent

Paste the guide address into your agent chat, together with what you want to build.

How to use Stack Menu with your agent →

When to use Next.js

Choose it when

  • Shared TypeScript contracts and React UI are useful
  • Server rendering or a web-facing backend is required
  • Team accepts explicit server/client and cache boundaries

Consider another path when

  • Only static editorial content is required
  • Existing API already owns all backend responsibilities and SSR is unnecessary
  • Native mobile or desktop UI is the deliverable

Use cases: Authenticated web products; Server-rendered React interfaces; Browser applications with owned APIs and relational data.

Generators: create the baseline first

Generator documentation reviewed: 2026-10-23. Evidence: documentation-reviewed; commands not executed.

Required workflow

  • Before writing a new project's application files, read its profile's starting points and use the official framework or language initializer appropriate to the component. Do not hand-recreate a generated skeleton. Where no full application generator exists, use the documented module/package initializer and assemble only the missing application pieces.
  • Choose one starting point per component. Check the installed generator's version and help, prerequisites, template, database, UI, test and runtime options. Commands are examples: replace names and paths, resolve compatible versions, and record the exact invocation and selected options. Do not blindly run every listed generator.
  • Generate in a new empty destination. For an existing repository, preserve its conventions and use relevant incremental generators. If a baseline comparison is needed, generate a separate disposable reference with matching versions/options and reconcile deliberately; never regenerate over existing work.
  • Keep applicable generated configuration, hidden files, formatter/linter settings, ignore rules, manifests, lockfiles, tests, wrappers and entry points. Extend them for the project instead of replacing them with abbreviated handwritten copies. A generator may initialize version control or offer a commit; respect the user's Git instructions.
  • Run the generated baseline's documented checks before feature changes, then relevant checks after configuration and implementation. Inspect what version control would include, including hidden files, build output, local secrets and operating-system metadata. Add needed ignore rules explicitly: no generator guarantees that .DS_Store or every local artifact is ignored.
  • Use follow-up generators only when their feature or tool is selected. Review generated changes and dependencies, apply required migrations in the intended environment, and exercise the generated behavior. Scaffolding does not establish authorization, safe retries, production readiness or every best practice.
  • If the required generator, template or IDE is unavailable, report the missing prerequisite and the documented route. Do not silently substitute a handwritten scaffold. Keep documentation review, generator execution, baseline checks and application qualification as separate evidence.

Choose a starting point

create-next-app

Use when: A new full-stack React/Next.js application; do not rerun over an existing application.

  1. Select a supported Node LTS and the repository package manager. Inspect the current CLI help before generation; examples below use npm.
  2. Run the official scaffolder in the parent directory. Review its current prompts for styling, Cache Components, agent instructions and optional feedback instead of accepting saved preferences blindly; use --reset-preferences if you need to clear saved choices.
  3. Record resolved generator/runtime/framework versions and chosen options in the root README; retain the lockfile.

Working directory: A local source-workspace parent outside synchronized storage; my-app must not already exist

Check the selected runtime.

node --version

Working directory: A local source-workspace parent outside synchronized storage; my-app must not already exist

Inspect the currently resolved scaffolder options.

npx create-next-app@latest --help

Working directory: A local source-workspace parent outside synchronized storage; my-app must not already exist

Generate the TypeScript App Router baseline while leaving styling, cache, instruction and feedback preferences available for interactive selection.

npx create-next-app@latest my-app --ts --eslint --app --src-dir --use-npm --disable-git
  • Replace my-app with the intended unused project name. @latest is a scaffold-time lookup; record the resolved version and use locked dependencies afterward.
  • The CLI's recommended defaults currently include TypeScript, ESLint, Tailwind CSS, App Router, Cache Components, AGENTS.md and agent feedback; customize these for the actual project. Cache Components are a framework behavior choice, not a required scaffold feature. Tailwind is the default styling option, but choose it only if it fits the product's styling approach.
  • The agent-instructions option is project-local. If the repository owner requires README-only guidance, pass --no-agents-md and, for Next versions supporting it, set agentRules: false in next.config before next dev so Next does not create/update its managed AGENTS.md block. Do not impose this repository-specific choice on all users or projects.
  • Agent feedback is optional and separate from agentRules. It prepares anonymized Next.js issue-report drafts for owner review; opening the review form does not send or publish them. Keep it off unless the project owner wants this review flow and understands its telemetry requirement.
  • Do not handwrite package.json, tsconfig, framework config or ignore rules as a replacement for this scaffold.

Conditional follow-up generators

Drizzle migration generation

Use when: Only after this application has selected PostgreSQL/Drizzle, installed compatible drizzle-orm/driver/drizzle-kit dependencies, and configured a schema and drizzle.config.ts.

Working directory: The generated my-app directory

Generate SQL migration files from the configured schema changes.

npx drizzle-kit generate

Working directory: The generated my-app directory

Apply reviewed migrations to the configured development database after checking its connection.

npx drizzle-kit migrate
  • Inspect local drizzle-kit help first; do not install a second ORM merely to run these examples. Review SQL and migration metadata before applying; production application needs its own release procedure.
  • Next.js has no general built-in model/controller/component generator analogous to Rails. Follow its installed-version route/file conventions; do not invent a next generate command.

Preserve the generated foundation

  • Generated ignore rules, package scripts, strict TypeScript configuration, Next configuration, application/layout conventions and lockfile; document intentional changes.
  • Generated README setup information, supplemented with project purpose/navigation; honor repository instruction ownership and record the chosen local agent-rule/feedback behavior.

Baseline and generation checks

  • Run generated development server and inspect a real route in a browser.
  • Run the generated build and lint scripts separately; Next build is not a lint check. Check generated script names rather than assuming next lint exists.
  • Verify intended generated files and documented deviations; a successful scaffold does not prove authentication, authorization or database integration.

Generation receipt

  • Component and selected starting point, or the documented absence of a full application generator
  • Generator/toolchain versions, exact commands or IDE template/settings, working directory and generated destination
  • Generated files retained, deliberate deviations with reasons, and required supplemental ignore rules
  • Baseline checks and later checks actually run, results and remaining prerequisites or untested behavior

Generator sources

Set up your project

  1. Complete the selected starting point and baseline checks in the Generators section first; then continue with the integration steps appropriate to the chosen component.
  2. From the generated project root, extend README with purpose, commands, environment variable names and links to the installed framework documentation. Confirm strict TypeScript and honor the selected instruction-file policy; README-only repositories need agentRules:false before next dev.
  3. Install only selected integrations: Zod for runtime validation; drizzle-orm plus pg and development drizzle-kit/@types/pg when PostgreSQL/Drizzle is required; Better Auth and its installed-version Drizzle adapter only when application-owned identity is selected. The current adapter guide uses @better-auth/drizzle-adapter. Mark database/auth modules server-only; add dotenv only if the chosen config loader needs it.
  4. When database/auth features are selected, configure the Drizzle schema/config and ignored development DATABASE_URL, then auth secret/base URL and Next route/client integration. For Better Auth with Drizzle, inspect the compatible auth CLI, run npx auth@latest generate, include generated schema in Drizzle paths, then follow the reviewed SQL generation/application procedure. Better Auth direct migrate is for its Kysely adapter, not the Drizzle path.
  5. Configure tests appropriate to the application: Vitest and a DOM environment/React Testing Library for synchronous client components or domain logic, plus Playwright for critical browser flows and async Server Components. Install matching browser binaries with npx playwright install only after selecting Playwright. Add required aliases/plugins and a production-build webServer configuration.
  6. Test invalid inputs, resource/tenant ownership and session boundaries where applicable. Add Inngest only for required durable workflows; configure its endpoint/worker and exercise retries and duplicate events.
  7. For database-backed applications, rehearse migrations on a fresh test database. Run the generated build, separate lint/type checks and selected tests. Configure the chosen host, secrets and a single migration job; verify deployed browser/session behavior and rollback compatibility as part of an authorized release.

Recommended tools and agent brief

Choose the tools your application needs. The brief includes your selections, their purpose and their limits; the complete guide keeps all recommendations and sources.

runtime and dependency management

Node LTS + npm lockfile

LTS production policy and reproducible dependency install

Limits: Pin resolved runtime/dependencies; latest is a scaffold-time lookup, not a CI dependency strategy

application framework

Next.js App Router + React + strict TypeScript

Full-stack React with version-matched agent documentation

Limits: TanStack Start 1.0 became stable on 13 October 2026; the imported 10 October RC observation is historical. Consider Start when its typed routing and server-function model fit the project, after checking the selected adapter and integrations. Web UI is not native UI; no framework supplies business correctness.

project instructions

README navigation + generated version-matched agent guidance

Keep repository instructions and installed framework documentation discoverable

Limits: Honor existing instruction ownership. Optional README-only policy: scaffold with --no-agents-md and set agentRules:false before next dev; neither is required by this stack.

Agent runtime inspection

Next.js development MCP endpoint with real browser inspection

Combine version-matched framework context, compilation errors, routes and runtime logs with the actual rendered application.

Limits: Development only. Inspect the installed Next.js and next-devtools-mcp versions before configuring tools. Some runtime inspection requires an open browser session; Turbopack compilation tools have their own constraints. Framework tools do not replace authorization tests or real browser verification. Do not install optional skills or enable automatic upgrades merely to read this reference.

validation

Zod

Runtime schemas with inferred TypeScript types

Limits: Validate requests/environment/external results; shape checks do not establish permission or truth

database and migrations

PostgreSQL + Drizzle + reviewed SQL migrations

Typed schema with inspectable SQL history

Limits: Configure schema paths and target environment; apply migration once; rehearse fresh DB and forward compatibility

identity

Better Auth + Drizzle PostgreSQL adapter

Integrated session/provider support without designing password/session primitives

Limits: Auth schema generate then Drizzle migrations; direct Better Auth migrate is Kysely-only; configure origin/secrets/recovery; authorize near data

background work

Inngest durable workflows when required

Checkpointed steps, retries and waits

Limits: Idempotent effects and authorized approvals required; use outbox for DB/event consistency; avoid beta durable endpoints as default

Static typing

TypeScript strict

Independent deterministic checks for generated edits

Limits: Next build does not run lint; never ignore build/type errors to satisfy a check

Linting

ESLint

Independent deterministic checks for generated edits

Limits: Next build does not run lint; never ignore build/type errors to satisfy a check

Domain tests

Vitest

Run isolated domain behavior checks with a configured test environment

Limits: Install/configure Vitest and DOM environment for UI tests. Next async Server Components require browser/integration coverage instead.

Component tests

React Testing Library

Test rendered components through user-visible DOM behavior

Limits: Install @testing-library/react and its @testing-library/dom peer; configure the test runner/DOM environment. This is a component utility library, not a test runner.

Browser tests

Playwright

Exercise critical journeys in actual browser engines

Limits: Install @playwright/test and matching browsers; configure an isolated test server/data and test session expiry, routing and authorization failures.

deploy

Project-selected Node or Docker host

Documented full Next feature support with Node/Docker

Limits: Adapters vary; verify chosen host, secrets, DB connections, sessions, migrations and restore; Cloudflare needs separate compatibility review

Server state cache

TanStack Query for client-owned remote state

Queries, mutations and invalidation support interactive API screens

Limits: Install @tanstack/react-query and provide QueryClient. Define tenant-aware keys, staleTime/retry policy and clear session data on sign-out; in Next prefer server loading until a client cache has a job.

Forms

React Hook Form for complex client forms

Separates client form state and validation from page components

Limits: Install react-hook-form only where needed; wire existing runtime schemas deliberately. Render accessible labels/errors; validate and authorize submissions on the server.

Styling

Tailwind CSS when utility styling fits the project

Reusable styling without a runtime component dependency

Limits: For Vite install tailwindcss and @tailwindcss/vite, add the plugin and CSS import. Use the framework-specific installation path for Next; retain established styles when present.

Accessible UI primitives

Radix Primitives for complex controls

Provides focus and keyboard behavior for common interaction patterns

Limits: Install only used components. Authors still supply names, contrast and meaningful composition; verify keyboard and assistive-technology behavior.

Accessibility checks

axe-core through Playwright + manual keyboard review

Automated audits identify common accessibility defects in real pages

Limits: Install @axe-core/playwright; scan representative states and use a screen reader/keyboard. Passing automated rules is not complete accessibility evidence.

Transactional email

Nodemailer with project-selected SMTP provider

SMTP transport avoids coupling email templates to a hosting vendor

Limits: Install nodemailer; configure server-only transport, verified sender and TLS. Test locally, queue required delivery, handle ambiguous results and bounces; SMTP acceptance is not inbox delivery. Verify the resolved Nodemailer release supports the pinned Node runtime and preserves TLS certificate validation.

Uploads and object storage

AWS SDK for JavaScript v3 S3 client

Object storage separates durable files from ephemeral application disks

Limits: Install @aws-sdk/client-s3; authorize object keys, restrict size/type and configure lifecycle. Keep credentials server-side; add presigner only for direct uploads and validate provider compatibility.

Live server updates

EventSource for one-way server events

Browser-standard streaming suits status/progress feeds

Limits: Backend owns authenticated stream and event IDs. Test reconnect, replay and proxy buffering; EventSource is one-way and its constructor offers no arbitrary authorization-header option. Confirm long-lived connection support on host.

Build with the stack’s strengths

Architecture and developer workflow.

A Next application contains server rendering and browser interactivity with an explicit trust boundary. Recommendation: preserve an existing backend's domain ownership during UI adoption; for a new owned backend use server-only data access. Node/Docker and alternative adapters require separate runtime checks.

Version scope: Documentation reviewed 11 October 2026: current Next.js App Router docs label 16.4.0. Pin the installed Next, React, Node and database packages; distinguish cacheComponents:true from the previous caching model. This profile does not qualify a lockfile or hosting adapter. · Reviewed 2026-10-11 · Evidence: documentation-reviewed · Source: inputs/research/migrations-clients.json

Architecture boundaries

  • Server Components load server-owned data; Client Components own interaction and browser APIs. Keep the client boundary near interactive leaves and pass minimal serializable DTOs.
  • Recommendation: one domain writer and migration owner per responsibility. Existing REST/GraphQL services remain authoritative during coexistence; a new server-only DAL can own validation, authorization and transactions after transfer.
  • Choose server, browser and CDN caches deliberately. Session-dependent output, invalidation and multiple instances require independent policies; adding a framework cache does not migrate a shared session or queue.
  • Recommendation: durable jobs and realtime run behind explicit service contracts when required. A render, Server Action or browser promise is not an execution backlog.

Tools and feedback

  • Start in the external source checkout. Read README and inspect package.json, lockfile, next.config, current API ownership, schema and runtime. Record whether Cache Components are enabled before selecting examples.
  • For an existing npm project run npm ci; use its scripts. With Next installed, npx next typegen then npx tsc --noEmit checks generated route types and TypeScript. Run the configured ESLint/Biome command separately; the build is not a substitute for lint.
  • Run npm run dev for iteration and npm run build for production compilation; use npm run start only if its script runs the selected production server. Configure Playwright's isolated backend/data first, then npx playwright test for mixed-version journeys.
  • Recommendation: deploy the same built artifact to all instances, record build/deployment identifiers, cache coordination, migrations, secrets and adapter support. Reproduce cold/warm loads, sign-out, old-tab actions and rollback on a staging topology.

Translations to avoid

  • Marking the root tree use client brings imported dependencies into the browser bundle and broadens the secret/exposure surface.
  • Passing whole ORM records to Client Components exposes fields unrelated to presentation; enforce authorization and DTO filtering on the server.
  • UI-hidden buttons or Proxy redirects cannot replace authorization at the mutation/data boundary.
  • Copying pre-16 cache defaults into a Cache Components app silently changes freshness behavior; identify the installed model.
  • Treating an old browser build as gone after deployment ignores existing tabs, prefetched payloads and action identifiers.

For an existing application

Keep the contracts while you change the stack.

These are documented options and proposed checks. Inspect the actual source application before adopting a transition.

Data and schema

Inspect the source

Existing REST/GraphQL behavior, DTOs, authorization, transactions and one schema owner.

During coexistence

Recommendation: Adopt server-rendered routes that call the old API; keep its writer and migration authority. Compare read-only fixtures without replaying real side effects.

Target design

Recommendation: Retain the API permanently if appropriate, or transfer a bounded domain to a server-only DAL with minimal DTOs after mixed-version checks.

Rollback

Route the cohort back while old-compatible schema remains; reconcile writes before any writer reversal.

Benefits and prerequisites

Server loading can reduce client waterfalls; an owned DAL centralizes authorization.
Requires: Server-reachable API or reviewed database ownership; measurable user journeys.
Tradeoffs: A new domain writer requires schema/transaction review; framework adoption alone changes neither.

Checks before transfer

  • Old and new UI decode identical success/error/empty fixtures.
  • Forged resource IDs remain denied on both mutation paths.
  • An acknowledged write is readable through the old interface.
Identity and authorization

Inspect the source

Existing session issuer, cookie/token format, expiry, revocation, tenant membership and CSRF behavior.

During coexistence

Recommendation: Retain the issuer behind an adapter and authorize data/mutations in both implementations; inventory origin/domain/cookie differences before sharing.

Target design

Recommendation: Use an explicitly selected issuer and server-side authorization boundary; transfer session contracts or require a controlled sign-in when compatibility cannot be preserved.

Rollback

Restore old routes/issuer acceptance only while its keys and session semantics remain supported; do not resurrect revoked sessions.

Benefits and prerequisites

One identity authority can support gradual UI/backend replacement.
Requires: Validated issuer integration and old/new expiry/revocation fixtures.
Tradeoffs: Session sharing is format/security work; sign-in continuity is not guaranteed by using cookies.

Checks before transfer

  • Revoked and expired sessions fail in old/new paths.
  • Cross-tenant direct action requests are denied.
  • Sign-out clears browser remote-state caches and back navigation reveals no protected data.
Caching and shared state

Inspect the source

Actual enabled caching model, personalized response policy, keys, freshness, invalidation and shared-instance behavior.

During coexistence

Recommendation: Keep the prior policy while converting one route/data category; separate old/new cache namespaces and observe stale/account-crossing outcomes.

Target design

Recommendation: Choose Cache Components or version-matched previous-model APIs explicitly; keep user-specific data outside shared public entries and coordinate shared invalidation when deployed across instances.

Rollback

Disable the new cache path and purge its namespace; keep source data unchanged and retain old invalidation until transfer completes.

Benefits and prerequisites

Repeated eligible work can be reused without refetching each time.
Requires: A documented freshness tolerance, safe keys and working invalidation.
Tradeoffs: Coordination/storage adds operations; freshness and cost gains require measurement.

Checks before transfer

  • A write refreshes affected reads through every serving instance.
  • Account A cannot read cached output for account B.
  • Cold, warm and forced-refresh responses agree with the selected policy.
Jobs and schedules

Inspect the source

Existing job producer/consumer payloads, ready/scheduled/retry/failed/in-flight states and external-effect idempotency.

During coexistence

Recommendation: Keep existing workers; move a named job type behind a versioned event contract. Reconcile backlog and retain one consumer owner per item.

Target design

Recommendation: Use selected durable execution such as Inngest for required workflows; a transactional outbox is a proposed design when domain commit and event submission are separate systems.

Rollback

Stop new producers for the transferred type, account for active runs and resume old workers with compatible payloads; reconcile irreversible effects first.

Benefits and prerequisites

Checkpointed completed steps allow retry/resume without starting every step again.
Requires: Selected service/SDK integration, stable step IDs, replay-safe external effects and delivery reconciliation.
Tradeoffs: Operating a workflow service adds dependency/cost; checkpoints do not supply business exactly-once effects.

Checks before transfer

  • Crash after an external effect but before step acknowledgement; retry causes one intended business effect.
  • Domain commit with event-send failure is eventually reconciled.
  • Old scheduled/retry work is drained or deliberately transformed.
Delivery and recovery

Inspect the source

Current hosting/runtime, static assets, open-tab state, prefetched payloads and Server Function compatibility.

During coexistence

Recommendation: Keep old assets and compatible API routes; promote one built artifact per deployment and rehearse an old tab against the new deployment.

Target design

Recommendation: Use version-matched deploymentId support and infrastructure routing as needed; same-build instances share required action keys and cache coordination.

Rollback

Restore the prior artifact and routing while compatible assets/data remain; do not claim traffic rollback reverses schema or external effects.

Benefits and prerequisites

Explicit rollout handling reduces missing-chunk/navigation mismatches.
Requires: A qualified host, retained assets and tested old-client behavior.
Tradeoffs: Hard navigation can lose component state; same encryption key does not make old action IDs compatible.

Checks before transfer

  • Open an old tab, deploy, navigate and submit an old form; observe success or a controlled update path.
  • Preserved drafts survive any required full reload.
  • All instances serve matching build/deployment identifiers.

Read the migration workflow →

Run checks

Run these in the configured application. They are instructions, not recorded passes.

npm ci
npx next typegen
npx tsc --noEmit
npx eslint .
npx vitest run
npm run build
npx playwright test

Working practices

  • Keep database imports and secrets in server-only modules. Treat Server Actions and route handlers as authenticated entry points.
  • Validate external input with schemas and authorize access close to the data. A page redirect is not an access-control boundary.
  • Commit reviewed SQL migrations. Generate the Better Auth schema into Drizzle's migration path.
  • Test async Server Components through browser journeys. Exercise logged-out and two-tenant cases, including cached pages.

Questions to resolve

  • Does the product require server-rendered routes, client-owned state, or both?
  • Which identity providers, account recovery and SSO requirements must be supported?
  • Which host, database connection budget and migration/restore process will own production?
  • Which workflows need retries, email, object storage or persistent realtime connections?

Sources

Current correction reviewed 2026-10-23: TanStack Start 1.0 became stable on 13 October 2026; the imported 10 October RC observation is historical. Consider Start when its typed routing and server-function model fit the project, after checking the selected adapter and integrations. Web UI is not native UI; no framework supplies business correctness. Evidence: documentation-reviewed; no TanStack Start application integration executed.

Original source review: 2026-10-10. Core guidance rechecked: 2026-10-13. Check scope.