File tree Expand file tree Collapse file tree
apps/sim/app/(landing)/components Expand file tree Collapse file tree Original file line number Diff line number Diff 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
1719const POPSTATE_WINDOW_MS = 200
1820
1921if ( 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 ] )
You can’t perform that action at this time.
0 commit comments