Cursor rule for the Next.js Server and Client Component boundary with leaf-level use client, serializable props, server-only modules and composition via children.
.cursor/rules/next-js-server-components.mdc. Downloaded files need to be placed in that repository path.---
description: "Cursor rule for the Next.js Server and Client Component boundary with leaf-level use client, serializable props, server-only modules and composition via children."
globs: "**/*"
alwaysApply: false
---
# Next.js Server Components
Applies to: deciding the Server/Client Component boundary in a Next.js App Router project and passing data across it.
Version baseline: checked against the Next.js 16.3 "Server and Client Components" guide and React 19.2. Read `package.json` first and follow the installed versions.
## Default to Server Components
- Components in `app/` are Server Components unless their module (or an importing module) starts with `'use client'`.
- Keep data access, secrets and heavy dependencies (markdown parsers, SDKs) in Server Components; they add no JavaScript to the client bundle.
- Await data directly in async Server Components. Do not use `useEffect` + `fetch` for data the server can read.
## Add `'use client'` at the smallest leaf
- Use a Client Component only when it needs state or effects, event handlers, browser APIs, or a client-only third-party component.
- `'use client'` marks a module-graph boundary: everything that file imports becomes client code. Put the directive on the interactive leaf (a `LikeButton`), not on a page or layout.
- Wrap third-party components that use client features but lack the directive in your own small `'use client'` file.
## Pass only serializable props
- Props from a Server Component to a Client Component must be serializable by React: plain objects, arrays, strings, numbers, booleans, `null`, `Date`, `Map`, `Set`, promises, JSX, and Server Actions. Class instances and ordinary functions are not.
- Pass the fields the client needs (`{ id, name }`), not full database records; they are embedded in the page payload and visible to the user.
- Only `NEXT_PUBLIC_` environment variables reach the browser; other variables become empty strings in client code.
## Compose instead of importing upward
A Client Component cannot import a Server Component, but it can render one passed as `children` or another prop. The server part still renders on the server.
```tsx
// app/components/collapsible.tsx
'use client'
import { useState } from 'react'
export function Collapsible({ title, children }: { title: string; children: React.ReactNode }) {
const [open, setOpen] = useState(false)
return (
<section>
<button type="button" aria-expanded={open} onClick={() => setOpen(!open)}>
{title}
</button>
{open ? children : null}
</section>
)
}
```
```tsx
// app/products/[id]/page.tsx
import { Suspense } from 'react'
import { Collapsible } from '@/app/components/collapsible'
import { getProduct } from '@/lib/products'
async function Specs({ id }: { id: string }) {
const product = await getProduct(id)
return <p>{product?.description}</p>
}
export default async function Page({ params }: { params: Promise<{ id: string }> }) {
const { id } = await params
return (
<Collapsible title="Specifications">
<Suspense fallback={<p>Loading specifications...</p>}>
<Specs id={id} />
</Suspense>
</Collapsible>
)
}
```
## Protect server-only modules
Add `import 'server-only'` at the top of modules that read secrets or the database. A Client Component that imports them then fails at build time instead of leaking code. Installing the `server-only` package is optional in Next.js but some lint setups require it.
## Context providers
Context only works in Client Components. Create a `'use client'` provider that accepts `children` and render it as deep in the tree as possible (around `{children}`, not around `<html>`).
## Streaming
- Wrap slow, independent parts in `<Suspense>` with a meaningful fallback; one boundary per independently loading region.
- To start a request on the server and read it in a Client Component, pass the un-awaited promise as a prop and read it with `use()` inside a `<Suspense>` boundary.
## Verify before finishing
- `npx tsc --noEmit` and `npx next build` pass. A `server-only` import reached from client code fails the build; a non-serializable prop errors when the page renders, so also load each changed page in `next dev`.
- `grep -rln "'use client'" app components` lists only interactive leaves and providers; no page or layout gained the directive.
- Inspect the page source: the RSC payload contains no fields you did not intend to send.
Cursor rules for Next.js 14+ App Router projects updated for Next.js 16 with async params, validated Route Handlers, generateMetadata and proxy.ts in place of middleware.
Cursor rules for strict TypeScript: compiler checks beyond strict, interfaces vs type aliases, real type guards, discriminated unions, utility types and branded IDs.
Cursor rules for Tailwind CSS v4: automatic class sorting, mobile-first breakpoints, CVA variants with merging, class-based dark mode via @custom-variant, and CSS-first @theme configuration.
Cursor
framework
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
A product page in a Next.js 16 app was marked 'use client' to add a collapsible section, pulling the database client into the browser bundle.
app/products/[id]/page.tsx starts with 'use client', uses useState for an expandable Specifications panel, and imports getProduct from lib/products.ts (which uses the DB). Refactor so only the toggle runs on the client.
Illustrative; not a recorded model run. The page becomes an async Server Component that awaits params; a small 'use client' Collapsible accepting title and children; the specs are fetched by a Server Component passed as children and wrapped in <Suspense>; lib/products.ts gets import 'server-only'; only serializable fields cross the boundary.
Cursor applies project rules stored as .mdc files in .cursor/rules. Each rule's frontmatter (description, globs, alwaysApply) decides whether it is always included, attached to matching files, chosen by the agent from its description, or only used when you @-mention it. Cursor also reads AGENTS.md in the project root and subdirectories for plain-Markdown instructions.
.cursor/rules/next-js-server-components.mdcStarting point, not a tested configuration for your repository. Attached when files matching the globs are in context (alwaysApply: false with globs). Review the instructions, adapt the file paths and run your project's existing checks.
---
description: "Next.js Server Components: Client Boundaries, Serializable Props and server-only project conventions"
globs: "**/*"
alwaysApply: false
---
# Next.js Server Components
Applies to: deciding the Server/Client Component boundary in a Next.js App Router project and passing data across it.
Version baseline: checked against the Next.js 16.3 "Server and Client Components" guide and React 19.2. Read `package.json` first and follow the installed versions.
## Default to Server Components
- Components in `app/` are Server Components unless their module (or an importing module) starts with `'use client'`.
- Keep data access, secrets and heavy dependencies (markdown parsers, SDKs) in Server Components; they add no JavaScript to the client bundle.
- Await data directly in async Server Components. Do not use `useEffect` + `fetch` for data the server can read.
## Add `'use client'` at the smallest leaf
- Use a Client Component only when it needs state or effects, event handlers, browser APIs, or a client-only third-party component.
- `'use client'` marks a module-graph boundary: everything that file imports becomes client code. Put the directive on the interactive leaf (a `LikeButton`), not on a page or layout.
- Wrap third-party components that use client features but lack the directive in your own small `'use client'` file.
## Pass only serializable props
- Props from a Server Component to a Client Component must be serializable by React: plain objects, arrays, strings, numbers, booleans, `null`, `Date`, `Map`, `Set`, promises, JSX, and Server Actions. Class instances and ordinary functions are not.
- Pass the fields the client needs (`{ id, name }`), not full database records; they are embedded in the page payload and visible to the user.
- Only `NEXT_PUBLIC_` environment variables reach the browser; other variables become empty strings in client code.
## Compose instead of importing upward
A Client Component cannot import a Server Component, but it can render one passed as `children` or another prop. The server part still renders on the server.
```tsx
// app/components/collapsible.tsx
'use client'
import { useState } from 'react'
export function Collapsible({ title, children }: { title: string; children: React.ReactNode }) {
const [open, setOpen] = useState(false)
return (
<section>
<button type="button" aria-expanded={open} onClick={() => setOpen(!open)}>
{title}
</button>
{open ? children : null}
</section>
)
}
```
```tsx
// app/products/[id]/page.tsx
import { Suspense } from 'react'
import { Collapsible } from '@/app/components/collapsible'
import { getProduct } from '@/lib/products'
async function Specs({ id }: { id: string }) {
const product = await getProduct(id)
return <p>{product?.description}</p>
}
export default async function Page({ params }: { params: Promise<{ id: string }> }) {
const { id } = await params
return (
<Collapsible title="Specifications">
<Suspense fallback={<p>Loading specifications...</p>}>
<Specs id={id} />
</Suspense>
</Collapsible>
)
}
```
## Protect server-only modules
Add `import 'server-only'` at the top of modules that read secrets or the database. A Client Component that imports them then fails at build time instead of leaking code. Installing the `server-only` package is optional in Next.js but some lint setups require it.
## Context providers
Context only works in Client Components. Create a `'use client'` provider that accepts `children` and render it as deep in the tree as possible (around `{children}`, not around `<html>`).
## Streaming
- Wrap slow, independent parts in `<Suspense>` with a meaningful fallback; one boundary per independently loading region.
- To start a request on the server and read it in a Client Component, pass the un-awaited promise as a prop and read it with `use()` inside a `<Suspense>` boundary.
## Verify before finishing
- `npx tsc --noEmit` and `npx next build` pass. A `server-only` import reached from client code fails the build; a non-serializable prop errors when the page renders, so also load each changed page in `next dev`.
- `grep -rln "'use client'" app components` lists only interactive leaves and providers; no page or layout gained the directive.
- Inspect the page source: the RSC payload contains no fields you did not intend to send.
Ask for a small change to one matching file. Check the tool's loaded context or instruction view, inspect the diff for the requested convention, and run the relevant build or test. A well-formatted file does not guarantee the model follows every instruction.
Cursor's docs recommend keeping individual rules under 500 lines and splitting large rules into composable ones. Cursor's documentation states that it supports AGENTS.md in the project root and in subdirectories. Use AGENTS.md for plain-Markdown instructions you also want other tools to read, and .mdc rules when you need Cursor's scoping options (globs, description-based selection, always-apply).
Documentation checked 2026-11-05: Cursor docs: Rules
Cursor rules for Next.js 14+ App Router projects updated for Next.js 16 with async params, validated Route Handlers, generateMetadata and proxy.ts in place of middleware.
Cursor rules for strict TypeScript: compiler checks beyond strict, interfaces vs type aliases, real type guards, discriminated unions, utility types and branded IDs.
Cursor rules for Tailwind CSS v4: automatic class sorting, mobile-first breakpoints, CVA variants with merging, class-based dark mode via @custom-variant, and CSS-first @theme configuration.