Lesson 2 of 10
SSG, SSR & ISR Patterns
Next.js App Router uses React's fetch() caching to control how data is fetched: static (build-time), server-side (per-request), or incrementally regenerated (ISR). The right choice depends on how frequently data changes and whether it's personalized.
Fetch Caching - Static vs Dynamic
// ---- Static generation (SSG) - fetches at BUILD TIME ----
// Default behavior - cached forever until next build
async function StaticBlogPost({ params }: { params: { slug: string } }) {
const post = await fetch(`https://api.example.com/posts/${params.slug}`);
// ↑ cached at build time - same response for all users, all requests
return <BlogPost post={await post.json()} />;
}
// ---- ISR - Incremental Static Regeneration ----
// Revalidates in background after the specified time
async function ProductPage({ params }: { params: { id: string } }) {
const product = await fetch(`https://api.example.com/products/${params.id}`, {
next: { revalidate: 3600 }, // cache for 1 hour, then stale-while-revalidate
});
return <ProductDetail product={await product.json()} />;
}
// On-demand revalidation - invalidate cache on data change
// app/api/revalidate/route.ts
import { revalidatePath, revalidateTag } from 'next/cache';
export async function POST(request: Request) {
const { secret, path, tag } = await request.json();
if (secret !== process.env.REVALIDATE_SECRET) return Response.json({ error: 'Unauthorized' }, { status: 401 });
if (path) revalidatePath(path); // invalidate specific URL
if (tag) revalidateTag(tag); // invalidate by cache tag
return Response.json({ revalidated: true });
}
// Add tags when fetching - for targeted invalidation
const product = await fetch(`/api/products/${id}`, {
next: { tags: ['products', `product-${id}`] },
});
// Call /api/revalidate with { tag: 'products' } to invalidate all product pages
// ---- SSR - Server-Side Rendering (per request) ----
// Opt out of all caching with cache: 'no-store'
async function PersonalizedDashboard() {
const user = await fetch('/api/user', {
cache: 'no-store', // re-fetch on every request
headers: { Cookie: ... }, // personalized - must be dynamic
});
// OR use cookies/headers which also opt into dynamic rendering
}
// ---- Force dynamic rendering ----
export const dynamic = 'force-dynamic'; // no caching at all for this route
export const revalidate = 0; // same effect
// ---- Dynamic rendering triggers (automatic) ----
// Next.js makes a route dynamic automatically if:
// - You call cookies(), headers(), or searchParams in a Server Component
// - You use fetch() with cache: 'no-store' or no-cache
// - You use an unstable dynamic API like Date.now() or Math.random()Data Fetching Decision Guide
- Static (default): marketing pages, blog posts, documentation - content changes rarely, same for all users
- ISR (revalidate: N): product pages, news articles - changes occasionally, same for all users, invalidatable on CMS publish
- SSR (no-store): dashboards, user-specific data, shopping cart - changes frequently, different per user
- Client-side TanStack Query: real-time updates, data after interaction, infinite scroll - user actions drive refetching
- Rule of thumb: Start static, add revalidate when data changes, switch to no-store only for truly personalized data
- generateStaticParams: prerender only the top N most visited pages statically; the rest generate on first request (fallback: 'blocking' or 'true')
Tip
Tip
Practice SSG SSR ISR Patterns in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Visual architecture and flow for SSG, SSR & ISR Patterns
Practice Task
Note
Practice Task - (1) Write a working example of SSG SSR ISR Patterns from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with SSG SSR ISR Patterns is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react code.
Key Takeaways
- Static (default): marketing pages, blog posts, documentation - content changes rarely, same for all users
- ISR (revalidate: N): product pages, news articles - changes occasionally, same for all users, invalidatable on CMS publish
- SSR (no-store): dashboards, user-specific data, shopping cart - changes frequently, different per user
Finished this lesson?