From f1caef10587615e305ebc25adfcebee26ebeb6f6 Mon Sep 17 00:00:00 2001 From: Dariusz Niemczyk Date: Thu, 13 Feb 2025 00:18:19 +0100 Subject: [PATCH] fix: laggy firefox due to replaceState leaking refs: https://bugzilla.mozilla.org/show_bug.cgi?id=1250972 Love me some decade old bugs. --- src/components/nav.tsx | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/components/nav.tsx b/src/components/nav.tsx index c6fda2f..f549469 100644 --- a/src/components/nav.tsx +++ b/src/components/nav.tsx @@ -34,14 +34,21 @@ export function Nav({ rootMargin: "-10px", threshold: 0.5, // Adjust the visibility threshold as needed }; + let timeout: NodeJS.Timeout | null = null; + const observer = new IntersectionObserver((entries) => { + if (timeout) { + clearTimeout(timeout); + } entries.forEach(entry => { const target = entry.target.id as keyof (typeof translations.pl)["nav"] if (entry.isIntersecting) { setActiveSection(target); - if (history.replaceState) { - history.replaceState(null, "", `#${target}`); + if (window.location.hash !== `#${target}` && history.replaceState) { + timeout = setTimeout(() => { + history.replaceState(null, "", `#${target}`) + }, 150) } } });