10 min read
ā¢Question 30 of 47hardNext.js Rendering Strategies Comparison
SSG, SSR, ISR, and dynamic rendering trade-offs.
Rendering Strategies in Next.js
Static Generation (SSG)
code.txtTSX
// Fully static - built at build time
export default async function StaticPage() {
const data = await fetch('https://api.example.com/data', {
cache: 'force-cache', // Default - caches indefinitely
});
return <div>{data}</div>;
}
// With generateStaticParams
export async function generateStaticParams() {
const posts = await getPosts();
return posts.map((post) => ({ slug: post.slug }));
}Server-Side Rendering (SSR)
code.txtTSX
// Dynamic - rendered on each request
export const dynamic = 'force-dynamic';
export default async function SSRPage() {
const data = await fetch('https://api.example.com/data', {
cache: 'no-store', // Always fresh
});
return <div>{data}</div>;
}Incremental Static Regeneration (ISR)
code.txtTSX
// Revalidate after specified seconds
export const revalidate = 60; // Revalidate every 60 seconds
export default async function ISRPage() {
const data = await fetch('https://api.example.com/data', {
next: { revalidate: 60 },
});
return <div>{data}</div>;
}
// On-demand revalidation
// app/api/revalidate/route.ts
import { revalidatePath, revalidateTag } from 'next/cache';
export async function POST() {
revalidatePath('/blog');
// or
revalidateTag('blog-posts');
return Response.json({ revalidated: true });
}Decision Matrix
| Strategy | Build Time | Request Time | Use Case |
|---|---|---|---|
| SSG | Heavy | Fast | Marketing, docs |
| SSR | Light | Slower | Personalized, real-time |
| ISR | Heavy | Fast + Fresh | Blogs, e-commerce |