In a community-board web app's infinite home feed, how would you design loading older posts, and what should happen when new posts arrive meanwhile?
answer
- load before the reader reaches the end
- one request per direction
- the client keeps the next cursor
- drop duplicates by id
- new posts wait behind a banner
basics
~20 sFetch older posts a screen or two before the end, one request at a time, with the server's cursor; drop repeated ids; show retry and end rows; and announce new posts with a banner, never inserting them above the reader.
solid answer
~50 sThe trigger is a sentinel a screen or two before the last loaded post, so the next page lands before the reader hits the bottom, with one request in flight per direction. Page size covers about two or three phone screens of mixed posts, smaller for the first page. The client keeps only the `next` cursor and has-more flag from the last page and continues with them; a cursor rather than an offset, because new posts at the top would shift offsets and repeat or skip items. Merging drops any id already loaded. A failed page becomes an inline row with Retry, and has-more false becomes an 'all caught up' row. New posts never go above a reader who is scrolled down: a 'N new posts' banner appears, and tapping it scrolls to the top and refreshes from the first page.
code
json · 6 lines{
"postsById": { "p_9120": { "type": "poll", "likes": 14 } },
"order": ["p_9120", "p_9117", "p_9102"],
"older": { "cursor": "c_9102", "hasMore": true, "status": "idle" },
"newer": { "pendingCount": 5, "bannerVisible": true }
}go deeper
Recall the parts: a trigger near the end, a page size, an error row, an end row, and a banner for new posts instead of inserting them.
Explain why the trigger fires early, why the client keeps a cursor rather than an offset, and how merging by id removes duplicates across pages.
Handle the awkward cases: a rejected cursor, a refresh whose first page leaves a gap, and a load-ahead distance tuned from field data on slow networks.
Decide what the feed promises readers about order and completeness, and which of those promises the client enforces versus the server.
## Two ends of one feed A community-board home feed grows at both ends. At the **bottom**, older posts load as the reader scrolls. At the **top**, neighbours keep posting while someone reads. The design has to keep the reader's place at both ends and never show a post twice. ## The bottom: when and how much to load The **paging trigger** is usually a sentinel element placed near the end of the loaded posts and watched with an IntersectionObserver whose root margin is grown downward, so the callback fires before the sentinel is actually visible. The feed pattern's own guidance points the same way: load new articles before the reader reaches the end. The observer's quirks — an initial callback, or a trigger that stops firing after a batch lands — are a mechanism question of their own; the design decisions are these: - **How far ahead.** One to two viewport heights is a sensible start on phones; a fast flick on a slow network needs more, a metered connection less. Tune it from field data on how often readers see the loading row. - **Page size.** Enough posts to fill two or three phone screens — often 10 to 20 for a mixed feed — so one request buys several seconds of reading. Keep the first page smaller so the first screen renders sooner. - **One request per direction.** While a page is loading, further triggers are ignored, so a fast scroll cannot fire three requests for the same cursor. - **Busy state.** While posts are appended, the feed container carries `aria-busy="true"`, set back to `false` when done. ## The cursor the client holds The client stores what the last page's envelope returned — a `next` cursor and a has-more flag — and continues by sending that cursor back. It never computes positions itself. | Choice | What happens when five new posts arrive at the top | |---|---| | Offset ("skip 40, take 20") | every post shifts down five places; the next page repeats five posts the reader has already seen | | Cursor ("after this post") | the next page continues from the last loaded post; new posts do not disturb it | Even with a cursor, ranked or edited feeds can return a post twice across pages, so **merging drops any id already loaded**. The client keeps posts in a map by id plus an ordered list of ids; a repeat is ignored, and an updated copy patches the existing entry. ## Failure and the end 1. **A page fails.** Replace the loading row with an inline error row: a short message and a Retry button. Keep every loaded post. Do not retry in a tight loop; one automatic retry when the connection returns is fine. 2. **The cursor is rejected** (it expired or the feed was rebuilt). Treat it as a reason to refresh from the top, not as an error the reader must solve. 3. **has-more is false.** Show an end row — "You're all caught up" — and stop watching the sentinel. A feed that silently stops loading looks broken. ## The top: new posts arrive Inserting new posts above someone reading post 150 is the classic mistake: content moves under the reader, positions shift for assistive technology, and the feed pattern's guidance is to avoid inserting or removing articles in the middle of a feed. - **Reader scrolled down:** show a "5 new posts" banner pinned near the top. Nothing moves. - **Banner tapped:** scroll to the top and **refresh from the top** — request the first page with no cursor. If its posts overlap the ones already loaded, stitch them in by id; if they do not, there is a gap, so replace the loaded list rather than show a silent hole. - **Reader already at the top:** inserting is fine; nobody's place is lost. - **Pull-to-refresh or return after a long absence:** same as the banner: refresh from the top. How the app learns that new posts exist — a periodic count check or a live channel — is the real-time design's decision; a cheap check every minute or so is enough for a neighbourhood board. ## What interviewers listen for - A trigger that fires early, one request per direction, and a page size argued from screens. - Cursor over offset for a feed, with the duplicate case handled by id anyway. - Visible error and end states rather than a spinner forever. - A banner instead of insertion above the reader, and a clear rule for continue versus refresh.
- The 'new posts' banner has been tapped, but the fresh first page shares no post with what is loaded. What do you do?There is a gap of unknown size between the fresh page and the old list, so stitching would hide missing posts. Replace the loaded list with the fresh page and its cursor, and scroll to the top. The reader asked for the newest posts, and an honest restart beats a silent hole.
- How would you choose the load-ahead distance for readers on slow mobile networks?Measure how often readers reach the loading row and how long they wait there, split by connection type. If slow-network readers hit it often, widen the distance or fetch the next page as soon as the previous one renders. The cost is extra data for pages some readers never reach.
- Should the feed retry a failed page automatically?Once, when the connection comes back, and otherwise leave it to the inline Retry button. A tight automatic loop burns battery and data on a dead network and can hammer a struggling server, while the inline row keeps the reader in control.
- Page the feed by offset; new posts at the top do not affect later pages.
- Insert new posts at the top as they arrive, wherever the reader is.
- A cursor feed can never return the same post twice, so no dedup is needed.
- On a failed page, show a full-screen error and drop the loaded posts.
- Load the next page only when the sentinel is fully visible at the very bottom.