Analyze routes and recommend whether to use Server Actions or API routes based on use case patterns including authentication, revalidation, external API calls, and client requirements...
Analyze existing routes and recommend whether to use Server Actions or traditional API routes based on specific use case patterns, including authentication flows, data revalidation, external API calls, and client requirements.
To analyze current route architecture:
scripts/analyze_routes.py for automated analysisBased on analysis, provide recommendations using the decision matrix from references/decision_matrix.md:
When refactoring is recommended:
Prefer Server Actions for:
revalidatePath() or revalidateTag()Benefits:
Prefer API routes for:
Benefits:
Use the analysis script to scan your codebase:
python scripts/analyze_routes.py --path /path/to/app --output analysis-report.md
The script identifies:
Consult references/decision_matrix.md for detailed decision criteria:
For each route, determine:
For routes requiring changes:
Current: API route with fetch from client Recommended: Server Action Reason: Simpler, built-in CSRF protection, progressive enhancement
// Before (API Route)
// app/api/entities/route.ts
export async function POST(request: Request) {
const data = await request.json();
await db.entity.create(data);
return Response.json({ success: true });
}
// After (Server Action)
// app/actions.ts
'use server';
export async function createEntity(formData: FormData) {
await db.entity.create(Object.fromEntries(formData));
revalidatePath('/entities');
}
Current: Client-side fetch to external API (exposes keys) Recommended: API route Reason: Hide API keys, rate limiting, response transformation
// Recommended: API Route
// app/api/external-service/route.ts
export async function GET(request: Request) {
const { searchParams } = new URL(request.url);
const query = searchParams.get('query');
const response = await fetch(`https://api.external.com?key=${process.env.API_KEY}&q=${query}`);
const data = await response.json();
return Response.json(data);
}
Current: None (new feature) Recommended: API route Reason: External service calls, needs public URL
// Recommended: API Route
// app/api/webhooks/stripe/route.ts
export async function POST(request: Request) {
const signature = request.headers.get('stripe-signature');
const body = await request.text();
// Verify webhook signature
// Process webhook event
return Response.json({ received: true });
}
Current: API route with manual cache invalidation Recommended: Server Action Reason: Built-in revalidation, simpler code
// Before (API Route)
// app/api/entities/[id]/route.ts
export async function PATCH(request: Request, { params }) {
const data = await request.json();
await db.entity.update({ where: { id: params.id }, data });
revalidatePath('/entities');
return Response.json({ success: true });
}
// After (Server Action)
// app/actions.ts
'use server';
export async function updateEntity(id: string, data: any) {
await db.entity.update({ where: { id }, data });
revalidatePath('/entities');
revalidateTag(`entity-${id}`);
}
Current: API middleware Recommended: Server Action for mutations, API route for public endpoints Reason: Simpler auth in Server Components
// Server Action (for mutations)
'use server';
export async function protectedAction() {
const session = await auth();
if (!session) throw new Error('Unauthorized');
// Perform action
}
// API Route (for public/external access)
export async function POST(request: Request) {
const token = request.headers.get('authorization');
if (!validateToken(token)) {
return Response.json({ error: 'Unauthorized' }, { status: 401 });
}
// Process request
}
Automated route analysis tool that scans your Next.js app directory to identify route patterns, Server Actions, authentication usage, revalidation calls, and external API integrations. Generates a detailed report with recommendations.
Comprehensive decision matrix with detailed criteria for choosing between Server Actions and API routes. Includes use case patterns, trade-offs, performance considerations, security implications, and real-world examples.
Common patterns in worldbuilding applications:
Consult references/decision_matrix.md for detailed analysis of each pattern.