fknop / hapi-pagination

Hapi plugin to handle "custom" pagination
MIT License
63 stars 36 forks source link

Using this module for infinite pagination #47

Closed tiagoalves closed 7 years ago

tiagoalves commented 7 years ago

I'm implementing an endpoint with "infinite navigation/pagination/scroll" but I don't think I can use this module for my use case.

Instead of a page parameter, I need something like lastId and, additionally, also a parameter that indicates the direction (ascending or descending) we're navigating to.

Another alternative would be to use a dynamically named query parameter name such as before and after to indicate the lastId and direction implicitly. That's what Facebook does for it's Cursor-based Pagination.

Just asking if I'm thinking right or if you have any suggestion of how I could get this to work with this module. Thanks!

fknop commented 7 years ago

This module does not implement after/before parameters but you can still achieve your goal with the next/previous links once you have done your first request.

tiagoalves commented 7 years ago

I'm not sure I follow. Wouldn't the problem resurface when the user visits any of the next/previous links?

For instance, my previous link would be something like /some/listing?before=61&limit=20 and my next link something like /some/listing?after=80&limit=20 and I wouldn't be able to get the after and before params.

fknop commented 7 years ago

I'm not sure I understand what you want then.

Why do you need before/after if you can have the previous/next items. Do you want to change the limit between calls ?

tiagoalves commented 7 years ago

Sorry, I think I didn't explain my use case very clearly.

Typical pagination, such as the one supported by this module, is page-based (or offset-based).

An alternative is to use cursor-based navigation (not sure if that's the right term) which we can often see in infinite scroll pages such as the Tweeter feed or Facebook timeline.

Assuming I'm in the middle of an example listing, I could have generated this response

{
  results: [
    { id: 61, ... },
    { id: 62, ... },
    // ...
    { id: 79, ... },
    { id: 80, ... }
  ],
  paging: {
    previous: '/listing?before=61&limit=20',
    next: '/listing?after=80&limit=20'
  }
}

The previous/next links indicate "this is the most recent/oldest item I have and I need the even newer/older items". There are no pages, just the continuous loading of adjacent items in a given direction. It's the method described in this article.

Maybe if the module would allow for custom query parameters to be defined, it could handle this kind of navigation.

fknop commented 7 years ago

Ok, even if it's still possible to achieve infinite scrolling with this module I understand why it would be preferable for certain types of data to have cursor-based pagination. Right now, I don't have the time to dig into this.

I think it can still be achievable with this module as you can pass any query parameter you want. The process of returning a result is entirely up to you. The paginate method allows you to return any data you want as part of your result. The problem is that your custom cursor that you will implement will not be as part of the pagination metadata but part of the result itself.

tiagoalves commented 7 years ago

Thanks for the feedback. I'll do some experiments to see if I can get it working for my use case. Feel free to close the issue.