So I'm a little bit confused about getStaticProps
.
getStaticProps
runs at build time to generate static pages.
My question is that if I have a blog that updates when a new post is posted I shouldn't use getStaticProps
, right? Because it is called only at build time and then never be called to fetch new available posts.
In this case I should use getServerSideProps
or fetch data client-side with useSWR
, right?
You should use getStaticProps if: The data required to render the page is available at build time ahead of a user's request. The data comes from a headless CMS. The page must be pre-rendered (for SEO) and be very fast — getStaticProps generates HTML and JSON files, both of which can be cached by a CDN for performance.
GetStaticProps is best suited for data that doesn't change frequently such as blog posts or news articles because unlike GetServerSideProps, when data is fetched, it generates a static HTML and stored in a JSON file so it can be cached and immediately available before loading.
You should use getServerSideProps only if you need to render a page whose data must be fetched at request time.
And again, you can't mix getServerSideProps and getStaticProps since those two behave entirely different from one another.
When a page with getStaticProps is pre-rendered at build time, in addition to the page HTML file, Next.js generates a JSON file holding the result of running getStaticProps. This JSON file will be used in client-side routing through next/link or next/router.
Alternatively, if you are not using API routes to fetch data, then the fetch () API can be used directly in getStaticProps to fetch data. To verify what Next.js eliminates from the client-side bundle, you can use the next-code-elimination tool.
When combined with Incremental Static Regeneration, getStaticProps will run in the background while the stale page is being revalidated, and the fresh page served to the browser. getStaticProps does not have access to the incoming request (such as query parameters or HTTP headers) as it generates static HTML.
Okay, once you've identified the posts you want to update, it's time to actually update them! Here are some best practices to consider. 1) Use the same post, and keep URL the same. Rather than publishing an additional article on your blog -- which could result in a ding from search engines for duplicate content -- modify the original article.
In Next.js, it's best to think of getStaticProps
as a way to create static web pages. Not necessarily pages that are prebuilt during the initial build.
So if you use incremental static regeneration, you can use getStaticProps
on the blog post pages.
Essentially, you use getStaticProps
to tell Next.js to generate a static page (or set of pages) that may change over time. So you can have page with all your blog posts and tell Next.js that it may update every day.
// posts.js
export const getStaticProps = async () => {
const blogPosts = await getAllBlogPosts()
return {
props: { blogPosts },
revalidate: 60 * 60 * 24 // 1 day in seconds
}
}
This way, Next.js will create a static version of the blog posts that expires after 1 day. So the next, day, it will regenerate a new static version of the page. That way, if you add a new blog post, it will show up the day it's published.
I'd recommend checking out the docs on this topic as it has a lot more detail: https://nextjs.org/docs/basic-features/data-fetching#incremental-static-regeneration
Next.js can also let you add new blog post pages without having to rebuild the entire site. To do this, you need to create an individual blog post page with a dynamic route.
First, create a page in posts/[slug].js
. This is a dynamic route where you can connect a slug to an individual blog post.
Then, add the getStaticPaths function. Make sure to set fallback to either true
or blocking
. The docs fleshes out the difference in more detail.
export const getStaticPaths = async () => {
return {
paths: []
fallback: 'blocking'
}
}
This tells Next.js that it can expect anything as a slug. That way, new blog posts can be added in the future without rebuilding the entire site.
Then, add your getStaticProps
function to give the page it's details.
export const getStaticProps = async (context) => {
const post = await getPostBySlug(context.params.slug)
if(!post) return { redirect: '/posts', permanent: false } // redirect to main blog posts page if post doesn't exist, or any other page you want
return {
props: { post }
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With