Cursor can scaffold a complete Next.js project structure when given clear conventions about the App Router, file-based routing, and component organization. By adding Next.js-specific rules to .cursorrules and prompting Cursor to follow the official App Router patterns, you get a well-organized project that Cursor can navigate and generate code for efficiently.
Structuring a Next.js project for optimal Cursor usage
Next.js App Router uses file-based routing with special files (page.tsx, layout.tsx, loading.tsx). Cursor needs to understand these conventions to generate routes, layouts, and server components correctly. This tutorial establishes a project structure that maximizes Cursor's ability to navigate and generate Next.js code.
Prerequisites
- Cursor installed with a Next.js project (App Router)
- Next.js 14+ with TypeScript
- Basic understanding of the App Router file conventions
- Familiarity with Cursor Chat (Cmd+L) and Composer (Cmd+I)
Step-by-step guide
Add Next.js conventions to .cursor/rules
Add Next.js conventions to .cursor/rules
Create rules that define your Next.js project structure and file conventions. This ensures Cursor generates code in the right locations with the correct patterns.
1---2description: Next.js App Router conventions3globs: "app/**/*.tsx,app/**/*.ts"4alwaysApply: true5---67## Next.js App Router Rules8- Use the app/ directory for all routes (NOT pages/)9- page.tsx = route page component (default export)10- layout.tsx = shared layout (wraps child routes)11- loading.tsx = loading UI (Suspense boundary)12- error.tsx = error boundary (must be 'use client')13- not-found.tsx = 404 page14- route.ts = API route handler (GET, POST, etc.)1516## Component Rules17- Server Components by default (NO 'use client' unless needed)18- Add 'use client' ONLY for: useState, useEffect, event handlers, browser APIs19- Data fetching in Server Components using async/await (not useEffect)20- Place shared components in src/components/21- Place page-specific components in app/[route]/_components/2223## File Organization24- app/ — routes and layouts25- src/components/ — shared UI components26- src/hooks/ — custom React hooks27- src/lib/ — utility functions and API clients28- src/types/ — TypeScript type definitions29- public/ — static assetsExpected result: Cursor follows Next.js App Router conventions for file placement and component types.
Scaffold the project structure with Composer
Scaffold the project structure with Composer
Use Composer Agent mode to create the initial folder structure with placeholder files. This gives Cursor a map of your project to reference in future prompts.
1// Composer prompt (Cmd+I):2// Create the following Next.js App Router project structure:3//4// app/5// layout.tsx (root layout with html, body, Providers)6// page.tsx (home page)7// loading.tsx (global loading spinner)8// not-found.tsx (404 page)9// (auth)/10// login/page.tsx11// register/page.tsx12// layout.tsx (auth layout, no navbar)13// (dashboard)/14// dashboard/page.tsx15// settings/page.tsx16// layout.tsx (dashboard layout with sidebar)17// api/18// users/route.ts19// auth/route.ts20//21// src/22// components/ui/ (Button, Input, Modal)23// components/layout/ (Navbar, Sidebar, Footer)24// hooks/ (useAuth, useMediaQuery)25// lib/ (fetcher, auth, utils)26// types/ (user, api)27//28// Generate minimal placeholder content for each file.Expected result: A complete Next.js project structure with placeholder files for Cursor to reference.
Generate a route with proper patterns
Generate a route with proper patterns
Test the setup by asking Cursor to generate a new route. It should create the correct files in the right locations following App Router conventions.
1// Cursor Chat prompt (Cmd+L):2// @.cursor/rules/nextjs.mdc Create a new /products route with:3// - Server Component page that fetches products from /api/products4// - Loading state with loading.tsx5// - Error boundary with error.tsx6// - Product card component in app/products/_components/7// Use Server Component data fetching (async page, not useEffect).89// Expected file structure:10// app/products/11// page.tsx (async Server Component)12// loading.tsx13// error.tsx ('use client')14// _components/ProductCard.tsxPro tip: Use the _components/ convention (underscore prefix) for route-specific components. The underscore prevents Next.js from treating the folder as a route segment.
Expected result: A complete route with Server Component page, loading state, error boundary, and local components.
Organize route groups for different layouts
Organize route groups for different layouts
Use route groups (parenthesized folders) to share layouts between routes without affecting the URL structure. Ask Cursor to set these up correctly.
1// Cursor Chat prompt (Cmd+L):2// @app/layout.tsx Create route groups:3// (marketing) — public pages with marketing navbar4// /pricing, /about, /blog5// (app) — authenticated pages with app sidebar6// /dashboard, /settings, /profile7// (auth) — auth pages with minimal layout8// /login, /register9// Each group should have its own layout.tsx.10// The root layout has only Providers and html/body.Expected result: Three route groups with distinct layouts sharing the same root layout.
Configure .cursorignore for Next.js
Configure .cursorignore for Next.js
Exclude Next.js build artifacts and cache from Cursor's indexing to improve performance and avoid stale suggestions.
1# .cursorignore2.next/3node_modules/4out/5.vercel/6coverage/7*.tsbuildinfoExpected result: Cursor indexing skips build artifacts, improving performance and suggestion quality.
Complete working example
1---2description: Next.js App Router project conventions3globs: "app/**/*.{ts,tsx},src/**/*.{ts,tsx}"4alwaysApply: true5---67## Route Files (app/ directory)8- `page.tsx` — page component (default export, async for server data)9- `layout.tsx` — shared layout wrapping child routes10- `loading.tsx` — loading UI (automatic Suspense boundary)11- `error.tsx` — error boundary (MUST have 'use client')12- `not-found.tsx` — 404 not found page13- `route.ts` — API route handler (named exports: GET, POST, PUT, DELETE)14- `_components/` — page-specific components (underscore = not a route)1516## Component Types17- Default: Server Component (no 'use client')18- Add 'use client' ONLY when component uses:19 - useState, useEffect, useRef, or other hooks20 - Event handlers (onClick, onChange, etc.)21 - Browser APIs (window, document, localStorage)22 - Third-party client libraries2324## Data Fetching25- Server Components: use async/await directly in the component26- Client Components: use SWR hooks from src/hooks/27- API Routes: export async functions named GET, POST, etc.28- NEVER use getServerSideProps or getStaticProps (Pages Router)2930## File Organization31```32app/ # Routes (file-based routing)33 (marketing)/ # Route group: public pages34 (app)/ # Route group: authenticated pages35 (auth)/ # Route group: auth pages36 api/ # API routes37src/38 components/39 ui/ # Reusable UI (Button, Input, Modal)40 layout/ # Layout parts (Navbar, Sidebar)41 hooks/ # Custom React hooks42 lib/ # Utilities, API clients, auth43 types/ # TypeScript definitions44public/ # Static assets45```Common mistakes when structuring a Next.js project for Cursor
Why it's a problem: Cursor generating pages/ directory files instead of app/
How to avoid: Add 'Use the app/ directory for all routes, NOT pages/' to .cursorrules. Explicitly mention App Router in prompts.
Why it's a problem: Adding 'use client' to every component
How to avoid: Add 'Server Components by default, add use client ONLY for hooks and event handlers' to your rules.
Why it's a problem: Using useEffect for data fetching in Server Components
How to avoid: Add 'Data fetching in Server Components using async/await, not useEffect' to your Next.js rules.
Best practices
- Use App Router conventions (app/) not Pages Router (pages/)
- Default to Server Components and only add 'use client' when necessary
- Use route groups (parentheses) for layout organization without URL impact
- Place page-specific components in _components/ subdirectories
- Fetch data directly in async Server Components, not with useEffect
- Configure .cursorignore to exclude .next/ and node_modules/
- Reference @.cursor/rules/nextjs.mdc when asking Cursor to generate routes
Still stuck?
Copy one of these prompts to get a personalized, step-by-step explanation.
Create a Next.js 14 App Router project structure with: route groups for marketing (public), app (authenticated), and auth (login/register). Include proper layouts, loading states, error boundaries, and API routes. Show the complete folder tree with file purposes. Use TypeScript and Server Components by default.
In Cursor Composer (Cmd+I): @.cursor/rules/nextjs.mdc Scaffold a new /products route with: async Server Component page fetching from /api/products, loading.tsx, error.tsx ('use client'), _components/ProductCard.tsx (Server Component), and api/products/route.ts with GET handler.
Frequently asked questions
Should I use src/ or put everything in app/?
Use src/ for shared code (components, hooks, lib, types) and app/ for routes only. This keeps routes clean and makes shared code easy to reference with @src/.
Can Cursor handle Next.js Server Actions?
Yes. Ask Cursor to generate Server Actions with 'use server' directive. Reference your form component and specify the action should be in the same file or a separate actions.ts file.
How do I handle route params in Cursor prompts?
Specify the dynamic route: 'Create app/products/[id]/page.tsx that receives params.id as a prop.' Cursor understands Next.js dynamic route conventions.
Does Cursor know about Next.js middleware?
Yes. Ask Cursor to generate middleware.ts at the project root for auth redirects, rate limiting, or request rewriting. Specify the matcher pattern in your prompt.
How do I handle Cursor generating Pages Router code?
Add a strict rule: 'This project uses App Router. NEVER use getServerSideProps, getStaticProps, or the pages/ directory.' Reference this rule in every prompt about routing.
Talk to an Expert
Our team has built 600+ apps. Get personalized help with your project.
Book a free consultation