Webpack优化动态主题加载

This commit is contained in:
tangly1024
2023-06-20 14:20:43 +08:00
parent 16bdf3a745
commit c92fbafc39
29 changed files with 127 additions and 391 deletions

View File

@@ -2,10 +2,10 @@ import { generateLocaleDict, initLocale } from './lang'
import { createContext, useContext, useEffect, useState } from 'react'
import { useRouter } from 'next/router'
import BLOG from '@/blog.config'
import { ALL_THEME, initDarkMode } from '@/lib/theme'
import { ALL_THEME, initDarkMode } from '@/themes/theme'
import NProgress from 'nprogress'
import LoadingCover from '@/components/LoadingCover'
import { getQueryParam, getQueryVariable, isBrowser } from './utils'
import { getQueryVariable, isBrowser } from './utils'
const GlobalContext = createContext()
@@ -19,7 +19,7 @@ export function GlobalContextProvider({ children }) {
const router = useRouter()
const [lang, updateLang] = useState(BLOG.LANG) // 默认语言
const [locale, updateLocale] = useState(generateLocaleDict(BLOG.LANG)) // 默认语言
const [theme, setTheme] = useState(getQueryParam(router.asPath, 'theme') || BLOG.THEME) // 默认博客主题
const [theme, setTheme] = useState(BLOG.THEME) // 默认博客主题
const [isDarkMode, updateDarkMode] = useState(BLOG.APPEARANCE === 'dark') // 默认深色模式
const [onLoading, setOnLoading] = useState(false) // 抓取文章数据
const [onReading, setOnReading] = useState(false) // 网页资源加载
@@ -47,7 +47,6 @@ export function GlobalContextProvider({ children }) {
const { theme } = router.query
if (theme && !url.includes(`theme=${theme}`)) {
const newUrl = `${url}${url.includes('?') ? '&' : '?'}theme=${theme}`
console.log('检查newUrl', newUrl)
router.push(newUrl)
}
setOnLoading(true)
@@ -57,7 +56,7 @@ export function GlobalContextProvider({ children }) {
setOnLoading(false)
}
const queryTheme = getQueryVariable('theme')
const queryTheme = getQueryVariable('theme') || BLOG.THEME
setTheme(queryTheme)
router.events.on('routeChangeStart', handleStart)