DEV Community

Discussion on: Pagination in an API: page number vs start index

Collapse
 
mehdibenhemdene profile image
mehdibenhemdene

Whether you should go for the first or the second approach really depends on your needs. Let's take an example. If you implement a page number / page size solution, this may not always be reliable if data is changing. Meaning you can get some "gaps" in the results whenever count / pageCount changes. If your data changes so often, you should probably use a start index / limit approach (AKA cursor based pagination) which is adopted by both Facebook and Twitter API.

If my response doesn't make that much sense to you, consider taking a look at this article:

sitepoint.com/paginating-real-time...

Hope you find this useful !

Collapse
 
joaofbantunes profile image
João Antunes

Agree, the cursor based pagination is certainly a great alternative, particularly in cases like the ones you mentioned.

Regarding the two "approaches" I mentioned in the post though, they are much more similar, the main difference is more of developer perception, not really having an impact on the end result as the choice between them and a cursor based approach have.

For an example of what I was thinking when I wrote this post:
Imagine an endpoint in which we want to get 10 items after skipping other 10, in the two examples:

  1. Page number and page size -> https://some-api/items?pageNumber=2&pageSize=10
  2. Start index and limit -> https://some-api/items?startIndex=10&limit=10

So my question was more regarding what people prefer between these two.

Collapse
 
mehdibenhemdene profile image
mehdibenhemdene

Well in this case I'd have to say that I really prefer the first method (page number and page size) because it's kind of similar to what the user is seeing and requesting (making it easier to render in client side instead of having to calculate the startIndex each time in case we want to display the page number in the UI for eg.).