Skip to content

Cursor rules for Next.js

App Router, TypeScript, server and client components.

.cursor/rules/nextjs.mdc

Check this file
---
description: Conventions and guardrails for the Next.js App Router codebase
globs: app/**/*.ts,app/**/*.tsx,components/**/*.ts,components/**/*.tsx,lib/**/*.ts
alwaysApply: false
---

# Next.js app

```bash
npm install  # install dependencies
npm run dev  # start the dev server
npm run lint  # lint
npx tsc --noEmit  # type-check
```

Run the tests after every code change.

- TypeScript strict mode. No any without a comment saying why.
- Validate request bodies and query params at the route boundary before using them.
- Read environment variables in one place and fail loudly when a required one is missing. Only variables prefixed NEXT_PUBLIC_ may reach the browser.

Rules for every edit:

- Never read, print, edit or commit .env files, credentials, API keys or private keys.
- Database changes go through migration files in the repo. Never edit a migration that has shipped; add a new one.
- Never deploy to production. I run production deploys myself.
- Never run git push --force, git reset --hard, git clean, git checkout -- <file>, git stash or git rebase, and never use --no-verify. Ask first.
- Lockfiles (package-lock.json and similar): do not edit by hand, and do not add or upgrade dependencies without asking.

Why it says what it says

Commands first

The agent guesses build and test commands when none are written down. Listing the real ones means it can verify its own work.

Server and client components

Mixing server-only code into client files is the most common App Router mistake. One line of layout guidance prevents most of it.

Database and migrations

Agents reach for destructive schema commands when a migration fails. Naming the safe path and the forbidden commands stops the worst outcome.

Deploys are yours

A deploy is the one action you cannot take back quickly. Saying who deploys removes the ambiguity.

Do not touch list

Lockfiles and config files cause large, hard-to-review diffs. Marking them off-limits keeps changes small.

Other formats