In a mobile news app's infinite feed, what should the client do with the server's opaque next-page cursor, and how does it detect the end?
answer
- the server's token, not yours
- store it beside its page
- send it back byte for byte
- never decode, never build one
- end means no next cursor
basics
~20 sStore the cursor with the page it came with and send it back unchanged; never decode or build one. The feed has ended only when a response carries no next cursor or the contract's explicit end flag.
solid answer
~50 sThe cursor is the server's bookmark, and its encoding is the server's business. The client keeps it exactly as received, next to the page that returned it in the local store so a relaunch can continue, and sends it back unchanged in whatever parameter the contract names. It never decodes it to read an article id or a timestamp and never builds one from the last cached article, because the server may change the encoding, the sort or the ranking behind it without telling anyone. The end of the feed is whatever the contract says it is: usually a missing next cursor, sometimes an explicit `hasMore: false`. A page shorter than requested, an empty page that still carries a cursor, or a failed request is not the end; a failure becomes a retry row, and only the end signal earns an "all caught up" footer.
go deeper
Recall the two rules: the cursor is stored and sent back exactly as received, and only the contract's end signal, usually a missing next cursor, ends the feed.
Explain why parsing a cursor couples the app to an encoding it does not own, and separate the end, a failure and being offline as three states with three different bottom rows.
Show the cursor persisted with its page in one transaction, and say what the client does when the server rejects a stored cursor after a week away.
Argue for one paging contract consumed identically by every feed surface in the app, so the server can change cursor encodings without breaking builds still installed in the field.
## What the cursor is, from the client's side An infinite feed in a news app is a list of article cards from many publishers that the server hands out one **page** at a time. With each page the server returns a **cursor**: a short token that means "continue from here". The contract calls it **opaque**, which is an instruction to the client, not a description of the bytes. However readable it looks, the client is told nothing about what is inside, and it must not rely on anything it might find there. That makes the client's job narrow and easy to get right: - **Keep it exactly as received.** No trimming, re-encoding, case changes or URL decoding beyond what the transport itself requires. - **Keep it with its page.** The cursor that came back with page 3 is what fetches page 4; store it next to page 3 in the local database. - **Send it back unchanged** in the parameter the contract names, together with the same feed and filters the earlier request used. - **Never create one.** The client has no way to make a valid cursor; it only ever replays one the server issued. How a cursor is encoded, how the server keeps it stable while articles are inserted and how long it stays valid belong to the API's pagination contract. What belongs to the installed client is consuming it correctly, offline and across relaunches. ## Why parsing or building a cursor breaks Teams decode cursors because the token often looks like an encoded id or timestamp, and decoding seems to unlock features such as "jump to an article" or "sort the cache". Each shortcut couples the app to an encoding it does not own, and installed apps stay on phones for months after the server changes. | Tempting shortcut | What breaks | |---|---| | Decode the cursor to read the last article id | The server switches encoding or adds a ranking field; old builds send garbage or crash on parse | | Build the next cursor from the last cached article | The feed is ranked, not sorted by id or time; the built cursor skips or repeats articles | | Derive an offset from the count of cached articles | Inserts at the top shift every offset; the client re-reads or misses articles | | Edit the cursor to change the page size | The server may bind the size or the filter into the token and reject the request | The rule that follows: the order of the cached feed is stored by the client as pages arrive (page sequence plus position), never reconstructed from what a cursor seems to contain. ## Recognising the end of the feed The end is a signal the contract defines, and the client should look for exactly that signal: 1. **A missing or null next cursor** is the usual form. The response says, in effect, "there is nothing after this". 2. **An explicit flag** such as `hasMore: false` is the other common form; some contracts send both. 3. **Nothing else counts.** A page with fewer items than requested may simply mean the server filtered some out (blocked publishers, removed articles) and still has more. An empty page that carries a next cursor means "keep going". A timeout, an error status or no connectivity says nothing about the end at all. When the end signal arrives, the client records it with the last page so a relaunch does not keep asking, and shows a terminal footer. A news feed rarely truly ends, but a feed filtered to one publisher or one topic does. ## What the reader sees at the bottom The bottom of the list is where these distinctions become visible, so each state needs its own row: | State | Bottom row | What a scroll near the end does | |---|---|---| | More available, cursor stored | Nothing yet, or a loading row once a request is out | Starts a request with the stored cursor | | Request failed or offline | An inline row explaining the failure, with a retry action | Nothing until the reader retries or the connection returns | | End signalled by the contract | An "all caught up" footer | Nothing | Confusing the second and third rows is the classic bug: a commuter goes through a tunnel, the page request times out, and the app tells them they have read everything. ## When the server refuses a stored cursor A cursor stored before a week away may no longer be honoured; how long it lives is the contract's decision, and so is the error that reports it. The client's side is simple: treat the refusal as "this tail cannot be extended", drop that cursor, fetch the feed from the start, and keep showing the cached articles until the new pages arrive. Patching the token or retrying it in a loop never helps.
- The server rejects a cursor the app stored a week ago. What should the client do?Treat the rejection as the contract's signal that the stored tail can no longer be extended: discard that cursor, fetch the feed from its start, and keep showing cached articles until the new pages land. Never patch the cursor or retry it in a loop; how long a cursor stays valid, and how rejection is reported, are the API contract's decisions.
- Why store the cursor in the local database rather than in memory beside the list?Because the page it belongs to is stored there. After the process is killed, the relaunch renders the stored pages at once, and scrolling past them needs the cursor that follows the last stored page. Writing page and cursor in one transaction keeps them consistent: a cursor without its page skips articles, and a page without its cursor gets fetched again.
A cursor is a cloakroom ticket: you keep it safe and hand it back to get your coat. You do not read the number to guess which hook your coat is on, you never write your own ticket, and having no ticket means there is nothing left to collect.
- Decoding the cursor is fine since it is only an encoded article id.
- The client can build the next cursor from the last article's id or timestamp.
- A page with fewer items than requested proves the feed has ended.
- A failed or timed-out page request means there is nothing more to load.
- An empty page means the end even when it carries a next cursor.