Web · Cursor Rules

Next.js + TypeScript Rules

Cursor rules for Next.js 14 App Router + strict TypeScript. Server Components, Server Actions, Zod, Tailwind, shadcn/ui.

Next.js · TypeScript

What it does

These Cursor rules teach Cursor the App Router mental model and the strict TypeScript conventions that modern Next.js teams ship with. Drop .cursorrules at your project root and every Cursor suggestion — tab completion, chat output, inline generation — will default to Server Components, Server Actions, strict types, and Tailwind + shadcn/ui styling. No more 'use client' on every component. No more API routes for every form. No more untyped fetches.

What Cursor does once these rules are installed

When you ask Cursor to generate a new route, it creates a Server Component page, colocates loading.tsx and error.tsx, and writes typed fetches against your backend. When you ask for a form, Cursor builds it with a Server Action, validates with Zod, and returns a typed result. TypeScript stays strict — no any, no @ts-ignore without an expected-error comment, no untyped props. File structure follows the App Router convention: app/ for routes, components/ for reusable UI, lib/ for logic and DB clients, one export per component file.

Who should use it

Teams that have adopted App Router and want every developer's Cursor to generate code that matches their production conventions. Solo developers building on Next.js 14 who are tired of reformatting Cursor output. Tech leads onboarding new developers — the rules encode the team's answer to every 'should this be server or client?' question, without anyone having to explain it.

What it solves

Cursor's default knowledge predates App Router stability. Without rules, it suggests pages/ patterns, sprinkles 'use client' everywhere, writes API routes when Server Actions are cleaner, and generates components with inferred TypeScript that breaks noUncheckedIndexedAccess. These rules flip the defaults to modern Next.js — your tab completions and chat outputs stop fighting the framework.

How to install

Which tool are you using?

Not sure? Claude.ai is the website. Claude Code is the command-line tool you install separately. Cursor is a code editor that reads .cursorrules.

  1. 01

    Open your Next.js project in Cursor

    Open Cursor and make sure your Next.js project is loaded (you should see package.json, app/, and next.config.js in the sidebar).

    Do not have Cursor? Download it free at cursor.com

  2. 02

    Check for an existing .cursorrules file

    In the left sidebar, look at the project root (same folder as package.json). If .cursorrules already exists, you can replace its contents or extend it.

    Hidden files are visible by default in Cursor. If not, toggle with Cmd/Ctrl+Shift+. in the file tree.

  3. 03

    Create .cursorrules if it does not exist

    Right-click in the file explorer → 'New File' → type exactly: .cursorrules (with the leading dot) → press Enter.

    The filename must be exactly '.cursorrules' — dot at the start, no extension.

  4. 04

    Copy the rules below and paste

    Click the Copy button in the rules file section below. Open .cursorrules in Cursor, select all with Cmd/Ctrl+A, then paste with Cmd/Ctrl+V.

  5. 05

    Save and verify

    Save with Cmd/Ctrl+S. Test by opening Cursor Chat (Cmd/Ctrl+L) and asking: 'Generate a new route for /dashboard that fetches user data'. The result should be a Server Component with typed fetch — proving the rules took effect.

    Cursor re-reads .cursorrules on every chat. No reload required. Commit the file to git so every teammate benefits automatically.

The cursor rules file

Copy the full contents below, or download the file directly.

.cursorrules
.cursorrules
# Next.js + TypeScript Cursor Rules You are an expert Next.js 14 App Router developer working in a strict TypeScriptcodebase. These rules apply to every suggestion you make in this project. ## App Router- App Router only — never suggest code in the pages/ directory- Server Components are the default; add 'use client' ONLY when a component needs event handlers, browser APIs, or React state- When you add 'use client', put it at the TOP of the file and keep the client boundary small — push state down, not up- Colocate loading.tsx, error.tsx, not-found.tsx next to the route they belong to- Use route groups (folder) to organize files without affecting URL paths- Use generateStaticParams for known dynamic routes to pre-render at build time ## TypeScript- strict: true is assumed; do not weaken it- noUncheckedIndexedAccess: true — guard every array / record access- Never use any — use unknown and narrow with type guards- Prefer type over interface except when you need declaration merging- Name component props as ComponentNameProps- Type Server Action signatures explicitly — never rely on inference for public API- Type every function parameter and return value- No @ts-ignore; use @ts-expect-error with a comment explaining the expected error ## Components- One exported component per file- Props interface / type at the top of the file, named ComponentNameProps- Default to function components; no classes- Server Components are async and return JSX directly- Keep files under 200 lines — extract once you exceed that ## Data- Server Actions ('use server') for mutations — not API routes, unless you need a public HTTP endpoint- Zod for all input validation, reused on client and server- Fetches on the server use the Next.js fetch with { next: { tags: ['tag'] } }- revalidateTag for targeted invalidation; revalidatePath only when tags do not fit- Never fetch from a client component on first render — fetch in a Server Component and pass data down ## File Structure- app/ for routes (layout, page, loading, error, route handlers)- components/ for reusable UI (one export per file)- components/ui/ for shadcn primitives- lib/ for pure logic, DB clients, server-only utilities- lib/validations/ for Zod schemas- types/ for shared ambient types ## Styling- Tailwind CSS only; no CSS modules unless integrating legacy- shadcn/ui for primitives (Button, Dialog, Form, Input, Select)- Combine classes with the cn() helper from lib/utils- No inline style except for truly dynamic values- Avoid className strings longer than ~120 characters — extract into a variant ## Performance- next/image with explicit width and height for every non-decorative image- next/font for custom fonts- dynamic() with ssr: false only for truly client-only libraries- Stream slow children behind Suspense with a meaningful fallback ## Error Handling- Every route has an error.tsx boundary- Server Actions return { ok: true, data } | { ok: false, error: { code, message } }- Never leak stack traces to the client- Log errors with a correlation id in production

Example output

What Claude does before and after you install this cursor rules.

Without this cursor rules

Cursor adds "use client" to every component and writes API routes for every form.

With this cursor rules

Cursor defaults to server components, writes Server Actions for forms, and only marks 'use client' when event handlers are needed.

Customization tips

If you use tRPC or GraphQL, update the Data section. If your team wants Zustand or Jotai for client state, add a State section. For projects with a design system other than shadcn/ui, swap the Styling section. Teams that keep legacy pages/ routes can add a paragraph explicitly allowing pages/ for specific paths.

Related resources

Frequently asked questions

Does it work with Next.js 13?

Mostly. App Router and Server Components work on 13, but Server Actions stability differs. Upgrade to 14+ for best results.

Does it enforce a specific tsconfig?

It assumes strict: true and noUncheckedIndexedAccess: true. It does not modify tsconfig.json. Turn those on in your tsconfig for the rules to have real impact.

What if my project still needs the pages/ directory?

Add a line to .cursorrules explicitly allowing pages/ for specific legacy routes. The rule file is plain text — extend it freely.

Does it work with Turbopack?

Yes. The rules are build-tool-agnostic — they govern code generation, not build configuration.

Can I combine it with Cursor Rules for my auth provider (Supabase, Clerk)?

Yes. Concatenate the relevant sections into one .cursorrules file. For Supabase, use the Supabase + Next.js rules as a companion.

Want more like this?

Browse the full RohanKit library — free resources for Claude and Cursor.

Back to RohanKit