Skip to content

project rulesFrontendnextjsreacttypescript

Next.js App Router Rules

Project rules for a Next.js App Router codebase: server components first, explicit caching, typed data boundaries and no client-side waterfalls.

npx workspacetemplates add nextjs-app-router-rules --tool claude-code

Writes 1 file to your project root

CLAUDE.mdadd to your file22 lines · 1.4 KBFile
# Project rules: Next.js App Router

## Components
- Every component is a server component unless it needs state, effects, event handlers or browser APIs. Add `'use client'` only in that leaf file, never in a layout or page just for convenience.
- Pass serializable props from server to client components. Never pass functions, class instances or secrets.
- Keep `page.tsx` and `layout.tsx` thin; put logic in `lib/` and UI in `components/`.

## Data
- Fetch on the server, in the component that needs the data. Start independent requests together with `Promise.all` to avoid waterfalls. Wrap slow parts in `<Suspense>` with a loading fallback.
- State caching on purpose: set `revalidate`, tags or `cache: 'no-store'` explicitly; do not rely on defaults.
- Mutations use Server Actions or route handlers. Validate every input with zod on the server and check the user's permission there, not in the UI.
- Read secrets and environment variables only in server code. Only `NEXT_PUBLIC_` values may reach the browser.

## Routing and files
- Use route groups for layout sections, `loading.tsx` and `error.tsx` for states, `notFound()` for missing records.
- Use `next/image`, `next/font` and `generateMetadata` for every public page.

## TypeScript
- Strict mode. No `any`; type API responses from a zod schema and infer the type.

## Done means
`npm run lint`, `tsc --noEmit` and `next build` pass without new warnings.

What it does

Most App Router mistakes come from treating it like the old pages directory. These rules tell the agent where code belongs: server by default, a client component only when it needs state or a browser API.

They also cover caching, data fetching, forms and type safety, the places where generated code tends to make slow or leaky pages.

Example

you › Add a page that lists the user's invoices with a filter

agent › The agent creates app/invoices/page.tsx as a server component that reads searchParams, loads invoices on the server, wraps the table in Suspense, and keeps only the filter input in a small client component.

Questions

Where does this file go?
It becomes the rules file of your agent, such as CLAUDE.md or a Cursor rules file, and is generated for you per tool.
Does it work with the Pages Router?
No. The caching and component rules assume the App Router.
  • .NET Blazor Rules

    Project rulescsharp

    Free
  • Angular Rules

    Project rulesangular

    Free
  • Astro Rules

    Project rulesastro

    Free
  • Electron Rules

    Project ruleselectron

    Free