Skip to content

Next.js

Scope: web apps built with Next.js and the App Router, following the current (Next.js 16) documentation. These guidelines build on React: follow its rules for components, state and testing, and the rules here for rendering, data access, caching and the boundary between server and client code.

Entries are sorted by score from 1 to 10 (criteria): 9–10 start here or strongly recommended, 6–8 recommended, 1–5 further reading or conditional.

Adopted references

Key rules

  • 9 NEXTJS-1 Treat every Server Action as a public endpoint: authenticate, authorize and validate its input inside the action. Actions are reachable by direct POST requests, so checks in Proxy, a layout or the page that renders the form aren't enough. (Next.js, Server Actions: Security, Data security, Production checklist)
  • 9 NEXTJS-2 Give an environment variable the NEXT_PUBLIC_ prefix only if its value may be public. Next.js inlines these values into the client JavaScript at build time. Keep secrets unprefixed and out of the repository, as general/security.md requires. (Next.js, Environment variables, Production checklist)
  • 8 NEXTJS-3 Put data access in a server-only Data Access Layer that checks authorization and returns minimal data transfer objects (DTOs). Next.js recommends this model for new projects; choose one data-handling model and don't mix them. (Next.js, Data security, Markbåge, How to Think About Security in Next.js)
  • 8 NEXTJS-4 Pass Client Components, and return from Server Actions, only the fields the UI renders. Props and action results are serialized and sent to the browser, so passing whole database records exposes private fields. (Next.js, The Server and Client Boundary, Server Actions: Security)
  • 7 NEXTJS-5 Add import 'server-only' to modules that use secrets or the database. Importing such a module into a Client Component then fails the build. (Next.js, Preventing environment poisoning)
  • 7 NEXTJS-6 Keep "use client" boundaries small: mark the interactive components, not whole layouts or pages, and pass server-rendered content to them as children or props. Everything a "use client" file imports goes into the client bundle. (Next.js, Reducing JS bundle size, Interleaving Server and Client Components)
  • 7 NEXTJS-7 Decide caching per data source, and invalidate the cache in every mutation that changes the data. With Cache Components, mark cached functions or components with use cache and give each a cacheLife; tag data that mutations change with cacheTag, and call updateTag or revalidateTag from the Server Action. (Next.js, Caching, Revalidating)
  • 6 NEXTJS-8 Read data in Server Components and change it with Server Actions. Don't fetch your own Route Handlers from Server Components, which adds a server request, and don't expect Server Actions to run in parallel: the client dispatches them one at a time. (Next.js, Production checklist, Server Actions: Sequential dispatch)
  • 6 NEXTJS-9 Avoid request waterfalls: start independent requests in parallel, and stream slow parts behind <Suspense> or loading.js. (Next.js, Parallel data fetching, Streaming)
  • 6 NEXTJS-10 Return expected errors, such as failed validation, from Server Actions as values, and let error.js and global-error.js boundaries catch uncaught exceptions. (Next.js, Error handling)
  • 6 NEXTJS-11 Use next/image, next/font and next/script instead of plain <img> tags, font links and script tags. They serve correctly sized images in modern formats, self-host fonts without layout shift, and defer third-party scripts. (Next.js, Image optimization, Font optimization, Production checklist)

How to enforce

Tools that check the key rules above automatically, in CI, the IDE or a commit hook. "Partly" means the tool covers only some of what the rule asks.

  • 7 server-only import guard: built into Next.js, which fails the build when a Client Component imports a module marked with import 'server-only'. Checks NEXTJS-5, and partly NEXTJS-3.
  • 7 eslint-config-next: the official ESLint config; use its core-web-vitals preset. @next/next/no-img-element, google-font-display, no-page-custom-font and no-sync-scripts flag plain images, badly loaded fonts and blocking scripts. Checks NEXTJS-11.
  • 6 Lighthouse CI: fails CI when lab metrics such as LCP and CLS, or a resource budget such as resource-summary:script:size, exceed your limits. Partly checks NEXTJS-6 and NEXTJS-11.

Tutorials and further reading