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¶
- 9 Next.js Docs: App Router: the baseline. The Getting Started chapters cover project structure, Server and Client Components, fetching and mutating data, caching, revalidating and error handling.
- 9 How to think about data security in Next.js: choosing a data-handling model, the Data Access Layer, Server Action security, tainting, and an audit checklist.
- 7 How to optimize your Next.js application for production: the official production checklist for rendering, data fetching, UI, security, metadata and bundle size.
- 7 How to implement authentication in Next.js: sessions, authorization in a Data Access Layer, and optional optimistic checks in Proxy.
Key rules¶
- 9
NEXTJS-1Treat 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-2Give an environment variable theNEXT_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-3Put 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-4Pass 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-5Addimport '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-6Keep"use client"boundaries small: mark the interactive components, not whole layouts or pages, and pass server-rendered content to them aschildrenor 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-7Decide caching per data source, and invalidate the cache in every mutation that changes the data. With Cache Components, mark cached functions or components withuse cacheand give each acacheLife; tag data that mutations change withcacheTag, and callupdateTagorrevalidateTagfrom the Server Action. (Next.js, Caching, Revalidating) - 6
NEXTJS-8Read 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-9Avoid request waterfalls: start independent requests in parallel, and stream slow parts behind<Suspense>orloading.js. (Next.js, Parallel data fetching, Streaming) - 6
NEXTJS-10Return expected errors, such as failed validation, from Server Actions as values, and leterror.jsandglobal-error.jsboundaries catch uncaught exceptions. (Next.js, Error handling) - 6
NEXTJS-11Usenext/image,next/fontandnext/scriptinstead 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-onlyimport guard: built into Next.js, which fails the build when a Client Component imports a module marked withimport 'server-only'. ChecksNEXTJS-5, and partlyNEXTJS-3. - 7 eslint-config-next: the official ESLint config; use its
core-web-vitalspreset.@next/next/no-img-element,google-font-display,no-page-custom-fontandno-sync-scriptsflag plain images, badly loaded fonts and blocking scripts. ChecksNEXTJS-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 checksNEXTJS-6andNEXTJS-11.
Tutorials and further reading¶
- 7 Next.js Learn: App Router course: the free official course, which builds a full-stack dashboard with a database, mutations, error handling and authentication.
- 7 How to Think About Security in Next.js (Sebastian Markbåge, 2023): the original article behind the data security guide, with an audit checklist.
- 7 The Server and Client Boundary (Next.js): how the server and client module graphs, serialized props and rendering environments fit together.