How to fetch data on server-side for dynamic routes in Next.js? #87027
-
SummaryHi everyone, I’m building a Next.js app with dynamic routes (e.g., /posts/[id]) and I want to fetch data for each post on the server side. I’m trying to use getServerSideProps to fetch data based on the post ID from the URL. Here is a simplified version of my code: However, I’m not sure if this is the best way to handle data fetching for dynamic routes, or if I should use getStaticProps with getStaticPaths instead. Could someone please explain the differences and recommend the best approach for fetching data in dynamic routes? Thanks a lot! Additional informationNo response ExampleNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Your code with getServerSideProps is fine if the data changes a lot or you need it fresh on every visit. Use getStaticProps + getStaticPaths if your posts don’t update often — Next.js will pre-build the pages at build time, so they load super fast. Quick rule of thumb: |
Beta Was this translation helpful? Give feedback.
Your code with getServerSideProps is fine if the data changes a lot or you need it fresh on every visit.
Use getStaticProps + getStaticPaths if your posts don’t update often — Next.js will pre-build the pages at build time, so they load super fast.
Quick rule of thumb:
Blog posts, docs, marketing pages → getStaticProps.
User dashboards, real-time stats → getServerSideProps.