tfrere HF Staff commited on
Commit
9eab2cc
·
1 Parent(s): e409b8e

fix(router): add retry mechanism for scrollTo element

Browse files

Wait longer and retry up to 10 times to find the element,
in case the page takes time to render.

Files changed (1) hide show
  1. src/App.jsx +8 -3
src/App.jsx CHANGED
@@ -21,13 +21,18 @@ function ScrollToTop() {
21
  const scrollTo = params.get('scrollTo');
22
 
23
  if (scrollTo) {
24
- // Small delay to let the DOM render
25
- setTimeout(() => {
26
  const element = document.getElementById(scrollTo);
27
  if (element) {
28
  element.scrollIntoView({ behavior: 'smooth', block: 'center' });
 
 
 
29
  }
30
- }, 100);
 
 
31
  } else {
32
  // Otherwise scroll to top
33
  window.scrollTo({ top: 0, behavior: 'smooth' });
 
21
  const scrollTo = params.get('scrollTo');
22
 
23
  if (scrollTo) {
24
+ // Retry mechanism to wait for element to be rendered
25
+ const scrollToElement = (retries = 0) => {
26
  const element = document.getElementById(scrollTo);
27
  if (element) {
28
  element.scrollIntoView({ behavior: 'smooth', block: 'center' });
29
+ } else if (retries < 10) {
30
+ // Retry up to 10 times with 100ms interval
31
+ setTimeout(() => scrollToElement(retries + 1), 100);
32
  }
33
+ };
34
+ // Initial delay for page render
35
+ setTimeout(() => scrollToElement(), 300);
36
  } else {
37
  // Otherwise scroll to top
38
  window.scrollTo({ top: 0, behavior: 'smooth' });