Skip to content

fix: keep Ask/Show bodies on restore, stop repeated and wrong-feed stories, and four smaller fixes - #285

Merged
therealsama merged 7 commits into
mainfrom
fix/verified-restore-and-feed-bugs
Oct 5, 2026
Merged

therealsama merged 7 commits into
mainfrom
fix/verified-restore-and-feed-bugs

Conversation

@therealsama

@therealsama therealsama commented Oct 4, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes seven bugs found in a follow-up audit. The first four were reproduced in a production build against the live HN APIs before fixing (phone emulation for the mobile ones). The other three came from reading the code. Each commit is one fix with its tests.

Mobile swipe viewer

  • Ask HN and Show HN posts lost their body after Back or a reload. The saved swipe position stores stories without text to stay small, and the restored copy is what the panel renders. useItemWithComments never fetched an item it was handed, so the body stayed empty, and the text-less copy was written over the cached one. It now fills text from the cached item, or from the item the comments fetch already reads (no extra request), and caches the result. The retry that runs when the connection comes back does the same. Only text is taken, because Firebase normalizes ask/show stories to type: 'story' and would blank the title of a deleted story.
  • Switching tabs during the first load mixed in the old feed's stories. Every feed route renders the same wrapper, so React reused the viewer. A tab switch made before the URL moved to /item/:id (first load still running, or a failed one) kept Top's state, and its in-flight page then landed in Best: in the browser, Best opened on Top's 4th story, and it was still there after a reload because it went into Best's saved position. The viewer is now keyed by feed type, as the domain viewer already is. On a slow connection the window is the whole first load.
  • /#/item/abc showed the Top feed instead of "Item not found". The swipe viewer can only fetch numeric ids, and its NaN id never matched the failed fetch, so it fell back to the feed. The mobile item route now shows the not-found state, with its document title, for any id that isn't all digits. Desktop already did.

Feeds and data

  • Desktop repeated a feed's stories after going Back to it. Opening a story saves the feed's session. Going Back to that feed from another tab keeps the list mounted, so StoryList calls reset(), which restored the session's stories but set the paging cursor back to zero. The next page then loaded the same stories again (Best's 3 became 6 in the e2e mock). reset() now starts from the feed cache, matching its "start fresh" contract and what a header-tab click does.

Smaller fixes

  • Blocked site storage threw from the swipe viewers. Their sessionStorage writes were guarded but the reads weren't, so returning to the tab or a back-forward cache restore threw an uncaught SecurityError. They now keep the offset in memory: both restores run in the page that was hidden, and bfcache keeps the JS heap. The shared scrollRestore helpers would have dropped it after 30 minutes and sent you to the top of the story.
  • A comment's thread link flashed "story". The comment page clears loading before the parent story's title arrives, and the generic "story" fallback showed in between. useCommentDetail now reports itemTitleLoading, so the fallback only appears when the title fetch fails. Until then the placeholder is already a link to the story, because that fetch (a Firebase get()) has no timeout and can wait out a dropped connection.
  • The sanitizer kept class and id. HN never emits them, and they let user-written HTML pick up the app's utility classes or shadow ids the app looks up. Only href is kept.

Testing

  • npm run typecheck, npm run lint and npm run test:run pass on every commit, not just the tip (1072 tests, 15 new).
  • npm run e2e: 842 passed, 42 skipped (chromium, firefox, Mobile Safari, Mobile Chrome), with no retries used.
  • New e2e tests:
    • The Ask HN body survives visiting the author and going Back, and a reload (mobile-swipe.spec.ts).
    • Switching to Best while Top's first load is delayed shows only Best's stories (mobile-swipe.spec.ts).
    • /#/item/abc shows "Item not found" and sets the title (mobile-swipe.spec.ts).
    • On desktop, Back to Best after opening a story and visiting Show still lists exactly 3 stories (story-browsing.spec.ts).
  • Without the fixes, the new e2e tests failed 14/14 runs. Firefox shows the desktop bug as a list stuck without its end-of-feed marker rather than as duplicates. With the fixes they passed 42/42 over three repeats.
  • Each new unit test fails with its fix reverted, with one exception: the test that the swipe scroll offset survives more than 30 minutes away passes on main too. It guards against the expiry the shared scrollRestore helpers would add.

Not changed / known limitations

  • The swipe viewer still only responds to touch swipes. There are no buttons or keys to move between stories, and inactive panels are display: none, so keyboard and screen-reader users on a phone layout can't move through the feed. WCAG 2.5.1 asks for a single-pointer alternative to path gestures. Adding one is a design decision, so it's left for a separate change.
  • On desktop, going Back to a feed through another tab now starts that feed fresh from its cache, as a header-tab click does, rather than restoring the session it saved.
  • The viewers' old __swipe_scrollY and __swipe_comment_scrollY sessionStorage keys are no longer read or written. Values already stored go away when the tab closes.

@therealsama
therealsama requested a review from kplachkov as a code owner October 4, 2026 22:36
The saved swipe position stores stories without `text`, and that copy wins
when the viewer comes back (Back from an author's profile, or a reload), so
an Ask HN or Show HN panel lost its body. useItemWithComments never fetched
the item when given one. It now fills `text` from the cached item, or from
the item the comments fetch already reads, and caches the result.
Opening a story saves the feed's session. Going Back to that feed from
another tab keeps the list mounted, so StoryList calls reset(), which
restored the session's stories but reset the paging cursor to zero. The
next page then loaded the same stories again. reset() now starts from the
feed cache, matching its "start fresh" contract and a header-tab click.
…y opens

Every feed route renders MobileStoryListWrapper, so React reused the same
SwipeStoryViewer across them. A tab switch made before the viewer moved the
URL to /item/:id (the first load still running, or a failed one) kept the
previous feed's stories, and its in-flight page then landed in the new
feed. Key the viewer by feed type, as the domain viewer already is.
/#/item/abc mounted the swipe viewer, which can only fetch numeric ids. Its
NaN id never matched the failed fetch, so the viewer fell back to the Top
feed instead of an error. The mobile item route now shows the not-found
state, with its document title, for any id that isn't all digits. Desktop
already did.
The swipe viewers guarded their sessionStorage writes but not the reads, so
with site storage blocked, returning to the tab (or a back-forward cache
restore) threw an uncaught error. Both restores only run in the page that
was hidden, and bfcache keeps the JS heap, so a ref is enough. The shared
scroll helpers aren't a fit: they drop positions after 30 minutes, which
would send you to the top of the story after a long break.
…oads

A comment page clears `loading` before the parent story's title arrives, and
the generic "story" fallback was shown whenever `loading` was false and the
title was missing. So the link read "story" for a moment, then changed to
the real title. useCommentDetail now reports itemTitleLoading, and until the
title fetch settles the placeholder stays, already as a link to the story:
that fetch has no timeout and can wait out a dropped connection.
The sanitizer kept `class` and `id`, which HN never emits. They let
user-written HTML pick up the app's utility classes or shadow ids the app
looks up. Only `href` is kept now.
@therealsama
therealsama force-pushed the fix/verified-restore-and-feed-bugs branch from 95600cd to 8a08f5a Compare October 5, 2026 11:16
@therealsama
therealsama enabled auto-merge October 5, 2026 11:39
@therealsama
therealsama merged commit 3723539 into main Oct 5, 2026
4 checks passed
@therealsama
therealsama deleted the fix/verified-restore-and-feed-bugs branch October 5, 2026 11:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants