diff --git a/themes/gitbook/index.js b/themes/gitbook/index.js index 415ad701..34002779 100644 --- a/themes/gitbook/index.js +++ b/themes/gitbook/index.js @@ -243,33 +243,46 @@ const LayoutBase = props => { */ const LayoutIndex = props => { const router = useRouter() - const index = siteConfig('GITBOOK_INDEX_PAGE') + const index = siteConfig('GITBOOK_INDEX_PAGE', 'about', CONFIG) + const [hasRedirected, setHasRedirected] = useState(false) // 添加状态追踪是否已重定向 useEffect(() => { - const checkArticleExists = async () => { - // 这里可以检查文章是否存在 - const article = document.getElementById('notion-article') - if (!article) { - console.log('请检查您的Notion数据库中是否包含此slug页面: ', index) + const tryRedirect = async () => { + if (!hasRedirected) { + // 仅当未重定向时执行 + setHasRedirected(true) // 更新状态,防止多次执行 - // 显示错误信息 - const containerInner = document.querySelector( - '#theme-gitbook #container-inner' - ) - const newHTML = `

配置有误

请在您的notion中添加一个slug为${index}的文章
` - containerInner?.insertAdjacentHTML('afterbegin', newHTML) - } else { - // 如果文章存在,立即重定向 - if (index) { - router.push(index) - } + // 重定向到指定文章 + router.push(index).then(() => { + setTimeout(() => { + const article = document.getElementById('notion-article') + if (!article) { + console.log( + '请检查您的Notion数据库中是否包含此slug页面: ', + index + ) + + // 显示错误信息 + const containerInner = document.querySelector( + '#theme-gitbook #container-inner' + ) + const newHTML = `

配置有误

请在您的notion中添加一个slug为${index}的文章
` + containerInner?.insertAdjacentHTML('afterbegin', newHTML) + } + }, 2000) + }) } } - checkArticleExists() - }, [index, router]) + if (index) { + console.log('重定向', index) + tryRedirect() + } else { + console.log('无重定向', index) + } + }, [index, hasRedirected]) // 将 hasRedirected 作为依赖确保状态变更时更新 - return // 返回 null 以不渲染任何内容 + return null // 不渲染任何内容 } /**