---
name: configure-nextjs-server-components
description: Uses current Next.js routing as intended - Server Components by default, a small client boundary, streaming with Suspense, and Server Actions for mutations. Use when the user asks about server versus client components, server actions, streaming, or a route segment.
license: MIT
metadata:
  category: frontend
  tags: "nextjs,server-components"
  author: johnny-bravo
  version: "1.0"
  updated: "2026-11-07"
  status: published
  aliases: "use-nextjs-app-router-patterns, server components, server actions, route segment"
  source_url: "https://github.com/wshobson/agents/tree/7e2fb559cf4b824b27f1fec49fe88f7511066d26/plugins/frontend-mobile-development/skills/nextjs-app-router-patterns"
  source_license: MIT
  copyright: "Copyright (c) 2024 Seth Hobson"
  original_author: "Seth Hobson (wshobson/agents, nextjs-app-router-patterns)"
  modified: "Adapted by TheSkillVault: rewritten as a short checklist for current Next (server by default, explicit cache, server actions); dropped the file-tree catalogue; added secret rules, gotchas and done-when."
  imported_on: "2026-11-07"
---

# Configure Next.js server components

Route files are Server Components unless you opt into the browser. Put `"use client"` on the smallest interactive leaf, not on the layout.

## When to use
- A whole page is marked `"use client"` and the bundle is mostly JavaScript.
- A form should submit on the server without a hand-written API route.
- A heavy section blocks the rest of the page instead of streaming in.

## When not to use
- You still need to create the project, add a CSS framework and push the first commit. That is a setup task.
- The build fails with a type or import error. Fix the build first.
- You want to host the site. Hosting is a separate step.

## Steps
1. Keep `page.tsx` and `layout.tsx` as Server Components. Fetch data there with `async` and `await`.
2. Add `"use client"` only on the leaf that needs state, effects or browser events. Pass it plain props, not functions from the server and not secrets.
3. From Next.js 15 onward, `fetch` is not cached unless you say so. Set `cache: "force-cache"` or a revalidate time only for data that can be shared. On older Next 14, the default was the opposite, so check the version before you copy a snippet.
4. Wrap a heavy child in `<Suspense fallback={...}>` and add `loading.tsx` for the route. The rest of the page can paint first.
5. Add `error.tsx` so a thrown error shows a message instead of a blank screen. It must be a client component. The framework already insists on that.
6. For a mutation, prefer a Server Action in a file that starts with `"use server"`. Check the session, validate the input, then write. Return a small result the form can show.
7. Read dynamic data (cookies, headers, a search param) only in the route that needs it, so the static parts of the layout stay static.

```tsx
async function storeName(formData: FormData) {
  "use server";
  const name = String(formData.get("name") ?? "").trim();
  if (!name) return { error: "Name is missing" };
  await db.user.update({ name });
  return { ok: true };
}
```

## Gotchas
- A secret, database URL or service token in a client component is sent to the browser. Keep it in the server action or a route handler.
- Importing a client component into a server file is allowed. Importing a server component into a client file is not. Pass the server tree as `children` instead.
- Server Actions are public endpoints. Check auth on every call. Do not trust a hidden field for the user id.
- `cookies()` and `headers()` opt the route into dynamic rendering. Do not call them in a layout that should stay static.

## Portability
This is specific to Next.js server components and server actions. If you use Remix or SvelteKit, the same split applies: load data on the server, keep the interactive leaf small, and validate every mutation. The file names differ.

## Done when
- Pages and layouts are server components, and `"use client"` is only on interactive leaves.
- A heavy block streams behind Suspense instead of blocking the page.
- Mutations check auth and validate input on the server, and no secret reaches the client bundle.

## Example prompt
"This dashboard page is all use client. Move the data fetch to a Server Component and leave only the filter buttons on the client."

---
Adapted from the Next.js router-patterns skill in wshobson/agents (MIT, Copyright (c) 2024 Seth Hobson). See LICENSE.txt.
