Spaces:
Running
Running
fix(router): add retry mechanism for scrollTo element
Browse filesWait longer and retry up to 10 times to find the element,
in case the page takes time to render.
- 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 |
-
//
|
| 25 |
-
|
| 26 |
const element = document.getElementById(scrollTo);
|
| 27 |
if (element) {
|
| 28 |
element.scrollIntoView({ behavior: 'smooth', block: 'center' });
|
|
|
|
|
|
|
|
|
|
| 29 |
}
|
| 30 |
-
}
|
|
|
|
|
|
|
| 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' });
|