Template: Next.js App
Ready-to-use CLAUDE.md for Next.js App Router projects. Covers routing, server components, testing, and Vercel deployment.
Use this template for standalone Next.js applications using the App Router with a src/ directory structure. It covers the conventions that matter most: server component defaults, async API patterns from Next.js 15+, and a testing setup that keeps tests close to the code they validate.
If you use the Pages Router or a different directory layout, adjust the project structure section accordingly.
# CLAUDE.md
## Project Overview
Next.js App Router application. TypeScript strict mode. Deployed on Vercel.
Directory layout:
- `src/app/` - Route segments, layouts, pages, loading/error states
- `src/components/` - Shared React components
- `src/lib/` - Utilities, helpers, constants
- `src/hooks/` - Custom React hooks
- `src/types/` - Shared TypeScript type definitions
- `public/` - Static assets
## Build Commands
```bash
npm run dev # Local dev server (Turbopack)
npm run build # Production build
npm run lint # ESLint check
npm run test # Run Jest tests
npm run test:watch # Watch mode for TDDKey Conventions
Server Components by Default
Every component is a React Server Component unless it needs interactivity.
Add 'use client' only when the component uses:
- Event handlers (onClick, onChange, onSubmit)
- Browser APIs (window, localStorage, IntersectionObserver)
- React hooks (useState, useEffect, useReducer, useContext)
Server Actions for Mutations
Use Server Actions ('use server') for form submissions and data mutations.
Never call external APIs directly from client components when a Server Action works.
Async APIs (Next.js 15+)
params, searchParams, cookies(), headers(), and draftMode() are all async.
Always await them:
export default async function Page({
params,
}: {
params: Promise<{ slug: string }>
}) {
const { slug } = await params
}Routing Rules
- Use
loading.tsxfor Suspense fallbacks at the route level - Use
error.tsxfor error boundaries at the route level - Use
not-found.tsxfor 404 states - Group related routes with
(group)folders when they share a layout - Dynamic segments use
[param], catch-all uses[...param]
Testing
Jest + React Testing Library. Test files live next to the source file they test.
src/components/Button.tsx
src/components/Button.test.tsxRules:
- Test user-visible behavior, not implementation details
- Use
screen.getByRoleovergetByTestIdwhenever possible - Mock external APIs at the fetch level, not inside components
- Minimum 80% coverage on new code
Code Style
- TypeScript strict mode. No
anytypes. Useunknownand narrow. - Prefer
constand immutable patterns. Never mutate function arguments. - Functions under 50 lines. Files under 400 lines. Extract when either grows.
- Named exports only. No default exports except for pages/layouts (Next.js requires them).
- Use
clsxorcn()for conditional class names. No ternaries in className strings. - Tailwind CSS for styling. No CSS modules unless overriding third-party components.
Deployment
Deployed on Vercel. Environment variables managed through vercel env.
- Never hardcode secrets. Use
process.env.VARIABLE_NAME. - Preview deployments run on every PR. Check the preview before merging.
- Production deploys from
mainbranch only. - Use
vercel.jsonfor redirects, headers, and rewrites. Notnext.config.ts.
Communication Style
Be direct. Show code, not explanations of code. When suggesting changes, edit the file directly rather than describing what to change.
### How to Use
1. Copy the code block above into a `CLAUDE.md` file at your project root
2. Update the build commands if you use `pnpm`, `yarn`, or `bun` instead of `npm`
3. Adjust the directory layout section if your project does not use a `src/` directory
4. Add project-specific rules as you discover patterns that Claude should follow