Cursor Rules for Next.js App Router: What to Include
You ask Cursor for a new dashboard page and get a file that starts with 'use client', fetches data inside useEffect, and reads params as if it were a plain object. It compiles in your head, then fails in the browser or quietly ships a client bundle ten times larger than it needs to be. You fix it, ask for the next page, and the same mistakes come back.
Cursor rules for Next.js App Router are short instruction files, stored in .cursor/rules, that tell the model how your project uses server components, data fetching, routing conventions and environment variables. This article lists the specific rules worth writing, shows how to scope them, and explains where writing them by hand stops being worth your time. BuyCoded sells ready-made rule packs for exactly this problem, and we point to the relevant one once the manual method gets tedious.
Why does Cursor write Pages Router code in an App Router project?
Cursor writes Pages Router patterns because a large share of the public Next.js code a model has seen predates the App Router, so the older patterns are the statistical default. Nothing in your prompt says which router you use, and the model fills the gap with the most common answer.
The giveaways are consistent: getServerSideProps in a file under app/, useRouter imported from next/router instead of next/navigation, and a pages/api handler where a route handler belongs. Each one is a single line a rule can forbid.
A rule is cheaper than correcting the output every time. One sentence in an always-on rule, such as "This project uses the App Router only. Never create files under pages/", removes an entire class of wrong answers.
What should Cursor rules for the Next.js App Router cover?
Good App Router rules cover five areas: component boundaries, async request APIs, data access, mutations, and environment variables. Each area is a place where the wrong default compiles fine but behaves badly.
| Area | Rule to write | Mistake it prevents |
|---|---|---|
| Component boundary | Components are server components by default. Add 'use client' only to the smallest leaf that needs state, effects or browser APIs. | Whole pages marked as client components, which pulls their imports into the client bundle. |
| Request APIs | Treat params and searchParams as promises and await them. Treat cookies() and headers() as async too. | Reading properties off an unresolved promise, which gives undefined values or type errors. |
| Data access | Fetch in server components or in a data module the server calls. Never fetch your own API route from a server component. | Extra network hops and useEffect fetches that flash empty states. |
| Mutations | Use server actions for form writes and validate input inside the action. Call the revalidation function after a write. | Stale pages after a save, and unvalidated input reaching the database. |
| Environment | Only variables prefixed NEXT_PUBLIC_ may be used in client code. Never prefix a secret. | Secrets inlined into the browser bundle. |
The Next.js documentation describes server and client components in its App Router section, and it is the right source to check each rule against before you commit it. Rules that contradict the docs for your installed version will cost you more than having none.
How do you scope App Router rules so Cursor loads them?
You scope a rule by giving its .mdc file a globs pattern in the frontmatter, so Cursor attaches it only when matching files are in context. Cursor's rules documentation lists four rule types: always apply, apply intelligently from the description, apply to specific files by glob, and apply manually.
For App Router work, split by file role rather than by topic. One small always-on rule states the router and the language. Separate glob-scoped rules cover the places where each convention matters:
app/**/page.tsxandapp/**/layout.tsxfor server component and async params rules.app/**/route.tsfor route handler rules, such as returningResponseobjects and validating the body.**/actions.tsfor server action rules: input validation, authorization check, revalidation.components/**/*.tsxfor the client boundary rule.
A glob-scoped rule that never matches is the most common silent failure, so test the pattern against a real path before trusting it.
What does a working App Router rule file look like?
A working rule file is short, imperative and checkable. This one covers page files and fits in under fifteen lines:
---
description: Server component and request API conventions for App Router pages
globs: app/**/page.tsx, app/**/layout.tsx
alwaysApply: false
---
- Pages and layouts are server components. Do not add 'use client' to them.
- params and searchParams are Promises. Declare the prop type as Promise<...> and await it.
- Fetch data directly in the component or through a function in lib/data. No useEffect fetching.
- Move interactive pieces into a separate file in components/ and mark only that file 'use client'.
- Export metadata or generateMetadata for every page that is publicly indexable.Each line can be verified by reading the diff. If you cannot tell whether Cursor obeyed a rule by looking at the output, the rule is too vague and the model will treat it as a suggestion.
Notice what is absent: no explanation of what a server component is, and no style essays. The model already knows the concept. It needs your decision, not a tutorial.
How do you add these rules to a project step by step?
You add them in a few minutes by creating the folder, writing one always-on rule, then adding scoped rules one at a time and checking each against a real prompt.
- Create
.cursor/rules/in the repository root and commit it, so the whole team shares the same rules. - Write one always-on rule naming the router, the language, and the package manager. Keep it under ten lines.
- Add the page and layout rule above, with its
globspattern. - Ask Cursor to create a new dynamic route such as
app/posts/[slug]/page.tsxand confirm it awaitsparams. - Add the route handler, server action and client component rules the same way, testing each with one prompt that would normally trigger the mistake.
- When Cursor breaks a rule, tighten that rule's wording instead of adding a new one beside it.
Step four matters most. A rule you have never seen change the output is a rule you do not know works.
Should you write these rules yourself or start from a pack?
Write the project-specific rules yourself and start from a pack for everything generic. Your folder layout, your auth helper and your data module are facts only you know. The generic conventions for the framework are the same in every project, and typing them out again is the tedious part.
That is the gap the BuyCoded Cursor Rules Mega-Pack fills. According to its product page it contains 1,057 production-grade Cursor rules across 20 stacks, shipped in both Cursor formats, so you copy the Next.js and TypeScript rules you want instead of drafting them. You still edit them to match your project, which takes less time than starting from a blank file.
The honest objection is that a pack adds rules you may not need, and too many always-on rules dilute each other. That is true, so do not drop everything in at once. Pick the rules that match your stack, keep always-on rules to a handful, and scope the rest by glob. A pack is a source of rules to select from, not a folder to copy wholesale.
How do you know the rules are working?
You know the rules work when the same prompt produces the compliant pattern across several fresh chats, not just once. Models vary between runs, so a single good result proves little.
Run a small regression check. Keep three prompts that used to fail, such as a dynamic page, a form with a write, and a route handler. Re-run them after every rule change in a new chat and read the diffs for the forbidden patterns. If a rule fails twice, shorten it or move it from an intelligent rule to a glob-scoped one so it attaches deterministically.
What is the next step?
Browse the full BuyCoded product catalog, pick the pack that matches your stack, and download it after checkout. Each product page lists what is inside, so you can check the rules against the checklist above before you buy.
Frequently asked questions
Do I need separate Cursor rules for the App Router and the Pages Router?
Yes, keep them separate if you maintain both. The routers use different data fetching, navigation imports and file conventions, so one combined rule gives contradictory instructions. Scope App Router rules to the app directory and Pages Router rules to the pages directory with globs, so each set only attaches when you edit files in its own router.
Where do Cursor rules for Next.js go in the project?
Cursor rules live in the .cursor/rules folder at the repository root, as .mdc files with frontmatter for description, globs and alwaysApply. Commit the folder to git so everyone on the team gets the same rules. The older single .cursorrules file at the root still exists, but the folder format allows scoping by file pattern.
How long should a Next.js Cursor rule file be?
Keep each rule file under about 20 lines and focused on one file role or one convention. Short files are easier to verify, and they use less of the context window on every request. If a file grows past that, split it by glob, for example pages and layouts in one file and route handlers in another.
Why does Cursor still add use client after I wrote a rule against it?
Usually the rule is not attached or is too vague. Check that the globs pattern matches the real file path, and that the rule type is not set to manual. Then make the wording concrete: name the files and state the exception. A rule that says only the smallest interactive leaf gets use client is easier to follow.
Can Cursor rules replace reading the Next.js documentation?
No. Rules record decisions you have already made, and they are only as correct as the documentation you checked them against. When you upgrade Next.js, re-read the relevant docs and update the rules that changed. An outdated rule is worse than no rule because the model follows it confidently.