Next.js App Router and Server Components
Core concepts
| Concept | Description |
|---|---|
| App Router | Routes are defined by folders under app/: page.tsx for pages, layout.tsx for shared layouts, loading.tsx for loading states, error.tsx for error boundaries |
| Server Component (default) | Runs only on the server, can read the database / call the backend directly, and its code is never sent to the browser |
| Client Component | "use client" at the top of the file; can use state, effects, events and browser APIs |
| Route Handler | app/api/xxx/route.ts, exporting functions such as GET/POST to write HTTP endpoints |
| Server Action / Server Function | An async function marked "use server" that can be called directly from a form or the client to perform server-side mutations |
proxy.ts (Next.js 16) | Replaces the old middleware.ts; runs before a request reaches the route (redirects, auth checks, etc.) on the Node.js runtime |
How it works: the Server / Client boundary
Server Component tree
├─ awaits data directly (database, internal APIs)
├─ renders to an RSC Payload (a serialized component tree) sent to the browser
└─ on hitting a "use client" component: only its props are sent (they must be serializable); the browser loads its JS and hydrates it"use client"marks a boundary: that file and every module it imports go into the client bundle. So put it as low as possible, on "leaf" components.- A Server Component can pass Server Components as
childrento a Client Component, embedding server-rendered content inside a client component. - Props passed to a Client Component can't be non-serializable values such as functions (except Server Actions) or class instances.
In practice
tsx
// app/tasks/page.tsx — Server Component, fetches data directly
import { TaskTable } from './task-table'
export default async function TasksPage() {
const res = await fetch(`${process.env.API_URL}/tasks`, { cache: 'no-store' })
const tasks = await res.json()
return <TaskTable initialTasks={tasks} />
}tsx
// app/tasks/task-table.tsx — only the interactive part is a Client Component
'use client'
import { useState } from 'react'
export function TaskTable({ initialTasks }: { initialTasks: Task[] }) {
const [status, setStatus] = useState('all')
// filtering, selection, opening details and other interactions
}ts
// app/tasks/actions.ts — Server Action: submit a human review result
'use server'
import { revalidatePath } from 'next/cache'
export async function submitReview(taskId: string, fields: unknown) {
await fetch(`${process.env.API_URL}/tasks/${taskId}/review`, { method: 'POST', body: JSON.stringify(fields) })
revalidatePath(`/tasks/${taskId}`)
}Division of labour with FastAPI: Next.js handles pages and the BFF (auth forwarding, aggregation); business logic and LLM calls live in FastAPI.
Common pitfalls
"use client"in the root layout: the whole tree becomes client components and you lose the benefits of RSC.- Server Actions without auth checks: a Server Action is essentially a public POST endpoint, so it must verify the user and permissions inside the function.
- Leaking secrets to the client: only environment variables prefixed with
NEXT_PUBLIC_are exposed to the browser; never give server secrets that prefix. - Middleware stops working after upgrading to 16:
middleware.tshas been renamed toproxy.ts(an official codemod is provided).
Common interview questions
- What's the difference between Server Components and Client Components? Where should
"use client"go? - How does RSC differ from traditional SSR?
- What is a Server Action? What security considerations apply?
- How do you choose between a Route Handler and a Server Action?
- What are the major changes in Next.js 16?
How it relates to my projects
- AI Document Review Pipeline: the task list page (RSC fetches data) + the review console (Client Component interactions + Server Action submission).
To be verified / may go stale
- Every major Next.js version brings breaking changes. This page is written for Next.js 16 (released 2025-10); read the official upgrade guide before upgrading.
