What does Next.js add on top of React? Explain the App Router, rendering and caching.
Next.js (Vercel) is the most widely used React framework. The App Router (app/ directory) is built on React Server Components.
Key concepts
- File-system routing:
app/orders/[id]/page.tsx;layout.tsx(persistent shared UI),loading.tsx(Suspense fallback),error.tsx(error boundary),not-found.tsx, route groups(marketing), parallel and intercepting routes for modals. - Server Components by default: components run on the server, can be
asyncand fetch data directly; add'use client'only for interactive parts. - Server Actions (
'use server'functions) for mutations from forms and buttons, withrevalidatePath/revalidateTagto refresh data. - Route Handlers (
app/api/.../route.ts) for REST endpoints and webhooks; Middleware (now also called proxy) for redirects, auth checks and rewrites at the edge. - Rendering: static (at build time), dynamic (per request), streaming with Suspense, and Partial Prerendering (static shell + dynamic holes) in newer versions; ISR with
revalidate. - Caching: data fetches and rendered routes can be cached and revalidated; recent versions make caching opt-in and explicit (
'use cache',cacheLife,cacheTag), so know which version a project uses. - Built-in optimisations:
next/image,next/font,next/script, metadata API for SEO, Turbopack bundler.
Deployment
Vercel, or self-hosted Node.js (next start, standalone Docker output), or static export for fully static sites.
Example
tsx
// app/products/[id]/page.tsx: a Server Component with metadata and streaming
import { Suspense } from 'react';
import { notFound } from 'next/navigation';
import AddToCart from './add-to-cart'; // 'use client' component
import Reviews from './reviews'; // async Server Component
async function getProduct(id: string) {
const res = await fetch(`${process.env.API_URL}/products/${id}`, { next: { revalidate: 300, tags: [`product-${id}`] } });
if (res.status === 404) return null;
return res.json();
}
export async function generateMetadata({ params }: { params: Promise<{ id: string }> }) {
const product = await getProduct((await params).id);
return { title: product?.name ?? 'Product', description: product?.summary };
}
export default async function ProductPage({ params }: { params: Promise<{ id: string }> }) {
const { id } = await params;
const product = await getProduct(id);
if (!product) notFound();
return (
<main>
<h1>{product.name}</h1>
<p>₹{product.price}</p>
<AddToCart id={id} />
<Suspense fallback={<p>Loading reviews…</p>}>
<Reviews productId={id} /> {/* streamed in when ready */}
</Suspense>
</main>
);
}ts
// app/products/[id]/actions.ts
'use server';
import { revalidateTag } from 'next/cache';
export async function updatePrice(id: string, price: number) {
await requireAdmin(); // Server Actions are public endpoints: authorise!
await db.product.update({ where: { id }, data: { price } });
revalidateTag(`product-${id}`);
}Follow-up questions interviewers ask
Try answering each one before you open it.
What is the most common Next.js mistake in interviews and production?
Treating everything as a Client Component ('use client' at the top of the tree), which ships all the code to the browser and loses Server Component benefits; and, conversely, using browser-only APIs in Server Components. Keep 'use client' at the leaves where interactivity is needed, and pass serialisable props across the boundary.