Pagination and infinite scroll
Loading a long list one slice at a time: a cursor or an offset to ask for the next slice, a button or a scroll trigger to request it, and a plan for the DOM as the list grows.
What it is
Pagination is how a client reads a long collection in slices instead of all at once. The two ways to address a slice differ in what the client sends back. An offset request names a position: page 3 of 10, or skip 20 and give me 10. A cursor request hands back an opaque string the server gave out with the previous slice; the client doesn't read it, and the server uses it to work out where to resume. Slack's Web API describes its cursors as pointers to the place in the list where your last request stopped, and uses them for its larger collections, while some older methods still take a numeric page and count.
The difference matters once the list changes between requests. With an offset, the server counts from the start each time. If a new post lands at the top between your first and second request, the post that was tenth is now eleventh, so the second slice opens with an item you already have, and a deletion above makes it skip one. A cursor that encodes the last item's id or sort key lets the server resume after that item, so inserts and deletions above it don't shift the next slice. That depends on the server: the cursor has to carry a stable key rather than a hidden offset, the sort order has to be stable, and a deleted anchor item needs a rule such as resuming at the next key after it. For a feed, chat history or any list that moves while someone reads, that is the property to ask for.
The GraphQL Cursor Connections spec fixes one shape for this: the client asks for first: 10, after: cursor; the response has edges, each with a cursor and a node, and a pageInfo object whose hasNextPage and endCursor tell the client whether to continue and from where. The cursor is opaque to the client and is passed back as received. Slack's version returns next_cursor in response_metadata; an empty or missing value means there are no further results. A slice can also come back with fewer items than limit while more remain, so its size is not an end signal, and cursors can expire, so a client shouldn't keep one for hours.
The trigger is the second half. A sentinel element sits after the last item, and an IntersectionObserver reports asynchronously when it enters the viewport, so the page doesn't run a scroll handler that measures elements with getBoundingClientRect() on the main thread. The rootMargin option grows the root's box before intersections are computed, so a positive bottom margin fires the callback while the sentinel is still below the fold. That gives the request a head start; a slow network can still leave a gap, which is why the list shows a loading row. The callback runs when a threshold is crossed, so if ten appended posts don't push the sentinel back out of the expanded root, nothing fires again: check once more after appending.
type Slice = { items: Post[]; endCursor: string | null; hasNextPage: boolean };
let cursor: string | null = null;
let hasNextPage = true;
let inFlight = false;
const observer = new IntersectionObserver(
(entries) => { if (entries.some((e) => e.isIntersecting)) void loadMore(); },
{ rootMargin: '0px 0px 600px 0px' }, // start 600px before the sentinel; tune to your layout
);
observer.observe(sentinel);
async function loadMore() {
if (inFlight || !hasNextPage) return; // one request at a time
inFlight = true;
feed.setAttribute('aria-busy', 'true');
try {
const after = cursor ? `&after=${encodeURIComponent(cursor)}` : '';
const res = await fetch(`/feed?first=10${after}`);
if (!res.ok) throw new Error('feed failed');
const slice: Slice = await res.json();
for (const post of slice.items) {
feed.insertBefore(renderPost(post), sentinel); // insert above the sentinel, so it stays last
}
cursor = slice.endCursor; // advance only after the slice is in the DOM
hasNextPage = slice.hasNextPage;
if (!hasNextPage) { observer.disconnect(); sentinel.remove(); showEnd(); }
} catch {
showRetry(); // the cursor is unchanged, so a retry asks for the same slice
} finally {
feed.setAttribute('aria-busy', 'false');
inFlight = false;
}
}When to use it
Reach for cursor pagination when the list can change while someone reads it, as in a feed, a comment thread or a message history where new items arrive at one end, and when the collection is large enough that the server shouldn't load it whole to return a slice. Pair it with an automatic trigger when the goal is uninterrupted reading and there is no natural stopping point below the list.
Plan for what the page holds after a long session. Lighthouse warns when body has more than about 800 nodes and reports an error past about 1,400; a large tree slows style and layout work, and code that keeps references to many nodes, say through querySelectorAll, holds memory on the reader's device. If one post component is about 30 elements, a hundred posts put the tree past 3,000 nodes, and the images in them hold decoded bitmaps on top. For long lists, Lighthouse's guidance suggests virtualizing with a windowing library, which renders the rows near the viewport and drops the rest. Make that shift when the node count is heading past those thresholds rather than at a fixed post count; count the elements in one post component to see where your feed crosses. See List virtualization for how, and keep the cursor and item data in memory so scrolling back doesn't refetch.
When new posts arrive at the top while someone reads halfway down, the browser helps more than it used to. Scroll anchoring adjusts the scroll position to compensate for changes outside the viewport, so content inserted above keeps the text being read in place; it's on by default where supported, and overflow-anchor: none turns it off for an element. MDN lists it as newly available across the latest browsers since September 2026, so older devices may still jump, and anchoring is suppressed when the anchor node or an ancestor changes a positioning property such as top or position. Our advice: don't rely on it for a feed. Hold arrivals in a buffer, show a "12 new posts" control at the top, and insert them when it's pressed or when the reader is already at the top.
Make the loading reachable without a mouse wheel. The WAI-ARIA feed pattern gives the container role="feed" and each post role="article" with aria-posinset for its position and aria-setsize for the total, or -1 when the total is unknown. The page sets aria-busy="true" on the feed while a multi-step update runs and back to false when it's done, since a value left at true can hide the new posts from some assistive technology. The pattern also makes the page responsible for loading more based on which article has focus, with keys such as Page Down and Page Up to move between articles, because loading driven purely by scroll events is unreliable for screen reader users.
When not to
Numbered pages are the better fit when position matters more than flow: search results, an order history, an admin table someone sorts and filters, anything a person wants to bookmark, share as a URL, or come back to at page 7. An offset gives each page an address; a cursor doesn't let a client jump to the middle. A static catalog that doesn't change between requests also loses nothing to offset drift, and offsets are easier to debug by hand.
A Load more button beats an automatic trigger when there is something below the list a person needs to reach, such as a footer with links or a form, when each slice is expensive to produce, or when a clear stopping point helps, as in a list someone is checking rather than browsing. It also makes the load an ordinary button press, which removes the scroll-driven part of the problem the ARIA feed pattern addresses; the appended posts still need structure, so the article roles and aria-busy handling still apply.
Compared
| Technique | What it does | Reach for it when | What it costs |
|---|---|---|---|
Offset pages | Asks for position N, count M | Pages need addresses: search, tables, bookmarks | Duplicates or skips when items shift above |
Cursor pages | Asks for items after an opaque marker from the last slice | The list changes while it's read: feeds, chat | No jump to page N; cursors can expire |
Load more button | A press appends the next slice | A footer must stay reachable, or each slice is costly | One more action per slice |
Automatic trigger | A sentinel observed with | Uninterrupted reading with no natural end | Needs an in-flight guard, and a keyboard path for screen reader users |
Virtualized list | Renders the rows near the viewport and drops or recycles the rest | The tree is past the node counts Lighthouse flags, about 800 and 1,400 | Row measurement and scroll math; overkill for short lists |
In a picture
Step 1 of 3
The first slice
The client asks for the first ten posts with no cursor. The server returns them with endCursor and hasNextPage: true. The client renders the list and places a sentinel element after the last post.