I'm currently trying to create a product page that displays all products and is a server component. The problem that I've run into is that I can't find a way to pass the last visible document snapshot required by the startAfter() query.
This is the function I'm using to get the documents:
export async function getProducts() {
const q = query(
collection(db, "products"),
where("isHidden", "==", false),
orderBy("createdAt", "desc"),
limit(12)
);
const querySnapshot = await getDocs(q);
const lastDoc = querySnapshot.docs[querySnapshot.docs.length - 1];
const products = [];
querySnapshot.forEach((doc) => {
products.push({ ...doc.data(), id: doc.id });
});
return { products, lastDoc };
}
And this is the page itself:
export default async function ProductsBikiniPage({ searchParams }) {
const page = searchParams["page"] ?? "1";
const { products, lastDoc } = await getProducts();
return (
<main>
<section>
//Listing products here
</section>
<section>
<PaginationControls>
</section>
</main>
);
}
The page params are there because at first I tried to paginate using pages but learned that firebase startAfter() needs a document snapshot to work (Can't just use .skip(12) for example). So yeah, I can't figure out a way to pass this lastDoc back to the getProducts function to get the next set of products without changing the component to a client component. Any ideas on how I can implement Firebase pagination in a Next server component would be appreciated.