nextjs-code-review by giuseppe-trisciuoglio/developer-kit
npx skills add https://github.com/giuseppe-trisciuoglio/developer-kit --skill nextjs-code-review此技能为使用 App Router 构建的 Next.js 应用程序提供结构化、全面的代码审查。它根据 Next.js 最佳实践、React 服务器组件模式、缓存策略和生产就绪标准来评估代码。审查会生成按严重性(关键、警告、建议)分类的可操作发现,并提供具体的代码改进示例。
当通过代理系统调用时,此技能会委托 typescript-software-architect-review 代理进行深入的架构分析。
确定范围:确定要审查哪些 Next.js 路由段和组件。使用 glob 来发现 page.tsx、layout.tsx、loading.tsx、、 和 文件。
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
error.tsxroute.tsmiddleware.ts分析组件边界:验证正确的服务器组件 / 客户端组件分离。检查 'use client' 是否仅在必要时放置,并尽可能深地放置在组件树中。确保服务器组件不导入仅限客户端的模块。
审查数据获取:验证获取模式——检查正确的 cache 和 revalidate 选项、使用 Promise.all 的并行数据获取,以及避免请求瀑布流。验证服务器端数据获取不会向客户端暴露敏感数据。
评估缓存策略:审查静态与动态渲染的决策。检查用于静态生成的 generateStaticParams 使用情况、用于按需重新验证的 revalidatePath/revalidateTag,以及 API 路由的正确缓存头。
评估服务器操作:审查表单操作的正确验证(客户端和服务器端)、错误处理、使用 useOptimistic 的乐观更新以及安全性(确保操作不会在未经授权的情况下暴露敏感操作)。
检查中间件:审查中间件的正确请求匹配、身份验证/授权逻辑、响应修改和性能影响。验证它仅在必要的路由上运行。
审查元数据和 SEO:检查 generateMetadata 函数、Open Graph 标签、结构化数据、robots.txt 和 sitemap.xml 配置。验证动态元数据是否针对具有可变内容的页面正确实现。
生成审查报告:生成一份结构化报告,包含按严重性分类的发现(关键、警告、建议)、积极的观察结果,以及带有代码示例的优先建议。
// ❌ 不佳:整个页面被标记为客户端,而实际上只有按钮需要交互性
'use client';
export default async function ProductPage({ params }: { params: { id: string } }) {
const product = await fetch(`/api/products/${params.id}`);
return (
<div>
<h1>{product.name}</h1>
<p>{product.description}</p>
<button onClick={() => addToCart(product.id)}>Add to Cart</button>
</div>
);
}
// ✅ 良好:服务器组件与隔离的客户端组件
// app/products/[id]/page.tsx (服务器组件)
import { AddToCartButton } from './add-to-cart-button';
export default async function ProductPage({ params }: { params: Promise<{ id: string }> }) {
const { id } = await params;
const product = await getProduct(id);
return (
<div>
<h1>{product.name}</h1>
<p>{product.description}</p>
<AddToCartButton productId={product.id} />
</div>
);
}
// app/products/[id]/add-to-cart-button.tsx (客户端组件)
'use client';
export function AddToCartButton({ productId }: { productId: string }) {
return <button onClick={() => addToCart(productId)}>Add to Cart</button>;
}
// ❌ 不佳:顺序数据获取造成瀑布流
export default async function DashboardPage() {
const user = await getUser();
const orders = await getOrders(user.id);
const analytics = await getAnalytics(user.id);
return <Dashboard user={user} orders={orders} analytics={analytics} />;
}
// ✅ 良好:使用适当的 Suspense 边界进行并行数据获取
export default async function DashboardPage() {
const user = await getUser();
const [orders, analytics] = await Promise.all([
getOrders(user.id),
getAnalytics(user.id),
]);
return <Dashboard user={user} orders={orders} analytics={analytics} />;
}
// ✅ 更佳:使用 Suspense 进行流式传输以实现独立部分
export default async function DashboardPage() {
const user = await getUser();
return (
<div>
<UserHeader user={user} />
<Suspense fallback={<OrdersSkeleton />}>
<OrdersSection userId={user.id} />
</Suspense>
<Suspense fallback={<AnalyticsSkeleton />}>
<AnalyticsSection userId={user.id} />
</Suspense>
</div>
);
}
// ❌ 不佳:服务器操作缺少验证或授权
'use server';
export async function deleteUser(id: string) {
await db.user.delete({ where: { id } });
}
// ✅ 良好:具有验证、授权和错误处理的服务器操作
'use server';
import { z } from 'zod';
import { auth } from '@/lib/auth';
import { revalidatePath } from 'next/cache';
const deleteUserSchema = z.object({ id: z.string().uuid() });
export async function deleteUser(rawData: { id: string }) {
const session = await auth();
if (!session || session.user.role !== 'admin') {
throw new Error('Unauthorized');
}
const { id } = deleteUserSchema.parse(rawData);
await db.user.delete({ where: { id } });
revalidatePath('/admin/users');
}
// ❌ 不佳:无缓存控制,每次请求都获取
export default async function BlogPage() {
const posts = await fetch('https://api.example.com/posts').then(r => r.json());
return <PostList posts={posts} />;
}
// ✅ 良好:具有基于时间的重新验证的显式缓存
export default async function BlogPage() {
const posts = await fetch('https://api.example.com/posts', {
next: { revalidate: 3600, tags: ['blog-posts'] },
}).then(r => r.json());
return <PostList posts={posts} />;
}
// 服务器操作中的重新验证
'use server';
export async function publishPost(data: FormData) {
await db.post.create({ data: parseFormData(data) });
revalidateTag('blog-posts');
}
// ❌ 不佳:中间件在所有路由上运行,包括静态资源
import { NextResponse } from 'next/server';
export function middleware(request: NextRequest) {
const session = request.cookies.get('session');
if (!session) {
return NextResponse.redirect(new URL('/login', request.url));
}
}
// 缺少 config.matcher
// ✅ 良好:具有适当匹配器的范围化中间件
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
export function middleware(request: NextRequest) {
const session = request.cookies.get('session');
if (!session) {
return NextResponse.redirect(new URL('/login', request.url));
}
return NextResponse.next();
}
export const config = {
matcher: ['/dashboard/:path*', '/api/protected/:path*'],
};
将所有代码审查发现按以下结构组织:
简要概述,包含总体质量评分(1-10)和关键观察结果。
导致安全漏洞、数据暴露或功能损坏的问题。
违反最佳实践、导致性能问题或降低可维护性的问题。
针对代码组织、性能或开发者体验的改进建议。
已良好实现的模式和值得肯定的良好实践。
具有代码示例的优先后续步骤,以实现最具影响力的改进。
'use client' 边界放置在组件树的深处Promise.all)以避免请求瀑布流revalidatePath/revalidateTag 而不是基于时间的重新验证config.matcher 将中间件范围限定到特定路由generateMetadatagenerateStaticParamsserver-only 包有关详细的审查清单和模式文档,请参阅 references/ 目录:
references/app-router-patterns.md — App Router 最佳实践和模式references/server-components.md — 服务器组件和客户端组件边界指南references/performance.md — Next.js 性能优化清单每周安装数
138
代码仓库
GitHub 星标数
173
首次出现
2026年2月28日
安全审计
安装于
codex124
gemini-cli124
github-copilot121
kimi-cli119
amp119
cline119
This skill provides structured, comprehensive code review for Next.js applications built with the App Router. It evaluates code against Next.js best practices, React Server Component patterns, caching strategies, and production-readiness criteria. The review produces actionable findings categorized by severity (Critical, Warning, Suggestion) with concrete code examples for improvements.
This skill delegates to the typescript-software-architect-review agent for deep architectural analysis when invoked through the agent system.
Identify Scope : Determine which Next.js route segments and components are under review. Use glob to discover page.tsx, layout.tsx, loading.tsx, error.tsx, route.ts, and middleware.ts files.
Analyze Component Boundaries : Verify proper Server Component / Client Component separation. Check that 'use client' is placed only where necessary and as deep in the component tree as possible. Ensure Server Components don't import client-only modules.
Review Data Fetching : Validate fetch patterns — check for proper cache and revalidate options, parallel data fetching with Promise.all, and avoidance of request waterfalls. Verify that server-side data fetching doesn't expose sensitive data to the client.
Evaluate Caching Strategy : Review static vs dynamic rendering decisions. Check generateStaticParams usage for static generation, revalidatePath/revalidateTag for on-demand revalidation, and proper cache headers for API routes.
Assess Server Actions : Review form actions for proper validation (both client and server-side), error handling, optimistic updates with useOptimistic, and security (ensure actions don't expose sensitive operations without authorization).
Check Middleware : Review middleware for proper request matching, authentication/authorization logic, response modification, and performance impact. Verify it runs only on necessary routes.
Review Metadata & SEO: Check generateMetadata functions, Open Graph tags, structured data, robots.txt, and sitemap.xml configurations. Verify dynamic metadata is properly implemented for pages with variable content.
Produce Review Report : Generate a structured report with severity-classified findings (Critical, Warning, Suggestion), positive observations, and prioritized recommendations with code examples.
// ❌ Bad: Entire page marked as client when only a button needs interactivity
'use client';
export default async function ProductPage({ params }: { params: { id: string } }) {
const product = await fetch(`/api/products/${params.id}`);
return (
<div>
<h1>{product.name}</h1>
<p>{product.description}</p>
<button onClick={() => addToCart(product.id)}>Add to Cart</button>
</div>
);
}
// ✅ Good: Server Component with isolated Client Component
// app/products/[id]/page.tsx (Server Component)
import { AddToCartButton } from './add-to-cart-button';
export default async function ProductPage({ params }: { params: Promise<{ id: string }> }) {
const { id } = await params;
const product = await getProduct(id);
return (
<div>
<h1>{product.name}</h1>
<p>{product.description}</p>
<AddToCartButton productId={product.id} />
</div>
);
}
// app/products/[id]/add-to-cart-button.tsx (Client Component)
'use client';
export function AddToCartButton({ productId }: { productId: string }) {
return <button onClick={() => addToCart(productId)}>Add to Cart</button>;
}
// ❌ Bad: Sequential data fetching creates waterfall
export default async function DashboardPage() {
const user = await getUser();
const orders = await getOrders(user.id);
const analytics = await getAnalytics(user.id);
return <Dashboard user={user} orders={orders} analytics={analytics} />;
}
// ✅ Good: Parallel data fetching with proper Suspense boundaries
export default async function DashboardPage() {
const user = await getUser();
const [orders, analytics] = await Promise.all([
getOrders(user.id),
getAnalytics(user.id),
]);
return <Dashboard user={user} orders={orders} analytics={analytics} />;
}
// ✅ Even better: Streaming with Suspense for independent sections
export default async function DashboardPage() {
const user = await getUser();
return (
<div>
<UserHeader user={user} />
<Suspense fallback={<OrdersSkeleton />}>
<OrdersSection userId={user.id} />
</Suspense>
<Suspense fallback={<AnalyticsSkeleton />}>
<AnalyticsSection userId={user.id} />
</Suspense>
</div>
);
}
// ❌ Bad: Server Action without validation or authorization
'use server';
export async function deleteUser(id: string) {
await db.user.delete({ where: { id } });
}
// ✅ Good: Server Action with validation, authorization, and error handling
'use server';
import { z } from 'zod';
import { auth } from '@/lib/auth';
import { revalidatePath } from 'next/cache';
const deleteUserSchema = z.object({ id: z.string().uuid() });
export async function deleteUser(rawData: { id: string }) {
const session = await auth();
if (!session || session.user.role !== 'admin') {
throw new Error('Unauthorized');
}
const { id } = deleteUserSchema.parse(rawData);
await db.user.delete({ where: { id } });
revalidatePath('/admin/users');
}
// ❌ Bad: No cache control, fetches on every request
export default async function BlogPage() {
const posts = await fetch('https://api.example.com/posts').then(r => r.json());
return <PostList posts={posts} />;
}
// ✅ Good: Explicit caching with time-based revalidation
export default async function BlogPage() {
const posts = await fetch('https://api.example.com/posts', {
next: { revalidate: 3600, tags: ['blog-posts'] },
}).then(r => r.json());
return <PostList posts={posts} />;
}
// Revalidation in Server Action
'use server';
export async function publishPost(data: FormData) {
await db.post.create({ data: parseFormData(data) });
revalidateTag('blog-posts');
}
// ❌ Bad: Middleware runs on all routes including static assets
import { NextResponse } from 'next/server';
export function middleware(request: NextRequest) {
const session = request.cookies.get('session');
if (!session) {
return NextResponse.redirect(new URL('/login', request.url));
}
}
// Missing config.matcher
// ✅ Good: Scoped middleware with proper matcher
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
export function middleware(request: NextRequest) {
const session = request.cookies.get('session');
if (!session) {
return NextResponse.redirect(new URL('/login', request.url));
}
return NextResponse.next();
}
export const config = {
matcher: ['/dashboard/:path*', '/api/protected/:path*'],
};
Structure all code review findings as follows:
Brief overview with an overall quality score (1-10) and key observations.
Issues causing security vulnerabilities, data exposure, or broken functionality.
Issues that violate best practices, cause performance problems, or reduce maintainability.
Improvements for code organization, performance, or developer experience.
Well-implemented patterns and good practices to acknowledge.
Prioritized next steps with code examples for the most impactful improvements.
'use client' boundaries as deep in the tree as possiblePromise.all) to avoid request waterfallsrevalidatePath/revalidateTag instead of time-based revalidation when possibleconfig.matchergenerateMetadata for dynamic pages with variable contentgenerateStaticParams for static pages with known parametersserver-only packageSee the references/ directory for detailed review checklists and pattern documentation:
references/app-router-patterns.md — App Router best practices and patternsreferences/server-components.md — Server Component and Client Component boundary guidereferences/performance.md — Next.js performance optimization checklistWeekly Installs
138
Repository
GitHub Stars
173
First Seen
Feb 28, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
codex124
gemini-cli124
github-copilot121
kimi-cli119
amp119
cline119
测试策略完整指南:单元/集成/E2E测试金字塔与自动化实践
11,200 周安装