diff --git a/pages/_app.js b/pages/_app.js index da9b3efc..463aa454 100644 --- a/pages/_app.js +++ b/pages/_app.js @@ -9,17 +9,70 @@ import '@/styles/notion.css' // 重写部分样式 import 'aos/dist/aos.css' // You can also use for styles import { GlobalContextProvider } from '@/lib/global' +import { isBrowser, loadExternalResource } from '@/lib/utils' +import { getGlobalLayoutByTheme } from '@/themes/theme' +import { useRouter } from 'next/router' +import { useCallback, useMemo } from 'react' +import { getQueryParam } from '../lib/utils' // 各种扩展插件 这个要阻塞引入 import ExternalPlugins from '@/components/ExternalPlugins' +import { CUSTOM_EXTERNAL_CSS, CUSTOM_EXTERNAL_JS, IMG_SHADOW, THEME } from '@/blog.config' const MyApp = ({ Component, pageProps }) => { + /** + * 首页布局 + * @param {*} props + * @returns + */ + const route = useRouter() + const queryParam = useMemo(() => { + return getQueryParam(route.asPath, 'theme') || THEME + }, [route]) + + const GLayout = useCallback( + props => { + // 根据页面路径加载不同Layout文件 + const Layout = getGlobalLayoutByTheme(queryParam) + return + }, + [queryParam] + ) + + // 自定义样式css和js引入 + if (isBrowser) { + // 初始化AOS动画 + // 静态导入本地自定义样式 + loadExternalResource('/css/custom.css', 'css') + loadExternalResource('/js/custom.js', 'js') + + // 自动添加图片阴影 + if (IMG_SHADOW) { + loadExternalResource('/css/img-shadow.css', 'css') + } + + // 导入外部自定义脚本 + if (CUSTOM_EXTERNAL_JS && CUSTOM_EXTERNAL_JS.length > 0) { + for (const url of CUSTOM_EXTERNAL_JS) { + loadExternalResource(url, 'js') + } + } + + // 导入外部自定义样式 + if (CUSTOM_EXTERNAL_CSS && CUSTOM_EXTERNAL_CSS.length > 0) { + for (const url of CUSTOM_EXTERNAL_CSS) { + loadExternalResource(url, 'css') + } + } + } + return ( - - - {/* 全局插件 , 自定义样式、组件等在这里统一引入 */} - - + + + + + + ) } diff --git a/themes/next/index.js b/themes/next/index.js index cb702903..9e0b1c4a 100644 --- a/themes/next/index.js +++ b/themes/next/index.js @@ -32,6 +32,7 @@ import { siteConfig } from '@/lib/config' * @returns {JSX.Element} * @constructor */ +let counter = 0 const LayoutBase = (props) => { const { children, headerSlot, floatSlot, rightAreaSlot, meta } = props const { onLoading } = useGlobal() @@ -39,7 +40,9 @@ const LayoutBase = (props) => { const floatButtonGroup = useRef(null) const [showRightFloat, switchShow] = useState(false) const [percent, changePercent] = useState(0) // 页面阅读百分比 - + useEffect(()=>{ + console.log('sidebar 渲染次数: ', counter++) + }, []) const scrollListener = () => { const targetRef = document.getElementById('wrapper') const clientHeight = targetRef?.clientHeight @@ -140,7 +143,7 @@ const LayoutIndex = (props) => { * @returns */ const LayoutPostList = (props) => { - return + return
@@ -148,7 +151,7 @@ const LayoutPostList = (props) => { ? : } - +
} /** @@ -174,7 +177,7 @@ const LayoutSearch = (props) => { }, []) return ( - +
{' '} @@ -187,7 +190,7 @@ const LayoutSearch = (props) => { : }
- +
) } @@ -210,7 +213,7 @@ const Layout404 = props => { }, 3000) }, []) - return + return

404

@@ -219,7 +222,7 @@ const Layout404 = props => {
-
+ } /** @@ -231,7 +234,7 @@ const LayoutArchive = (props) => { const { archivePosts } = props return ( - +
{Object.keys(archivePosts).map(archiveTitle => ( { /> ))}
- +
) } @@ -261,7 +264,7 @@ const LayoutSlug = (props) => { return ( - +
{post && !lock && } @@ -272,7 +275,7 @@ const LayoutSlug = (props) => {
} -
+ ) } @@ -285,7 +288,7 @@ const LayoutCategoryIndex = (props) => { const { allPosts, categoryOptions } = props const { locale } = useGlobal() return ( - +
{locale.COMMON.CATEGORY}: @@ -307,7 +310,7 @@ const LayoutCategoryIndex = (props) => { })}
- +
) } @@ -319,7 +322,7 @@ const LayoutCategoryIndex = (props) => { const LayoutTagIndex = (props) => { const { tagOptions } = props const { locale } = useGlobal() - return + return
{locale.COMMON.TAGS}:
@@ -328,11 +331,12 @@ const LayoutTagIndex = (props) => { })}
- +
} export { CONFIG as THEME_CONFIG, + LayoutBase, LayoutIndex, LayoutSearch, LayoutArchive, @@ -340,5 +344,5 @@ export { Layout404, LayoutCategoryIndex, LayoutPostList, - LayoutTagIndex + LayoutTagIndex, } diff --git a/themes/simple/components/SideBar.js b/themes/simple/components/SideBar.js index 6ff2075c..0d17afec 100644 --- a/themes/simple/components/SideBar.js +++ b/themes/simple/components/SideBar.js @@ -2,14 +2,19 @@ import { AdSlot } from '@/components/GoogleAdsense' import Live2D from '@/components/Live2D' import Announcement from './Announcement' import Catalog from './Catalog' +import { useEffect } from 'react' /** * 侧边栏 * @param {*} props * @returns */ +let counter = 0; export default function SideBar (props) { const { notice } = props + useEffect(()=>{ + console.log('sidebar 渲染次数: ', counter++) +}, []) return (<>