Cursor rule for project-wide Next.js 14+ conventions updated for Next.js 16, covering structure, TypeScript, next/font, env vars, security, accessibility and testing.
.cursor/rules/nextjs-14-best-practices.mdc. Downloaded files need to be placed in that repository path.---
description: "Cursor rule for project-wide Next.js 14+ conventions updated for Next.js 16, covering structure, TypeScript, next/font, env vars, security, accessibility and testing."
globs: "**/*"
alwaysApply: false
---
# Next.js Best Practices (Next.js 14+, updated for Next.js 16)
Applies to: project-wide conventions for a Next.js App Router application: TypeScript, components, assets, configuration, security and testing.
Version baseline: originally written for Next.js 14; checked against the Next.js 16.3 documentation. Read `package.json` first. Key differences to respect: `params`, `searchParams`, `cookies()` and `headers()` are async from Next.js 15; `proxy.ts` replaces `middleware.ts` in 16; the `lint` script runs `eslint` directly in 16 (`next lint` is gone).
## Structure
- New routes go in `app/` (App Router). Do not create files in `pages/` unless the project already uses the Pages Router for that route.
- Keep shared UI in `components/` (or `app/_components/`), server-only data access in `lib/` or a `data/` layer, and route-specific pieces colocated in the route folder.
- Follow the repository's existing file-name convention. Component identifiers are PascalCase (`SiteHeader`, without a `Component` suffix); route files keep the framework names (`page.tsx`, `layout.tsx`, `route.ts`).
## TypeScript
- Keep `strict: true`. Type component props and function boundaries; let TypeScript infer locals.
- Type route props with `params: Promise<{ ... }>` or the generated `PageProps<'/route'>` / `LayoutProps<'/route'>` helpers.
- Validate untrusted input (form data, request bodies, search params) at runtime with a schema; types alone do not validate.
## Components and state
- Default to Server Components. Add `'use client'` only on interactive leaves.
- Use function components and hooks. Before reaching for global state, try URL search params (shareable state) or lifting state to the nearest common parent; add context or a state library when several distant components need the same client state.
## Assets
- Images: `next/image` with `alt`, explicit size or `fill` + `sizes`; `remotePatterns` for remote hosts.
- Fonts: load with `next/font/google` or `next/font/local` once in the root layout and apply via `className` or a CSS variable. It self-hosts the font files, so there is no request to Google at runtime.
- Use `next/link` for internal navigation and `next/script` with an explicit `strategy` for third-party scripts.
## Configuration and secrets
- Only variables prefixed `NEXT_PUBLIC_` reach the browser, and they are inlined at build time; a Docker image built once keeps the build-time values.
- Never prefix secrets with `NEXT_PUBLIC_`. Read secrets only in server code, and mark such modules with `import 'server-only'`.
- Commit `.env.example`, never `.env*.local`.
## Security
- Authenticate and authorize inside every Server Action and Route Handler; rendering a form only for signed-in users is not protection.
- React escapes text by default. Treat `dangerouslySetInnerHTML` as a review flag: sanitize HTML or escape `<` in JSON-LD.
- Return only the fields the UI needs from data functions.
## Accessibility
- Use semantic elements (`button`, `nav`, `main`, headings in order) and label every form control.
- Enable `eslint-plugin-jsx-a11y` (included in `eslint-config-next`) and fix its findings; test keyboard navigation for interactive components.
## Testing
- Unit and component tests: Vitest or Jest with React Testing Library for synchronous components, hooks and utilities. Async Server Components are not supported by these runners; cover them with end-to-end tests.
- End-to-end: Playwright (or Cypress) against `next build && next start` for critical flows.
## Verify before finishing
Run the scripts the project defines, typically:
- `npx tsc --noEmit`
- `npm run lint`
- `npm test` (unit) and the end-to-end command for flows you touched
- `npx next build`, checking the route summary for unexpected dynamic routes
- `grep -rn "NEXT_PUBLIC_" .env* ` confirms no secret is exposed
Cursor rules for Next.js 14+ App Router projects updated for Next.js 16 with async params, validated Route Handlers, generateMetadata and proxy.ts in place of middleware.
Cursor rules for strict TypeScript: compiler checks beyond strict, interfaces vs type aliases, real type guards, discriminated unions, utility types and branded IDs.
Cursor rules for Tailwind CSS v4: automatic class sorting, mobile-first breakpoints, CVA variants with merging, class-based dark mode via @custom-variant, and CSS-first @theme configuration.
Cursor
coding
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
Reviewing a pull request in a Next.js 16 App Router project for project-level conventions before merge.
Review this PR: adds pages/pricing.tsx, a HeaderComponent in components/header-component.js, uses <link> to load Google Fonts in app/layout.tsx, reads process.env.NEXT_PUBLIC_STRIPE_SECRET in a Server Action, and adds Jest tests for an async Server Component.
Illustrative; not a recorded model run. Review comments: move pricing to app/pricing/page.tsx; name the component SiteHeader in .tsx following the repo's file-name convention; load the font with next/font/google in the root layout; rename the secret without NEXT_PUBLIC_ and read it only in server code with server-only; cover the async Server Component with a Playwright test instead of Jest; run tsc, npm run lint (eslint, since next lint is removed in 16) and next build.
Cursor applies project rules stored as .mdc files in .cursor/rules. Each rule's frontmatter (description, globs, alwaysApply) decides whether it is always included, attached to matching files, chosen by the agent from its description, or only used when you @-mention it. Cursor also reads AGENTS.md in the project root and subdirectories for plain-Markdown instructions.
.cursor/rules/nextjs-14-best-practices.mdcStarting point, not a tested configuration for your repository. Attached when files matching the globs are in context (alwaysApply: false with globs). Review the instructions, adapt the file paths and run your project's existing checks.
---
description: "Next.js 14 Best Practices, Updated for Next.js 16 project conventions"
globs: "**/*"
alwaysApply: false
---
# Next.js Best Practices (Next.js 14+, updated for Next.js 16)
Applies to: project-wide conventions for a Next.js App Router application: TypeScript, components, assets, configuration, security and testing.
Version baseline: originally written for Next.js 14; checked against the Next.js 16.3 documentation. Read `package.json` first. Key differences to respect: `params`, `searchParams`, `cookies()` and `headers()` are async from Next.js 15; `proxy.ts` replaces `middleware.ts` in 16; the `lint` script runs `eslint` directly in 16 (`next lint` is gone).
## Structure
- New routes go in `app/` (App Router). Do not create files in `pages/` unless the project already uses the Pages Router for that route.
- Keep shared UI in `components/` (or `app/_components/`), server-only data access in `lib/` or a `data/` layer, and route-specific pieces colocated in the route folder.
- Follow the repository's existing file-name convention. Component identifiers are PascalCase (`SiteHeader`, without a `Component` suffix); route files keep the framework names (`page.tsx`, `layout.tsx`, `route.ts`).
## TypeScript
- Keep `strict: true`. Type component props and function boundaries; let TypeScript infer locals.
- Type route props with `params: Promise<{ ... }>` or the generated `PageProps<'/route'>` / `LayoutProps<'/route'>` helpers.
- Validate untrusted input (form data, request bodies, search params) at runtime with a schema; types alone do not validate.
## Components and state
- Default to Server Components. Add `'use client'` only on interactive leaves.
- Use function components and hooks. Before reaching for global state, try URL search params (shareable state) or lifting state to the nearest common parent; add context or a state library when several distant components need the same client state.
## Assets
- Images: `next/image` with `alt`, explicit size or `fill` + `sizes`; `remotePatterns` for remote hosts.
- Fonts: load with `next/font/google` or `next/font/local` once in the root layout and apply via `className` or a CSS variable. It self-hosts the font files, so there is no request to Google at runtime.
- Use `next/link` for internal navigation and `next/script` with an explicit `strategy` for third-party scripts.
## Configuration and secrets
- Only variables prefixed `NEXT_PUBLIC_` reach the browser, and they are inlined at build time; a Docker image built once keeps the build-time values.
- Never prefix secrets with `NEXT_PUBLIC_`. Read secrets only in server code, and mark such modules with `import 'server-only'`.
- Commit `.env.example`, never `.env*.local`.
## Security
- Authenticate and authorize inside every Server Action and Route Handler; rendering a form only for signed-in users is not protection.
- React escapes text by default. Treat `dangerouslySetInnerHTML` as a review flag: sanitize HTML or escape `<` in JSON-LD.
- Return only the fields the UI needs from data functions.
## Accessibility
- Use semantic elements (`button`, `nav`, `main`, headings in order) and label every form control.
- Enable `eslint-plugin-jsx-a11y` (included in `eslint-config-next`) and fix its findings; test keyboard navigation for interactive components.
## Testing
- Unit and component tests: Vitest or Jest with React Testing Library for synchronous components, hooks and utilities. Async Server Components are not supported by these runners; cover them with end-to-end tests.
- End-to-end: Playwright (or Cypress) against `next build && next start` for critical flows.
## Verify before finishing
Run the scripts the project defines, typically:
- `npx tsc --noEmit`
- `npm run lint`
- `npm test` (unit) and the end-to-end command for flows you touched
- `npx next build`, checking the route summary for unexpected dynamic routes
- `grep -rn "NEXT_PUBLIC_" .env* ` confirms no secret is exposed
Ask for a small change to one matching file. Check the tool's loaded context or instruction view, inspect the diff for the requested convention, and run the relevant build or test. A well-formatted file does not guarantee the model follows every instruction.
Cursor's docs recommend keeping individual rules under 500 lines and splitting large rules into composable ones. Cursor's documentation states that it supports AGENTS.md in the project root and in subdirectories. Use AGENTS.md for plain-Markdown instructions you also want other tools to read, and .mdc rules when you need Cursor's scoping options (globs, description-based selection, always-apply).
Documentation checked 2026-11-05: Cursor docs: Rules
Cursor rules for Next.js 14+ App Router projects updated for Next.js 16 with async params, validated Route Handlers, generateMetadata and proxy.ts in place of middleware.
Cursor rules for strict TypeScript: compiler checks beyond strict, interfaces vs type aliases, real type guards, discriminated unions, utility types and branded IDs.
Cursor rules for Tailwind CSS v4: automatic class sorting, mobile-first breakpoints, CVA variants with merging, class-based dark mode via @custom-variant, and CSS-first @theme configuration.