What if your app could load endless data smoothly without confusing users or wasting time?
Why Cursor-based pagination (startAfter, endBefore) in Firebase? - Purpose & Use Cases
Imagine you have a huge list of items in your app, like thousands of messages or products, and you want to show them page by page.
Without cursor-based pagination, you try to load all items at once or jump to pages by counting items manually.
This manual way is slow because loading everything wastes time and data.
It's also easy to make mistakes, like skipping or repeating items when users move between pages.
Plus, if new items are added or removed, the pages get mixed up and confusing.
Cursor-based pagination uses a marker (cursor) to remember where you left off.
With startAfter and endBefore, you can quickly fetch the next or previous set of items without loading everything.
This keeps pages consistent even if the list changes.
query.limit(10).offset(20)
query.startAfter(lastVisible).limit(10)It lets your app load data smoothly and reliably, giving users a fast and seamless browsing experience.
Think of scrolling through a social media feed where new posts appear constantly.
Cursor-based pagination ensures you see posts in order without missing or repeating any, even as new posts arrive.
Manual pagination loads too much data and can cause errors.
Cursor-based pagination uses markers to fetch data efficiently.
This method keeps data consistent and improves user experience.
Practice
startAfter method do in Firebase cursor-based pagination?Solution
Step 1: Understand the purpose of
startAfterstartAftertells Firebase to begin loading documents after the given document snapshot, skipping that snapshot itself.Step 2: Compare with other cursor methods
endBeforestops before a snapshot, butstartAftermoves forward after it.Final Answer:
It starts loading documents after a specific document snapshot. -> Option DQuick Check:
startAfter= start after snapshot [OK]
- Confusing startAfter with endBefore
- Thinking startAfter includes the given document
- Assuming startAfter loads all documents from start
lastDoc in Firebase?Solution
Step 1: Recall Firebase query cursor methods
The correct method to start after a document isstartAfter().Step 2: Match method with snapshot variable
UsingstartAfter(lastDoc)correctly applies the cursor after thelastDocsnapshot.Final Answer:
query.startAfter(lastDoc) -> Option AQuick Check:
Correct method is startAfter() [OK]
- Using non-existent methods like endAfter or startBefore
- Mixing up startAfter and endBefore
- Forgetting to pass the snapshot as argument
const firstPage = await collection.orderBy('name').limit(3).get();
const nextPage = await collection.orderBy('name').startAfter(firstPage.docs[2]).limit(3).get();Solution
Step 1: Understand firstPage query
The first query orders by 'name' and limits to 3 documents, so it returns documents 1, 2, and 3.Step 2: Understand nextPage query with startAfter
UsingstartAfter(firstPage.docs[2])starts after the third document, so nextPage returns documents 4, 5, and 6.Final Answer:
Documents 1 to 3 on firstPage, documents 4 to 6 on nextPage -> Option AQuick Check:
startAfter skips last doc, nextPage continues [OK]
- Assuming startAfter includes the last document again
- Confusing document indexes (0-based vs 1-based)
- Not ordering consistently before pagination
const prevPage = await collection.orderBy('date').endBefore(lastDoc).limitToLast(3).get();
What is the likely cause?Solution
Step 1: Check cursor snapshot origin
The snapshotlastDocmust come from a query with the same ordering and filters.Step 2: Understand error cause
IflastDocis from a different query or unordered, Firebase throws an error.Final Answer:
ThelastDocsnapshot is not from the same query order -> Option CQuick Check:
Cursor snapshot must match query order [OK]
- Using snapshot from different query or order
- Mixing startAfter and endBefore incorrectly
- Ignoring need for consistent ordering
currentDoc. Which Firebase query correctly achieves this?Solution
Step 1: Understand backward pagination
To load documents beforecurrentDoc, useendBefore(currentDoc).Step 2: Use limitToLast for correct order
UsinglimitToLast(5)returns the last 5 documents beforecurrentDocin correct order.Final Answer:
collection.orderBy('timestamp').endBefore(currentDoc).limitToLast(5) -> Option BQuick Check:
Backward pagination uses endBefore + limitToLast [OK]
- Using startAfter for backward pagination
- Using limit instead of limitToLast when going backward
- Confusing endBefore with endAt
Ready to prove it?
Pass the test and this pattern moves unknown
