·
Across the Great Wall, we can reach every corner in the world.

分页接口应该是后端提供的,而且尽量避免使用 offset,pageSize,pageNum (点击页码)这种方式,对数据库性能非常不友好,有很多文章写为什么 offset 是 evil 的。应该做 cursor paging,和 facebook/twitter/reddit 那样的分页才是用户友好的,可以看看这几篇文章:

why facebook says cursor pagination is the-greatest

how to implement cursor pagination like a pro

evolving api pagination at slack

比如facebook的分页返回是这样的:

{
  "data": [
    {
      "something": "Endpoint data is here"
    }
  ],
  "paging": {
    "cursors": {
      "after": "MTAxNTExOTQ1MjAwNzI5NDE=",
      "before": "NDMyNzQyODI3OTQw"
    },
    "previous": "https://graph.facebook.com/{your-user-id}/albums?limit=25&before=NDMyNzQyODI3OTQw",
    "next": "https://graph.facebook.com/{your-user-id}/albums?limit=25&after=MTAxNTExOTQ1MjAwNzI5NDE="
  }
}

基于游标的分页

Replies
1

你可能会有疑问,为什么游标要设计成 base64 编码的:

Requiring the cursor to be a string value promotes the use of an opaque cursor value. The javascript implementation of the Relay spec for instance uses Base64 encoded IDs as cursor values. This discourages the client from implying what value goes in this field and gives the server the ability to encode additional information within the cursor.

摘自:https://slack.engineering/evolving-api-pagination-at-slack/