diff --git a/.env.local b/.env.local index db9decf2..073b8bbb 100644 --- a/.env.local +++ b/.env.local @@ -1,5 +1,5 @@ # 环境变量 @see https://www.nextjs.cn/docs/basic-features/environment-variables -NEXT_PUBLIC_VERSION=4.4.3 +NEXT_PUBLIC_VERSION=4.4.6 # 可在此添加环境变量,去掉最左边的(# )注释即可 @@ -62,6 +62,7 @@ NEXT_PUBLIC_VERSION=4.4.3 # NEXT_PUBLIC_ALGOLIA_INDEX= # NEXT_PUBLIC_PREVIEW_CATEGORY_COUNT= # NEXT_PUBLIC_PREVIEW_TAG_COUNT= +# NEXT_PUBLIC_POST_TITLE_ICON= # NEXT_PUBLIC_POST_DISABLE_GALLERY_CLICK= # NEXT_PUBLIC_FIREWORKS= # NEXT_PUBLIC_FIREWORKS_COLOR= diff --git a/blog.config.js b/blog.config.js index abfc4cd2..2eb564ab 100644 --- a/blog.config.js +++ b/blog.config.js @@ -9,7 +9,7 @@ const BLOG = { THEME: process.env.NEXT_PUBLIC_THEME || 'simple', // 当前主题,在themes文件夹下可找到所有支持的主题;主题名称就是文件夹名,例如 example,fukasawa,gitbook,heo,hexo,landing,matery,medium,next,nobelium,plog,simple THEME_SWITCH: process.env.NEXT_PUBLIC_THEME_SWITCH || false, // 是否显示切换主题按钮 LANG: process.env.NEXT_PUBLIC_LANG || 'zh-CN', // e.g 'zh-CN','en-US' see /lib/lang.js for more. - SINCE: process.env.NEXT_SINCE || 2021, // e.g if leave this empty, current year will be used. + SINCE: process.env.NEXT_PUBLIC_SINCE || 2021, // e.g if leave this empty, current year will be used. APPEARANCE: process.env.NEXT_PUBLIC_APPEARANCE || 'light', // ['light', 'dark', 'auto'], // light 日间模式 , dark夜间模式, auto根据时间和主题自动夜间模式 APPEARANCE_DARK_TIME: process.env.NEXT_PUBLIC_APPEARANCE_DARK_TIME || [18, 6], // 夜间模式起至时间,false时关闭根据时间自动切换夜间模式 @@ -230,6 +230,7 @@ const BLOG = { PREVIEW_CATEGORY_COUNT: 16, // 首页最多展示的分类数量,0为不限制 PREVIEW_TAG_COUNT: 16, // 首页最多展示的标签数量,0为不限制 + POST_TITLE_ICON: process.env.NEXT_PUBLIC_POST_TITLE_ICON || true, // 是否显示标题icon POST_DISABLE_GALLERY_CLICK: process.env.NEXT_PUBLIC_POST_DISABLE_GALLERY_CLICK || false, // 画册视图禁止点击,方便在友链页面的画册插入链接 diff --git a/components/Comment.js b/components/Comment.js index 4f4107b8..ba0d4256 100644 --- a/components/Comment.js +++ b/components/Comment.js @@ -1,9 +1,10 @@ -import dynamic from 'next/dynamic' import Tabs from '@/components/Tabs' -import { isBrowser, isSearchEngineBot } from '@/lib/utils' -import { useRouter } from 'next/router' -import Artalk from './Artalk' import { siteConfig } from '@/lib/config' +import { isBrowser, isSearchEngineBot } from '@/lib/utils' +import dynamic from 'next/dynamic' +import { useRouter } from 'next/router' +import { useEffect, useRef, useState } from 'react' +import Artalk from './Artalk' const WalineComponent = dynamic( () => { @@ -57,11 +58,14 @@ const ValineComponent = dynamic(() => import('@/components/ValineComponent'), { /** * 评论组件 + * 只有当前组件在浏览器可见范围内才会加载内容 * @param {*} param0 * @returns */ -const Comment = ({ siteInfo, frontMatter, className }) => { +const Comment = ({ frontMatter, className }) => { const router = useRouter() + const [shouldLoad, setShouldLoad] = useState(false) + const commentRef = useRef(null) const COMMENT_ARTALK_SERVER = siteConfig('COMMENT_ARTALK_SERVER') const COMMENT_TWIKOO_ENV_ID = siteConfig('COMMENT_TWIKOO_ENV_ID') @@ -73,16 +77,39 @@ const Comment = ({ siteInfo, frontMatter, className }) => { const COMMENT_GITALK_CLIENT_ID = siteConfig('COMMENT_GITALK_CLIENT_ID') const COMMENT_WEBMENTION_ENABLE = siteConfig('COMMENT_WEBMENTION_ENABLE') - if (isSearchEngineBot()) { - return null - } + useEffect(() => { + // Check if the component is visible in the viewport + const observer = new IntersectionObserver(entries => { + entries.forEach(entry => { + if (entry.isIntersecting) { + setShouldLoad(true) + observer.unobserve(entry.target) + } + }) + }) + + if (commentRef.current) { + observer.observe(commentRef.current) + } + + return () => { + if (commentRef.current) { + observer.unobserve(commentRef.current) + } + } + }, [frontMatter]) // 当连接中有特殊参数时跳转到评论区 - if (isBrowser && ('giscus' in router.query || router.query.target === 'comment')) { + if ( + isBrowser && + ('giscus' in router.query || router.query.target === 'comment') + ) { setTimeout(() => { const url = router.asPath.replace('?target=comment', '') history.replaceState({}, '', url) - document?.getElementById('comment')?.scrollIntoView({ block: 'start', behavior: 'smooth' }) + document + ?.getElementById('comment') + ?.scrollIntoView({ block: 'start', behavior: 'smooth' }) }, 1000) } @@ -90,48 +117,85 @@ const Comment = ({ siteInfo, frontMatter, className }) => { return <>Loading... } + if (isSearchEngineBot()) { + return null + } + return ( -
- - {COMMENT_ARTALK_SERVER && (
- -
)} - - {COMMENT_TWIKOO_ENV_ID && (
- -
)} - - {COMMENT_WALINE_SERVER_URL && (
- -
)} - - {COMMENT_VALINE_APP_ID && (
- -
)} - - {COMMENT_GISCUS_REPO && ( -
- -
- )} - - {COMMENT_CUSDIS_APP_ID && (
- -
)} - - {COMMENT_UTTERRANCES_REPO && (
- -
)} - - {COMMENT_GITALK_CLIENT_ID && (
- -
)} - - {COMMENT_WEBMENTION_ENABLE && (
- -
)} -
+
+ {/* 延迟加载评论区 */} + {!shouldLoad && ( +
+ Loading... +
+ )} + + {shouldLoad && ( + + {COMMENT_ARTALK_SERVER && ( +
+ +
+ )} + + {COMMENT_TWIKOO_ENV_ID && ( +
+ +
+ )} + + {COMMENT_WALINE_SERVER_URL && ( +
+ +
+ )} + + {COMMENT_VALINE_APP_ID && ( +
+ +
+ )} + + {COMMENT_GISCUS_REPO && ( +
+ +
+ )} + + {COMMENT_CUSDIS_APP_ID && ( +
+ +
+ )} + + {COMMENT_UTTERRANCES_REPO && ( +
+ +
+ )} + + {COMMENT_GITALK_CLIENT_ID && ( +
+ +
+ )} + + {COMMENT_WEBMENTION_ENABLE && ( +
+ +
+ )} +
+ )} +
) } diff --git a/components/ExternalPlugins.js b/components/ExternalPlugins.js index 477bdce3..b77f960d 100644 --- a/components/ExternalPlugins.js +++ b/components/ExternalPlugins.js @@ -100,8 +100,8 @@ const ExternalPlugin = props => { const CHATBASE_ID = siteConfig('CHATBASE_ID') const COMMENT_DAO_VOICE_ID = siteConfig('COMMENT_DAO_VOICE_ID') const AD_WWADS_ID = siteConfig('AD_WWADS_ID') - const COMMENT_TWIKOO_ENV_ID = siteConfig('COMMENT_TWIKOO_ENV_ID') - const COMMENT_TWIKOO_CDN_URL = siteConfig('COMMENT_TWIKOO_CDN_URL') + // const COMMENT_TWIKOO_ENV_ID = siteConfig('COMMENT_TWIKOO_ENV_ID') + // const COMMENT_TWIKOO_CDN_URL = siteConfig('COMMENT_TWIKOO_CDN_URL') const COMMENT_ARTALK_SERVER = siteConfig('COMMENT_ARTALK_SERVER') const COMMENT_ARTALK_JS = siteConfig('COMMENT_ARTALK_JS') const COMMENT_TIDIO_ID = siteConfig('COMMENT_TIDIO_ID') @@ -152,12 +152,16 @@ const ExternalPlugin = props => { } useEffect(() => { + // 异步渲染谷歌广告 if (ADSENSE_GOOGLE_ID) { setTimeout(() => { - // 异步渲染谷歌广告 initGoogleAdsense() }, 1000) } + + // 执行注入脚本 + // eslint-disable-next-line no-eval + eval(GLOBAL_JS) }, []) if (DISABLE_PLUGIN) { @@ -206,16 +210,6 @@ const ExternalPlugin = props => { )} - {/* 注入JS脚本 */} - {GLOBAL_JS && ( - )} - {COMMENT_TWIKOO_ENV_ID &&