Next.js
nextjsWeb42 lines
React framework with server components, file-based routing and a build-in server runtime.
Current — as published in v1.3.2. Written by hand for Next.js, not generated.
The rule
Server by default
- Components are Server Components unless the file starts with
'use client'. - Add
'use client'as far down the tree as possible. On a layout it turns the entire subtree into client code and the bundle balloons. - Data fetching, database access and secrets belong in Server Components.
Environment variables
NEXT_PUBLIC_*is inlined into the browser bundle. Everything else is server-only.- Never add the prefix to fix an
undefinedin a client component — that publishes the value to every visitor. Move the code that reads it to the server instead. - Mark server-only modules with
import 'server-only'so a wrong import fails the build rather than leaking.
Route handlers and Server Actions
- Both are public HTTP entry points.
'use server'means "runs on the server", not "only my code can call it". - Validate input and check authorisation in every one, the same as a REST endpoint.
Caching
- Be explicit:
next: { revalidate: n }orcache: 'no-store'. - Reading cookies or headers opts a route out of static rendering — know which routes are dynamic rather than finding out in production.
Rendering
- No
Date.now(),Math.random()orwindowduring render — hydration mismatches. next/imagewith explicit dimensions for anything above the fold.
Never
- Never put a secret in a client component or a
NEXT_PUBLIC_*variable. - Never suppress a type error to get a build through.
As each tool receives it
The same guidance, in the shape each editor reads. Cursor and Claude Code are written separately by hand; the rest are derived from the Cursor rule.
.cursor/rules/nextjs.mdc
---
description: Next.js conventions
globs: ["app/**", "src/app/**", "next.config.*", "**/*.tsx"]
alwaysApply: false
---
# Next.js
## Server by default
- Components are Server Components unless the file starts with `'use client'`.
- Add `'use client'` as far down the tree as possible. On a layout it turns the
entire subtree into client code and the bundle balloons.
- Data fetching, database access and secrets belong in Server Components.
## Environment variables
- `NEXT_PUBLIC_*` is **inlined into the browser bundle**. Everything else is
server-only.
- Never add the prefix to fix an `undefined` in a client component — that
publishes the value to every visitor. Move the code that reads it to the
server instead.
- Mark server-only modules with `import 'server-only'` so a wrong import fails
the build rather than leaking.
## Route handlers and Server Actions
- Both are **public HTTP entry points**. `'use server'` means "runs on the
server", not "only my code can call it".
- Validate input and check authorisation in every one, the same as a REST
endpoint.
## Caching
- Be explicit: `next: { revalidate: n }` or `cache: 'no-store'`.
- Reading cookies or headers opts a route out of static rendering — know which
routes are dynamic rather than finding out in production.
## Rendering
- No `Date.now()`, `Math.random()` or `window` during render — hydration
mismatches.
- `next/image` with explicit dimensions for anything above the fold.
## Never
- Never put a secret in a client component or a `NEXT_PUBLIC_*` variable.
- Never suppress a type error to get a build through.
What else this module writes
Selecting Next.js contributes more than a rule file — all of it merged with every other module you pick, with conflicts resolved rather than duplicated.
Environment
NEXT_PUBLIC_APP_URLrequiredPublic URL of this deployment, used for absolute links and redirects.NEXT_PUBLIC_API_URLoptionalBase URL of an external API called from the browser.NODE_ENVoptionalSet by Next.js. Do not override it manually.Dependencies
next^16.3.0react^19.2.3react-dom^19.2.3@types/react^19.2.0dev@types/react-dom^19.2.0devserver-only^0.0.1
Folders
app/src/components/src/lib/src/services/public/
Related
Put this in your repo
Adds Next.js to a project this tool generated, without starting over. Files you have hand-edited are left alone.