Skip to content

Commit 1cfa7fd

Browse files
committed
fix: Lost footer
1 parent cbc4a6c commit 1cfa7fd

File tree

1 file changed

+91
-9
lines changed

1 file changed

+91
-9
lines changed

docs/assets/js/footer-menu.js

Lines changed: 91 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -391,14 +391,25 @@ const DEFAULT_FOOTER_MENUS = [
391391
}
392392
]
393393

394+
let cachedFooterMenus = null;
395+
394396
async function fetchFooterMenus() {
397+
// If we already have cached data, return it
398+
if (cachedFooterMenus) {
399+
return cachedFooterMenus;
400+
}
401+
395402
try {
396403
const response = await fetch('https://www.bnbchain.org/api/v1/landing-menus');
397404
const data = await response.json();
398-
return data.footerMenus ?? DEFAULT_FOOTER_MENUS;
405+
// Cache the response
406+
cachedFooterMenus = data.footerMenus ?? DEFAULT_FOOTER_MENUS;
407+
return cachedFooterMenus;
399408
} catch (error) {
400409
console.error('Error fetching footer menus:', error);
401-
return DEFAULT_FOOTER_MENUS;
410+
// Cache the default menus in case of error
411+
cachedFooterMenus = DEFAULT_FOOTER_MENUS;
412+
return cachedFooterMenus;
402413
}
403414
}
404415

@@ -424,21 +435,92 @@ function createMenuHTML(menu) {
424435
`;
425436
}
426437

427-
async function initializeFooterMenus() {
428-
const footerMenus = await fetchFooterMenus();
438+
let observer = null;
439+
let isUpdating = false;
440+
441+
function setupObserver() {
442+
if (observer) {
443+
observer.disconnect();
444+
}
445+
446+
const targetNode = document.body;
447+
const config = { childList: true, subtree: true };
448+
449+
const callback = (mutationsList, observer) => {
450+
// Prevent recursive calls
451+
if (isUpdating) {
452+
return;
453+
}
454+
455+
for (const mutation of mutationsList) {
456+
if (mutation.type === 'childList') {
457+
const footerInner = document.querySelector('.doc-footer__inner');
458+
const copyrightInner = document.querySelector('.doc-copyright__inner');
459+
460+
// Only update if footer elements are missing or empty
461+
if ((!footerInner || !footerInner.children.length) ||
462+
(!copyrightInner || !copyrightInner.children.length)) {
463+
464+
// Set flag before updating
465+
isUpdating = true;
466+
467+
// Use setTimeout to break the synchronous execution chain
468+
setTimeout(() => {
469+
initializeFooterMenus();
470+
isUpdating = false;
471+
}, 0);
472+
473+
break;
474+
}
475+
}
476+
}
477+
};
478+
479+
observer = new MutationObserver(callback);
480+
observer.observe(targetNode, config);
481+
}
482+
483+
// Initialize footer content
484+
function initializeFooterMenus() {
485+
// Skip if already has content
486+
const footerInner = document.querySelector('.doc-footer__inner');
487+
const copyrightElement = document.querySelector('.doc-copyright__inner');
488+
489+
if (footerInner?.children.length && copyrightElement?.children.length) {
490+
return;
491+
}
492+
493+
// If we have cached data, use it immediately
494+
if (cachedFooterMenus) {
495+
updateFooterContent(cachedFooterMenus);
496+
return;
497+
}
498+
499+
// Only fetch if we don't have cached data
500+
fetchFooterMenus().then(footerMenus => {
501+
updateFooterContent(footerMenus);
502+
});
503+
}
504+
505+
// Update footer content without triggering observer
506+
function updateFooterContent(footerMenus) {
429507
if (!footerMenus) return;
430508

431509
const footerInner = document.querySelector('.doc-footer__inner');
432-
if (footerInner) {
510+
const copyrightElement = document.querySelector('.doc-copyright__inner');
511+
512+
if (footerInner && (!footerInner.children.length)) {
433513
footerInner.innerHTML = footerMenus.map(menu => createMenuHTML(menu)).join('');
434514
}
435515

436-
// Update copyright year
437-
const copyrightElement = document.querySelector('.doc-copyright__inner');
438-
if (copyrightElement) {
516+
if (copyrightElement && (!copyrightElement.children.length)) {
439517
const currentYear = new Date().getFullYear();
440518
copyrightElement.innerHTML = ${currentYear} Bnbchain.org. All rights reserved.`;
441519
}
442520
}
443521

444-
document.addEventListener('DOMContentLoaded', initializeFooterMenus);
522+
// Handle initial page load
523+
document.addEventListener('DOMContentLoaded', () => {
524+
initializeFooterMenus();
525+
setupObserver();
526+
});

0 commit comments

Comments
 (0)