Copilot instructions for Next.js App Router code with awaited params, authenticated Server Actions, typed Route Handlers and explicit caching.
.github/instructions/github-copilot-nextjs-development.instructions.md. Downloaded files need to be placed in that repository path.---
applyTo: "**/*"
---
# Next.js App Router Instructions
Applies to: `app/` routes, Server Actions and Route Handlers in a Next.js App Router project.
Version baseline: checked against the Next.js 16.3 documentation. Read `package.json` first; on Next.js 14 `params` is a plain object, and on 15+ it is a Promise. Follow the installed version.
## Server and Client Components
- Components in `app/` are Server Components by default. Add `'use client'` only to the smallest component that needs state, effects, event handlers or browser APIs.
- Props passed from a Server Component to a Client Component must be serializable (no functions except Server Actions, no class instances).
- `params` and `searchParams` are Promises in Next.js 15+; await them.
```tsx
// app/products/[id]/page.tsx (Server Component)
import { notFound } from 'next/navigation';
import { getProduct } from '@/lib/products';
import { AddToCart } from './add-to-cart'; // 'use client' component
export default async function ProductPage({
params,
}: {
params: Promise<{ id: string }>;
}) {
const { id } = await params;
const product = await getProduct(id);
if (!product) notFound();
return (
<main>
<h1>{product.name}</h1>
<p>{product.description}</p>
<AddToCart productId={product.id} />
</main>
);
}
```
## Server Actions
Every Server Action is reachable as a POST request, even if the form is only rendered for signed-in users. Inside each action: authenticate, validate input, authorize, then mutate.
```tsx
// app/orders/actions.ts
'use server';
import { revalidatePath } from 'next/cache';
import { redirect } from 'next/navigation';
import { getSession } from '@/lib/auth';
import { db } from '@/lib/db';
export async function createOrder(formData: FormData) {
const session = await getSession();
if (!session) throw new Error('Unauthorized');
const productId = formData.get('productId');
if (typeof productId !== 'string' || productId.length === 0) {
throw new Error('productId is required');
}
await db.order.create({ data: { productId, userId: session.userId } });
revalidatePath('/orders');
redirect('/orders');
}
```
- Get the user from the server-side session, never from a form field.
- Call `redirect()` outside `try/catch`; it works by throwing.
## Route Handlers
Use `app/**/route.ts` for webhooks, public APIs and non-React clients. For your own UI mutations, prefer Server Actions.
```ts
// app/api/products/[id]/route.ts
import { NextResponse, type NextRequest } from 'next/server';
import { getProduct } from '@/lib/products';
export async function GET(
_request: NextRequest,
{ params }: { params: Promise<{ id: string }> },
) {
const { id } = await params;
const product = await getProduct(id);
if (!product) {
return NextResponse.json({ error: 'Not found' }, { status: 404 });
}
return NextResponse.json(product);
}
```
## Caching and metadata
- `fetch` is not cached by default in current Next.js. Opt in deliberately: use `'use cache'` with `cacheLife` when `cacheComponents` is enabled, or the project's existing caching model otherwise.
- Use `generateMetadata` for dynamic titles and descriptions, and await `params` inside it.
- In Next.js 16 `middleware.ts` is renamed to `proxy.ts`. Use it only for optimistic redirects, not as the only authorization check.
## Verify before finishing
Run these when they are available in the project:
- `npx tsc --noEmit` (wrong `params` types fail here)
- `npm run lint` and `npm run build`
- A test or manual request showing that the action rejects a request with no session
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.
CLAUDE.md rules for the Next.js 16 App Router with async params, streaming via Suspense, secured Server Actions, metadata conventions and explicit caching choices.
Copilot instructions for TypeScript project setup covering strict tsconfig flags and their effects, type-only imports, module hygiene and TSDoc for public APIs.
Copilot
fullstack
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
A Next.js 16 App Router repository keeps these instructions for Copilot code review and the cloud agent while a developer adds a product page and an order action.
Create app/products/[id]/page.tsx that shows a product from getProduct(id) with an AddToCart client button, and a createOrder Server Action that records an order for the signed-in user and returns to /orders.
Illustrative; not a recorded model run. A Server Component page typed with params: Promise<{ id: string }> that awaits params and calls notFound() for a missing product; only AddToCart marked 'use client'; a 'use server' action that reads the session server-side, rejects with no session, validates productId with a typeof check instead of an 'as string' cast, writes the order with the session user ID, then revalidatePath and redirect outside any try/catch.GitHub Copilot reads repository-wide custom instructions from .github/copilot-instructions.md and path-specific instructions from .github/instructions/*.instructions.md files with an applyTo glob. Copilot can also use AGENTS.md files, where the nearest file in the directory tree takes precedence. Which file types are honored depends on the surface: VS Code, Visual Studio, JetBrains, Xcode, Eclipse and GitHub.com differ.
.github/instructions/github-copilot-nextjs-development.instructions.mdStarting point, not a tested configuration for your repository. Used for files matching applyTo on surfaces that support path-specific instructions. Review the instructions, adapt the file paths and run your project's existing checks.
---
applyTo: "**/*"
---
# Next.js App Router Instructions
Applies to: `app/` routes, Server Actions and Route Handlers in a Next.js App Router project.
Version baseline: checked against the Next.js 16.3 documentation. Read `package.json` first; on Next.js 14 `params` is a plain object, and on 15+ it is a Promise. Follow the installed version.
## Server and Client Components
- Components in `app/` are Server Components by default. Add `'use client'` only to the smallest component that needs state, effects, event handlers or browser APIs.
- Props passed from a Server Component to a Client Component must be serializable (no functions except Server Actions, no class instances).
- `params` and `searchParams` are Promises in Next.js 15+; await them.
```tsx
// app/products/[id]/page.tsx (Server Component)
import { notFound } from 'next/navigation';
import { getProduct } from '@/lib/products';
import { AddToCart } from './add-to-cart'; // 'use client' component
export default async function ProductPage({
params,
}: {
params: Promise<{ id: string }>;
}) {
const { id } = await params;
const product = await getProduct(id);
if (!product) notFound();
return (
<main>
<h1>{product.name}</h1>
<p>{product.description}</p>
<AddToCart productId={product.id} />
</main>
);
}
```
## Server Actions
Every Server Action is reachable as a POST request, even if the form is only rendered for signed-in users. Inside each action: authenticate, validate input, authorize, then mutate.
```tsx
// app/orders/actions.ts
'use server';
import { revalidatePath } from 'next/cache';
import { redirect } from 'next/navigation';
import { getSession } from '@/lib/auth';
import { db } from '@/lib/db';
export async function createOrder(formData: FormData) {
const session = await getSession();
if (!session) throw new Error('Unauthorized');
const productId = formData.get('productId');
if (typeof productId !== 'string' || productId.length === 0) {
throw new Error('productId is required');
}
await db.order.create({ data: { productId, userId: session.userId } });
revalidatePath('/orders');
redirect('/orders');
}
```
- Get the user from the server-side session, never from a form field.
- Call `redirect()` outside `try/catch`; it works by throwing.
## Route Handlers
Use `app/**/route.ts` for webhooks, public APIs and non-React clients. For your own UI mutations, prefer Server Actions.
```ts
// app/api/products/[id]/route.ts
import { NextResponse, type NextRequest } from 'next/server';
import { getProduct } from '@/lib/products';
export async function GET(
_request: NextRequest,
{ params }: { params: Promise<{ id: string }> },
) {
const { id } = await params;
const product = await getProduct(id);
if (!product) {
return NextResponse.json({ error: 'Not found' }, { status: 404 });
}
return NextResponse.json(product);
}
```
## Caching and metadata
- `fetch` is not cached by default in current Next.js. Opt in deliberately: use `'use cache'` with `cacheLife` when `cacheComponents` is enabled, or the project's existing caching model otherwise.
- Use `generateMetadata` for dynamic titles and descriptions, and await `params` inside it.
- In Next.js 16 `middleware.ts` is renamed to `proxy.ts`. Use it only for optimistic redirects, not as the only authorization check.
## Verify before finishing
Run these when they are available in the project:
- `npx tsc --noEmit` (wrong `params` types fail here)
- `npm run lint` and `npm run build`
- A test or manual request showing that the action rejects a request with no session
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.
Instruction support differs by surface: VS Code supports repository-wide, path-specific and AGENTS.md instructions; Visual Studio supports repository-wide and path-specific; JetBrains and Xcode use .github/copilot-instructions.md. GitHub's documentation describes AGENTS.md files stored anywhere in the repository, with the nearest file in the directory tree taking precedence, or alternatively a single CLAUDE.md or GEMINI.md in the root. Support differs by surface; GitHub's IDE page lists AGENTS.md among the instruction types for VS Code.
Documentation checked 2026-11-05: GitHub Docs: Adding repository custom instructionsGitHub Docs: Repository instructions in your IDE
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.
CLAUDE.md rules for the Next.js 16 App Router with async params, streaming via Suspense, secured Server Actions, metadata conventions and explicit caching choices.
Copilot instructions for TypeScript project setup covering strict tsconfig flags and their effects, type-only imports, module hygiene and TSDoc for public APIs.