Skip to content

Next.js Optimization Recipes

Next.js App Router changed the mental model for React applications. Server Components, streaming, parallel routes, intercepting routes — the surface area is huge and AI tools sometimes generate Pages Router patterns by mistake. These recipes produce App Router code exclusively, with correct server/client boundaries and proper data patterns.

  • Server Component and Server Action recipes with correct boundaries
  • Data fetching patterns using RSC, streaming, and Suspense
  • Authentication and middleware recipes for App Router
  • Performance optimization prompts for Core Web Vitals

Recipe 1: Streaming with Independent Suspense Boundaries

Section titled “Recipe 1: Streaming with Independent Suspense Boundaries”

Scenario: Your dashboard page makes 4 independent API calls. They all block rendering until the slowest one resolves.

Expected output: Page with 4 Suspense boundaries, 4 async section components, 4 skeletons, and metadata.


Recipe 2: Server Actions for Form Handling

Section titled “Recipe 2: Server Actions for Form Handling”

Scenario: You need a contact form that validates server-side, handles errors, and shows pending state without a separate API route.

Expected output: Server action, page component, client form component, and tests.


Recipe 3: Authentication with NextAuth.js v5

Section titled “Recipe 3: Authentication with NextAuth.js v5”

Scenario: You need email/password and OAuth authentication with session management and protected routes.

Expected output: Auth config, middleware, route handler, client utilities, components, HOC, and tests.


Recipe 4: Parallel and Intercepting Routes for Modals

Section titled “Recipe 4: Parallel and Intercepting Routes for Modals”

Scenario: Product details should open in a modal from the list but show a full page when accessed directly.

Expected output: Product pages, intercepting route, layout with parallel slots, modal component, and tests.


Scenario: You need low-latency API routes running on the edge with typing, validation, and caching.

Expected output: Route handlers, API utility library, and tests for each endpoint.


Recipe 6: Incremental Static Regeneration for Blog

Section titled “Recipe 6: Incremental Static Regeneration for Blog”

Scenario: Your blog has 500 posts. Full static generation takes 10 minutes. You need ISR.

Expected output: Blog pages with ISR, revalidation webhook, sitemap, OG image generation, and tests.


Recipe 7: Edge Middleware for A/B Testing and Feature Flags

Section titled “Recipe 7: Edge Middleware for A/B Testing and Feature Flags”

Scenario: Split traffic between landing page variants and gate features behind flags at the edge.

Expected output: Middleware, utility functions, variant directories, and tests.


Recipe 8: Dynamic Metadata and Structured Data System

Section titled “Recipe 8: Dynamic Metadata and Structured Data System”

Scenario: Every page needs unique meta tags, dynamic OG images, and JSON-LD structured data.

Expected output: Metadata utility, OG image generation, JSON-LD helpers, robots.ts, sitemap.ts, and tests.


Scenario: Lighthouse performance score is 65. LCP 4.2s, CLS 0.25, FID 180ms.

Expected output: Optimized components, config changes, font setup, Lighthouse CI workflow, and metrics.


Scenario: Your SaaS serves multiple tenants from a single deployment with subdomain-based routing and data isolation.

Expected output: Middleware, tenant utility, provider, scoped queries, themed layout, and isolation tests.


Recipe 11: Plugin Architecture for Extensible Dashboards

Section titled “Recipe 11: Plugin Architecture for Extensible Dashboards”

Scenario: Your dashboard needs user-installed plugins that add pages, API routes, and UI components.

Expected output: Plugin interface, registry, catch-all routes, sidebar integration, installation flow, and tests.


Recipe 12: End-to-End Type Safety with tRPC

Section titled “Recipe 12: End-to-End Type Safety with tRPC”

Scenario: Your 20 API endpoints have types that are always out of sync with the frontend.

Expected output: tRPC setup, routers, handler, typed client, React Query integration, and type-safety verification.