A better way to fetch and render records with "load more"
I recently changed the index page of this blog to list only the 5 latest blog posts and show a link to all posts if there are more.
How would you do it?
The most straightforward approach is to make a query to fetch 5 records and then make another count query to check whether there are more.
A better way is to fetch 6 (N+1) records, render 5 (N), and check whether you got that extra 6th (Nth) record to determine whether to show the “load more” action. This saves you the count query.
An apparently very simple trick that never occurred to me before reading it somewhere!