Understanding Next.js Static Site Generation (SSG)
Next.js has become the go-to framework for building modern web applications. One of its most powerful features is Static Site Generation (SSG).
What is Static Site Generation?
Static Site Generation pre-renders pages at build time. This means:
- HTML is generated when you build your app
- The same HTML is served to each user
- Pages can be cached by CDNs for maximum performance
When to Use SSG?
SSG is perfect when:
- Your data can be pre-rendered ahead of a user's request
- You want the best possible performance
- You don't need user-specific or real-time data
Great examples include:
- Marketing pages
- Blog posts
- Documentation
- Product listings (that don't change frequently)
Getting Started with SSG
Basic Static Page
// app/page.tsx
export default function Home() {
return <h1>Hello, World!</h1>;
}
This page is automatically static - no configuration needed!
Dynamic Routes with SSG
// app/blog/[slug]/page.tsx
export async function generateStaticParams() {
const posts = await getPosts();
return posts.map((post) => ({
slug: post.slug,
}));
}
export default function BlogPost({ params }) {
return <div>Post: {params.slug}</div>;
}
SSG vs SSR vs ISR
| Feature | SSG | SSR | ISR | |---------|-----|-----|-----| | Build time | ✓ | - | ✓ | | Request time | - | ✓ | - | | CDN caching | Perfect | Good | Great | | Real-time data | ✗ | ✓ | ✓ (stale) |
Best Practices
- Use SSG by default - It's the fastest option
- Add ISR for dynamic content - Get the best of both worlds
- Leverage
revalidate- Keep content fresh without full rebuilds
export const revalidate = 3600; // Revalidate every hour
Conclusion
Static Site Generation is a powerful tool in your Next.js toolkit. Use it whenever you can for the best performance and user experience.
Happy coding! Check out the Next.js documentation for more.
Written by Abhiram Chakkiyar
Developer, writer, and creator. I write about code, technology, and everything in between.