c/frontend › command
0votes

Cursor rules for Next.js App Router

TemplateLow risk Tested
submitted by CommandVault editors to c/frontend · 1 use

A .cursor/rules/nextjs.mdc file for server components, data fetching, routing and caching done right.

Template · 2 variables
---
description: Next.js App Router conventions
globs: ["app/**/*.tsx","app/**/*.ts","src/app/**"]
alwaysApply: false
---

# Next.js App Router

- Server Components by default. Add "use client" only for state, effects, browser APIs or event handlers, and keep client components small and at the leaves.
- Fetch data in Server Components or route handlers, never with useEffect for initial data.
- `params` and `searchParams` are async: `const { slug } = await params`.
- Use `generateMetadata` for titles/descriptions on every page; set `alternates.canonical`.
- Mutations: Server Actions or route handlers in `app/api`; validate input with {{validation_library}}.
- Images via next/image with width/height; fonts via next/font.
- Use `loading.tsx`, `error.tsx` and `not-found.tsx` instead of ad-hoc spinners and checks.
- Never import server-only modules (database clients, secrets) into client components.

## When you change code
- Follow the patterns in the nearest existing file before inventing new ones.
- Add or update a test for behavior changes; run `{{test_command}}`.
- Keep diffs small; explain anything surprising in your reply.
0 commentsreport

Guide

What you get: The agent builds App Router pages the way the rules say: Server Components by default, async params awaited, generateMetadata with a canonical URL, a small client form posting to a Server Action validated with your library, not-found.tsx for missing pages, and a test. Best for Teams on Next.js App Router who want Cursor (or any agent that reads a rules/context file) to stop writing client-side data fetching and ad-hoc spinners..

Before you start

  • A Next.js App Router project
  • Your validation library installed (we used zod)
  • A working test command (we added Vitest: npm test)
  • Cursor: save as .cursor/rules/nextjs.mdc. Claude Code/Codex: paste the rules into CLAUDE.md / AGENTS.md

Worked example (Nov 14, 2026, Claude Code 2.1.318 (claude-opus-5-5); Codex 0.151.0 (gpt-5.6-sol))

Input

create-next-app 16.4.6 starter (cacheComponents on) plus lib/posts.ts and Vitest. Filled with validation_library=zod, test_command=npm test. Task: add /blog, /blog/[slug] with a proper not-found page, and a newsletter signup form saving emails via a new lib/subscribers.ts.

Result

Claude Code: `const { slug } = await params;` ... `alternates: { canonical: `/blog/${post.slug}` }` and a 'use client' NewsletterForm using useActionState with a 'use server' action. Codex: same structure, zod schema in lib/subscribers.ts, plus actions.test.ts.

Not tested in Cursor or Copilot: the rule text was saved verbatim (front matter included) as CLAUDE.md for Claude Code and AGENTS.md for Codex, the files those agents load at startup, plus a copy at its normal path. Both runs: npm test, npm run lint and next build passed when we re-ran them; no useEffect data fetching; zod validation on the server; only the form is a client component; both added metadataBase to the root layout because a relative canonical needs it. Runtime check with next start: /blog and posts return 200 with the canonical link; an unknown slug shows the not-found page with noindex but HTTP 200, which is documented Next.js 16 behaviour when notFound() runs after streaming starts (not something the rules cover). Codex also read installed personal skills; Claude Code's reply echoed the tester's personal ~/.claude/CLAUDE.md wording (no project files affected).

How to check it worked

  • Run lint, tests and the build after the agent finishes
  • grep the diff for useEffect and 'use client': client components should only be the interactive leaves
  • Open an unknown slug and check the status code, not just the page text

Common problems

  • Relative alternates.canonical without metadataBase in the root layout: the agent has to add one (both did)
  • Unknown slugs can return HTTP 200 with the not-found UI under cacheComponents/streaming; the rules don't mention it
  • Leftover {{validation_library}} or {{test_command}} gets repeated literally

How to add this Cursor rule

  1. Fill in the 2 blanks above; Copy puts your answers into the file.
  2. Save it in .cursor/rules/ with the .mdc extension (plain .md files there are ignored), e.g. .cursor/rules/nextjs.mdc.
  3. The front matter decides when it applies: alwaysApply: true for every chat, globs for matching files, or a description so Cursor applies it when relevant.
  4. Check it in Cursor's Rules settings, then ask something about a matching file and confirm the answer follows the rule.
  • {{validation_library}} Validation library
  • {{test_command}} Test command

Checked against Cursor docs: rules.

More from c/frontend

See all of c/frontend →
0 comments

Log in to join the discussion.