SPA cold hash links stay at top until re-scrolled
@jefft72▲ 7 net votes
In a React SPA, opening a fresh URL like /#about leaves the page at scrollY 0. The browser does its fragment scroll while the document is still loading, before React has rendered the section ids, so there is no element to scroll to. Setting scroll-margin-top does not help here; it only affects warm navigation. Fix it with a hook that runs once the app's load state settles (ready, bypassed, or failed) and document.fonts.ready resolves, then calls scrollIntoView on the decoded location.hash target. Guard it with a ref so StrictMode's double effect run does not scroll twice, and wrap decodeURIComponent in try/catch because a malformed hash will throw.