Why this matters
Next.js is the most common framework for React products and AI apps. The App Router changes where code runs, which affects performance, cost and security. Mistakes here leak secrets to the browser or make pages slow.
Core ideas
- Server components (the default) render on the server, can read databases and secrets, and send HTML, not their JavaScript, to the browser.
- Client components (
"use client") run in the browser for interactivity: state, effects, event handlers. - Rendering modes: static (built once, served from a CDN), dynamic (per request), streamed (send the shell first, then parts as they're ready with
Suspense). - Route handlers (
app/api/.../route.ts) are backend endpoints inside the app. Server actions let forms call server functions directly. - Static export (
output: "export") produces plain files with no server at all; this guide is built that way. - Version-matched docs ship inside the
nextpackage (node_modules/next/dist/docs/); read those for the version you're on.
How to do it
// app/orders/page.tsx: a server component, no "use client"
import { getOrdersForUser } from "@/server/orders"; // server-only module
import { OrderFilters } from "./OrderFilters"; // a client component
export default async function OrdersPage() {
const orders = await getOrdersForUser(); // runs on the server, checks auth
return <><OrderFilters /><OrderTable orders={orders} /></>;
}
Rules: keep secrets and database access in server-only modules (the server-only package makes importing them from client code a build error); pass client components only the data they need; validate every route handler and server action input like any public API, because they are public APIs.
Architecture note
In larger systems the frontend never talks to the database directly; it goes through the backend API → service → repository chain. Next.js server code can be that backend for small products, or a thin layer calling a separate Python API for AI-heavy ones. Decide deliberately.
Why this way
Server components cut JavaScript shipped to the browser and keep data access close to the database. Static rendering is fastest and cheapest; reserve dynamic rendering for pages that truly differ per request.
Common mistakes
- Putting
"use client"at the top of everything, shipping the whole app to the browser. - Reading
process.env.SECRETin a client component (it's either undefined or, if prefixedNEXT_PUBLIC_, published to everyone). - Trusting server-action input because "only our form calls it".
Interview follow-ups
- "How do you stream an LLM response in Next.js?" A route handler returning a
ReadableStream(or the AI SDK's stream helpers) on the Node runtime, consumed by a client component; cancellation viaAbortControllerandrequest.signal. - "Server action or route handler?" Server actions for form mutations from your own UI; route handlers for public or third-party APIs, webhooks and streaming endpoints. Both need validation and auth.
- "How does caching work in the App Router?" It differs by Next.js version. Read the version-matched docs in
node_modules/next/dist/docs/; be explicit about static vs dynamic rendering and revalidation rather than relying on defaults. - "How do you keep the frontend from touching the database?" Data access lives in server-only modules or a separate backend API; client components receive only the data they render.
Check yourself
What's the difference between a server and a client component?
Server components run only on the server, can access secrets and data directly, and ship no JavaScript for themselves. Client components run in the browser (and pre-render on the server), and are needed for state, effects and event handlers.
Why must server actions validate input?
They're exposed as HTTP endpoints; anyone can call them with any payload, not just your form.
Build it
Build a Next.js page that lists items from a server-only data function and has a client-side filter component; add a route handler with input validation (zod).