Nextjs patterns

From event4u-app/agent-config · View interactive version

Nextjs patterns is an AI agent skill from the GitHub repository event4u-app/agent-config. Writes Next.js App Router code — Server Components, Server Actions, RSC boundaries, route handlers, caching, and streaming — matching framework conventions and project architecture.

How to use this skill

Skill file

nextjs-patterns

Compatibility

fall back to plain React skills; ask before introducing App Router.

When to use

Use this skill for Next.js-specific code generation and editing on the App Router:

When to use the analysis sibling

When the task is understanding how a Next.js app boots, where the

server/client boundary lives, what is cached vs streamed, or why a

hydration mismatch fires — defer to project-analysis-nextjs first,

then return here for the edit.

Procedure: write Next.js code

  1. Confirm App Router — app/ directory exists; next.config.{js,ts,mjs} present.
  2. Confirm version — package.json major for next. 14.x vs 15.x differs in

default caching (15.x is uncached by default for fetch) and in cookies() /

headers() being async.

  1. Inspect routing layout — flat app/ vs grouped (app/(marketing)/,

app/(app)/); identify shared layouts and parallel routes.

  1. Inspect data layer — Server Actions vs route handlers vs external API;

ORM (Prisma, Drizzle) or fetch-based.

  1. Inspect styling — Tailwind, CSS Modules, or styled engine; respect the

chosen stack, do not mix.

  1. Check test conventions — Vitest / Jest + RTL for components; Playwright

for E2E; route-handler tests via node runtime.

Server vs Client boundary

component needs state, effects, browser APIs, or event handlers.

can keep a Server Component rendered as a child of a Client Component

by passing it as a prop / children — that pattern keeps server logic out

of the client bundle.

Component or a file imported by one. Use import "server-only" at the top

of a server module to make leaks fail at build time.

server (e.g. browser-only SDKs).

Dates with custom prototypes, no Map / Set — use plain JSON.

Server Actions

pure data fetching — that is what Server Components are for.

the caller is the client; treat it as hostile.

union — never throw across the action boundary for expected errors.

progressive enhancement; the form must work without JS.

Route Handlers

the dependency set supports it.

consistent status codes.

middleware.ts; do not assume any framework default.

export const dynamic = 'force-static' / revalidate = N explicitly.

Caching and revalidation (14.x vs 15.x)

{ cache: 'no-store' } or segment-level export const dynamic = 'force-dynamic'.

{ cache: 'force-cache' } or { next: { revalidate: N } }.

fetch(url, { next: { tags: ['posts'] } }) then revalidateTag('posts').

within a request; respect the difference between request-scoped and

cross-request caching.

Data fetching patterns

layout, to allow per-route streaming.

avoid sequential await chains that block streaming.

data dependency in its own Suspense, not the whole page.

as a global spinner.

server and pass props.

Layouts, templates, error and not-found

per-page logic there.

cannot.

to localize fallbacks.

reset props.

Middleware

request hits a route — use for auth redirects, locale negotiation, A/B.

drivers. Use a session cookie + lightweight verification.

on every asset.

Metadata, performance, env

generateStaticParams for static dynamic-segment generation.

Client Components — never bare <img>, CSS @import Google Fonts, or

unguarded heavy imports.

client bundle — keep the boundary at the leaf.

NEXT_PUBLIC_* and is NOT secret. Validate env at boot (zod) and import

the validated object — never process.env ad hoc.

Output format

  1. Next.js code following App Router conventions and the project's existing layout.
  2. All related files (page, layout, server action, route handler, schema, test) as needed.
  3. When a cache surface changes — point at the invalidation call (revalidateTag / revalidatePath).

Do NOT

subtree into the client bundle.

file — even transitively. Guard with import "server-only".

result union.

re-check.

Gotcha

async in 15.x. Code that ran on 14.x will silently misbehave (Promise

rendered as an object) until awaited.

locale-dependent strings without suppressHydrationWarning or a Client

boundary causes a hydration error.

using useState, useEffect, useRef, or browser globals.

accept FormData. Mixing FormData and typed args breaks progressive

enhancement.

caching behavior in a production build (next build && next start).

segments — bare paths silently miss.

APIs (fs, crypto.createHash) fail at the edge. Pin runtime per route.

Auto-trigger keywords

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.