Skip to content
Back to skills

Next Js Clean Code

ASecurity

Audits a Next.js App Router codebase, or one feature, for clean-code architecture and writes one report. Checks who owns client state (zustand), where server state lives (TanStack Query), container vs dumb-leaf components, feature folders and barrels, hydration of persisted stores, i18n, and accessibility, then writes CLEAN_CODE_AUDIT.md with rule IDs, file and line evidence, severity, fixes, and an ordered refactor roadmap. Use when asked to audit, scan, or clean up a Next.js or React projec...

  • 3 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added December 3, 2026
developmenttypescriptpythongoreactnextjsnodegitapi

Works with

  • claude code
  • cli
  • api

Security analysis

A100/100

Pro scans all 10 files and shows the line behind each finding

Scanned December 3, 2026

npx -y skills add ezzatmakar/ai-code-skills --skill next-js-clean-code --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Next Js Clean Code?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Next Js Clean Code
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/ezzatmakar-next-js-clean-code/badge)](https://www.skillsdirectory.com/skills/ezzatmakar-next-js-clean-code)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: next-js-clean-code
description: Audits a Next.js App Router codebase, or one feature, for clean-code architecture and writes one report. Checks who owns client state (zustand), where server state lives (TanStack Query), container vs dumb-leaf components, feature folders and barrels, hydration of persisted stores, i18n, and accessibility, then writes CLEAN_CODE_AUDIT.md with rule IDs, file and line evidence, severity, fixes, and an ordered refactor roadmap. Use when asked to audit, scan, or clean up a Next.js or React project or feature, review its state management or data fetching, plan a feature refactor ("move this wizard into one store", "is our zustand and react-query setup clean", "how should I restructure features/checkout"), or check code against its own CLAUDE.md or AGENTS.md, even when the user only says "audit this project". Project rules win over generic ones. Read-only on source. Not for PR or diff review (use nextjs-pr-review), Core Web Vitals (use web-perf-audit), or comment wording (use rephrase-code-comments).
license: MIT
compatibility: Requires git, node, and python3 (3.8+, standard library only). Built for Next.js 13–16 App Router with React 18/19 and TypeScript; the state, data, and component rules also apply to Vite or Remix React apps that use zustand and TanStack Query. Designed for Claude Code, Codex, and OpenCode using the Agent Skills open standard.
metadata:
  author: Ezzat Malak
  version: "1.0.0"
---

# Next.js Clean Code Audit

Audit a Next.js / React codebase for the structural problems that make features hard to change: state kept
in two places, server data copied into client stores, components that fetch, store, and render at once,
features that leak their internals, and persisted state that breaks returning users. The deliverable is one
file, **`CLEAN_CODE_AUDIT.md`**, plus a short chat summary.

This is a **hybrid** skill. `scripts/scan-patterns.py` finds *candidates* quickly and cheaply. You turn
candidates into findings by reading the code, and you find what a regex cannot see: duplicated sources of
truth, wrong ownership, prop drilling, and a refactor order that keeps the app working at every step.

References, loaded only when the scope touches them:

- [references/STATE.md](references/STATE.md): store ownership, server vs client state, middleware order,
  persistence and migrations, selectors, hydration.
- [references/DATA.md](references/DATA.md): query hooks, key factories, the client data path, `enabled`
  guards, debouncing, placeholder data, locale in keys, error paths, server-computed totals.
- [references/COMPONENTS.md](references/COMPONENTS.md): container vs leaf, config-driven steps, shared
  primitives, prop drilling, derived state, i18n keys, choice-group accessibility, dependent choices.
- [references/ARCHITECTURE.md](references/ARCHITECTURE.md): feature folders, barrels, cross-feature
  extension, configured line items, naming, layer rules, tests, hygiene.
- [references/REFACTOR_PLAYBOOK.md](references/REFACTOR_PLAYBOOK.md): the incremental refactor order, a
  worked multi-step wizard example, and the acceptance-criteria pattern.

Helper scripts (resolve `<skill-directory>` from the loaded skill path):

- `<skill-directory>/scripts/detect-stack.sh [root]`: versions, router, path alias, features and barrels,
  and the project rule files that override this skill.
- `<skill-directory>/scripts/scan-patterns.py --root <repo> [--scope src/features/x] [--json]`: candidate
  finder. It prints leads, never findings.
- `<skill-directory>/scripts/validate-report.py CLEAN_CODE_AUDIT.md`: structure and evidence check.

## Non-negotiable output

- Write exactly one report, `CLEAN_CODE_AUDIT.md` at the repository root unless the user names another
  path. Use [assets/CLEAN_CODE_AUDIT_TEMPLATE.md](assets/CLEAN_CODE_AUDIT_TEMPLATE.md).
- Stay **read-only on source**. Don't edit application code, config, tests, or dependencies, and don't
  install anything. The user decides what to fix after reading the report.
- Every finding carries a rule ID, `path:line` evidence you have read yourself, severity, confidence, why it
  matters *in this codebase*, and a concrete fix. A scanner hit you have not opened is not a finding.
- **Project rules win.** Read `CLAUDE.md`, `AGENTS.md`, `CONTRIBUTING.md`, and the lint config before
  judging anything. When a project rule contradicts a rule here (file naming, where client code may fetch,
  whether locale belongs in a query key, a ban on comments), apply the project rule, list the override in
  the report's *Project Rules Applied* section, and never report the project's own convention as a defect.
  A violation of a project rule *is* a finding, cited to that file.
- Rules marked *configurator-only* in the references (payload-shaped stores, dependent choices, configured
  line items) apply only to multi-step builders, wizards, and configurators. Applying them to an ordinary
  page produces noise, so don't.
- In chat, return the health verdict, counts by severity, the five most important findings, and the report
  path. Don't paste the report.
- Never reproduce a secret value. Name the variable and file only.

## Phase 0: Resolve inputs

Echo these back in the report's *Scope and Stack* section.

1. **Scope.** Whole repository (default), a path (`src/features/checkout`), or one feature by name. A
   request like "plan the refactor of X" means feature scope with the roadmap as the main output: keep the
   report's sections, but keep *Findings* to that feature and what its plan depends on, one short paragraph
   each, write *Summary* around the plan, and make *Refactor Roadmap* the longest section. Aim for 6 to 10
   PRs of about five files each, and a report a reviewer reads in ten minutes (roughly 20 KB). Every file a
   step names must exist already or be created by an earlier step.
2. **Mode.** Default **Standard**.
   - **Quick**: scanner plus verification of the STATE and DATA families only, High and Critical findings
     only. For a fast health check.
   - **Standard**: every family across the scope, the scorecard, and conformance for the features the scope
     touches.
   - **Deep**: Standard, plus typecheck, lint, and tests when the scripts exist, a production-bundle check for
     server-only code reaching the browser (Phase 3), conformance for every feature, and the full self-audit.
3. **Report path.** `CLEAN_CODE_AUDIT.md` unless the user gives one.

## Phase 1: Detect the stack and the house rules

1. Run `<skill-directory>/scripts/detect-stack.sh <root>`.
2. Read every project rule file it lists. Write down each rule that changes how a generic rule applies. These
   become the *Project Rules Applied* table.
3. Confirm the versions that change behaviour: Next.js 15+ (async request APIs, uncached `fetch`), zustand
   4 vs 5 (`persist` migrations run only when the stored and configured `version` differ), TanStack Query 5
   (`placeholderData: keepPreviousData`, `skipToken`), React 19 (compiler, `use`).
4. Learn the project's data path before judging any fetch: where server-only code lives, whether client code
   must go through route handlers or Server Actions, and what the shared HTTP client is.

## Phase 2: Scan for candidates

Run `python3 <skill-directory>/scripts/scan-patterns.py --root <root> [--scope <path>]`. Use `--json` when
you want to sort or count. The scanner knows nothing about intent: a `fetch(` inside a route handler is
fine, and the same call in a client hook may not be. Treat its output as a reading list, ordered by the
checks most likely to hide real bugs (migrations, server modules in client files, server data in stores).

## Phase 3: Verify and look beyond the scanner

For each candidate family, open the files and decide: real finding, project-sanctioned, or false positive.
Then read for what regexes miss. Load the matching reference first.

- **State** ([STATE.md](references/STATE.md)). Find every store and every `useState`/`useReducer` that holds
  domain data. For each piece of selection or domain state, ask "who owns this?" Two owners is a finding.
  Check persisted stores for `version`, `migrate`, `partialize`, and storage keys. Check hydration gating
  wherever persisted values render.
- **Data** ([DATA.md](references/DATA.md)). List every query and mutation. Trace each `queryFn` to its network
  call, and confirm client code follows the project's data path. Look for server data written into stores,
  totals computed on the client, missing `enabled` guards, keys that disagree with what the `queryFn`
  reads (locale, payload), and route handlers or clients that turn an upstream failure into a 200.
- **Components** ([COMPONENTS.md](references/COMPONENTS.md)). For large or multi-component files, name the
  responsibilities mixed together. Trace selected values through props: three or more levels is drilling.
  Look for effects that derive state or act as event handlers, and for step logic written as if/else chains.
- **Architecture** ([ARCHITECTURE.md](references/ARCHITECTURE.md)). Map each feature's folder against the
  target layout. Count deep imports past or around barrels, cross-feature reach-ins, naming drift, and the
  project's own layer rules.
- **Deep mode only.** If `next build` can run without secrets, build once, then search
  `.next/static/chunks` for server-only markers: an internal API host, a server module's exported names,
  `server-only`. A hit proves the browser ships server code. Without a build, cap that finding at Medium
  confidence and say why.

Prefer fewer, well-evidenced findings. One root cause is one finding: list its other locations inside it
rather than filing ten copies.

The references don't name every bug. A confirmed defect that no rule names is still a finding: put it in the
closest family with the slug `<family>/other` (for example `data/other`), explain it, and rate it like any
other. Never move a real bug into *Out of Scope and Limits* because no rule covers it. That section holds
only what you couldn't confirm.

## Phase 4: Feature conformance and roadmap

For each feature in scope (every feature in Deep mode):

1. Compare its folder with the target layout in [ARCHITECTURE.md](references/ARCHITECTURE.md), adapted to the
   project's naming rules. Mark each part as present, partial, or missing.
2. For a multi-step builder or wizard, also check the configurator-only rules and the step contract
   (`key`, i18n label, `isValid`, a lookup map, gated navigation).
3. Write the roadmap in the order of [REFACTOR_PLAYBOOK.md](references/REFACTOR_PLAYBOOK.md): contracts,
   types, api and keys, store, hooks, config and shared primitive, container, leaves one at a time, preview,
   the commit action, cleanup. Each step must leave the app working and ship as its own small PR. Attach
   acceptance criteria that a reviewer can check in the browser or the network tab.

## Phase 5: Classify and write the report

Finding IDs by family: `STATE-001`, `DATA-001`, `COMP-001`, `ARCH-001`, `I18N-001`, `A11Y-001`, `TEST-001`,
`HYG-001`. Each finding cites one rule slug from the references, such as `state/persist-hygiene`.

Severity:

- **Critical**: data loss for existing users, or server-only credentials or private data shipped to the
  browser.
- **High**: a confirmed correctness bug or broken contract: a migration that never runs for data users hold,
  a deleted `version` that makes stored state restore empty, two sources of
  truth that already diverge, client-computed money that can disagree with the server, configured items
  merged into one line, a project data-path rule broken in production code.
- **Medium**: structural debt likely to cause the next defect: server data cached in a client store,
  ungated persisted reads, deep imports that bypass a barrel, a component mixing fetch, state, and layout,
  prop drilling of selected values.
- **Low**: localized hygiene: a stray `console.log`, a duplicated helper, a magic number, a dead export.

Confidence: **High** when you read the code path (or ran the check); **Medium** when runtime or
configuration could change the result; **Low** belongs under *Out of Scope and Limits* as an open question,
not in Findings.

Health verdict, from the evidence rather than the count: **Healthy** (no High or Critical), **Needs
attention** (High findings, or Medium findings clustered in one area), **At risk** (any Critical, or High
findings in state ownership or the data path across several features).

Fill [assets/CLEAN_CODE_AUDIT_TEMPLATE.md](assets/CLEAN_CODE_AUDIT_TEMPLATE.md). Keep every section. A family
with nothing to report says "No substantiated findings in the reviewed scope." in the scorecard. Remove every
`{{…}}` token. Code in backticks may contain JSX and generics freely.

## Phase 6: Validate, self-audit, summarize

1. Run `python3 <skill-directory>/scripts/validate-report.py <report-path> --root <repo-root>` and fix every
   error. It also confirms that every cited `path:line` exists, so a guessed folder fails here.
2. Self-audit (brief in Standard, thorough in Deep):
   - Every finding's evidence was opened and still matches the line cited.
   - No finding reports a project convention as a defect.
   - No configurator-only rule was applied outside a configurator.
   - Each fix is proportionate. Don't propose a rewrite where a moved line or a selector fixes it.
   - The roadmap only fixes reported findings. Nothing you dismissed reappears as a roadmap step.
   - The verdict follows the evidence.
3. Confirm `git status` shows no source changes, only the report.
4. Reply in chat with the verdict, severity counts, the top five findings (ID, one line each), and the
   report path.

Files in this skill

  • SKILL.md13.4 KB
  • assets/CLEAN_CODE_AUDIT_TEMPLATE.md2.8 KB
  • references/ARCHITECTURE.md4.8 KB
  • references/COMPONENTS.md5.1 KB
  • references/DATA.md5.5 KB
  • references/REFACTOR_PLAYBOOK.md4.6 KB
  • references/STATE.md6.9 KB
  • scripts/detect-stack.sh4.4 KB
  • scripts/scan-patterns.py28.6 KB
  • scripts/validate-report.py15.3 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.