Skip to content

Commit fb02c2a

Browse files
committed
fix(landing-nav): use -Infinity sentinel and consume popstate timestamp on use
1 parent 2ecfa51 commit fb02c2a

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

apps/sim/app/(landing)/components/scroll-to-top.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ import { usePathname } from 'next/navigation'
99
* where the outgoing layout's component instances unmount before the incoming
1010
* layout's effect runs.
1111
*
12-
* A timestamp window (rather than a sticky boolean) self-expires the signal —
13-
* a hash-only back/forward fires popstate but doesn't change `usePathname`, so
14-
* a boolean would never be consumed and would poison the next real navigation.
12+
* Initialized to `-Infinity` so initial mounts don't mimic a recent popstate.
13+
* Consumed on first use (reset back to `-Infinity`) so a real link navigation
14+
* immediately after Back isn't swallowed. The timestamp window provides a
15+
* safety net for popstates that never trigger a pathname effect (e.g.,
16+
* hash-only back/forward), letting the signal self-expire.
1517
*/
16-
let lastPopstateAt = 0
18+
let lastPopstateAt = Number.NEGATIVE_INFINITY
1719
const POPSTATE_WINDOW_MS = 200
1820

1921
if (typeof window !== 'undefined') {
@@ -36,7 +38,10 @@ export function ScrollToTop() {
3638
const pathname = usePathname()
3739

3840
useEffect(() => {
39-
if (performance.now() - lastPopstateAt < POPSTATE_WINDOW_MS) return
41+
if (performance.now() - lastPopstateAt < POPSTATE_WINDOW_MS) {
42+
lastPopstateAt = Number.NEGATIVE_INFINITY
43+
return
44+
}
4045
if (window.location.hash) return
4146
window.scrollTo(0, 0)
4247
}, [pathname])

0 commit comments

Comments
 (0)