diff --git a/blog.config.js b/blog.config.js index 50acd55f..5c189b9b 100644 --- a/blog.config.js +++ b/blog.config.js @@ -176,6 +176,8 @@ const BLOG = { POSTS_PER_PAGE: 12, // post counts per page POSTS_SORT_BY: process.env.NEXT_PUBLIC_POST_SORT_BY || 'notion', // 排序方式 'date'按时间,'notion'由notion控制 + POST_WAITING_TIME_FOR_404: process.env.NEXT_PUBLIC_POST_WAITING_TIME_FOR_404 || '8', // 文章加载超时时间,单位秒;超时后跳转到404页面 + ALGOLIA_APP_ID: process.env.NEXT_PUBLIC_ALGOLIA_APP_ID || null, // 在这里查看 https://dashboard.algolia.com/account/api-keys/ ALGOLIA_ADMIN_APP_KEY: process.env.ALGOLIA_ADMIN_APP_KEY || null, // 管理后台的KEY,不要暴露在代码中,在这里查看 https://dashboard.algolia.com/account/api-keys/ ALGOLIA_SEARCH_ONLY_APP_KEY: process.env.NEXT_PUBLIC_ALGOLIA_SEARCH_ONLY_APP_KEY || null, // 客户端搜索用的KEY diff --git a/pages/[prefix]/index.js b/pages/[prefix]/index.js index 2a779790..91f1ec76 100644 --- a/pages/[prefix]/index.js +++ b/pages/[prefix]/index.js @@ -8,7 +8,7 @@ import { getNotion } from '@/lib/notion/getNotion' import { getPageTableOfContents } from '@/lib/notion/getPageTableOfContents' import { getLayoutByTheme } from '@/themes/theme' import md5 from 'js-md5' -import { checkContainHttp, isBrowser } from '@/lib/utils' +import { checkContainHttp } from '@/lib/utils' import { uploadDataToAlgolia } from '@/lib/algolia' import { siteConfig } from '@/lib/config' @@ -20,7 +20,6 @@ import { siteConfig } from '@/lib/config' */ const Slug = props => { const { post } = props - const router = useRouter() // 文章锁🔐 const [lock, setLock] = useState(post?.password && post?.password !== '') @@ -40,20 +39,6 @@ const Slug = props => { // 文章加载 useEffect(() => { - // 404 - if (!post) { - setTimeout(() => { - if (isBrowser) { - const article = document.getElementById('notion-article') - if (!article) { - router.push('/404').then(() => { - console.warn('找不到页面', router.asPath) - }) - } - } - }, 8 * 1000) // 404时长 8秒 - } - // 文章加密 if (post?.password && post?.password !== '') { setLock(true) diff --git a/themes/example/index.js b/themes/example/index.js index 099a334c..f0d6daef 100644 --- a/themes/example/index.js +++ b/themes/example/index.js @@ -147,6 +147,22 @@ const LayoutPostList = props => { */ const LayoutSlug = props => { const { post, lock, validPassword } = props + const router = useRouter() + useEffect(() => { + // 404 + if (!post) { + setTimeout(() => { + if (isBrowser) { + const article = document.getElementById('notion-article') + if (!article) { + router.push('/404').then(() => { + console.warn('找不到页面', router.asPath) + }) + } + } + }, siteConfig('POST_WAITING_TIME_FOR_404') * 1000) + } + }, [post]) return ( <> {lock diff --git a/themes/fukasawa/index.js b/themes/fukasawa/index.js index 159ac237..a9f6aa72 100644 --- a/themes/fukasawa/index.js +++ b/themes/fukasawa/index.js @@ -119,7 +119,23 @@ const LayoutPostList = (props) => { * @returns */ const LayoutSlug = (props) => { - const { lock, validPassword } = props + const { post, lock, validPassword } = props + const router = useRouter() + useEffect(() => { + // 404 + if (!post) { + setTimeout(() => { + if (isBrowser) { + const article = document.getElementById('notion-article') + if (!article) { + router.push('/404').then(() => { + console.warn('找不到页面', router.asPath) + }) + } + } + }, siteConfig('POST_WAITING_TIME_FOR_404') * 1000) + } + }, [post]) return ( <> {lock ? : } diff --git a/themes/gitbook/index.js b/themes/gitbook/index.js index 928ec9ad..10d88378 100644 --- a/themes/gitbook/index.js +++ b/themes/gitbook/index.js @@ -212,7 +212,22 @@ const LayoutPostList = (props) => { */ const LayoutSlug = (props) => { const { post, prev, next, lock, validPassword } = props - + const router = useRouter() + useEffect(() => { + // 404 + if (!post) { + setTimeout(() => { + if (isBrowser) { + const article = document.getElementById('notion-article') + if (!article) { + router.push('/404').then(() => { + console.warn('找不到页面', router.asPath) + }) + } + } + }, siteConfig('POST_WAITING_TIME_FOR_404') * 1000) + } + }, [post]) return ( <> {/* 文章锁 */} diff --git a/themes/heo/index.js b/themes/heo/index.js index e32a59c6..1fdb1fd7 100644 --- a/themes/heo/index.js +++ b/themes/heo/index.js @@ -40,6 +40,7 @@ import WWAds from '@/components/WWAds' import { AdSlot } from '@/components/GoogleAdsense' import { siteConfig } from '@/lib/config' import { LAYOUT_MAPPINGS } from '@/blog.config' +import { isBrowser } from '@/lib/utils' /** * 基础布局 采用上中下布局,移动端使用顶部侧边导航栏 @@ -267,6 +268,22 @@ const LayoutSlug = props => { siteConfig('COMMENT_GISCUS_REPO') || siteConfig('COMMENT_CUSDIS_APP_ID') || siteConfig('COMMENT_UTTERRANCES_REPO') || siteConfig('COMMENT_GITALK_CLIENT_ID') || siteConfig('COMMENT_WEBMENTION_ENABLE') + const router = useRouter() + useEffect(() => { + // 404 + if (!post) { + setTimeout(() => { + if (isBrowser) { + const article = document.getElementById('notion-article') + if (!article) { + router.push('/404').then(() => { + console.warn('找不到页面', router.asPath) + }) + } + } + }, siteConfig('POST_WAITING_TIME_FOR_404') * 1000) + } + }, [post]) return ( <>
diff --git a/themes/hexo/index.js b/themes/hexo/index.js index 46660580..fa467a0f 100644 --- a/themes/hexo/index.js +++ b/themes/hexo/index.js @@ -223,7 +223,22 @@ const LayoutArchive = (props) => { */ const LayoutSlug = props => { const { post, lock, validPassword } = props - + const router = useRouter() + useEffect(() => { + // 404 + if (!post) { + setTimeout(() => { + if (isBrowser) { + const article = document.getElementById('notion-article') + if (!article) { + router.push('/404').then(() => { + console.warn('找不到页面', router.asPath) + }) + } + } + }, siteConfig('POST_WAITING_TIME_FOR_404') * 1000) + } + }, [post]) return ( <>
diff --git a/themes/landing/index.js b/themes/landing/index.js index cee99448..8944f522 100644 --- a/themes/landing/index.js +++ b/themes/landing/index.js @@ -22,6 +22,7 @@ import { isBrowser } from '@/lib/utils' import { siteConfig } from '@/lib/config' import { Pricing } from './components/Pricing' import { LAYOUT_MAPPINGS } from '@/blog.config' +import { useEffect } from 'react' /** * 布局框架 @@ -72,8 +73,26 @@ const LayoutIndex = (props) => { * @returns */ const LayoutSlug = (props) => { + const { post } = props + // 如果 是 /article/[slug] 的文章路径则进行重定向到另一个域名 const router = useRouter() + useEffect(() => { + // 404 + if (!post) { + setTimeout(() => { + if (isBrowser) { + const article = document.getElementById('notion-article') + if (!article) { + router.push('/404').then(() => { + console.warn('找不到页面', router.asPath) + }) + } + } + }, siteConfig('POST_WAITING_TIME_FOR_404') * 1000) + } + }, [post]) + if (JSON.parse(siteConfig('LANDING_POST_REDIRECT_ENABLE', null, CONFIG)) && isBrowser && router.route === '/[prefix]/[slug]') { const redirectUrl = siteConfig('LANDING_POST_REDIRECT_URL', null, CONFIG) + router.asPath.replace('?theme=landing', '') router.push(redirectUrl) diff --git a/themes/matery/index.js b/themes/matery/index.js index 81bcc469..b9cd8add 100644 --- a/themes/matery/index.js +++ b/themes/matery/index.js @@ -31,6 +31,7 @@ import { Style } from './style' import replaceSearchResult from '@/components/Mark' import { siteConfig } from '@/lib/config' import { LAYOUT_MAPPINGS } from '@/blog.config' +import { isBrowser } from '@/lib/utils' /** * 基础布局 @@ -198,7 +199,22 @@ const LayoutArchive = (props) => { const LayoutSlug = props => { const { post, lock, validPassword } = props const { fullWidth } = useGlobal() - + const router = useRouter() + useEffect(() => { + // 404 + if (!post) { + setTimeout(() => { + if (isBrowser) { + const article = document.getElementById('notion-article') + if (!article) { + router.push('/404').then(() => { + console.warn('找不到页面', router.asPath) + }) + } + } + }, siteConfig('POST_WAITING_TIME_FOR_404') * 1000) + } + }, [post]) return (<>
diff --git a/themes/medium/index.js b/themes/medium/index.js index 608e498c..62170f8e 100644 --- a/themes/medium/index.js +++ b/themes/medium/index.js @@ -156,6 +156,23 @@ const LayoutSlug = props => {
) + const router = useRouter() + useEffect(() => { + // 404 + if (!post) { + setTimeout(() => { + if (isBrowser) { + const article = document.getElementById('notion-article') + if (!article) { + router.push('/404').then(() => { + console.warn('找不到页面', router.asPath) + }) + } + } + }, siteConfig('POST_WAITING_TIME_FOR_404') * 1000) + } + }, [post]) + return (
{/* 文章锁 */} diff --git a/themes/nav/index.js b/themes/nav/index.js index 3defaabf..a793e3e2 100755 --- a/themes/nav/index.js +++ b/themes/nav/index.js @@ -35,6 +35,8 @@ import Live2D from '@/components/Live2D' import BlogArchiveItem from './components/BlogArchiveItem' import NotionIcon from '@/components/NotionIcon' import { LAYOUT_MAPPINGS } from '@/blog.config' +import { useRouter } from 'next/router' +import { isBrowser } from '@/lib/utils' const WWAds = dynamic(() => import('@/components/WWAds'), { ssr: false }) @@ -215,6 +217,22 @@ const LayoutPostList = props => { */ const LayoutSlug = (props) => { const { post, lock, validPassword } = props + const router = useRouter() + useEffect(() => { + // 404 + if (!post) { + setTimeout(() => { + if (isBrowser) { + const article = document.getElementById('notion-article') + if (!article) { + router.push('/404').then(() => { + console.warn('找不到页面', router.asPath) + }) + } + } + }, siteConfig('POST_WAITING_TIME_FOR_404') * 1000) + } + }, [post]) return ( <> {/* 文章锁 */} diff --git a/themes/next/index.js b/themes/next/index.js index bc0c5f83..7e327774 100644 --- a/themes/next/index.js +++ b/themes/next/index.js @@ -277,6 +277,23 @@ const LayoutArchive = (props) => { */ const LayoutSlug = (props) => { const { post, lock, validPassword } = props + + const router = useRouter() + useEffect(() => { + // 404 + if (!post) { + setTimeout(() => { + if (isBrowser) { + const article = document.getElementById('notion-article') + if (!article) { + router.push('/404').then(() => { + console.warn('找不到页面', router.asPath) + }) + } + } + }, siteConfig('POST_WAITING_TIME_FOR_404') * 1000) + } + }, [post]) return ( <> diff --git a/themes/nobelium/index.js b/themes/nobelium/index.js index cdeb8a3e..b7cc2b08 100644 --- a/themes/nobelium/index.js +++ b/themes/nobelium/index.js @@ -25,6 +25,7 @@ import replaceSearchResult from '@/components/Mark' import AlgoliaSearchModal from '@/components/AlgoliaSearchModal' import { siteConfig } from '@/lib/config' import { LAYOUT_MAPPINGS } from '@/blog.config' +import { useRouter } from 'next/router' // 主题全局状态 const ThemeGlobalNobelium = createContext() @@ -199,7 +200,22 @@ const LayoutArchive = props => { */ const LayoutSlug = props => { const { post, lock, validPassword } = props - + const router = useRouter() + useEffect(() => { + // 404 + if (!post) { + setTimeout(() => { + if (isBrowser) { + const article = document.getElementById('notion-article') + if (!article) { + router.push('/404').then(() => { + console.warn('找不到页面', router.asPath) + }) + } + } + }, siteConfig('POST_WAITING_TIME_FOR_404') * 1000) + } + }, [post]) return ( <> diff --git a/themes/plog/index.js b/themes/plog/index.js index 36e30a56..d4b0836a 100644 --- a/themes/plog/index.js +++ b/themes/plog/index.js @@ -165,7 +165,22 @@ const LayoutArchive = props => { */ const LayoutSlug = props => { const { post, lock, validPassword } = props - + const router = useRouter() + useEffect(() => { + // 404 + if (!post) { + setTimeout(() => { + if (isBrowser) { + const article = document.getElementById('notion-article') + if (!article) { + router.push('/404').then(() => { + console.warn('找不到页面', router.asPath) + }) + } + } + }, siteConfig('POST_WAITING_TIME_FOR_404') * 1000) + } + }, [post]) return ( <> diff --git a/themes/simple/index.js b/themes/simple/index.js index 62cdad60..b7598819 100644 --- a/themes/simple/index.js +++ b/themes/simple/index.js @@ -12,6 +12,7 @@ import dynamic from 'next/dynamic' import NotionPage from '@/components/NotionPage' import AlgoliaSearchModal from '@/components/AlgoliaSearchModal' import { LAYOUT_MAPPINGS } from '@/blog.config' +import { useRouter } from 'next/router' // 主题组件 const BlogListScroll = dynamic(() => import('./components/BlogListScroll'), { ssr: false }); @@ -217,6 +218,23 @@ const LayoutSlug = props => { * @returns */ const Layout404 = (props) => { + const { post } = props + const router = useRouter() + useEffect(() => { + // 404 + if (!post) { + setTimeout(() => { + if (isBrowser) { + const article = document.getElementById('notion-article') + if (!article) { + router.push('/404').then(() => { + console.warn('找不到页面', router.asPath) + }) + } + } + }, siteConfig('POST_WAITING_TIME_FOR_404') * 1000) + } + }, [post]) return <> 404 Not found.