What’s the difference between getStaticProps and getServerSideProps in Next.js? #81356
-
SummaryI’m building a blog using Next.js and came across two data fetching methods: getStaticProps and getServerSideProps. I understand that both are used to fetch data for pre-rendering pages, but I’m not sure when to use which one. Can someone explain the key differences between them and give examples of when to use each? Additional informationNo response ExampleNo response |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Great question! 🔹 🔹 💡 Pro Tip: Use |
Beta Was this translation helpful? Give feedback.
-
Please refer to the documentation:
I'd say, favor the App Router, but if you must use Pages Router... You should favor using This also means that search params in UI, have special behavior, in Pages Router, the page is statically optimized using an empty search params object, and immediately re-rendered client side, you still need to cover the empty search params code path for the build time run. However, there's data, personalized, real-time, etc, that shouldn't be cached, and should always be shown at their very latest state, then That being said, App Router covers both of these, by allowing you to fetch the data within the React component.
Also good to know that |
Beta Was this translation helpful? Give feedback.
Great question!
🔹
getStaticProps
: Runs at build time. It pre-generates the page as static HTML. It’s perfect for content that doesn’t change often — like blog posts or marketing pages.🔹
getServerSideProps
: Runs on every request. It generates the page at runtime. Use it when the data is dynamic or depends on the request (e.g., user session, query params).💡 Pro Tip: Use
getStaticProps
+revalidate
(ISR) if you want to serve static content that can also update in the background!