|
1 | 1 | import Tabs from '@/components/Tabs' |
2 | 2 | import { siteConfig } from '@/lib/config' |
3 | 3 | import { isBrowser, isSearchEngineBot } from '@/lib/utils' |
| 4 | +import { stripTransientQueryParamsFromAsPath } from '@/lib/utils/stripTransientUrlParams' |
4 | 5 | import dynamic from 'next/dynamic' |
5 | 6 | import { useRouter } from 'next/router' |
6 | 7 | import { useEffect, useRef, useState } from 'react' |
@@ -49,19 +50,29 @@ const Comment = ({ frontMatter, className }) => { |
49 | 50 | } |
50 | 51 | }, [frontMatter]) |
51 | 52 |
|
52 | | - // 当连接中有特殊参数时跳转到评论区 |
53 | | - if ( |
54 | | - isBrowser && |
55 | | - ('giscus' in router.query || router.query.target === 'comment') |
56 | | - ) { |
57 | | - setTimeout(() => { |
58 | | - const url = router.asPath.replace('?target=comment', '') |
59 | | - history.replaceState({}, '', url) |
60 | | - document |
61 | | - ?.getElementById('comment') |
62 | | - ?.scrollIntoView({ block: 'start', behavior: 'smooth' }) |
63 | | - }, 1000) |
64 | | - } |
| 53 | + useEffect(() => { |
| 54 | + if (!isBrowser || !router.isReady) { |
| 55 | + return |
| 56 | + } |
| 57 | + const hasGiscus = 'giscus' in router.query |
| 58 | + const scrollComment = router.query.target === 'comment' |
| 59 | + if (!hasGiscus && !scrollComment) { |
| 60 | + return |
| 61 | + } |
| 62 | + const cleanPath = stripTransientQueryParamsFromAsPath(router.asPath) |
| 63 | + window.history.replaceState(window.history.state, '', cleanPath) |
| 64 | + router |
| 65 | + .replace(cleanPath, undefined, { scroll: false, shallow: true }) |
| 66 | + .catch(() => {}) |
| 67 | + if (scrollComment || hasGiscus) { |
| 68 | + const t = window.setTimeout(() => { |
| 69 | + document |
| 70 | + ?.getElementById('comment') |
| 71 | + ?.scrollIntoView({ block: 'start', behavior: 'smooth' }) |
| 72 | + }, 400) |
| 73 | + return () => window.clearTimeout(t) |
| 74 | + } |
| 75 | + }, [router.isReady, router.asPath, router.query]) |
65 | 76 |
|
66 | 77 | if (!frontMatter) { |
67 | 78 | return null |
|
0 commit comments