gitbook 重定向bug

This commit is contained in:
tangly1024.com
2024-09-27 10:06:14 +08:00
parent 6e6d8eef50
commit a749e3b2c5

View File

@@ -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 = `<h1 class="text-3xl pt-12 dark:text-gray-300">配置有误</h1><blockquote class="notion-quote notion-block-ce76391f3f2842d386468ff1eb705b92"><div>请在您的notion中添加一个slug为${index}的文章</div></blockquote>`
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 = `<h1 class="text-3xl pt-12 dark:text-gray-300">配置有误</h1><blockquote class="notion-quote notion-block-ce76391f3f2842d386468ff1eb705b92"><div>请在您的notion中添加一个slug为${index}的文章</div></blockquote>`
containerInner?.insertAdjacentHTML('afterbegin', newHTML)
}
}, 2000)
})
}
}
checkArticleExists()
}, [index, router])
if (index) {
console.log('重定向', index)
tryRedirect()
} else {
console.log('无重定向', index)
}
}, [index, hasRedirected]) // 将 hasRedirected 作为依赖确保状态变更时更新
return <LoadingCover /> // 返回 null 以不渲染任何内容
return null // 不渲染任何内容
}
/**