diff --git a/components/BlogPostCard.js b/components/BlogPostCard.js index 19852627..b8a4f9af 100644 --- a/components/BlogPostCard.js +++ b/components/BlogPostCard.js @@ -12,7 +12,7 @@ const BlogPostCard = ({ post, tags }) => { w-full bg-white dark:bg-gray-800 dark:hover:bg-gray-700'>
- 1) ? post.page_cover : BLOG.defaultImgCover} alt={post.title} layout='fill' objectFit='cover' loading='lazy' /> + 1) ? post.page_cover : BLOG.defaultImgCover} alt={post.title} layout='fill' objectFit='cover' loading='lazy' />
diff --git a/components/LoadingCover.js b/components/LoadingCover.js new file mode 100644 index 00000000..3b9910f5 --- /dev/null +++ b/components/LoadingCover.js @@ -0,0 +1,11 @@ +import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' +import { faSpinner } from '@fortawesome/free-solid-svg-icons' + +export default function LoadingCover () { + return (
+
+ +
+
+ ) +} diff --git a/layouts/BaseLayout.js b/layouts/BaseLayout.js index 80f93915..0e9bf7a2 100644 --- a/layouts/BaseLayout.js +++ b/layouts/BaseLayout.js @@ -9,6 +9,7 @@ import SideBar from '@/components/SideBar' import JumpToTopButton from '@/components/JumpToTopButton' import { useGlobal } from '@/lib/global' import DarkModeButton from '@/components/DarkModeButton' +import LoadingCover from '@/components/LoadingCover' /** * 基础布局 采用左右两侧布局,移动端使用顶部导航栏 @@ -61,7 +62,7 @@ const BaseLayout = ({ window.removeEventListener('scroll', scrollTrigger) } }) - const { theme } = useGlobal() + const { onLoading, theme } = useGlobal() const targetRef = useRef(null) return ( @@ -77,9 +78,13 @@ const BaseLayout = ({
-
- {children} -
+ + {onLoading + ? + :
+ {children} +
+ }
diff --git a/lib/global.js b/lib/global.js index a228295f..d8c25e63 100644 --- a/lib/global.js +++ b/lib/global.js @@ -1,6 +1,7 @@ import lang from './lang' import { useContext, createContext, useState, useEffect } from 'react' import cookie from 'react-cookies' +import Router from 'next/router' const GlobalContext = createContext() @@ -13,6 +14,15 @@ const GlobalContext = createContext() export function GlobalContextProvider ({ children }) { const [locale, changeLocale] = useState(generateLocaleDict('en-US')) const [theme, changeTheme] = useState(loadUserThemeFromCookies()) + const [onLoading, changeLoadingState] = useState(false) + Router.events.on('routeChangeStart', (...args) => { + changeLoadingState(true) + }) + + Router.events.on('routeChangeComplete', (...args) => { + changeLoadingState(false) + }) + // 服务端静态渲染,在渲染hooks后根据前端变量做初始化工作 useEffect(() => { initTheme(theme, changeTheme) @@ -20,7 +30,9 @@ export function GlobalContextProvider ({ children }) { }) return ( - {children} + + {children} + ) }