β
getStaticProps (For static pages β built once)
Think of it like this:
ποΈ βI want to build this page once, ahead of time, when I build my project β and then reuse it for every user.β
- Runs at build time (when you run
npm run build) - The page becomes a static HTML file
- Faster for users β no server wait
- Great for pages that donβt change often (e.g., blog post, about page)
Example use case: A blog page where the post content doesn't change often.
β
getServerSideProps (For dynamic pages β built on every request)
Think of it like this:
π βI want to fetch fresh data every time a user visits the page.β
- Runs on the server, every time someone visits the page
- You always get the latest data
- Slower than static, but always up to date
- Good for things like user dashboards, live data, or personalized content
Example use case: A dashboard showing real-time user data or logged-in user's settings.
You donβt call them yourself. Next.js automatically calls them for you when:
- A user visits a page that exports one of them
- You define it like this inside a page component (not in a regular component):