In a community-board web app, a reader opens a post two hundred items down and presses Back; how do you return them to the same place?
answer
- which kind of navigation was it
- the back/forward cache is a whole-page snapshot
- soft navigations restore themselves
- an anchor post plus its offset
- refresh what changed after restoring
basics
~20 sIf the post is a separate document, keep the feed eligible for the back/forward cache, which restores it whole. Otherwise the app restores itself from saved post ids, cursor, heights and an anchor post with its offset, then refreshes counts.
solid answer
~50 sFirst, which navigation is it. If the post page is a separate document, the back/forward cache can restore the feed page whole — every loaded post, the scroll position, the JavaScript heap — so I keep the feed eligible: no `unload` listener, no `Cache-Control: no-store` on the feed, live connections closed on `pagehide` and reopened on `pageshow`. A restore is not revalidated, so on `pageshow` with `persisted` true I refresh counts and check for new posts. If the post opens as a client-side route, that cache does not apply and the app restores itself: the feed's state — post ids and data, cursor, measured heights, and an anchor post with its offset from the viewport top — is kept outside the feed screen. With `history.scrollRestoration` set to `manual`, the feed re-renders those posts with their known heights, scrolls the anchor back to its offset, then refreshes in the background.
code
json · 9 lines{
"feed": "home",
"anchor": { "postId": "p_8812", "offsetPx": 64 },
"postIds": ["p_9120", "p_9117", "p_8812", "p_8790"],
"heightsPx": { "p_9120": 412, "p_9117": 176, "p_8812": 538 },
"older": { "cursor": "c_8790", "hasMore": true },
"openedPostId": "p_8812",
"savedAt": "2026-10-06T19:42:10Z"
}go deeper
Recall that Back should return the reader to the same post, and that a long, paged feed needs help to do that.
Explain the two cases: a separate document the back/forward cache can restore, and a client-side route the app must restore from saved ids, cursor and anchor.
Keep the feed eligible for the back/forward cache, refresh after a restore, restore by anchor post rather than pixels, return focus, and cap what is kept.
Decide whether post pages are separate documents or routes partly on what Back must feel like, and measure the restore success rate as a product number.
## Why this is hard after two hundred posts On a short page, Back is the browser's problem: it remembers a scroll offset and puts the reader back. A long community-board feed breaks that. The two hundred posts were loaded page by page from a cursor; if the feed is windowed, most of them are not even in the DOM; and their heights were measured as they rendered. A pixel offset into a page whose content no longer exists means nothing. Restoring the place means restoring **the content, its heights and the position within it**. ## Case 1: the post page is a separate document When opening a post is a full navigation to a new document, the browser's **back/forward cache** (bfcache) can keep the feed page as a snapshot in memory — the DOM, the scroll position and the JavaScript heap — and show it again instantly on Back. Nothing has to be rebuilt. The design work is keeping the feed **eligible**: - **No `unload` listener.** Some browsers refuse to cache pages that register one; use `pagehide` instead. - **No `Cache-Control: no-store`** on the feed document unless it truly carries sensitive data; historically that has kept pages out of the cache. - **Close live connections on `pagehide`** and reopen them on `pageshow`; in some browsers an open WebSocket keeps a page out. - **Refresh after a restore.** A bfcache restore comes from memory and is not revalidated with the server, so on `pageshow` with `event.persisted === true`, refetch like counts and check for new posts, patching them in place. The cache is best-effort: pages do not stay in it long, and plenty of things evict them. So Case 1 still needs Case 2 as its fallback. ## Case 2: a client-side route, a cache miss or a reload The back/forward cache works on browser-managed navigations, not on "soft" navigations inside a single-page app. If the post opens as a client-side route, the feed screen is torn down and rebuilt, and the app owns the restore. (The router's own scroll, focus and title handling on each navigation is a separate concern; this is the feed's.) What the feed saves before leaving: | Saved | Why | |---|---| | ordered post ids and their data | rebuild the list without refetching 200 posts | | the older-direction cursor and has-more | keep paging from where the reader was | | measured heights by id | place spacers exactly, so the window lands correctly | | anchor post id and its offset from the viewport top | the reader's place, independent of pixel totals | Where it lives depends on what must survive: 1. **App memory**, held above the feed screen, covers a soft navigation and back. 2. **`history.state` or `sessionStorage`** covers a reload or a cache miss in the same tab. Their space is limited, so store ids, cursor, heights and anchor, and let the data cache or a refetch supply post bodies. Then the restore runs in a fixed order: 1. Set `history.scrollRestoration = "manual"` for the feed, because the browser's automatic restore could jump to an offset before the posts exist. 2. Render the saved posts with their stored heights. 3. Scroll so the anchor post sits at its saved offset. 4. Return focus to the post the reader opened, so a keyboard or screen-reader user is back where they left. 5. Refresh in the background: counts patched in place, new posts behind the banner, never inserted above the reader. ## Why an anchor post beats a pixel offset A pixel offset of 48,000 is only right if every post above has exactly the height it had before. Fonts load differently, an image's box was estimated, a post was edited or deleted. The anchor — "post p_8812, 64px below the top of the viewport" — survives all of that: whatever the heights above, the reader sees the same post at the same spot. ## The memory ceiling A reader who scrolls a thousand posts and goes back and forth all evening cannot keep everything. Cap what the feed keeps — for example the pages around the anchor plus the cursor — and drop the far end. If the reader later scrolls past the dropped region, refetching it is cheaper than a tab that runs out of memory. ## When it cannot be restored A cold load of the post's URL from a shared link has no feed state at all. Back then leads wherever the browser history says, and the feed starts from the top. Getting back to post 200 would need the server to page from a given post, which is a contract decision rather than a client trick.
- How would you tell whether readers actually get their place back?On `pageshow`, record whether `event.persisted` is true for back navigations to the feed; that gives the back/forward cache hit rate for the full-document case. For client-side routes, log whether the saved state was found and the anchor post was reached. Together they show how often readers lose their place.
- Why restore focus to the post the reader opened, not just the scroll position?A keyboard or screen-reader user's place is where focus is, not what is on screen. If focus lands on the page body, they must navigate from the start of the feed to find post 200 again. Focusing the opened post's article puts them back exactly where they were.
Coming back to a long novel after the publisher re-typeset it in a bigger font: the page number you wrote down now lands somewhere else, but the sentence you underlined is still the sentence you were reading. An anchor post is the underlined sentence; a pixel offset is the page number.
- The back/forward cache restores a single-page app's previous route on Back.
- A bfcache restore revalidates the page with the server first, so counts are current.
- Saving the pixel scroll offset is enough to restore a long feed.
- Set Cache-Control: no-store on the feed so Back always shows fresh data.
- Store every loaded post's full body in sessionStorage for the restore.