小编mik*_*lov的帖子

如何在 Next.js SSR 应用程序中对 Firebase 中的数据进行分页?

我正在尝试构建一个 next.js 服务器端渲染博客。为此,我需要对帖子数据进行分页。但是,我还没有找到一种方法来使用 firebase 提供的查询游标对数据进行分页。建筑物查询代码为:

let postsQuery = firebase.firestore().collection('/posts').orderBy('postedOn', 'asc').limitToLast(10);
if (currentTagFilter !== 'All') {
  postsQuery = postsQuery.where('tag', '==', currentTagFilter);
}
Run Code Online (Sandbox Code Playgroud)

现在,这适用于第一页,但我不知道如何请求接下来的 10 个帖子。我可以保存查询的第一个文档并使用 .endBefore(firstPost)。但是,例如,如果我在 _app.js 中创建一些状态并将第一个文档保存在数组中,我无法找到如何使其在 getServerSideProps 中可访问。更不用说,如果用户直接访问 /page/2,则不会向他显示任何内容,因为尚未执行第 1 页的查询。

如何正确对数据进行分页?

firebase next.js google-cloud-firestore

5
推荐指数
1
解决办法
1513
查看次数

标签 统计

firebase ×1

google-cloud-firestore ×1

next.js ×1