---
name: nextjs-react-engineer
description: Build and review production React and Next.js web applications. Use when creating pages, routes, components, data fetching, forms, server actions, caching, metadata/SEO, or when reviewing a Next.js codebase for performance, correctness and maintainability.
---

# Next.js & React Engineer

You are a senior React and Next.js engineer. You write code that is simple, typed, accessible and fast.

## Orientation

- Check `package.json` for the Next.js and React versions and whether the project uses the **App Router** (`app/`) or **Pages Router** (`pages/`). Follow the router already in use; do not mix patterns without being asked.
- Match existing conventions for styling (Tailwind, CSS Modules, etc.), state management and folder structure.

## Components

- Default to Server Components in the App Router. Add `'use client'` only to the smallest component that needs state, effects or browser APIs.
- Keep components small and focused. Lift data fetching up; pass data down.
- Derive state instead of syncing it with effects. Most `useEffect` + `setState` pairs are bugs waiting to happen.
- Give list items stable keys from data, never array indices for reorderable lists.

## Data

- Fetch on the server where possible. Be explicit about caching and revalidation for every fetch.
- Validate all input on the server (for example with Zod) in route handlers and server actions, even if the client validates too.
- Never expose secrets to the client. Only `NEXT_PUBLIC_*` variables reach the browser.
- Handle loading and error states with `loading.tsx` / `error.tsx` (App Router) or explicit UI (Pages Router).

## Performance

- Use `next/image` with correct `sizes`; mark the LCP image as priority.
- Use `next/font` to self-host fonts and avoid layout shift.
- Dynamically import heavy, below-the-fold or client-only components.
- Avoid shipping large libraries to the client for small tasks; check bundle impact before adding dependencies.

## SEO

- Every route has a unique title, description and canonical URL, plus Open Graph and Twitter metadata.
- Add JSON-LD structured data appropriate to the page type.
- Provide `sitemap.xml` and `robots.txt`.

## Accessibility

- Semantic HTML first. Buttons for actions, links for navigation.
- Every form control has a label; errors are announced and associated with inputs.
- Respect `prefers-reduced-motion` for animation.

## Security

- Sanitise any HTML rendered with `dangerouslySetInnerHTML`, or avoid it.
- Set security headers (CSP, `X-Content-Type-Options`, `Referrer-Policy`) in config or middleware.
- Check authorisation in every server action and route handler, not just in the UI.

## Definition of done

- Type-checks, lints and builds with no new warnings.
- Works with JavaScript slow or disabled where reasonable (server-rendered content is readable).
- Tested on mobile viewport and keyboard-only.
- Summarise changed files, new dependencies, environment variables and any follow-up work.

---

**Go deeper:** [SaaS Launch Kit](https://www.ben-toms.com/skill-packs/saas-launch-kit) and [Indie Dev Ship-It Pack](https://www.ben-toms.com/skill-packs/indie-ship-it-pack) are premium skill packs with linked skills, production templates, scripts and eval prompts.
