Limit amount of posts #14939
-
Hi, Imagine that I would only like to show the latest two blog posts on the homepage, where would I most efficiently handle that? I am using the NextJS starter-template (https://nextjs.org/learn/basics/data-fetching/implement-getstaticprops) Any advice, best practice or docs on this? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
You could do this in In the example, the posts are sorted by date: return allPostsData.sort((a, b) => {
if (a.date < b.date) {
return 1
} else {
return -1
}
}) and instead of returning all of them, just return the most recent 2 using return allPostsData.sort((a, b) => {
if (a.date < b.date) {
return 1
} else {
return -1
}
}).slice(0,2) |
Beta Was this translation helpful? Give feedback.
You could do this in
getStaticProps
for yourpages/index.js
file.In the example, the posts are sorted by date:
and instead of returning all of them, just return the most recent 2 using
.slice(0,2)
: